1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 04:32:35 +01:00

Fix user guild settings

This commit is contained in:
Madeline 2022-09-28 16:39:26 +10:00
parent 1bde83635c
commit 419df470a4
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47

View File

@ -1,22 +1,14 @@
import { Router, Response, Request } from "express";
import {
Channel,
ChannelOverride,
Member,
UserGuildSettings,
OrmUtils,
UserGuildSettingsSchema,
} from "@fosscord/util";
import { route } from "@fosscord/api";
const router = Router();
// This sucks. I would use a DeepPartial, my own or typeorms, but they both generate inncorect schema
export interface UserGuildSettingsSchema
extends Partial<Omit<UserGuildSettings, "channel_overrides">> {
channel_overrides: {
[channel_id: string]: Partial<ChannelOverride>;
};
}
// GET doesn't exist on discord.com
router.get("/", route({}), async (req: Request, res: Response) => {
const user = await Member.findOneOrFail({
@ -30,7 +22,7 @@ router.patch(
"/",
route({ body: "UserGuildSettingsSchema" }),
async (req: Request, res: Response) => {
const body = req.body as UserGuildSettings;
const body = req.body as UserGuildSettingsSchema;
if (body.channel_overrides) {
for (var channel in body.channel_overrides) {
@ -40,9 +32,11 @@ router.patch(
const user = await Member.findOneOrFail({
where: { id: req.user_id, guild_id: req.params.guild_id },
select: ["settings"]
});
user.settings = { ...user.settings, ...body };
await user.save();
OrmUtils.mergeDeep(user.settings || {}, body);
Member.update({ id: req.user_id, guild_id: req.params.guild_id }, user);
res.json(user.settings);
},