nodebb-plugin-integration 0.4.1 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,346 +1,63 @@
1
- /*
2
-
3
- NodeBB Integration Layer
4
-
5
-
6
- NodeBB Users Adapter
7
-
8
-
9
-
10
- */
11
-
12
-
13
-
14
-
15
- const NodeBB =
16
- require("./index");
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
- let nativeUsers = null;
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
- function loadNative() {
45
-
46
-
47
-
48
-
49
- if(nativeUsers) {
50
-
51
-
52
- return nativeUsers;
53
-
54
-
55
- }
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
- try {
65
-
66
-
67
-
68
- if(
69
-
70
- require.main &&
71
-
72
- require.main.require
73
-
74
- ) {
75
-
76
-
77
-
78
- nativeUsers =
79
-
80
- require.main.require(
81
-
82
- "./src/user"
83
-
84
- );
85
-
86
-
87
- }
88
-
89
-
90
-
91
- }
92
-
93
-
94
- catch(error) {
95
-
96
-
97
- nativeUsers = null;
98
-
99
-
100
- }
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
- return nativeUsers;
109
-
110
-
111
-
112
-
113
- }
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
- async function getUser(uid) {
128
-
129
-
130
-
131
-
132
- const users =
133
-
134
- loadNative();
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
- /*
144
- * Native NodeBB mode
145
- */
146
-
147
-
148
- if(
149
-
150
- users &&
151
-
152
- typeof users.getUserFields === "function"
153
-
154
- ) {
155
-
156
-
157
-
158
- return await users.getUserFields(
159
-
160
- uid,
161
-
162
- [
163
-
164
- "uid",
165
-
166
- "username",
167
-
168
- "userslug",
169
-
170
- "email",
171
-
172
- "picture"
173
-
174
- ]
175
-
176
- );
177
-
178
-
179
- }
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
- /*
189
- * Standalone mode
190
- */
191
-
192
-
193
- return {
194
-
195
-
196
- uid,
197
-
198
-
199
- username:
200
-
201
- "NodeBB unavailable",
202
-
203
-
204
-
205
- userslug:
206
-
207
- null,
208
-
209
-
210
-
211
- email:
212
-
213
- null,
214
-
215
-
216
-
217
- picture:
218
-
219
- null
220
-
221
-
222
-
223
- };
224
-
225
-
226
-
227
-
228
- }
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
- async function getUsers(uids = []) {
243
-
244
-
245
-
246
-
247
- const result = [];
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
- for(const uid of uids) {
257
-
258
-
259
-
260
- result.push(
261
-
262
- await getUser(uid)
263
-
264
- );
265
-
266
-
267
- }
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
- return result;
276
-
277
-
278
-
279
-
280
- }
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
- function info() {
295
-
296
-
297
-
298
-
299
- return {
300
-
301
-
302
- nodebb:
303
-
304
- NodeBB.info(),
305
-
306
-
307
-
308
- adapter:
309
-
310
- "users"
311
-
312
-
313
-
314
- };
315
-
316
-
317
-
318
-
319
- }
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
- module.exports = {
334
-
335
- getUser,
336
-
337
-
338
- getUsers,
339
-
340
-
341
- info
342
-
343
-
344
-
345
-
1
+ "use strict";
2
+
3
+ const NodeBB = require("./index");
4
+
5
+ const FIELDS = [
6
+ "uid",
7
+ "username",
8
+ "userslug",
9
+ "picture"
10
+ ];
11
+
12
+ async function getUser(uid) {
13
+ uid = Number(uid);
14
+
15
+ if (!Number.isSafeInteger(uid) || uid <= 0) {
16
+ throw new TypeError("Invalid user ID");
17
+ }
18
+
19
+ const users = NodeBB.get("users");
20
+
21
+ if (typeof users.getUserFields !== "function") {
22
+ throw new Error("NodeBB users.getUserFields is unavailable");
23
+ }
24
+
25
+ return users.getUserFields(uid, FIELDS);
26
+ }
27
+
28
+ async function getUsers(uids = []) {
29
+ if (!Array.isArray(uids)) {
30
+ throw new TypeError("User IDs must be an array");
31
+ }
32
+
33
+ const unique = [...new Set(
34
+ uids.map(Number).filter(
35
+ uid => Number.isSafeInteger(uid) && uid > 0
36
+ )
37
+ )];
38
+
39
+ if (!unique.length) {
40
+ return [];
41
+ }
42
+
43
+ const users = NodeBB.get("users");
44
+
45
+ if (typeof users.getUsersFields === "function") {
46
+ return users.getUsersFields(unique, FIELDS);
47
+ }
48
+
49
+ const result = [];
50
+
51
+ for (const uid of unique) {
52
+ const user = await getUser(uid);
53
+ if (user) result.push(user);
54
+ }
55
+
56
+ return result;
57
+ }
58
+
59
+ module.exports = {
60
+ getUser,
61
+ getUsers,
62
+ FIELDS
346
63
  };