79 lines
2.3 KiB
TypeScript

import { Router } from 'express';
import { checkSocialEnabled } from './social.middleware';
import { socialActionRateLimit } from './social.rate-limits';
import { pokeService } from './poke.service';
import { sendFriendRequestSchema, friendsPaginationSchema } from './social.schemas';
const router = Router();
// All poke routes require social to be enabled
router.use(checkSocialEnabled);
/** POST /api/social/pokes — Send a poke */
router.post('/', socialActionRateLimit, async (req, res, next) => {
try {
const { userId } = sendFriendRequestSchema.parse(req.body); // reuse schema: { userId: cuid }
const poke = await pokeService.sendPoke(req.user!.id, userId);
res.status(201).json(poke);
} catch (err: any) {
if (err.statusCode) {
res.status(err.statusCode).json({ error: { message: err.message } });
return;
}
next(err);
}
});
/** GET /api/social/pokes — List received pokes */
router.get('/', async (req, res, next) => {
try {
const { page, limit } = friendsPaginationSchema.parse(req.query);
const result = await pokeService.listReceivedPokes(req.user!.id, page, limit);
res.json(result);
} catch (err) {
next(err);
}
});
/** GET /api/social/pokes/count — Unread poke count */
router.get('/count', async (req, res, next) => {
try {
const count = await pokeService.getUnreadPokeCount(req.user!.id);
res.json({ count });
} catch (err) {
next(err);
}
});
/** POST /api/social/pokes/:id/read — Mark poke as read */
router.post('/:id/read', async (req, res, next) => {
try {
const id = parseInt(req.params.id as string, 10);
if (isNaN(id)) {
res.status(400).json({ error: { message: 'Invalid poke ID' } });
return;
}
const poke = await pokeService.markPokeRead(req.user!.id, id);
res.json(poke);
} catch (err: any) {
if (err.statusCode) {
res.status(err.statusCode).json({ error: { message: err.message } });
return;
}
next(err);
}
});
/** GET /api/social/pokes/cooldown/:userId — Check poke cooldown */
router.get('/cooldown/:userId', async (req, res, next) => {
try {
const targetUserId = req.params.userId as string;
const ttl = await pokeService.getPokeCooldown(req.user!.id, targetUserId);
res.json({ onCooldown: ttl !== null, ttlSeconds: ttl });
} catch (err) {
next(err);
}
});
export { router as pokeRouter };