feat: added user search to various components

This commit is contained in:
2025-06-01 17:44:43 +02:00
parent ec24700b8a
commit df745c78e2
3 changed files with 16 additions and 21 deletions

View File

@@ -18,10 +18,10 @@ export interface PushResult {
export class Message { export class Message {
private options: RequestOptions private options: RequestOptions
private message: { notification: SimpleMessage } private message: { notification: SimpleMessage }
private rcptType: "uname" | "room" | "group" private rcptType: "uid" | "room" | "group"
private rcpt: string 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 let keys: VapidKeys = vapidKeys.keys
this.options = { this.options = {
vapidDetails: { vapidDetails: {
@@ -35,9 +35,9 @@ export class Message {
this.rcpt = rcpt this.rcpt = rcpt
} }
async findUserNotif(uname: string) { async findUserNotif(uid: string) {
var notif = await Notification.find().populate<{user: Pick<IUser, 'uname'> & {_id: Types.ObjectId}}>('user', ['uname', '_id']).exec() var notif = await Notification.find().populate<{user: Pick<IUser, 'uname'> & {_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) { async findRoomNotif(room: string) {
@@ -54,9 +54,9 @@ export class Message {
var subscriptions var subscriptions
var rcptIds: Types.ObjectId[] var rcptIds: Types.ObjectId[]
switch (this.rcptType) { switch (this.rcptType) {
case "uname": case "uid":
subscriptions = await this.findUserNotif(this.rcpt) subscriptions = await this.findUserNotif(this.rcpt)
rcptIds = (await User.find({uname: this.rcpt})).map(v => v._id) rcptIds = [new Types.ObjectId(this.rcpt)]
break; break;
case "room": case "room":
subscriptions = await this.findRoomNotif(this.rcpt) subscriptions = await this.findRoomNotif(this.rcpt)

View File

@@ -16,17 +16,15 @@ keysRouter.get("/", async (req, res) => {
}) })
keysRouter.post("/", async (req, res) => { keysRouter.post("/", async (req, res) => {
var newKey: { var user = await User.findById(req.body.whom._id)
room: string; if (!user) {
whom: string;
} = req.body
var user = await User.findOne({uname: newKey.whom})
if (user) {
newKey.whom = user._id.toString()
} else {
return res.status(404).send("User not found").end() 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}) res.status(201).send({status: 201})
} else { } else {
res.sendStatus(500) res.sendStatus(500)

View File

@@ -3,9 +3,6 @@ import { Perms, adminPerm } from "@/utility";
import Group from "@schemas/Group"; import Group from "@schemas/Group";
import { PushResult, Message } from "@/notif"; import { PushResult, Message } from "@/notif";
import capability, { Features } from "@/helpers/capability"; import capability, { Features } from "@/helpers/capability";
import Inbox from "@/schemas/Inbox";
import { Types } from "mongoose";
import { IUser } from "@/schemas/User";
import { outboxRouter } from "./outbox"; import { outboxRouter } from "./outbox";
const notifRouter = Router() const notifRouter = Router()
@@ -14,7 +11,7 @@ notifRouter.use(adminPerm(Perms.Notif))
notifRouter.use(capability.mw(Features.Notif)) notifRouter.use(capability.mw(Features.Notif))
type PushSendBody = {recp: type PushSendBody = {recp:
{type: "uname", uname: string} | {type: "uid", uid: string} |
{type: "room", room: string} | {type: "room", room: string} |
{type: "group", group: string}, {type: "group", group: string},
title: string, title: string,
@@ -24,8 +21,8 @@ type PushSendBody = {recp:
notifRouter.post("/send", async (req: Request<undefined, PushResult, PushSendBody>, res: Response<PushResult>) => { notifRouter.post("/send", async (req: Request<undefined, PushResult, PushSendBody>, res: Response<PushResult>) => {
let recp: string let recp: string
switch (req.body.recp.type) { switch (req.body.recp.type) {
case "uname": case "uid":
recp = req.body.recp.uname recp = req.body.recp.uid
break; break;
case "room": case "room":
recp = req.body.recp.room recp = req.body.recp.room