1
+ const APIError = require ( "../api/APIError" ) ;
2
+ const auth2 = require ( "../middleware/auth2" ) ;
3
+ const { Endpoint } = require ( "../util/expressutil" ) ;
1
4
const BaseService = require ( "./BaseService" ) ;
2
5
const { DB_WRITE } = require ( "./database/consts" ) ;
3
6
@@ -10,6 +13,7 @@ const UsernameNotifSelector = username => async (self) => {
10
13
class NotificationService extends BaseService {
11
14
static MODULES = {
12
15
uuidv4 : require ( 'uuid' ) . v4 ,
16
+ express : require ( 'express' ) ,
13
17
}
14
18
15
19
async _init ( ) {
@@ -29,6 +33,39 @@ class NotificationService extends BaseService {
29
33
} ) ;
30
34
}
31
35
36
+ [ '__on_install.routes' ] ( _ , { app } ) {
37
+ const require = this . require ;
38
+ const express = require ( 'express' ) ;
39
+ const router = express . Router ( ) ;
40
+ app . use ( '/notif' , router ) ;
41
+
42
+ router . use ( auth2 ) ;
43
+
44
+ Endpoint ( {
45
+ route : '/mark-read' ,
46
+ methods : [ 'POST' ] ,
47
+ handler : async ( req , res ) => {
48
+ // TODO: validate uid
49
+ if ( typeof req . body . uid !== 'string' ) {
50
+ throw APIError . create ( 'field_invalid' , null , {
51
+ key : 'uid' ,
52
+ expected : 'a valid UUID' ,
53
+ got : 'non-string value'
54
+ } )
55
+ }
56
+
57
+ await this . db . write (
58
+ 'UPDATE `notification` SET read=1 ' +
59
+ 'WHERE uid = ? AND user_id = ? ' +
60
+ 'LIMIT 1' ,
61
+ [ req . body . uid , req . user . id ] ,
62
+ ) ;
63
+
64
+ res . json ( { } ) ;
65
+ }
66
+ } ) . attach ( router ) ;
67
+ }
68
+
32
69
async on_user_connected ( { user } ) {
33
70
// query the users unread notifications
34
71
const notifications = await this . db . read (
@@ -74,11 +111,6 @@ class NotificationService extends BaseService {
74
111
} ,
75
112
} ) ;
76
113
77
- const ll = o => {
78
- this . log . noticeme ( 'debug: ' + require ( 'node:util' ) . inspect ( o ) ) ;
79
- return o ;
80
- } ;
81
-
82
114
( async ( ) => {
83
115
for ( const user_id of user_id_list ) {
84
116
await this . db . write ( ...ll ( [
0 commit comments