diff --git a/src/notif.ts b/src/notif.ts index 654d939..538e911 100644 --- a/src/notif.ts +++ b/src/notif.ts @@ -18,10 +18,10 @@ export interface PushResult { export class Message { private options: RequestOptions private message: { notification: SimpleMessage } - private rcptType: "uname" | "room" | "group" + private rcptType: "uid" | "room" | "group" private rcpt: string - constructor (title: string, body: string, rcptType: "uname" | "room" | "group", rcpt: string) { + constructor (title: string, body: string, rcptType: "uid" | "room" | "group", rcpt: string) { let keys: VapidKeys = vapidKeys.keys this.options = { vapidDetails: { @@ -35,9 +35,9 @@ export class Message { this.rcpt = rcpt } - async findUserNotif(uname: string) { + async findUserNotif(uid: string) { var notif = await Notification.find().populate<{user: Pick & {_id: Types.ObjectId}}>('user', ['uname', '_id']).exec() - return notif.filter(val => val.user.uname == uname) + return notif.filter(val => val.user._id.toString() == uid) } async findRoomNotif(room: string) { @@ -54,9 +54,9 @@ export class Message { var subscriptions var rcptIds: Types.ObjectId[] switch (this.rcptType) { - case "uname": + case "uid": subscriptions = await this.findUserNotif(this.rcpt) - rcptIds = (await User.find({uname: this.rcpt})).map(v => v._id) + rcptIds = [new Types.ObjectId(this.rcpt)] break; case "room": subscriptions = await this.findRoomNotif(this.rcpt) diff --git a/src/routes/api/admin/keys.ts b/src/routes/api/admin/keys.ts index c631b2f..9c6f195 100644 --- a/src/routes/api/admin/keys.ts +++ b/src/routes/api/admin/keys.ts @@ -16,17 +16,15 @@ keysRouter.get("/", async (req, res) => { }) keysRouter.post("/", async (req, res) => { - var newKey: { - room: string; - whom: string; - } = req.body - var user = await User.findOne({uname: newKey.whom}) - if (user) { - newKey.whom = user._id.toString() - } else { + var user = await User.findById(req.body.whom._id) + if (!user) { return res.status(404).send("User not found").end() } - if (await Key.create(newKey)) { + const newKey = new Key({ + room: req.body.room, + whom: user._id + }) + if (await newKey.save()) { res.status(201).send({status: 201}) } else { res.sendStatus(500) diff --git a/src/routes/api/admin/notif/index.ts b/src/routes/api/admin/notif/index.ts index b084dcc..5051d49 100644 --- a/src/routes/api/admin/notif/index.ts +++ b/src/routes/api/admin/notif/index.ts @@ -3,9 +3,6 @@ import { Perms, adminPerm } from "@/utility"; import Group from "@schemas/Group"; import { PushResult, Message } from "@/notif"; import capability, { Features } from "@/helpers/capability"; -import Inbox from "@/schemas/Inbox"; -import { Types } from "mongoose"; -import { IUser } from "@/schemas/User"; import { outboxRouter } from "./outbox"; const notifRouter = Router() @@ -14,7 +11,7 @@ notifRouter.use(adminPerm(Perms.Notif)) notifRouter.use(capability.mw(Features.Notif)) type PushSendBody = {recp: - {type: "uname", uname: string} | + {type: "uid", uid: string} | {type: "room", room: string} | {type: "group", group: string}, title: string, @@ -24,8 +21,8 @@ type PushSendBody = {recp: notifRouter.post("/send", async (req: Request, res: Response) => { let recp: string switch (req.body.recp.type) { - case "uname": - recp = req.body.recp.uname + case "uid": + recp = req.body.recp.uid break; case "room": recp = req.body.recp.room