-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdisplay.js
92 lines (70 loc) · 2.56 KB
/
display.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
'use strict';
const Schmervice = require('@hapipal/schmervice');
const internals = {};
module.exports = class DisplayService extends Schmervice.Service {
user({ password, ...user }, token) {
return { ...user, token };
}
async profile(currentUserId, user, transaction) {
const { User } = this.server.models();
const { toProfile } = internals;
const result = await User.fetchGraph(user, `[
followedBy(currentUser) as following
]`, {
transaction
}).modifiers({
currentUser: (builder) => builder.where('Users.id', currentUserId)
});
return toProfile(result);
}
async articles(currentUserId, articles, transaction) {
const { Article } = this.server.models();
const { toArticle } = internals;
const results = await Article.fetchGraph(articles, `[
tags,
favoritedBy(currentUser) as favorited,
favoritedBy(count) as favoritesCount,
author.[
followedBy(currentUser) as following
]
]`, {
transaction
}).modifiers({
currentUser: (builder) => builder.where('Users.id', currentUserId),
count: (builder) => builder.count('* as count').groupBy('articleId')
});
return Array.isArray(results) ? results.map(toArticle) : toArticle(results);
}
tags(tags) {
return tags.map((tag) => tag.name);
}
async comments(currentUserId, comments, transaction) {
const { Comment } = this.server.models();
const { toComment } = internals;
const results = await Comment.fetchGraph(comments, `[
author.[
followedBy(currentUser) as following
]
]`, {
transaction
}).modifiers({
currentUser: (builder) => builder.where('Users.id', currentUserId)
});
return Array.isArray(results) ? results.map(toComment) : toComment(results);
}
};
internals.toProfile = ({ password, email, following, ...user }) => ({
...user,
following: (following.length > 0)
});
internals.toArticle = ({ tags, favorited, favoritesCount, authorId, author, ...article }) => ({
...article,
tagList: tags.map((tag) => tag.name),
favorited: (favorited.length > 0),
favoritesCount: favoritesCount[0] ? favoritesCount[0].count : 0,
author: internals.toProfile(author)
});
internals.toComment = ({ articleId, authorId, author, ...comment }) => ({
...comment,
author: internals.toProfile(author)
});