mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-25 16:12:09 +00:00
Bunch of fixes and improvements, everything appears to work now
This commit is contained in:
@@ -5,6 +5,7 @@ import fetch, { Response as FetchResponse, Headers } from "node-fetch";
|
||||
import ProxyAgent from 'proxy-agent';
|
||||
import { Config } from "@fosscord/util";
|
||||
import { AssetCacheItem } from "../util/entities/AssetCacheItem"
|
||||
import { green } from "picocolors";
|
||||
|
||||
export default function TestClient(app: Application) {
|
||||
const agent = new ProxyAgent();
|
||||
@@ -18,11 +19,16 @@ export default function TestClient(app: Application) {
|
||||
|
||||
//load asset cache
|
||||
let newAssetCache: Map<string, AssetCacheItem> = new Map<string, AssetCacheItem>();
|
||||
if(!fs.existsSync(path.join(__dirname, "..", "..", "assets", "cache"))) {
|
||||
fs.mkdirSync(path.join(__dirname, "..", "..", "assets", "cache"));
|
||||
let assetCacheDir = path.join(__dirname, "..", "..", "assets", "cache");
|
||||
if(process.env.ASSET_CACHE_DIR)
|
||||
assetCacheDir = process.env.ASSET_CACHE_DIR
|
||||
|
||||
console.log(`[TestClient] ${green(`Using asset cache path: ${assetCacheDir}`)}`)
|
||||
if(!fs.existsSync(assetCacheDir)) {
|
||||
fs.mkdirSync(assetCacheDir);
|
||||
}
|
||||
if(fs.existsSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json"))) {
|
||||
let rawdata = fs.readFileSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json"));
|
||||
if(fs.existsSync(path.join(assetCacheDir, "index.json"))) {
|
||||
let rawdata = fs.readFileSync(path.join(assetCacheDir, "index.json"));
|
||||
newAssetCache = new Map<string, AssetCacheItem>(Object.entries(JSON.parse(rawdata.toString())));
|
||||
}
|
||||
|
||||
@@ -39,6 +45,7 @@ export default function TestClient(app: Application) {
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log(`[TestClient] Downloading file not yet cached! Asset file: ${req.params.file}`);
|
||||
response = await fetch(`https://discord.com/assets/${req.params.file}`, {
|
||||
agent,
|
||||
// @ts-ignore
|
||||
@@ -49,11 +56,11 @@ export default function TestClient(app: Application) {
|
||||
|
||||
//set cache info
|
||||
assetCacheItem.Headers = Object.fromEntries(stripHeaders(response.headers));
|
||||
assetCacheItem.FilePath = path.join(__dirname, "..", "..", "assets", "cache", req.params.file);
|
||||
assetCacheItem.FilePath = path.join(assetCacheDir, req.params.file);
|
||||
assetCacheItem.Key = req.params.file;
|
||||
//add to cache and save
|
||||
newAssetCache.set(req.params.file, assetCacheItem);
|
||||
fs.writeFileSync(path.join(__dirname, "..", "..", "assets", "cache", "index.json"), JSON.stringify(Object.fromEntries(newAssetCache), null, 4));
|
||||
fs.writeFileSync(path.join(assetCacheDir, "index.json"), JSON.stringify(Object.fromEntries(newAssetCache), null, 4));
|
||||
//download file
|
||||
fs.writeFileSync(assetCacheItem.FilePath, await response.buffer());
|
||||
}
|
||||
|
||||
@@ -77,8 +77,8 @@ router.patch("/", route({ body: "ChannelModifySchema", permission: "MANAGE_CHANN
|
||||
const { channel_id } = req.params;
|
||||
if (payload.icon) payload.icon = await handleFile(`/channel-icons/${channel_id}`, payload.icon);
|
||||
|
||||
const channel = await Channel.findOneOrFail({ where: { id: channel_id } });
|
||||
channel.assign(payload);
|
||||
let channel = await Channel.findOneOrFail({ where: { id: channel_id } });
|
||||
channel = Object.assign(channel, payload);
|
||||
|
||||
await Promise.all([
|
||||
channel.save(),
|
||||
|
||||
@@ -33,7 +33,7 @@ router.post("/", route({ body: "InviteCreateSchema", permission: "CREATE_INSTANT
|
||||
|
||||
const expires_at = new Date(req.body.max_age * 1000 + Date.now());
|
||||
|
||||
const invite = await new Invite({
|
||||
const invite = await Object.assign(new Invite(),{
|
||||
code: random(),
|
||||
temporary: req.body.temporary,
|
||||
uses: 0,
|
||||
|
||||
@@ -20,7 +20,7 @@ router.post("/", route({ body: "MessageAcknowledgeSchema" }), async (req: Reques
|
||||
permission.hasThrow("VIEW_CHANNEL");
|
||||
|
||||
let read_state = await ReadState.findOne({ where: { user_id: req.user_id, channel_id } });
|
||||
if (!read_state) read_state = new ReadState({ where: { user_id: req.user_id, channel_id } });
|
||||
if (!read_state) read_state = Object.assign(new ReadState(), { user_id: req.user_id, channel_id });
|
||||
read_state.last_message_id = message_id;
|
||||
|
||||
await read_state.save();
|
||||
|
||||
@@ -28,7 +28,7 @@ router.put("/:user_id", route({}), async (req: Request, res: Response) => {
|
||||
throw DiscordApiErrors.INVALID_RECIPIENT; //TODO is this the right error?
|
||||
}
|
||||
|
||||
channel.recipients!.push(new Recipient({ channel_id, user_id: user_id }));
|
||||
channel.recipients!.push(Object.assign(new Recipient(), { channel_id, user_id: user_id }));
|
||||
await channel.save();
|
||||
|
||||
await emitEvent({
|
||||
|
||||
@@ -90,7 +90,7 @@ router.put("/:user_id", route({ body: "BanCreateSchema", permission: "BAN_MEMBER
|
||||
|
||||
const banned_user = await User.getPublicUser(banned_user_id);
|
||||
|
||||
const ban = new Ban({
|
||||
const ban = Object.assign(new Ban(),{
|
||||
user_id: banned_user_id,
|
||||
guild_id: guild_id,
|
||||
ip: getIpAdress(req),
|
||||
@@ -122,7 +122,7 @@ router.put("/@me", route({ body: "BanCreateSchema"}), async (req: Request, res:
|
||||
if (req.permission!.cache.guild?.owner_id === req.params.user_id)
|
||||
throw new HTTPError("You are the guild owner, hence can't ban yourself", 403);
|
||||
|
||||
const ban = new Ban({
|
||||
const ban = Object.assign(new Ban(), {
|
||||
user_id: req.params.user_id,
|
||||
guild_id: guild_id,
|
||||
ip: getIpAdress(req),
|
||||
|
||||
@@ -50,7 +50,7 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A
|
||||
const user = await User.findOneOrFail({ where: { id: req.user_id } });
|
||||
body.image = (await handleFile(`/emojis/${id}`, body.image)) as string;
|
||||
|
||||
const emoji = await new Emoji({
|
||||
const emoji = await Object.assign(new Emoji(), {
|
||||
id: id,
|
||||
guild_id: guild_id,
|
||||
...body,
|
||||
@@ -80,7 +80,7 @@ router.patch(
|
||||
const { emoji_id, guild_id } = req.params;
|
||||
const body = req.body as EmojiModifySchema;
|
||||
|
||||
const emoji = await new Emoji({ ...body, id: emoji_id, guild_id: guild_id }).save();
|
||||
const emoji = await Object.assign(new Emoji(), { ...body, id: emoji_id, guild_id: guild_id }).save();
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_EMOJIS_UPDATE",
|
||||
|
||||
@@ -59,7 +59,7 @@ router.patch("/", route({ body: "GuildUpdateSchema"}), async (req: Request, res:
|
||||
relations: ["emojis", "roles", "stickers"]
|
||||
});
|
||||
// TODO: check if body ids are valid
|
||||
guild.assign(body);
|
||||
guild = Object.assign(guild, body);
|
||||
|
||||
//TODO: check this, removed toJSON call
|
||||
const data = JSON.parse(JSON.stringify(guild));
|
||||
|
||||
@@ -31,7 +31,7 @@ router.patch("/", route({ body: "MemberChangeSchema" }), async (req: Request, re
|
||||
permission.hasThrow("MANAGE_ROLES");
|
||||
|
||||
if (body.roles.indexOf(everyone.id) === -1) body.roles.push(everyone.id);
|
||||
member.roles = body.roles.map((x) => new Role({ id: x })); // foreign key constraint will fail if role doesn't exist
|
||||
member.roles = body.roles.map((x) => Object.assign(new Role(), { id: x })); // foreign key constraint will fail if role doesn't exist
|
||||
}
|
||||
|
||||
await member.save();
|
||||
|
||||
@@ -43,7 +43,7 @@ router.patch("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }
|
||||
|
||||
if (body.icon) body.icon = await handleFile(`/role-icons/${role_id}`, body.icon as string);
|
||||
|
||||
const role = new Role({
|
||||
const role = Object.assign(new Role(), {
|
||||
...body,
|
||||
id: role_id,
|
||||
guild_id,
|
||||
|
||||
@@ -51,7 +51,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" })
|
||||
|
||||
if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles);
|
||||
|
||||
const role = new Role({
|
||||
let role: Role = Object.assign(new Role(),{
|
||||
// values before ...body are default and can be overriden
|
||||
position: 0,
|
||||
hoist: false,
|
||||
|
||||
@@ -43,7 +43,7 @@ router.post(
|
||||
const id = Snowflake.generate();
|
||||
|
||||
const [sticker] = await Promise.all([
|
||||
new Sticker({
|
||||
Object.assign(new Sticker(), {
|
||||
...body,
|
||||
guild_id,
|
||||
id,
|
||||
@@ -105,7 +105,7 @@ router.patch(
|
||||
const { guild_id, sticker_id } = req.params;
|
||||
const body = req.body as ModifyGuildStickerSchema;
|
||||
|
||||
const sticker = await new Sticker({ ...body, guild_id, id: sticker_id }).save();
|
||||
const sticker = await Object.assign(new Sticker(), { ...body, guild_id, id: sticker_id }).save();
|
||||
await sendStickerUpdateEvent(guild_id);
|
||||
|
||||
return res.json(sticker);
|
||||
|
||||
@@ -47,7 +47,7 @@ router.post("/", route({ body: "TemplateCreateSchema", permission: "MANAGE_GUILD
|
||||
const exists = await Template.findOneOrFail({ where: { id: guild_id } }).catch((e) => {});
|
||||
if (exists) throw new HTTPError("Template already exists", 400);
|
||||
|
||||
const template = await new Template({
|
||||
const template = await Object.assign(new Template(), {
|
||||
...req.body,
|
||||
code: generateCode(),
|
||||
creator_id: req.user_id,
|
||||
@@ -75,7 +75,7 @@ router.put("/:code", route({ permission: "MANAGE_GUILD" }), async (req: Request,
|
||||
const { code, guild_id } = req.params;
|
||||
const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection });
|
||||
|
||||
const template = await new Template({ code, serialized_source_guild: guild }).save();
|
||||
const template = await Object.assign(new Template(), { code, serialized_source_guild: guild }).save();
|
||||
|
||||
res.json(template);
|
||||
});
|
||||
@@ -84,7 +84,7 @@ router.patch("/:code", route({ body: "TemplateModifySchema", permission: "MANAGE
|
||||
const { code, guild_id } = req.params;
|
||||
const { name, description } = req.body;
|
||||
|
||||
const template = await new Template({ code, name: name, description: description, source_guild_id: guild_id }).save();
|
||||
const template = await Object.assign(new Template(), { code, name: name, description: description, source_guild_id: guild_id }).save();
|
||||
|
||||
res.json(template);
|
||||
});
|
||||
|
||||
@@ -47,7 +47,7 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" })
|
||||
|
||||
const { id } = await Channel.findOneOrFail({ where: { guild_id, type: ChannelType.GUILD_TEXT } });
|
||||
|
||||
await new Invite({
|
||||
await Object.assign(new Invite(), {
|
||||
vanity_url: true,
|
||||
code: code,
|
||||
temporary: false,
|
||||
|
||||
@@ -33,7 +33,7 @@ router.patch("/", route({ body: "VoiceStateUpdateSchema" }), async (req: Request
|
||||
if (!body.suppress) body.request_to_speak_timestamp = new Date();
|
||||
if (body.request_to_speak_timestamp) perms.hasThrow("REQUEST_TO_SPEAK");
|
||||
|
||||
const voice_state = await VoiceState.findOne({
|
||||
let voice_state = await VoiceState.findOne({
|
||||
where: {
|
||||
guild_id,
|
||||
channel_id: body.channel_id,
|
||||
@@ -42,7 +42,7 @@ router.patch("/", route({ body: "VoiceStateUpdateSchema" }), async (req: Request
|
||||
});
|
||||
if (!voice_state) throw DiscordApiErrors.UNKNOWN_VOICE_STATE;
|
||||
|
||||
voice_state.assign(body);
|
||||
voice_state = Object.assign(voice_state, body);
|
||||
const channel = await Channel.findOneOrFail({ where: { guild_id, id: body.channel_id } });
|
||||
if (channel.type !== ChannelType.GUILD_STAGE_VOICE) {
|
||||
throw DiscordApiErrors.CANNOT_EXECUTE_ON_THIS_CHANNEL_TYPE;
|
||||
|
||||
@@ -57,13 +57,13 @@ router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req:
|
||||
const guild_id = Snowflake.generate();
|
||||
|
||||
const [guild, role] = await Promise.all([
|
||||
new Guild({
|
||||
Object.assign(new Guild(), {
|
||||
...body,
|
||||
...template.serialized_source_guild,
|
||||
id: guild_id,
|
||||
owner_id: req.user_id
|
||||
}).save(),
|
||||
new Role({
|
||||
(Object.assign(new Role(), {
|
||||
id: guild_id,
|
||||
guild_id: guild_id,
|
||||
color: 0,
|
||||
@@ -74,7 +74,7 @@ router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req:
|
||||
permissions: BigInt("2251804225"),
|
||||
position: 0,
|
||||
tags: null
|
||||
}).save()
|
||||
}) as Role).save()
|
||||
]);
|
||||
|
||||
await Member.addToGuild(req.user_id, guild_id);
|
||||
|
||||
@@ -32,9 +32,7 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
|
||||
const body = req.body as UserModifySchema;
|
||||
|
||||
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 User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
|
||||
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);let user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
|
||||
|
||||
if (body.password) {
|
||||
if (user.data?.hash) {
|
||||
@@ -65,7 +63,7 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
|
||||
}
|
||||
}
|
||||
|
||||
user.assign(body);
|
||||
user = Object.assign(user, body);
|
||||
await user.save();
|
||||
|
||||
// @ts-ignore
|
||||
|
||||
@@ -140,7 +140,7 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
relationship.type = RelationshipType.blocked;
|
||||
await relationship.save();
|
||||
} else {
|
||||
relationship = await new Relationship({ to_id: id, type: RelationshipType.blocked, from_id: req.user_id }).save();
|
||||
relationship = await Object.assign(new Relationship(), { to_id: id, type: RelationshipType.blocked, from_id: req.user_id }).save();
|
||||
}
|
||||
|
||||
if (friendRequest && friendRequest.type !== RelationshipType.blocked) {
|
||||
@@ -166,8 +166,8 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
const { maxFriends } = Config.get().limits.user;
|
||||
if (user.relationships.length >= maxFriends) throw DiscordApiErrors.MAXIMUM_FRIENDS.withParams(maxFriends);
|
||||
|
||||
let incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming, to: user, from: friend });
|
||||
let outgoing_relationship = new Relationship({
|
||||
let incoming_relationship = Object.assign(new Relationship(), { nickname: undefined, type: RelationshipType.incoming, to: user, from: friend });
|
||||
let outgoing_relationship = Object.assign(new Relationship(), {
|
||||
nickname: undefined,
|
||||
type: RelationshipType.outgoing,
|
||||
to: friend,
|
||||
@@ -178,7 +178,7 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you");
|
||||
if (friendRequest.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user");
|
||||
// accept friend request
|
||||
incoming_relationship = friendRequest;
|
||||
incoming_relationship = friendRequest as any; //TODO: checkme, any cast
|
||||
incoming_relationship.type = RelationshipType.friends;
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
|
||||
if (relationship.type === RelationshipType.outgoing) throw new HTTPError("You already sent a friend request");
|
||||
if (relationship.type === RelationshipType.blocked) throw new HTTPError("Unblock the user before sending a friend request");
|
||||
if (relationship.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user");
|
||||
outgoing_relationship = relationship;
|
||||
outgoing_relationship = relationship as any; //TODO: checkme, any cast
|
||||
outgoing_relationship.type = RelationshipType.friends;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
const channel = await Channel.findOneOrFail({ where: { id: opts.channel_id }, relations: ["recipients"] });
|
||||
if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404);
|
||||
|
||||
const message = new Message({
|
||||
const message = Object.assign(new Message(), {
|
||||
...opts,
|
||||
sticker_items: opts.sticker_ids?.map((x) => ({ id: x })),
|
||||
guild_id: channel.guild_id,
|
||||
@@ -132,9 +132,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
}
|
||||
}
|
||||
|
||||
message.mention_channels = mention_channel_ids.map((x) => new Channel({ id: x }));
|
||||
message.mention_roles = mention_role_ids.map((x) => new Role({ id: x }));
|
||||
message.mentions = mention_user_ids.map((x) => new User({ id: x }));
|
||||
message.mention_channels = mention_channel_ids.map((x) => Object.assign(new Channel(), { id: x }));
|
||||
message.mention_roles = mention_role_ids.map((x) => Object.assign(new Role(), { id: x }));
|
||||
message.mentions = mention_user_ids.map((x) => Object.assign(new User(), { id: x }));
|
||||
message.mention_everyone = mention_everyone;
|
||||
|
||||
// TODO: check and put it all in the body
|
||||
|
||||
Reference in New Issue
Block a user