forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.js
49 lines (45 loc) · 1.63 KB
/
notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import koa_router from 'koa-router';
import koa_body from 'koa-body';
import Tarantool from 'db/tarantool';
function toResArray(result) {
if (!result || result.length < 1) return [];
return result[0].slice(1);
}
export default function useNotificationsApi(app) {
const router = koa_router({prefix: '/api/v1'});
app.use(router.routes());
// get all notifications for account
router.get('/notifications/:account', function *() {
const account = this.params.account;
// TODO: make sure account name matches session
console.log('-- GET /notifications/:account -->', account);
try {
const res = yield Tarantool.instance().select('notifications', 0, 1, 0, 'eq', account);
this.body = toResArray(res);
} catch (error) {
console.error('-- /notifications/:account error -->', error.message);
this.body = [];
}
return;
});
// mark account's notification as read
router.put('/notifications/:account/:ids', function *() {
const {account, ids} = this.params;
if (!ids) {
this.body = []; return;
}
console.log('-- PUT /notifications/:account/:id -->', account, ids);
const fields = ids.split('-');
try {
let res;
for(const id of fields) {
res = yield Tarantool.instance().call('notification_read', account, id);
}
this.body = toResArray(res);
} catch (error) {
console.error('-- /notifications/:account/:id error -->', error.message);
this.body = [];
}
return;
});
}