File size: 6,871 Bytes
1e92f2d |
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 |
// @flow
const { db } = require('shared/db');
import type { DBChannel } from 'shared/types';
// reusable query parts -- begin
const channelsByCommunitiesQuery = (...communityIds: string[]) =>
db
.table('channels')
.getAll(...communityIds, { index: 'communityId' })
.filter(channel => channel.hasFields('deletedAt').not());
const channelsByIdsQuery = (...channelIds: string[]) =>
db
.table('channels')
.getAll(...channelIds)
.filter(channel => channel.hasFields('deletedAt').not());
const threadsByChannelsQuery = (...channelIds: string[]) =>
channelsByIdsQuery(...channelIds)
.eqJoin('id', db.table('threads'), { index: 'channelId' })
.map(row => row('right'))
.filter(thread => db.not(thread.hasFields('deletedAt')));
const membersByChannelsQuery = (...channelIds: string[]) =>
channelsByIdsQuery(...channelIds)
.eqJoin('id', db.table('usersChannels'), { index: 'channelId' })
.map(row => row('right'))
.filter({ isBlocked: false, isPending: false, isMember: true });
// reusable query parts -- end
// prettier-ignore
const getChannelsByCommunity = (communityId: string): Promise<Array<DBChannel>> => {
return channelsByCommunitiesQuery(communityId).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
*/
// prettier-ignore
const getPublicChannelsByCommunity = (communityId: string): Promise<Array<string>> => {
return channelsByCommunitiesQuery(communityId)
.filter({ isPrivate: false })
.filter(row => row.hasFields('archivedAt').not())
.map(c => c('id'))
.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
*/
// prettier-ignore
const getChannelsByUserAndCommunity = async (communityId: string, userId: string): Promise<Array<string>> => {
const channels = await channelsByCommunitiesQuery(communityId).run();
const unarchived = channels.filter(channel => !channel.archivedAt)
const channelIds = unarchived.map(channel => channel.id)
return db
.table('usersChannels')
.getAll(...channelIds.map(id => ([userId, id])), {
index: 'userIdAndChannelId',
})
.filter({ isMember: true })('channelId')
.run();
};
const getChannelsByUser = (userId: string): Promise<Array<DBChannel>> => {
return db
.table('usersChannels')
.getAll([userId, 'member'], [userId, 'moderator'], [userId, 'owner'], {
index: 'userIdAndRole',
})
.eqJoin('channelId', db.table('channels'))
.without({ left: ['id', 'channelId', 'userId', 'createdAt'] })
.zip()
.filter(channel => db.not(channel.hasFields('deletedAt')))
.run();
};
const getChannelBySlug = async (
channelSlug: string,
communitySlug: string
): Promise<?DBChannel> => {
const [communityId] = await db
.table('communities')
.getAll(communitySlug, { index: 'slug' })('id')
.run();
if (!communityId) return null;
return db
.table('channels')
.getAll(communityId, { index: 'communityId' })
.filter(channel =>
channel('slug')
.eq(channelSlug)
.and(db.not(channel.hasFields('deletedAt')))
)
.run()
.then(res => {
if (Array.isArray(res) && res.length > 0) return res[0];
return null;
});
};
const getChannelById = async (id: string) => {
return (await channelsByIdsQuery(id).run())[0] || null;
};
type GetChannelByIdArgs = {|
id: string,
|};
type GetChannelBySlugArgs = {|
slug: string,
communitySlug: string,
|};
export type GetChannelArgs = GetChannelByIdArgs | GetChannelBySlugArgs;
const getChannels = (channelIds: Array<string>): Promise<Array<DBChannel>> => {
return channelsByIdsQuery(...channelIds).run();
};
export type EditChannelInput = {
input: {
channelId: string,
name: string,
description: string,
slug: string,
isPrivate: Boolean,
},
};
// prettier-ignore
const editChannel = async ({ input }: EditChannelInput): Promise<DBChannel> => {
const { name, slug, description, isPrivate, channelId } = input;
const channelRecord = await db
.table('channels')
.get(channelId)
.run()
.then(result => {
return Object.assign({}, result, {
name,
description,
slug,
isPrivate,
});
});
return db
.table('channels')
.get(channelId)
.update({ ...channelRecord }, { 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;
}
return null;
});
};
const deleteChannel = (channelId: string, userId: string): Promise<Boolean> => {
return db
.table('channels')
.get(channelId)
.update(
{
deletedBy: userId,
deletedAt: new Date(),
slug: db.uuid(),
},
{
returnChanges: true,
nonAtomic: true,
}
)
.run();
};
const setMemberCount = (
channelId: string,
value: number
): Promise<DBChannel> => {
return db
.table('channels')
.get(channelId)
.update(
{
memberCount: value,
},
{ returnChanges: true }
)
.run()
.then(result => result.changes[0].new_val || result.changes[0].old_val);
};
const decrementMemberCount = (channelId: string): Promise<DBChannel> => {
return db
.table('channels')
.get(channelId)
.update(
{
memberCount: db
.row('memberCount')
.default(1)
.sub(1),
},
{ returnChanges: true }
)
.run()
.then(result => result.changes[0].new_val || result.changes[0].old_val);
};
type GroupedCount = {
group: string,
reduction: number,
};
// prettier-ignore
const getChannelsThreadCounts = (channelIds: Array<string>): Promise<Array<GroupedCount>> => {
return threadsByChannelsQuery(...channelIds)
.group('channelId')
.count()
.run();
};
module.exports = {
getChannelBySlug,
getChannelById,
getChannelsByUser,
getChannelsByCommunity,
getPublicChannelsByCommunity,
getChannelsByUserAndCommunity,
editChannel,
deleteChannel,
getChannels,
setMemberCount,
decrementMemberCount,
getChannelsThreadCounts,
__forQueryTests: {
channelsByCommunitiesQuery,
channelsByIdsQuery,
threadsByChannelsQuery,
membersByChannelsQuery,
},
};
|