|
2 | 2 | import type { DBCommunity } from 'shared/types'; |
3 | 3 | import type { GraphQLContext } from '../../'; |
4 | 4 | import UserError from '../../utils/UserError'; |
| 5 | +import { canAdministerCommunity } from '../../utils/permissions'; |
5 | 6 | import { getMessageCount } from '../../models/message'; |
6 | 7 | import { |
7 | 8 | getThreads, |
8 | 9 | getThreadsByCommunityInTimeframe, |
9 | 10 | } from '../../models/thread'; |
10 | 11 |
|
11 | | -export default ({ id }: DBCommunity, __: any, { user }: GraphQLContext) => { |
| 12 | +export default async ( |
| 13 | + { id }: DBCommunity, |
| 14 | + __: any, |
| 15 | + { user, loaders }: GraphQLContext |
| 16 | +) => { |
12 | 17 | const currentUser = user; |
13 | 18 |
|
14 | 19 | if (!currentUser) { |
15 | 20 | return new UserError('You must be signed in to continue.'); |
16 | 21 | } |
17 | 22 |
|
18 | | - return getThreadsByCommunityInTimeframe(id, 'weekly').then(async threads => { |
19 | | - if (!threads) return { topThreads: [], newThreads: [] }; |
20 | | - |
21 | | - const messageCountPromises = threads.map(async ({ id }) => ({ |
22 | | - id, |
23 | | - messageCount: await getMessageCount(id), |
24 | | - })); |
25 | | - |
26 | | - // promise all the active threads and message counts |
27 | | - const threadsWithMessageCounts = await Promise.all(messageCountPromises); |
28 | | - |
29 | | - const topThreads = threadsWithMessageCounts |
30 | | - .filter(t => t.messageCount > 0) |
31 | | - .sort((a, b) => { |
32 | | - const bc = parseInt(b.messageCount, 10); |
33 | | - const ac = parseInt(a.messageCount, 10); |
34 | | - return bc <= ac ? -1 : 1; |
35 | | - }) |
36 | | - .slice(0, 10) |
37 | | - .map(t => t.id); |
38 | | - |
39 | | - const newThreads = threadsWithMessageCounts |
40 | | - .filter(t => t.messageCount === 0) |
41 | | - .map(t => t.id); |
42 | | - |
43 | | - return { |
44 | | - topThreads: await getThreads([...topThreads]), |
45 | | - newThreads: await getThreads([...newThreads]), |
46 | | - }; |
47 | | - }); |
| 23 | + if (!await canAdministerCommunity(currentUser.id, id, loaders)) { |
| 24 | + return new UserError( |
| 25 | + 'You must be a team member to view community analytics.' |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + const threads = await getThreadsByCommunityInTimeframe(id, 'weekly'); |
| 30 | + |
| 31 | + if (!threads || threads.length === 0) |
| 32 | + return { topThreads: [], newThreads: [] }; |
| 33 | + |
| 34 | + const threadsWithMessageCounts = await Promise.all( |
| 35 | + threads.map(({ id }) => |
| 36 | + getMessageCount(id).then(messageCount => ({ |
| 37 | + id, |
| 38 | + messageCount, |
| 39 | + })) |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | + const topThreads = threadsWithMessageCounts |
| 44 | + .filter(t => t.messageCount > 0) |
| 45 | + .sort((a, b) => { |
| 46 | + const bc = parseInt(b.messageCount, 10); |
| 47 | + const ac = parseInt(a.messageCount, 10); |
| 48 | + return bc <= ac ? -1 : 1; |
| 49 | + }) |
| 50 | + .slice(0, 10) |
| 51 | + .map(t => t.id); |
| 52 | + |
| 53 | + const newThreads = threadsWithMessageCounts |
| 54 | + .filter(t => t.messageCount === 0) |
| 55 | + .map(t => t.id); |
| 56 | + |
| 57 | + return { |
| 58 | + topThreads: await getThreads([...topThreads]), |
| 59 | + newThreads: await getThreads([...newThreads]), |
| 60 | + }; |
48 | 61 | }; |
0 commit comments