Skip to content

Commit 8c49793

Browse files
committed
dev: thread subscriptions
1 parent 60ceced commit 8c49793

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

src/backend/src/modules/web/WebServerService.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,6 @@ class WebServerService extends BaseService {
254254

255255
const context = Context.get();
256256
socketio.on('connection', (socket) => {
257-
/**
258-
* Starts the web server and associated services.
259-
*
260-
* This method is responsible for starting the web server and its associated services. It first initializes the middlewares and routes for the server, then begins the server with the specified HTTP port. If the specified port is not available, it will try to find an available port within a range.
261-
*
262-
* @returns {Promise} A promise that resolves when the server is started.
263-
*/
264-
// eslint-disable-next-line no-unused-vars
265-
WebServerService.prototype.__on_start_webserver = async function () {
266-
// ...
267-
};
268257
socket.on('disconnect', () => {
269258
});
270259
socket.on('trash.is_empty', (msg) => {

src/backend/src/services/ThreadService.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,48 @@ class ThreadService extends BaseService {
118118
return {};
119119
}
120120
}));
121+
122+
await this.init_socket_subs_();
123+
}
124+
125+
async init_socket_subs_ () {
126+
this.socket_subs_ = {};
127+
128+
const svc_event = this.services.get('event');
129+
svc_event.on('web.socket.user-connected', async (_, { socket }) => {
130+
socket.on('disconnect', () => {
131+
for ( const uid in this.socket_subs_ ) {
132+
this.socket_subs_[uid].delete(socket.id);
133+
}
134+
});
135+
136+
socket.on('thread.sub-request', async ({ uid }) => {
137+
if ( ! this.socket_subs_[uid] ) {
138+
this.socket_subs_[uid] = new Set();
139+
}
140+
141+
this.socket_subs_[uid].add(socket.id);
142+
});
143+
144+
socket.on('thread.sub-cancel', async ({ uid }) => {
145+
if ( this.socket_subs_[uid] ) {
146+
this.socket_subs_[uid].delete(socket.id);
147+
}
148+
});
149+
});
121150
}
151+
152+
async notify_subscribers (uid, action, data) {
153+
if ( ! this.socket_subs_[uid] ) return;
154+
155+
const svc_socketio = this.services.get('socketio');
156+
await svc_socketio.send(
157+
Array.from(this.socket_subs_[uid]).map(socket => ({ socket })),
158+
'thread.' + action,
159+
data,
160+
);
161+
}
162+
122163
async ['__on_install.routes'] (_, { app }) {
123164
const r_threads = (() => {
124165
const require = this.require;
@@ -210,6 +251,16 @@ class ThreadService extends BaseService {
210251
}
211252

212253
res.json({ uid });
254+
255+
// Notify subscribers
256+
await this.notify_subscribers(parent_uid, 'post', {
257+
uid,
258+
text,
259+
user: {
260+
username: actor.type.user.username,
261+
uuid: actor.type.user.id,
262+
},
263+
});
213264
}
214265
}).attach(router);
215266

@@ -247,12 +298,12 @@ class ThreadService extends BaseService {
247298

248299
// Get existing thread
249300
const thread = await this.get_thread({ uid });
250-
console.log('thread???', thread);
251301
if ( !thread ) {
252302
throw APIError.create('thread_not_found', null, {
253303
uid,
254304
});
255305
}
306+
const parent_uid = thread.parent_uid;
256307

257308
const actor = Context.get('actor');
258309

@@ -276,6 +327,18 @@ class ThreadService extends BaseService {
276327
);
277328

278329
res.json({});
330+
331+
// Notify subscribers
332+
await this.notify_subscribers(uid, 'edit', {
333+
uid,
334+
text,
335+
});
336+
337+
// Notify parent subscribers
338+
await this.notify_subscribers(parent_uid, 'child-edit', {
339+
uid,
340+
text,
341+
});
279342
}
280343
}).attach(router);
281344

@@ -324,6 +387,11 @@ class ThreadService extends BaseService {
324387
);
325388

326389
res.json({});
390+
391+
// Notify subscribers
392+
await this.notify_subscribers(parent_uid, 'delete', {
393+
uid,
394+
});
327395
}
328396
}).attach(router);
329397

0 commit comments

Comments
 (0)