-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnotifications.js
226 lines (208 loc) · 8.34 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {Microservices} from '../configs/microservices';
import rp from 'request-promise';
import Util from '../components/common/Util';
const log = require('../configs/log').log;
export default {
name: 'notifications',
// At least one of the CRUD methods is Required
read: (req, resource, params, config, callback) => {
req.reqId = req.reqId ? req.reqId : -1;
log.info({Id: req.reqId, Service: __filename.split('/').pop(), Resource: resource, Operation: 'read', Method: req.method});
let args = params.params? params.params : params;
let uid = args.uid;
if (uid === undefined) {
uid = 0;
}
if (resource === 'notifications.list'){
rp.get({uri: Microservices.notification.uri + '/notifications/' + uid + '?metaonly=false'}).then((res) => {
let notifications = JSON.parse(res).items;
//create path for slides and subdecks- GET DECK DATA for content_root_id FROM DECK SERVICE
let deckTreePromises = [];
for(let notification of notifications){
if (notification.content_root_id && notification.content_id.split('-')[0] !== notification.content_root_id.split('-')[0]) {
deckTreePromises.push(
rp.get({
uri: `${Microservices.deck.uri}/decktree/${notification.content_root_id}`,
json: true
})
);
}
}
Promise.all(deckTreePromises).then( (data) => {
let deckTrees = data;
for (let i = 0; i < deckTrees.length; i++) {
let deckTree = makePathForTree(deckTrees[i], []);
let deckId = deckTree.id;
let flatTree = flattenTree(deckTree);
for (let j = 0; j < notifications.length; j++) {
if (notifications[j].content_root_id && notifications[j].content_root_id.split('-')[0] === deckId.split('-')[0]) {
notifications[j].path = getPath(notifications[j].content_id, notifications[j].content_kind, deckId, flatTree);
}
}
}
callback(null, {notifications: notifications});
}).catch( (err) => {
console.log(err);
callback(null, {notifications: []});
});
}).catch((err) => {
console.log(err);
callback(null, {notifications: []});
});
} else if (resource === 'notifications.countnew'){
rp.get({uri: Microservices.notification.uri + '/notifications/' + uid + '?metaonly=true'}).then((res) => {
let count = JSON.parse(res).count;
callback(null, {count: count});
}).catch((err) => {
console.log(err);
callback(null, {count: 0});
});
}
},
delete: (req, resource, params, config, callback) => {
req.reqId = req.reqId ? req.reqId : -1;
log.info({Id: req.reqId, Service: __filename.split('/').pop(), Resource: resource, Operation: 'delete', Method: req.method});
let args = params.params? params.params : params;
if (resource === 'notifications.item'){
/*********connect to microservices*************/
const id = args.id;
let options = {
method: 'DELETE',
uri: Microservices.notification.uri + '/notification/delete',
body:JSON.stringify({
id: id
})
};
rp(options).then((res) => {
callback(null, {id: id});
}).catch((err) => {
console.log(err);
callback(err, params);
});
} else if (resource === 'notifications.all'){
/*********connect to microservices*************/
let uid = String(args.uid);
let options = {
method: 'DELETE',
uri: Microservices.notification.uri + '/notifications/delete',
body:JSON.stringify({
subscribed_user_id: uid
})
};
rp(options).then((res) => {
callback(null, params);
}).catch((err) => {
console.log(err);
callback(err, params);
});
}
},
update: (req, resource, params, body, config, callback) => {
req.reqId = req.reqId ? req.reqId : -1;
log.info({Id: req.reqId, Service: __filename.split('/').pop(), Resource: resource, Operation: 'read', Method: req.method});
let args = params.params? params.params : params;
let uid = args.uid;
if (uid === undefined) {
uid = 0;
}
if (resource === 'notifications.read'){
let id = args.id;
rp.put({
uri: Microservices.notification.uri + '/notification/mark/' + id,
body: JSON.stringify({
read: true
}),
resolveWithFullResponse: true
}).then((res) => {
callback(null, {id: id});
}).catch((err) => {
console.log(err);
callback(null, {});
});
} else if (resource === 'notifications.readall'){
rp.put({
uri: Microservices.notification.uri + '/notifications/markall/' + uid,
body: JSON.stringify({
read: true
}),
resolveWithFullResponse: true
}).then((res) => {
callback(null, {args: args});
}).catch((err) => {
console.log(err);
callback(null, {});
});
}
}
// other methods
// create: (req, resource, params, body, config, callback) => {},
};
//return the position of the node in the deck
function getPath(sid, type, deckId, flatTree){
let path = '';
for (let i = 0; i < flatTree.length; i++) {
if (flatTree[i].type === type && flatTree[i].id === sid) {
path = flatTree[i].path;
let nodeSelector = {id: deckId, stype: type, sid: sid, spath: path};
let nodeURL = Util.makeNodeURL(nodeSelector, 'deck', 'view', undefined, undefined, true);
return nodeURL;
}
}
return path;
}
//flat tree is used to avoid complex recursive functions on tree
//it is a trade off: updating the tree needs this to be synchronized
//this also propagates themes from decks to slide children
function flattenTree(deckTree, theme) {
if (!theme) theme = deckTree.theme;
let list = [];
list.push({
id: deckTree.id,
title: deckTree.title,
type: deckTree.type,
path: deckTree.path,
language: deckTree.language,
theme: theme
});
if (deckTree.type === 'deck') {
deckTree.children.forEach((item) => {
let theme = item.theme;
if (item.type === 'slide') theme = deckTree.theme;
list = list.concat(flattenTree(item, theme));
});
}
return list;
}
//deckTree: original deckTree from service without path
//path: array of binary id:position
function makePathForTree(deckTree, path) {
let nodePath = makeSelectorPathString(path);
let newTree = {
id: deckTree.id,
title: deckTree.title,
type: deckTree.type,
path: nodePath,
theme: deckTree.theme,
language: deckTree.language,
selected: false,
editable: false,
onAction: 0
};
if (deckTree.type === 'deck') {
newTree.children = [];
newTree.expanded = true;
deckTree.children.forEach((item, index) => {
newTree.children.push(makePathForTree(item, path.concat([[item.id, index + 1]])) );
});
}
return newTree;
}
//parses the nodePath and builds a selector path for navigation
function makeSelectorPathString(nodePath) {
let out = [], slectorPath = '';
nodePath.forEach((element) => {
out.push(element.join(':'));
});
slectorPath = out.join(';');
return slectorPath;
}