Skip to content

Commit 252647c

Browse files
committed
fix: issues with thread subscriptions
I really screwed that up on my first attempt. Puter apps don't trigger the user-connected websocket event, which rendered thread subscriptions in apps useless. Furthermore, the uuid of the thread that was subscribed too wasn't present in the event, which is also pretty useless.
1 parent 26591e4 commit 252647c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,12 @@ class WebServerService extends BaseService {
259259
socket.on('trash.is_empty', (msg) => {
260260
socket.broadcast.to(socket.user.id).emit('trash.is_empty', msg);
261261
});
262+
const svc_event = this.services.get('event');
263+
svc_event.emit('web.socket.connected', {
264+
socket,
265+
user: socket.user
266+
});
262267
socket.on('puter_is_actually_open', async (msg) => {
263-
const svc_event = this.services.get('event');
264268
await context.sub({
265269
actor: socket.actor,
266270
}).arun(async () => {

src/backend/src/services/ThreadService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class ThreadService extends BaseService {
126126
this.socket_subs_ = {};
127127

128128
const svc_event = this.services.get('event');
129-
svc_event.on('web.socket.user-connected', async (_, { socket }) => {
129+
svc_event.on('web.socket.connected', async (_, { socket }) => {
130130
socket.on('disconnect', () => {
131131
for ( const uid in this.socket_subs_ ) {
132132
this.socket_subs_[uid].delete(socket.id);
@@ -156,7 +156,7 @@ class ThreadService extends BaseService {
156156
await svc_socketio.send(
157157
Array.from(this.socket_subs_[uid]).map(socket => ({ socket })),
158158
'thread.' + action,
159-
data,
159+
{ ...data, subscription: uid },
160160
);
161161
}
162162

src/puter-js/src/modules/Threads.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class Threads {
2525

2626
async create (spec, parent) {
2727
if ( typeof spec === 'string' ) spec = { text: spec };
28-
await this.req_('POST', '/threads/create', {
28+
return await this.req_('POST', '/threads/create', {
2929
...spec,
3030
...(parent ? { parent } : {}),
3131
});
@@ -49,7 +49,19 @@ export default class Threads {
4949
);
5050
}
5151

52-
async subscribe (uid) {
52+
async subscribe (uid, callback) {
5353
puter.fs.socket.emit('thread.sub-request', { uid });
54+
55+
// socket.io, which we use unfortunatelly, doesn't handle
56+
// wildcard events, so we have to just put them all here.
57+
const events = [
58+
'post', 'edit', 'delete', 'child-edit', 'child-delete',
59+
];
60+
61+
for ( const event of events ) {
62+
puter.fs.socket.on(`thread.${event}`, (data) => {
63+
if ( data.subscription === uid ) callback(event, data);
64+
});
65+
}
5466
}
5567
}

0 commit comments

Comments
 (0)