🐛 db query fixes
This commit is contained in:
parent
d073c52f29
commit
cc8c5359ce
@ -1,7 +0,0 @@
|
||||
export type Status = "idle" | "dnd" | "online" | "offline";
|
||||
|
||||
export interface ClientStatus {
|
||||
desktop?: string; // e.g. Windows/Linux/Mac
|
||||
mobile?: string; // e.g. iOS/Android
|
||||
web?: string; // e.g. browser, bot account
|
||||
}
|
||||
BIN
api/package-lock.json
generated
BIN
api/package-lock.json
generated
Binary file not shown.
@ -52,7 +52,7 @@
|
||||
"saslprep": "^1.0.3",
|
||||
"ts-node": "^9.1.1",
|
||||
"ts-node-dev": "^1.1.6",
|
||||
"typescript": "^4.1.2"
|
||||
"typescript": "^4.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fosscord/util": "file:../util",
|
||||
|
||||
@ -18,9 +18,9 @@ export const API_PREFIX_TRAILING_SLASH = /^\/api(\/v\d+)?\//;
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user_id: any;
|
||||
user_id: string;
|
||||
user_bot: boolean;
|
||||
token: any;
|
||||
token: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -47,7 +47,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc
|
||||
req.user_id = decoded.id;
|
||||
req.user_bot = user.bot;
|
||||
return next();
|
||||
} catch (error) {
|
||||
return next(new HTTPError(error.toString(), 400));
|
||||
} catch (error: any) {
|
||||
return next(new HTTPError(error?.toString(), 400));
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,10 +31,7 @@ export function isTextChannel(type: ChannelType): boolean {
|
||||
// get messages
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const channel_id = req.params.channel_id;
|
||||
const channel = await Channel.findOneOrFail(
|
||||
{ id: channel_id },
|
||||
{ select: ["guild_id", "type", "permission_overwrites", "recipient_ids", "owner_id"] }
|
||||
); // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids
|
||||
const channel = await Channel.findOneOrFail({ id: channel_id });
|
||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
||||
|
||||
isTextChannel(channel.type);
|
||||
@ -56,7 +53,12 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
permissions.hasThrow("VIEW_CHANNEL");
|
||||
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
|
||||
|
||||
var query: FindManyOptions<Message> & { where: { id?: any } } = { order: { id: "DESC" }, take: limit, where: { channel_id } };
|
||||
var query: FindManyOptions<Message> & { where: { id?: any } } = {
|
||||
order: { id: "DESC" },
|
||||
take: limit,
|
||||
where: { channel_id },
|
||||
relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"]
|
||||
};
|
||||
|
||||
if (after) query.where.id = MoreThan(after);
|
||||
else if (before) query.where.id = LessThan(before);
|
||||
@ -69,18 +71,20 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
|
||||
const messages = await Message.find(query);
|
||||
|
||||
return res.json(messages).map((x) => {
|
||||
(x.reactions || []).forEach((x: any) => {
|
||||
return res.json(
|
||||
messages.map((x) => {
|
||||
(x.reactions || []).forEach((x: any) => {
|
||||
// @ts-ignore
|
||||
if ((x.user_ids || []).includes(req.user_id)) x.me = true;
|
||||
// @ts-ignore
|
||||
delete x.user_ids;
|
||||
});
|
||||
// @ts-ignore
|
||||
if ((x.user_ids || []).includes(req.user_id)) x.me = true;
|
||||
// @ts-ignore
|
||||
delete x.user_ids;
|
||||
});
|
||||
// @ts-ignore
|
||||
if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
|
||||
if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
|
||||
|
||||
return x;
|
||||
});
|
||||
return x;
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// TODO: config max upload size
|
||||
@ -136,5 +140,5 @@ router.post("/", messageUpload.single("file"), async (req: Request, res: Respons
|
||||
edited_timestamp: undefined
|
||||
});
|
||||
|
||||
return res.send(data);
|
||||
return res.json(data);
|
||||
});
|
||||
|
||||
@ -14,7 +14,7 @@ router.put("/:message_id", async (req: Request, res: Response) => {
|
||||
// * in dm channels anyone can pin messages -> only check for guilds
|
||||
if (message.guild_id) permission.hasThrow("MANAGE_MESSAGES");
|
||||
|
||||
const pinned_count = await Message.count({ channel_id, pinned: true });
|
||||
const pinned_count = await Message.count({ channel: { id: channel_id }, pinned: true });
|
||||
const { maxPins } = Config.get().limits.channel;
|
||||
if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins);
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ router.post("/", async (req: Request, res: Response) => {
|
||||
channel_id: channel_id,
|
||||
data: {
|
||||
// this is the paylod
|
||||
member: { ...member, roles: member.role_ids },
|
||||
member: { ...member, roles: member.roles.map((x) => x.id) },
|
||||
channel_id,
|
||||
timestamp,
|
||||
user_id,
|
||||
|
||||
@ -5,7 +5,7 @@ import bcrypt from "bcrypt";
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail(req.user_id); //User object
|
||||
const user = await User.findOneOrFail({ id: req.user_id }); //User object
|
||||
let correctpass = true;
|
||||
|
||||
if (user.data.hash) {
|
||||
|
||||
@ -37,7 +37,7 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) =
|
||||
if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
|
||||
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
|
||||
|
||||
const user = await new User({ ...body }, { id: req.user_id }).save();
|
||||
const user = await new User({ ...body, id: req.user_id }).save();
|
||||
// TODO: dispatch user update event
|
||||
|
||||
res.json(user);
|
||||
|
||||
@ -26,7 +26,7 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
const id = friend.id;
|
||||
if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend");
|
||||
|
||||
const user = await User.findOneOrFail(req.user_id, { relations: ["relationships"], select: userProjection });
|
||||
const user = await User.findOneOrFail({ id: req.user_id }, { relations: ["relationships"], select: userProjection });
|
||||
|
||||
var relationship = user.relationships.find((x) => x.id === id);
|
||||
const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
|
||||
@ -67,8 +67,8 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
return res.sendStatus(204);
|
||||
}
|
||||
|
||||
var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming }, { id: req.user_id });
|
||||
var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing }, { id });
|
||||
var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming, id: req.user_id });
|
||||
var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing, id });
|
||||
|
||||
if (friendRequest) {
|
||||
if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you");
|
||||
@ -113,7 +113,7 @@ router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Reque
|
||||
return await updateRelationship(
|
||||
req,
|
||||
res,
|
||||
await User.findOneOrFail(req.params.id, { relations: ["relationships"], select: userProjection }),
|
||||
await User.findOneOrFail({ id: req.params.id }, { relations: ["relationships"], select: userProjection }),
|
||||
req.body.type
|
||||
);
|
||||
});
|
||||
@ -135,8 +135,8 @@ router.delete("/:id", async (req: Request, res: Response) => {
|
||||
const { id } = req.params;
|
||||
if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend");
|
||||
|
||||
const user = await User.findOneOrFail(req.user_id, { select: userProjection, relations: ["relationships"] });
|
||||
const friend = await User.findOneOrFail(id, { select: userProjection, relations: ["relationships"] });
|
||||
const user = await User.findOneOrFail({ id: req.user_id }, { select: userProjection, relations: ["relationships"] });
|
||||
const friend = await User.findOneOrFail({ id: id }, { select: userProjection, relations: ["relationships"] });
|
||||
|
||||
const relationship = user.relationships.find((x) => x.id === id);
|
||||
const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
|
||||
|
||||
@ -81,7 +81,7 @@ export interface MessageCreateSchema {
|
||||
message_id: string;
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
fail_if_not_exists: boolean;
|
||||
fail_if_not_exists?: boolean;
|
||||
};
|
||||
payload_json?: string;
|
||||
file?: any;
|
||||
|
||||
BIN
bundle/package-lock.json
generated
BIN
bundle/package-lock.json
generated
Binary file not shown.
@ -51,6 +51,7 @@
|
||||
"@fosscord/util": "file:../util",
|
||||
"async-exit-hook": "^2.0.1",
|
||||
"express": "^4.17.1",
|
||||
"missing-native-js-functions": "^1.2.13",
|
||||
"mongodb-memory-server": "^7.3.6",
|
||||
"node-os-utils": "^1.3.5"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user