@mulingai-npm/redis 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,20 +9,28 @@ export type MulingstreamSpeakerData = {
9
9
  prompts: string;
10
10
  recordingsDuration: number;
11
11
  targetLanguages: string[];
12
+ timestamp: string;
12
13
  };
13
14
  export declare class MulingstreamSpeakerManager {
14
15
  private redisClient;
15
16
  constructor(redisClient: RedisClient);
16
- private parseHashData;
17
+ private parseHash;
17
18
  private serialize;
18
- private buildKey;
19
19
  private buildId;
20
- addSpeaker(payload: Omit<MulingstreamSpeakerData, 'speakerId'>): Promise<string>;
21
- removeSpeaker(roomId: string, socketId: string): Promise<boolean>;
22
- getSpeakersByRoom(roomId: string): Promise<MulingstreamSpeakerData[]>;
23
- getSpeakerBySocketId(roomId: string, socketId: string): Promise<MulingstreamSpeakerData | null>;
24
- getSpeakerByUserId(roomId: string, userId: string): Promise<MulingstreamSpeakerData | null>;
25
- updateSourceLanguage(roomId: string, socketId: string, newLang: string): Promise<boolean>;
26
- updateTargetLanguages(roomId: string, socketId: string, languages: string[]): Promise<boolean>;
27
- increaseRecordingDuration(roomId: string, socketId: string, delta: number): Promise<number>;
20
+ private buildKey;
21
+ addSpeaker(payload: Omit<MulingstreamSpeakerData, 'speakerId' | 'timestamp'>): Promise<string>;
22
+ removeSpeakerBySocketId(socketId: string): Promise<boolean>;
23
+ removeSpeakersByUserId(userId: string): Promise<number>;
24
+ removeSpeakersByRoomId(roomId: string): Promise<number>;
25
+ /** internal: remove hash + all index references */
26
+ private removeSpeakerById;
27
+ getSpeakerBySpeakerId(speakerId: string): Promise<MulingstreamSpeakerData | null>;
28
+ getSpeakerBySocketId(socketId: string): Promise<MulingstreamSpeakerData | null>;
29
+ getSpeakersByRoomId(roomId: string): Promise<MulingstreamSpeakerData[]>;
30
+ getSpeakersByUserId(userId: string): Promise<MulingstreamSpeakerData[]>;
31
+ getSpeakerByUserRoom(roomId: string, userId: string): Promise<MulingstreamSpeakerData | null>;
32
+ updateSourceLanguage(socketId: string, newLang: string): Promise<boolean>;
33
+ updateTargetLanguages(socketId: string, languages: string[]): Promise<boolean>;
34
+ increaseRecordingDuration(socketId: string, delta: number): Promise<number>;
35
+ private cleanIndexes;
28
36
  }
@@ -6,7 +6,8 @@ class MulingstreamSpeakerManager {
6
6
  constructor(redisClient) {
7
7
  this.redisClient = redisClient;
8
8
  }
9
- parseHashData(hash) {
9
+ /* ------------------------------------------------ helpers */
10
+ parseHash(hash) {
10
11
  return {
11
12
  speakerId: hash.speakerId,
12
13
  userId: hash.userId,
@@ -16,7 +17,8 @@ class MulingstreamSpeakerManager {
16
17
  theme: hash.theme,
17
18
  prompts: hash.prompts,
18
19
  recordingsDuration: parseInt(hash.recordingsDuration, 10) || 0,
19
- targetLanguages: JSON.parse(hash.targetLanguages || '[]')
20
+ targetLanguages: JSON.parse(hash.targetLanguages || '[]'),
21
+ timestamp: hash.timestamp
20
22
  };
21
23
  }
22
24
  serialize(data) {
@@ -29,79 +31,194 @@ class MulingstreamSpeakerManager {
29
31
  theme: data.theme,
30
32
  prompts: data.prompts,
31
33
  recordingsDuration: data.recordingsDuration.toString(),
32
- targetLanguages: JSON.stringify(data.targetLanguages)
34
+ targetLanguages: JSON.stringify(data.targetLanguages),
35
+ timestamp: data.timestamp
33
36
  };
34
37
  }
35
- buildKey(speakerId) {
36
- return `speaker:${speakerId}`;
37
- }
38
38
  buildId(roomId, userId, socketId) {
39
39
  return `[${roomId}]-[${userId}]-[${socketId}]`;
40
40
  }
41
+ buildKey(speakerId) {
42
+ return `speaker:${speakerId}`;
43
+ }
44
+ /* ------------------------------------------------ insert */
41
45
  async addSpeaker(payload) {
42
46
  const speakerId = this.buildId(payload.roomId, payload.userId, payload.socketId);
43
- await this.redisClient.hset(this.buildKey(speakerId), this.serialize({ ...payload, speakerId }));
44
- await this.redisClient.expire(this.buildKey(speakerId), EXPIRATION);
47
+ const key = this.buildKey(speakerId);
48
+ const speakerData = {
49
+ ...payload,
50
+ speakerId,
51
+ timestamp: new Date(Date.now()).toISOString()
52
+ };
53
+ await this.redisClient.hset(key, this.serialize(speakerData));
54
+ await this.redisClient.expire(key, EXPIRATION);
55
+ // index sets / mapping
56
+ await this.redisClient.sadd(`room:${payload.roomId}:speakers`, speakerId);
57
+ await this.redisClient.sadd(`user:${payload.userId}:speakers`, speakerId);
58
+ await this.redisClient.set(`socket:${payload.socketId}:speaker`, speakerId);
59
+ await this.redisClient.expire(`socket:${payload.socketId}:speaker`, EXPIRATION);
45
60
  return speakerId;
46
61
  }
47
- async removeSpeaker(roomId, socketId) {
48
- const speaker = await this.getSpeakerBySocketId(roomId, socketId);
49
- if (!speaker) {
62
+ /* ------------------------------------------------ remove helpers */
63
+ async removeSpeakerBySocketId(socketId) {
64
+ const speakerId = await this.redisClient.get(`socket:${socketId}:speaker`);
65
+ if (speakerId === null) {
50
66
  return false;
51
67
  }
52
- await this.redisClient.del(this.buildKey(speaker.speakerId));
68
+ const removed = await this.removeSpeakerById(speakerId);
69
+ return removed;
70
+ }
71
+ async removeSpeakersByUserId(userId) {
72
+ const ids = await this.redisClient.smembers(`user:${userId}:speakers`);
73
+ if (ids.length === 0) {
74
+ return 0;
75
+ }
76
+ let deletedCount = 0;
77
+ for (const id of ids) {
78
+ if (await this.removeSpeakerById(id)) {
79
+ deletedCount += 1;
80
+ }
81
+ }
82
+ return deletedCount;
83
+ }
84
+ async removeSpeakersByRoomId(roomId) {
85
+ const ids = await this.redisClient.smembers(`room:${roomId}:speakers`);
86
+ if (ids.length === 0) {
87
+ return 0;
88
+ }
89
+ let deletedCount = 0;
90
+ for (const id of ids) {
91
+ if (await this.removeSpeakerById(id)) {
92
+ deletedCount += 1;
93
+ }
94
+ }
95
+ return deletedCount;
96
+ }
97
+ /** internal: remove hash + all index references */
98
+ async removeSpeakerById(speakerId) {
99
+ const key = this.buildKey(speakerId);
100
+ const data = await this.redisClient.hgetall(key);
101
+ if (data === null || Object.keys(data).length === 0) {
102
+ // already gone; clean indexes anyway
103
+ await this.cleanIndexes(speakerId);
104
+ return false;
105
+ }
106
+ await this.redisClient.del(key);
107
+ await this.cleanIndexes(speakerId);
53
108
  return true;
54
109
  }
55
- async getSpeakersByRoom(roomId) {
56
- const keys = await this.redisClient.keys(`speaker:[${roomId}]-*`);
57
- if (!keys.length) {
58
- return [];
110
+ /* ------------------------------------------------ lookup helpers */
111
+ async getSpeakerBySpeakerId(speakerId) {
112
+ const key = this.buildKey(speakerId);
113
+ const hash = await this.redisClient.hgetall(key);
114
+ if (hash === null || Object.keys(hash).length === 0) {
115
+ /* the hash has expired or was never there – remove any stray index refs */
116
+ await this.cleanIndexes(speakerId);
117
+ return null;
118
+ }
119
+ return this.parseHash(hash);
120
+ }
121
+ async getSpeakerBySocketId(socketId) {
122
+ const speakerId = await this.redisClient.get(`socket:${socketId}:speaker`);
123
+ if (speakerId === null) {
124
+ return null;
59
125
  }
60
- const out = [];
61
- for (const k of keys) {
62
- const hash = await this.redisClient.hgetall(k);
63
- if (hash && Object.keys(hash).length)
64
- out.push(this.parseHashData(hash));
126
+ const hash = await this.redisClient.hgetall(this.buildKey(speakerId));
127
+ if (hash !== null && Object.keys(hash).length > 0) {
128
+ return this.parseHash(hash);
65
129
  }
66
- return out;
130
+ // hash expired: tidy indexes
131
+ await this.cleanIndexes(speakerId);
132
+ return null;
67
133
  }
68
- async getSpeakerBySocketId(roomId, socketId) {
69
- var _a;
70
- const speakers = await this.getSpeakersByRoom(roomId);
71
- return (_a = speakers.find((s) => s.socketId === socketId)) !== null && _a !== void 0 ? _a : null;
134
+ async getSpeakersByRoomId(roomId) {
135
+ const ids = await this.redisClient.smembers(`room:${roomId}:speakers`);
136
+ const result = [];
137
+ for (const id of ids) {
138
+ const hash = await this.redisClient.hgetall(this.buildKey(id));
139
+ if (hash !== null && Object.keys(hash).length > 0) {
140
+ result.push(this.parseHash(hash));
141
+ }
142
+ else {
143
+ await this.cleanIndexes(id);
144
+ }
145
+ }
146
+ return result;
72
147
  }
73
- async getSpeakerByUserId(roomId, userId) {
74
- var _a;
75
- const speakers = await this.getSpeakersByRoom(roomId);
76
- return (_a = speakers.find((s) => s.userId === userId)) !== null && _a !== void 0 ? _a : null;
148
+ async getSpeakersByUserId(userId) {
149
+ const ids = await this.redisClient.smembers(`user:${userId}:speakers`);
150
+ const result = [];
151
+ for (const id of ids) {
152
+ const hash = await this.redisClient.hgetall(this.buildKey(id));
153
+ if (hash !== null && Object.keys(hash).length > 0) {
154
+ result.push(this.parseHash(hash));
155
+ }
156
+ else {
157
+ await this.cleanIndexes(id);
158
+ }
159
+ }
160
+ return result;
77
161
  }
78
- async updateSourceLanguage(roomId, socketId, newLang) {
79
- const speaker = await this.getSpeakerBySocketId(roomId, socketId);
80
- if (!speaker)
162
+ async getSpeakerByUserRoom(roomId, userId) {
163
+ const ids = await this.redisClient.smembers(`room:${roomId}:speakers`);
164
+ for (const id of ids) {
165
+ if (id.includes(`]-[${userId}]-[`)) {
166
+ const hash = await this.redisClient.hgetall(this.buildKey(id));
167
+ if (hash !== null && Object.keys(hash).length > 0) {
168
+ return this.parseHash(hash);
169
+ }
170
+ await this.cleanIndexes(id);
171
+ return null;
172
+ }
173
+ }
174
+ return null;
175
+ }
176
+ /* ------------------------------------------------ update helpers */
177
+ async updateSourceLanguage(socketId, newLang) {
178
+ const speaker = await this.getSpeakerBySocketId(socketId);
179
+ if (speaker === null) {
81
180
  return false;
181
+ }
82
182
  await this.redisClient.hset(this.buildKey(speaker.speakerId), {
83
183
  sourceLanguage: newLang
84
184
  });
85
185
  return true;
86
186
  }
87
- async updateTargetLanguages(roomId, socketId, languages) {
88
- const speaker = await this.getSpeakerBySocketId(roomId, socketId);
89
- if (!speaker)
187
+ async updateTargetLanguages(socketId, languages) {
188
+ const speaker = await this.getSpeakerBySocketId(socketId);
189
+ if (speaker === null) {
90
190
  return false;
191
+ }
91
192
  await this.redisClient.hset(this.buildKey(speaker.speakerId), {
92
193
  targetLanguages: JSON.stringify(languages)
93
194
  });
94
195
  return true;
95
196
  }
96
- async increaseRecordingDuration(roomId, socketId, delta) {
97
- const speaker = await this.getSpeakerBySocketId(roomId, socketId);
98
- if (!speaker)
197
+ async increaseRecordingDuration(socketId, delta) {
198
+ const speaker = await this.getSpeakerBySocketId(socketId);
199
+ if (speaker === null) {
99
200
  return -1;
201
+ }
100
202
  speaker.recordingsDuration += delta;
101
203
  await this.redisClient.hset(this.buildKey(speaker.speakerId), {
102
204
  recordingsDuration: speaker.recordingsDuration.toString()
103
205
  });
104
206
  return speaker.recordingsDuration;
105
207
  }
208
+ /* ------------------------------------------------ index cleanup */
209
+ async cleanIndexes(speakerId) {
210
+ const parts = speakerId
211
+ .substring(1, speakerId.length - 1) // remove outer [...]
212
+ .split(']-['); // ['roomId','userId','socketId']
213
+ if (parts.length !== 3) {
214
+ return;
215
+ }
216
+ const roomId = parts[0];
217
+ const userId = parts[1];
218
+ const socketId = parts[2];
219
+ await this.redisClient.srem(`room:${roomId}:speakers`, speakerId);
220
+ await this.redisClient.srem(`user:${userId}:speakers`, speakerId);
221
+ await this.redisClient.del(`socket:${socketId}:speaker`);
222
+ }
106
223
  }
107
224
  exports.MulingstreamSpeakerManager = MulingstreamSpeakerManager;
@@ -52,10 +52,11 @@ class RedisClient {
52
52
  }
53
53
  // flush methods
54
54
  async flushAll() {
55
- console.log('Removing all data!');
55
+ console.log('Flushing All Redis Data!');
56
56
  return this.client.flushall();
57
57
  }
58
58
  async flushDb() {
59
+ console.log('Flushing Redis Data from DB!');
59
60
  return this.client.flushdb();
60
61
  }
61
62
  async expire(key, seconds) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {