🐛 db query fixes

This commit is contained in:
Flam3rboy 2021-08-31 17:58:47 +02:00
parent d073c52f29
commit cc8c5359ce
13 changed files with 59 additions and 48 deletions

View File

@ -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
}

18
api/package-lock.json generated
View File

@ -60,7 +60,7 @@
"saslprep": "^1.0.3",
"ts-node": "^9.1.1",
"ts-node-dev": "^1.1.6",
"typescript": "^4.1.2"
"typescript": "^4.4.2"
}
},
"../util": {
@ -83,7 +83,7 @@
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"typeorm": "^0.2.37",
"typescript": "^4.3.5",
"typescript": "^4.4.2",
"typescript-json-schema": "^0.50.1"
},
"devDependencies": {
@ -11430,9 +11430,9 @@
}
},
"node_modules/typescript": {
"version": "4.3.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz",
"integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==",
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.2.tgz",
"integrity": "sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
@ -12637,7 +12637,7 @@
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"typeorm": "^0.2.37",
"typescript": "^4.3.5",
"typescript": "^4.4.2",
"typescript-json-schema": "^0.50.1"
}
},
@ -21387,9 +21387,9 @@
}
},
"typescript": {
"version": "4.3.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz",
"integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==",
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.2.tgz",
"integrity": "sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==",
"dev": true
},
"umd": {

View File

@ -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",

View File

@ -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));
}
}

View File

@ -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,7 +71,8 @@ router.get("/", async (req: Request, res: Response) => {
const messages = await Message.find(query);
return res.json(messages).map((x) => {
return res.json(
messages.map((x) => {
(x.reactions || []).forEach((x: any) => {
// @ts-ignore
if ((x.user_ids || []).includes(req.user_id)) x.me = true;
@ -80,7 +83,8 @@ router.get("/", async (req: Request, res: Response) => {
if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
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);
});

View File

@ -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);

View File

@ -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,

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -16,6 +16,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"
},
@ -70,6 +71,7 @@
"mongoose-long": "^0.3.2",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
"supertest": "^6.1.6",
"typeorm": "^0.2.37"
},
"devDependencies": {
@ -1224,6 +1226,11 @@
"node": "*"
}
},
"node_modules/missing-native-js-functions": {
"version": "1.2.13",
"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
"integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
},
"node_modules/mkdirp": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
@ -1916,6 +1923,7 @@
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
"saslprep": "^1.0.3",
"supertest": "^6.1.6",
"ts-node": "^9.1.1",
"ts-node-dev": "^1.1.6",
"typeorm": "^0.2.37",
@ -2800,6 +2808,11 @@
"brace-expansion": "^1.1.7"
}
},
"missing-native-js-functions": {
"version": "1.2.13",
"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
"integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
},
"mkdirp": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",

View File

@ -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"
}