feat: added user search to various components
This commit is contained in:
12
src/notif.ts
12
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<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) {
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<undefined, PushResult, PushSendBody>, res: Response<PushResult>) => {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user