User Notes (#707)

* Notes implementation.
Bug: Client does not save note locally after uploading to server. Client does save after reloading page. Is this due to the response being sent by PUT?

* I don't know why the client doesn't do optimistic UI updates with this, or any updates at all without reloading the page

* Added USER_NOTE_UPDATE event, thanks @TheRealGeoDash2019 !
This commit is contained in:
Madeline 2022-04-05 19:58:34 +10:00 committed by GitHub
parent 99a5030530
commit ecf59d30c5
3 changed files with 35 additions and 5 deletions

View File

@ -1,14 +1,39 @@
import { Request, Response, Router } from "express";
import { route } from "@fosscord/api";
import { User, emitEvent } from "@fosscord/util";
const router: Router = Router();
router.get("/:id", route({}), async (req: Request, res: Response) => {
const { id } = req.params;
const user = await User.findOneOrFail({ where: { id: req.user_id }, select: ["notes"] });
const note = user.notes[id];
return res.json({
note: note,
note_user_id: id,
user_id: user.id,
});
});
router.put("/:id", route({}), async (req: Request, res: Response) => {
//TODO
res.json({
message: "Unknown User",
code: 10013
}).status(404);
const { id } = req.params;
const user = await User.findOneOrFail({ where: { id: req.user_id } });
const noteUser = await User.findOneOrFail({ where: { id: id }}); //if noted user does not exist throw
const { note } = req.body;
await User.update({ id: req.user_id }, { notes: { ...user.notes, [noteUser.id]: note } });
await emitEvent({
event: "USER_NOTE_UPDATE",
data: {
note: note,
id: noteUser.id
},
user_id: user.id,
})
return res.status(204);
});
export default router;

View File

@ -164,6 +164,9 @@ export class User extends BaseClass {
@Column({ type: "simple-json", select: false })
settings: UserSettings;
@Column({ type: "simple-json" })
notes: { [key: string]: string }; //key is ID of user
toPublicUser() {
const user: any = {};
PublicUserProjection.forEach((x) => {
@ -271,6 +274,7 @@ export class User extends BaseClass {
},
settings: { ...defaultSettings, locale: language },
fingerprints: [],
notes: {},
});
await user.save();

View File

@ -623,6 +623,7 @@ export type EVENT =
| "PRESENCE_UPDATE"
| "TYPING_START"
| "USER_UPDATE"
| "USER_NOTE_UPDATE"
| "WEBHOOKS_UPDATE"
| "INTERACTION_CREATE"
| "VOICE_STATE_UPDATE"