Skip to content

Commit

Permalink
fix bugs with dms
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Dec 26, 2024
1 parent 206ab8d commit b268471
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/routes/users/userOpenDMChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { customExpressValidatorResult } from '../../common/errorHandler';
import { authenticate } from '../../middleware/authenticate';
import { rateLimit } from '../../middleware/rateLimit';
import { openDMChannel } from '../../services/User/User';
import { redisClient } from '../../common/redis';
import { createQueue } from '@nerimity/mimiqueue';

export const queue = createQueue({
name: 'openDM',
redisClient,
});

export function userOpenDMChannel(Router: Router) {
Router.post(
'/users/:userId/open-channel',
authenticate(),
param('userId')
.not()
.isEmpty()
.withMessage('userId is required.')
.isString()
.withMessage('Invalid userId.')
.isLength({ min: 1, max: 320 })
.withMessage('userId must be between 1 and 320 characters long.'),
param('userId').not().isEmpty().withMessage('userId is required.').isString().withMessage('Invalid userId.').isLength({ min: 1, max: 320 }).withMessage('userId must be between 1 and 320 characters long.'),
rateLimit({
name: 'open_dm_channel',
restrictMS: 10000,
Expand All @@ -27,16 +27,19 @@ export function userOpenDMChannel(Router: Router) {
}

async function route(req: Request, res: Response) {
await queue.add(async () => {
await handleRoute(req, res);
});
}

async function handleRoute(req: Request, res: Response) {
const validateError = customExpressValidatorResult(req);

if (validateError) {
return res.status(400).json(validateError);
}

const [inbox, error] = await openDMChannel(
req.userCache.id,
req.params.userId
);
const [inbox, error] = await openDMChannel(req.userCache.id, req.params.userId);
if (error) {
return res.status(400).json(error);
}
Expand Down
5 changes: 5 additions & 0 deletions src/services/User/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export const closeDMChannel = async (userId: string, channelId: string) => {
// if the recipient has not opened the channel, it will be created.
// if the recipient has opened the channel, we will create a new inbox with the existing channel id.
export const openDMChannel = async (userId: string, friendId: string) => {
const isValidFriendId = await prisma.user.findUnique({ where: { id: friendId } });
if (!isValidFriendId) {
return [null, generateError('Invalid userId')] as const;
}

const inbox = await prisma.inbox.findFirst({
where: {
OR: [
Expand Down

0 comments on commit b268471

Please sign in to comment.