Skip to content

Commit a1e6887

Browse files
committed
feat: add service for notifications
1 parent 7e13ab1 commit a1e6887

File tree

6 files changed

+87
-22
lines changed

6 files changed

+87
-22
lines changed

packages/backend/src/CoreModule.js

+3
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ const install = async ({ services, app, useapi }) => {
242242

243243
const { BroadcastService } = require('./services/BroadcastService');
244244
services.registerService('broadcast', BroadcastService);
245+
246+
const { NotificationService } = require('./services/NotificationService');
247+
services.registerService('notification', NotificationService);
245248
}
246249

247250
const install_legacy = async ({ services }) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const BaseService = require("./BaseService");
2+
const { DB_WRITE } = require("./database/consts");
3+
4+
const UsernameNotifSelector = username => async (self) => {
5+
const svc_getUser = self.services.get('get-user');
6+
const user = await svc_getUser.get_user({ username });
7+
return [user.id];
8+
};
9+
10+
class NotificationService extends BaseService {
11+
static MODULES = {
12+
uuidv4: require('uuid').v4,
13+
}
14+
15+
async _init () {
16+
const svc_database = this.services.get('database');
17+
this.db = svc_database.get(DB_WRITE, 'notification');
18+
19+
const svc_script = this.services.get('script');
20+
svc_script.register('test-notification', async ({ log }, [username, summary]) => {
21+
log('creating notification: ' + summary);
22+
23+
this.notify(UsernameNotifSelector(username), { summary });
24+
});
25+
}
26+
async notify (selector, notification) {
27+
const uid = this.modules.uuidv4();
28+
const svc_event = this.services.get('event');
29+
const user_id_list = await selector(this);
30+
svc_event.emit('outer.gui.notif.message', {
31+
user_id_list,
32+
response: {
33+
uid,
34+
notification,
35+
},
36+
});
37+
38+
const ll = o => {
39+
this.log.noticeme('debug: ' + require('node:util').inspect(o));
40+
return o;
41+
};
42+
43+
(async () => {
44+
for ( const user_id of user_id_list ) {
45+
await this.db.write(...ll([
46+
'INSERT INTO `notification` ' +
47+
'(`user_id`, `uid`, `value`) ' +
48+
'VALUES (?, ?, ?)',
49+
[user_id, uid, JSON.stringify(notification)],
50+
]));
51+
}
52+
svc_event.emit('outer.gui.notif.persisted', {
53+
user_id_list,
54+
response: {
55+
uid,
56+
},
57+
});
58+
})();
59+
}
60+
}
61+
62+
module.exports = {
63+
NotificationService,
64+
UsernameNotifSelector,
65+
};

packages/backend/src/services/WSPushService.js

-21
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ class WSPushService extends AdvancedBase {
6868

6969
Object.assign(response, metadata);
7070

71-
const io = socketio.getio();
72-
for ( const user_id of user_id_list ) {
73-
io.to(user_id).emit('item.added', response);
74-
}
75-
7671
this.svc_event.emit('outer.gui.item.added', {
7772
user_id_list,
7873
response,
@@ -107,11 +102,6 @@ class WSPushService extends AdvancedBase {
107102

108103
Object.assign(response, metadata);
109104

110-
const io = socketio.getio();
111-
for ( const user_id of user_id_list ) {
112-
io.to(user_id).emit('item.updated', response);
113-
}
114-
115105
this.svc_event.emit('outer.gui.item.updated', {
116106
user_id_list,
117107
response,
@@ -147,11 +137,6 @@ class WSPushService extends AdvancedBase {
147137
response.old_path = old_path;
148138
Object.assign(response, metadata);
149139

150-
const io = socketio.getio();
151-
for ( const user_id of user_id_list ) {
152-
io.to(user_id).emit('item.moved', response);
153-
}
154-
155140
this.svc_event.emit('outer.gui.item.moved', {
156141
user_id_list,
157142
response,
@@ -185,10 +170,6 @@ class WSPushService extends AdvancedBase {
185170

186171
Object.assign(response, metadata);
187172

188-
const io = socketio.getio();
189-
for ( const user_id of user_id_list ) {
190-
io.to(user_id).emit('item.pending', response);
191-
}
192173
this.svc_event.emit('outer.gui.item.pending', {
193174
user_id_list,
194175
response,
@@ -248,8 +229,6 @@ class WSPushService extends AdvancedBase {
248229
}
249230

250231
async _on_outer_gui (key, { user_id_list, response }, meta) {
251-
if ( ! meta.from_outside ) return;
252-
253232
key = key.slice('outer.gui.'.length);
254233

255234
const { socketio } = this.modules;

packages/backend/src/services/database/SqliteDatabaseAccessService.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
4242
this.db = new Database(this.config.path);
4343

4444
// Database upgrade logic
45-
const TARGET_VERSION = 8;
45+
const TARGET_VERSION = 9;
4646

4747
if ( do_setup ) {
4848
this.log.noticeme(`SETUP: creating database at ${this.config.path}`);
@@ -105,6 +105,10 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
105105
upgrade_files.push('0010_add-git-app.sql');
106106
}
107107

108+
if ( user_version <= 8 ) {
109+
upgrade_files.push('0011_notification.sql');
110+
}
111+
108112
if ( upgrade_files.length > 0 ) {
109113
this.log.noticeme(`Database out of date: ${this.config.path}`);
110114
this.log.noticeme(`UPGRADING DATABASE: ${user_version} -> ${TARGET_VERSION}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE `notification` (
2+
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
3+
`user_id` INTEGER NOT NULL,
4+
`uid` TEXT NOT NULL UNIQUE,
5+
`value` JSON NOT NULL,
6+
`read` tinyint(1) DEFAULT '0',
7+
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
8+
);

src/UI/UIDesktop.js

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ async function UIDesktop(options){
110110
if(msg.is_empty)
111111
$(`.window[data-path="${html_encode(window.trash_path)}" i]`).find('.item-container').empty();
112112
})
113+
114+
window.socket.on('notif.message', ({ notification }) => {
115+
UINotification({
116+
content: notification.summary
117+
});
118+
});
113119

114120
window.socket.on('app.opened', async (app) => {
115121
// don't update if this is the original client that initiated the action

0 commit comments

Comments
 (0)