-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-queue-api.ts
More file actions
186 lines (154 loc) · 6.68 KB
/
vite-plugin-queue-api.ts
File metadata and controls
186 lines (154 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Plugin } from 'vite';
import Redis from 'ioredis';
export function queueApiPlugin(): Plugin {
let redis: Redis;
return {
name: 'queue-api-middleware',
configureServer(server) {
// Initialize Redis client
redis = new Redis({
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379'),
});
server.middlewares.use(async (req, res, next) => {
// Get track history for user in room (with weekday/time context)
if (req.url?.startsWith('/api/rooms/') && req.url?.includes('/history') && req.method === 'GET') {
try {
const urlParts = req.url.split('/');
const roomId = urlParts[3];
const userId = urlParts[5]; // /api/rooms/:roomId/history/:userId
if (!roomId || !userId) {
res.statusCode = 400;
res.end(JSON.stringify({ error: 'Missing roomId or userId' }));
return;
}
// Get history from Redis sorted set
const historyKey = `room:${roomId}:user:${userId}:history`;
const entries = await redis.zrange(historyKey, 0, -1);
const history = entries.map(entry => JSON.parse(entry));
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(history));
} catch (error) {
console.error('[Queue API] Get history error:', error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Failed to get history' }));
}
return;
}
// Get play counts for user in room
if (req.url?.startsWith('/api/rooms/') && req.url?.includes('/playcounts') && req.method === 'GET') {
try {
const urlParts = req.url.split('/');
const roomId = urlParts[3];
const userId = urlParts[5]; // /api/rooms/:roomId/playcounts/:userId
if (!roomId || !userId) {
res.statusCode = 400;
res.end(JSON.stringify({ error: 'Missing roomId or userId' }));
return;
}
// Get all keys for this user's play counts in this room
const pattern = `room:${roomId}:user:${userId}:track:*`;
const keys = await redis.keys(pattern);
const playCounts: Record<string, number> = {};
for (const key of keys) {
const count = await redis.get(key);
// Extract track ID from key: room:roomId:user:userId:track:trackId
const trackId = key.split(':').slice(5).join(':');
playCounts[trackId] = parseInt(count || '0', 10);
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(playCounts));
} catch (error) {
console.error('[Queue API] Get play counts error:', error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Failed to get play counts' }));
}
return;
}
// Move track up in queue (for own tracks only)
if (req.url?.startsWith('/api/rooms/') && req.url?.includes('/queue/move') && req.method === 'POST') {
try {
const roomId = req.url.split('/')[3];
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', async () => {
try {
const { trackId, userId, direction } = JSON.parse(body);
if (!trackId || !userId || !direction) {
res.statusCode = 400;
res.end(JSON.stringify({ error: 'Missing trackId, userId, or direction' }));
return;
}
// Get current queue from Redis
const queueData = await redis.get(`room:${roomId}:queue`);
const queue = queueData ? JSON.parse(queueData) : { tracks: [] };
// Find the track
const trackIndex = queue.tracks.findIndex((t: { id: string }) => t.id === trackId);
if (trackIndex === -1) {
res.statusCode = 404;
res.end(JSON.stringify({ error: 'Track not found' }));
return;
}
const track = queue.tracks[trackIndex];
// Verify ownership
if (track.addedBy !== userId) {
res.statusCode = 403;
res.end(JSON.stringify({ error: 'Can only move your own tracks' }));
return;
}
// Can't move the currently playing track (index 0)
if (trackIndex === 0) {
res.statusCode = 400;
res.end(JSON.stringify({ error: 'Cannot move currently playing track' }));
return;
}
// Calculate new position
let newIndex = trackIndex;
if (direction === 'up') {
newIndex = Math.max(1, trackIndex - 1); // Can't go before currently playing
} else if (direction === 'down') {
newIndex = Math.min(queue.tracks.length - 1, trackIndex + 1);
}
// If position didn't change, just return current queue
if (newIndex === trackIndex) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ tracks: queue.tracks }));
return;
}
// Move the track
queue.tracks.splice(trackIndex, 1);
queue.tracks.splice(newIndex, 0, track);
// Save to Redis
await redis.set(`room:${roomId}:queue`, JSON.stringify(queue));
console.log(`[Queue API] Moved track ${trackId} ${direction} in room ${roomId}`);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ tracks: queue.tracks }));
} catch (error) {
console.error('[Queue API] Move track error:', error);
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Failed to move track' }));
}
});
return;
} catch (error) {
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Failed to process request' }));
return;
}
}
next();
});
},
closeBundle() {
if (redis) {
redis.disconnect();
}
},
};
}