This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathchannel.js
301 lines (277 loc) · 7.17 KB
/
channel.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// @flow
const { db } = require('./db');
import { addQueue } from '../utils/workerQueue';
import UserError from '../utils/UserError';
const getChannelsByCommunity = (
communityId: string
): Promise<Array<Object>> => {
return db
.table('channels')
.getAll(communityId, { index: 'communityId' })
.filter(channel => db.not(channel.hasFields('deletedAt')))
.run();
};
/*
If a non-user is viewing a community page, they should only see threads
from public channels. We use this function to return an array of channelIds
that are public, and pass them into a getThreads function
*/
const getPublicChannelsByCommunity = (
communityId: string
): Promise<Array<Object>> => {
return db
.table('channels')
.getAll(communityId, { index: 'communityId' })
.filter(channel => db.not(channel.hasFields('deletedAt')))
.filter({ isPrivate: false })
.run();
};
/*
If a user is viewing a community, they should see threads from all public channels as well as from private channels they are a member of.
This function returns an array of objects with the field 'id' that corresponds
to a channelId. This array of IDs will be passed into a threads method which
will only return threads in those channels
*/
const getChannelsByUserAndCommunity = (
communityId: string,
userId: string
): Promise<Array<Object>> => {
return (
db
.table('channels')
.getAll(communityId, { index: 'communityId' })
.eqJoin('id', db.table('usersChannels'), { index: 'channelId' })
// get channels where the user is a member OR the channel is public
.filter(row =>
row('left')('isPrivate')
.eq(false)
.or(
row('right')('isMember')
.eq(true)
.and(row('right')('userId').eq(userId))
)
)
.without({ right: 'id' })
.zip()
.pluck('id')
.distinct()
.run()
);
};
const getChannelsByUser = (userId: string): Promise<Array<Object>> => {
return (
db
.table('usersChannels')
// get all the user's channels
.getAll(userId, { index: 'userId' })
// only return channels where the user is a member
.filter({ isMember: true })
// get the channel objects for each channel
.eqJoin('channelId', db.table('channels'))
// get rid of unnecessary info from the usersChannels object on the left
.without({ left: ['id', 'channelId', 'userId', 'createdAt'] })
// zip the tables
.zip()
// ensure we don't return any deleted channels
.filter(channel => db.not(channel.hasFields('deletedAt')))
.run()
);
};
const getChannelBySlug = (
channelSlug: string,
communitySlug: string
): Promise<Object> => {
return db
.table('channels')
.filter(channel =>
channel('slug')
.eq(channelSlug)
.and(db.not(channel.hasFields('deletedAt')))
)
.eqJoin('communityId', db.table('communities'))
.filter({ right: { slug: communitySlug } })
.run()
.then(result => {
if (result && result[0]) {
return result[0].left;
}
});
};
type GetChannelByIdArgs = {
id: string,
};
type GetChannelBySlugArgs = {
slug: string,
communitySlug: string,
};
export type GetChannelArgs = GetChannelByIdArgs | GetChannelBySlugArgs;
const getChannels = (channelIds: Array<string>): Promise<Array<Object>> => {
return db
.table('channels')
.getAll(...channelIds)
.filter(channel => db.not(channel.hasFields('deletedAt')))
.run();
};
const getChannelMetaData = (channelId: string): Promise<Array<number>> => {
const getThreadCount = db
.table('threads')
.getAll(channelId, { index: 'channelId' })
.count()
.run();
const getMemberCount = db
.table('usersChannels')
.getAll(channelId, { index: 'channelId' })
.filter({ isBlocked: false, isPending: false })
.count()
.run();
return Promise.all([getThreadCount, getMemberCount]);
};
export type CreateChannelArguments = {
input: {
communityId: string,
name: string,
description: string,
slug: string,
isPrivate: boolean,
isDefault: boolean,
},
};
export type EditChannelArguments = {
input: {
channelId: string,
name: string,
description: string,
slug: string,
isPrivate: Boolean,
},
};
const createChannel = (
{
input: { communityId, name, slug, description, isPrivate, isDefault },
}: CreateChannelArguments,
userId: string
): Promise<Object> => {
return db
.table('channels')
.insert(
{
communityId,
createdAt: new Date(),
name,
description,
slug,
isPrivate,
isDefault: isDefault ? true : false,
},
{ returnChanges: true }
)
.run()
.then(result => result.changes[0].new_val)
.then(channel => {
// only trigger a new channel notification is the channel is public
if (!channel.isPrivate) {
addQueue('channel notification', { channel, userId });
}
return channel;
});
};
const createGeneralChannel = (
communityId: string,
userId: string
): Promise<Object> => {
return createChannel(
{
input: {
name: 'General',
slug: 'general',
description: 'General Chatter',
communityId,
isPrivate: false,
isDefault: true,
},
},
userId
);
};
const editChannel = ({
input: { name, slug, description, isPrivate, channelId },
}: EditChannelArguments): Object => {
return db
.table('channels')
.get(channelId)
.run()
.then(result => {
return Object.assign({}, result, {
name,
description,
slug,
isPrivate,
});
})
.then(obj => {
return db
.table('channels')
.get(channelId)
.update({ ...obj }, { returnChanges: 'always' })
.run()
.then(result => {
// if an update happened
if (result.replaced === 1) {
return result.changes[0].new_val;
}
// an update was triggered from the client, but no data was changed
if (result.unchanged === 1) {
return result.changes[0].old_val;
}
});
});
};
/*
We delete data non-destructively, meaning the record does not get cleared
from the db.
*/
const deleteChannel = (channelId: string): Promise<Boolean> => {
return db
.table('channels')
.get(channelId)
.update(
{
deletedAt: new Date(),
slug: db.uuid(),
},
{
returnChanges: true,
nonAtomic: true,
}
)
.run()
.then(result => {
// update was successful
if (result.replaced >= 1) {
return true;
}
// update failed
return new UserError(
"Something went wrong and we weren't able to delete this channel."
);
});
};
const getChannelMemberCount = (channelId: string): number => {
return db.table('channels').get(channelId)('members')
.count()
.run();
};
module.exports = {
getChannelBySlug,
getChannelMetaData,
getChannelsByUser,
getChannelsByCommunity,
getPublicChannelsByCommunity,
getChannelsByUserAndCommunity,
createChannel,
createGeneralChannel,
editChannel,
deleteChannel,
getChannelMemberCount,
getChannels,
};