Files
spotlightcam/backend/src/routes/events.js
Radosław Gierwiało 9d8fc9f6d6 feat: add chat message history and infinite scroll
Backend changes:
- Socket.IO: Send last 20 messages on join_event_room
- REST API: Add GET /api/events/:eventId/messages endpoint with pagination
- Support for 'before' cursor-based pagination for loading older messages

Frontend changes:
- Load initial 20 messages when joining event chat
- Implement infinite scroll to load older messages on scroll to top
- Add loading indicator for older messages
- Preserve scroll position when loading older messages
- Add eventsAPI.getMessages() function for pagination

User experience:
- New users see last 20 messages immediately
- Scrolling up automatically loads older messages in batches of 20
- Smooth scrolling experience with position restoration

Note: Messages are encrypted in transit via HTTPS/WSS but stored
as plain text in database (no E2E encryption).
2025-11-13 20:16:58 +01:00

135 lines
2.8 KiB
JavaScript

const express = require('express');
const { prisma } = require('../utils/db');
const { authenticate } = require('../middleware/auth');
const router = express.Router();
// GET /api/events - List all events
router.get('/', async (req, res, next) => {
try {
const events = await prisma.event.findMany({
orderBy: {
startDate: 'asc',
},
select: {
id: true,
name: true,
location: true,
startDate: true,
endDate: true,
worldsdcId: true,
participantsCount: true,
description: true,
createdAt: true,
},
});
res.json({
success: true,
count: events.length,
data: events,
});
} catch (error) {
next(error);
}
});
// GET /api/events/:id - Get event by ID
router.get('/:id', async (req, res, next) => {
try {
const { id } = req.params;
const event = await prisma.event.findUnique({
where: {
id: parseInt(id),
},
include: {
chatRooms: true,
_count: {
select: {
matches: true,
},
},
},
});
if (!event) {
return res.status(404).json({
success: false,
error: 'Event not found',
});
}
res.json({
success: true,
data: event,
});
} catch (error) {
next(error);
}
});
// GET /api/events/:eventId/messages - Get event chat messages with pagination
router.get('/:eventId/messages', authenticate, async (req, res, next) => {
try {
const { eventId } = req.params;
const { before, limit = 20 } = req.query;
// Find event chat room
const chatRoom = await prisma.chatRoom.findFirst({
where: {
eventId: parseInt(eventId),
type: 'event',
},
});
if (!chatRoom) {
return res.status(404).json({
success: false,
error: 'Chat room not found',
});
}
// Build query with pagination
const where = { roomId: chatRoom.id };
if (before) {
where.id = { lt: parseInt(before) };
}
const messages = await prisma.message.findMany({
where,
include: {
user: {
select: {
id: true,
username: true,
avatar: true,
},
},
},
orderBy: { createdAt: 'desc' },
take: parseInt(limit),
});
// Return in chronological order (oldest first)
res.json({
success: true,
data: messages.reverse().map(msg => ({
id: msg.id,
roomId: msg.roomId,
userId: msg.user.id,
username: msg.user.username,
avatar: msg.user.avatar,
content: msg.content,
type: msg.type,
createdAt: msg.createdAt,
})),
hasMore: messages.length === parseInt(limit),
});
} catch (error) {
next(error);
}
});
module.exports = router;