This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFeed.js
102 lines (97 loc) · 2.86 KB
/
Feed.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
const {descending} = require('d3-array')
module.exports = {
async userIsEligitable (feed, args, {pgdb, user}) {
if (!user) { return false }
return !!(await pgdb.public.memberships.findFirst({userId: user.id}))
},
async userWaitUntil (feed, args, {pgdb, user}) {
if (!user || !feed.commentInterval) { return }
const now = new Date().getTime()
const lastCommentByUser = await pgdb.public.comments.findFirst({
userId: user.id,
feedId: feed.id,
published: true
}, {
orderBy: ['createdAt desc']
})
if (lastCommentByUser && lastCommentByUser.createdAt.getTime() > now - feed.commentInterval) { return new Date((lastCommentByUser.createdAt.getTime() + feed.commentInterval)) }
},
async comments (feed, args, {pgdb}) {
const {offset, limit, firstId, tags, order} = args
const firstComment = firstId
? await pgdb.public.comments.findOne({
id: firstId,
feedId: feed.id,
published: true,
adminUnpublished: false
})
: null
let orderBy = 'hottnes DESC'
if (order === 'NEW') { orderBy = '"createdAt" DESC' }
if (order === 'TOP') { orderBy = '"upVotes" - "downVotes" DESC' }
const comments = (await pgdb.public.comments.find({
feedId: feed.id,
published: true,
adminUnpublished: false,
'tags @>': tags,
tags: (tags && tags.length === 0) ? '[]' : undefined
}, {
offset,
limit,
orderBy: [orderBy],
skipUndefined: true
})).map(c => Object.assign({}, c, {
score: c.upVotes - c.downVotes
}))
return firstComment
? [firstComment].concat(comments.filter(c => c.id !== firstComment.id))
: comments
},
async stats (feed, args, {pgdb}) {
return {
count: pgdb.public.comments.count({
feedId: feed.id,
published: true,
adminUnpublished: false
}),
tags: [
{
tag: null,
count: await pgdb.queryOneField(`
SELECT
count(*)
FROM
comments c
WHERE
c."feedId"=:feedId AND
c.tags = '[]' AND
c.published=:published AND
c."adminUnpublished"=:adminUnpublished
`, {
feedId: feed.id,
published: true,
adminUnpublished: false
})
}
]
.concat(await pgdb.query(`
SELECT
tag as tag,
count(*) as count
FROM
comments c,
json_array_elements_text(c.tags::json) tag
WHERE
c."feedId"=:feedId AND
c.published=:published AND
c."adminUnpublished"=:adminUnpublished
GROUP BY 1
`, {
feedId: feed.id,
published: true,
adminUnpublished: false
}))
.sort((a, b) => descending(a.count, b.count))
}
}
}