More relations
This commit is contained in:
parent
2f1b131b0f
commit
b6cfb32d5a
@ -45,13 +45,7 @@ import {
|
|||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, Router } from "express";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import {
|
import { FindManyOptions, FindOperator, LessThan, MoreThan, MoreThanOrEqual } from "typeorm";
|
||||||
FindManyOptions,
|
|
||||||
FindOperator,
|
|
||||||
LessThan,
|
|
||||||
MoreThan,
|
|
||||||
MoreThanOrEqual,
|
|
||||||
} from "typeorm";
|
|
||||||
import { URL } from "url";
|
import { URL } from "url";
|
||||||
import fetch from "node-fetch-commonjs";
|
import fetch from "node-fetch-commonjs";
|
||||||
import { CloudAttachment } from "../../../../../util/entities/CloudAttachment";
|
import { CloudAttachment } from "../../../../../util/entities/CloudAttachment";
|
||||||
@ -75,8 +69,7 @@ router.get(
|
|||||||
},
|
},
|
||||||
limit: {
|
limit: {
|
||||||
type: "number",
|
type: "number",
|
||||||
description:
|
description: "max number of messages to return (1-100). defaults to 50",
|
||||||
"max number of messages to return (1-100). defaults to 50",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
responses: {
|
responses: {
|
||||||
@ -102,14 +95,9 @@ router.get(
|
|||||||
const before = req.query.before ? `${req.query.before}` : undefined;
|
const before = req.query.before ? `${req.query.before}` : undefined;
|
||||||
const after = req.query.after ? `${req.query.after}` : undefined;
|
const after = req.query.after ? `${req.query.after}` : undefined;
|
||||||
const limit = Number(req.query.limit) || 50;
|
const limit = Number(req.query.limit) || 50;
|
||||||
if (limit < 1 || limit > 100)
|
if (limit < 1 || limit > 100) throw new HTTPError("limit must be between 1 and 100", 422);
|
||||||
throw new HTTPError("limit must be between 1 and 100", 422);
|
|
||||||
|
|
||||||
const permissions = await getPermission(
|
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
req.user_id,
|
|
||||||
channel.guild_id,
|
|
||||||
channel_id,
|
|
||||||
);
|
|
||||||
permissions.hasThrow("VIEW_CHANNEL");
|
permissions.hasThrow("VIEW_CHANNEL");
|
||||||
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
|
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
|
||||||
|
|
||||||
@ -131,6 +119,8 @@ router.get(
|
|||||||
"referenced_message",
|
"referenced_message",
|
||||||
"referenced_message.author",
|
"referenced_message.author",
|
||||||
"referenced_message.mentions",
|
"referenced_message.mentions",
|
||||||
|
"referenced_message.mention_roles",
|
||||||
|
"referenced_message.mention_channels",
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -151,9 +141,7 @@ router.get(
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
left.push(...right);
|
left.push(...right);
|
||||||
messages = left.sort(
|
messages = left.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
||||||
(a, b) => a.timestamp.getTime() - b.timestamp.getTime(),
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
query.take = 1;
|
query.take = 1;
|
||||||
const message = await Message.findOne({
|
const message = await Message.findOne({
|
||||||
@ -164,20 +152,12 @@ router.get(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (after) {
|
if (after) {
|
||||||
if (BigInt(after) > BigInt(Snowflake.generate()))
|
if (BigInt(after) > BigInt(Snowflake.generate())) throw new HTTPError("after parameter must not be greater than current time", 422);
|
||||||
throw new HTTPError(
|
|
||||||
"after parameter must not be greater than current time",
|
|
||||||
422,
|
|
||||||
);
|
|
||||||
|
|
||||||
query.where.id = MoreThan(after);
|
query.where.id = MoreThan(after);
|
||||||
query.order = { timestamp: "ASC" };
|
query.order = { timestamp: "ASC" };
|
||||||
} else if (before) {
|
} else if (before) {
|
||||||
if (BigInt(before) > BigInt(Snowflake.generate()))
|
if (BigInt(before) > BigInt(Snowflake.generate())) throw new HTTPError("before parameter must not be greater than current time", 422);
|
||||||
throw new HTTPError(
|
|
||||||
"before parameter must not be greater than current time",
|
|
||||||
422,
|
|
||||||
);
|
|
||||||
|
|
||||||
query.where.id = LessThan(before);
|
query.where.id = LessThan(before);
|
||||||
}
|
}
|
||||||
@ -205,9 +185,7 @@ router.get(
|
|||||||
});
|
});
|
||||||
x.attachments?.forEach((y: Attachment) => {
|
x.attachments?.forEach((y: Attachment) => {
|
||||||
// dynamically set attachment proxy_url in case the endpoint changed
|
// dynamically set attachment proxy_url in case the endpoint changed
|
||||||
const uri = y.proxy_url.startsWith("http")
|
const uri = y.proxy_url.startsWith("http") ? y.proxy_url : `https://example.org${y.proxy_url}`;
|
||||||
? y.proxy_url
|
|
||||||
: `https://example.org${y.proxy_url}`;
|
|
||||||
|
|
||||||
const url = new URL(uri);
|
const url = new URL(uri);
|
||||||
if (endpoint) {
|
if (endpoint) {
|
||||||
@ -310,10 +288,7 @@ router.post(
|
|||||||
relations: ["recipients", "recipients.user"],
|
relations: ["recipients", "recipients.user"],
|
||||||
});
|
});
|
||||||
if (!channel.isWritable()) {
|
if (!channel.isWritable()) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(`Cannot send messages to channel of type ${channel.type}`, 400);
|
||||||
`Cannot send messages to channel of type ${channel.type}`,
|
|
||||||
400,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (body.nonce) {
|
if (body.nonce) {
|
||||||
@ -335,12 +310,7 @@ router.post(
|
|||||||
const count = await Message.count({
|
const count = await Message.count({
|
||||||
where: {
|
where: {
|
||||||
channel_id,
|
channel_id,
|
||||||
timestamp: MoreThan(
|
timestamp: MoreThan(new Date(Date.now() - limits.absoluteRate.sendMessage.window)),
|
||||||
new Date(
|
|
||||||
Date.now() -
|
|
||||||
limits.absoluteRate.sendMessage.window,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -357,13 +327,8 @@ router.post(
|
|||||||
const files = (req.files as Express.Multer.File[]) ?? [];
|
const files = (req.files as Express.Multer.File[]) ?? [];
|
||||||
for (const currFile of files) {
|
for (const currFile of files) {
|
||||||
try {
|
try {
|
||||||
const file = await uploadFile(
|
const file = await uploadFile(`/attachments/${channel.id}`, currFile);
|
||||||
`/attachments/${channel.id}`,
|
attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
|
||||||
currFile,
|
|
||||||
);
|
|
||||||
attachments.push(
|
|
||||||
Attachment.create({ ...file, proxy_url: file.url }),
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(400).json({ message: error?.toString() });
|
return res.status(400).json({ message: error?.toString() });
|
||||||
}
|
}
|
||||||
@ -400,9 +365,7 @@ router.post(
|
|||||||
recipient.save(),
|
recipient.save(),
|
||||||
emitEvent({
|
emitEvent({
|
||||||
event: "CHANNEL_CREATE",
|
event: "CHANNEL_CREATE",
|
||||||
data: channel_dto.excludedRecipients([
|
data: channel_dto.excludedRecipients([recipient.user_id]),
|
||||||
recipient.user_id,
|
|
||||||
]),
|
|
||||||
user_id: recipient.user_id,
|
user_id: recipient.user_id,
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
@ -423,16 +386,13 @@ router.post(
|
|||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
message.member.roles = message.member.roles
|
message.member.roles = message.member.roles.filter((x) => x.id != x.guild_id).map((x) => x.id);
|
||||||
.filter((x) => x.id != x.guild_id)
|
|
||||||
.map((x) => x.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let read_state = await ReadState.findOne({
|
let read_state = await ReadState.findOne({
|
||||||
where: { user_id: req.user_id, channel_id },
|
where: { user_id: req.user_id, channel_id },
|
||||||
});
|
});
|
||||||
if (!read_state)
|
if (!read_state) read_state = ReadState.create({ user_id: req.user_id, channel_id });
|
||||||
read_state = ReadState.create({ user_id: req.user_id, channel_id });
|
|
||||||
read_state.last_message_id = message.id;
|
read_state.last_message_id = message.id;
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
@ -443,19 +403,12 @@ router.post(
|
|||||||
channel_id: channel_id,
|
channel_id: channel_id,
|
||||||
data: message,
|
data: message,
|
||||||
} as MessageCreateEvent),
|
} as MessageCreateEvent),
|
||||||
message.guild_id
|
message.guild_id ? Member.update({ id: req.user_id, guild_id: message.guild_id }, { last_message_id: message.id }) : null,
|
||||||
? Member.update(
|
|
||||||
{ id: req.user_id, guild_id: message.guild_id },
|
|
||||||
{ last_message_id: message.id },
|
|
||||||
)
|
|
||||||
: null,
|
|
||||||
channel.save(),
|
channel.save(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// no await as it shouldnt block the message send function and silently catch error
|
// no await as it shouldnt block the message send function and silently catch error
|
||||||
postHandleMessage(message).catch((e) =>
|
postHandleMessage(message).catch((e) => console.error("[Message] post-message handler failed", e));
|
||||||
console.error("[Message] post-message handler failed", e),
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.json(
|
return res.json(
|
||||||
message.withSignedAttachments(
|
message.withSignedAttachments(
|
||||||
|
|||||||
@ -227,6 +227,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
|||||||
where: {
|
where: {
|
||||||
id: opts.message_reference.message_id,
|
id: opts.message_reference.message_id,
|
||||||
},
|
},
|
||||||
|
relations: ["author", "mentions", "mention_roles", "mention_channels"],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (message.referenced_message.channel_id !== opts.message_reference.channel_id) throw new HTTPError("Referenced message not found in the specified channel", 404);
|
if (message.referenced_message.channel_id !== opts.message_reference.channel_id) throw new HTTPError("Referenced message not found in the specified channel", 404);
|
||||||
|
|||||||
Reference in New Issue
Block a user