File size: 10,389 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 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
// @flow
const { db } = require('shared/db');
import intersection from 'lodash.intersection';
import { uploadImage } from '../utils/file-storage';
import getRandomDefaultPhoto from '../utils/get-random-default-photo';
import type { DBCommunity } from 'shared/types';
export const getCommunityById = (id: string): Promise<DBCommunity> => {
return db
.table('communities')
.get(id)
.run()
.then(result => {
if (result && result.deletedAt) return null;
return result;
});
};
// prettier-ignore
export const getCommunities = (communityIds: Array<string>): Promise<Array<DBCommunity>> => {
return db
.table('communities')
.getAll(...communityIds)
.filter(community => db.not(community.hasFields('deletedAt')))
.run();
};
// prettier-ignore
export const getCommunitiesBySlug = (slugs: Array<string>): Promise<Array<DBCommunity>> => {
return db
.table('communities')
.getAll(...slugs, { index: 'slug' })
.filter(community => db.not(community.hasFields('deletedAt')))
.run();
};
export const getCommunityBySlug = (slug: string): Promise<?DBCommunity> => {
return db
.table('communities')
.getAll(slug, { index: 'slug' })
.filter(community => db.not(community.hasFields('deletedAt')))
.run()
.then(results => {
if (!results || results.length === 0) return null;
return results[0];
});
};
// prettier-ignore
export const getCommunitiesByUser = (userId: string): Promise<Array<DBCommunity>> => {
return (
db
.table('usersCommunities')
// get all the user's communities
.getAll([userId, true], { index: 'userIdAndIsMember' })
// get the community objects for each community
.eqJoin('communityId', db.table('communities'))
// get rid of unnecessary info from the usersCommunities object on the left
.without({ left: ['id', 'communityId', 'userId', 'createdAt'] })
// zip the tables
.zip()
// ensure we don't return any deleted communities
.filter(community => db.not(community.hasFields('deletedAt')))
.run()
);
};
// prettier-ignore
export const getVisibleCommunitiesByUser = async (evaluatingUserId: string, currentUserId: string) => {
const evaluatingUserMemberships = await db
.table('usersCommunities')
// get all the user's communities
.getAll([evaluatingUserId, true], { index: 'userIdAndIsMember' })
// get the community objects for each community
.eqJoin('communityId', db.table('communities'))
// get rid of unnecessary info from the usersCommunities object on the left
.without({ left: ['id', 'communityId', 'userId', 'createdAt'] })
// zip the tables
.zip()
// ensure we don't return any deleted communities
.filter(community => db.not(community.hasFields('deletedAt')))
.run()
const currentUserMemberships = await db
.table('usersCommunities')
// get all the user's communities
.getAll([currentUserId, true], { index: 'userIdAndIsMember' })
// get the community objects for each community
.eqJoin('communityId', db.table('communities'))
// get rid of unnecessary info from the usersCommunities object on the left
.without({ left: ['id', 'communityId', 'userId', 'createdAt'] })
// zip the tables
.zip()
// ensure we don't return any deleted communities
.filter(community => db.not(community.hasFields('deletedAt')))
.run()
const evaluatingUserCommunityIds = evaluatingUserMemberships.map(community => community.id)
const currentUserCommunityIds = currentUserMemberships.map(community => community.id)
const publicCommunityIds = evaluatingUserMemberships
.filter(community => !community.isPrivate)
.map(community => community.id)
const overlappingMemberships = intersection(evaluatingUserCommunityIds, currentUserCommunityIds)
const allVisibleCommunityIds = [...publicCommunityIds, ...overlappingMemberships]
const distinctCommunityIds = allVisibleCommunityIds.filter((x, i, a) => a.indexOf(x) === i)
return await db
.table('communities')
.getAll(...distinctCommunityIds)
.run()
}
export const getPublicCommunitiesByUser = async (userId: string) => {
return await db
.table('usersCommunities')
// get all the user's communities
.getAll([userId, true], { index: 'userIdAndIsMember' })
// get the community objects for each community
.eqJoin('communityId', db.table('communities'))
// only return public community ids
.filter(row => row('right')('isPrivate').eq(false))
// get rid of unnecessary info from the usersCommunities object on the left
.without({ left: ['id', 'communityId', 'userId', 'createdAt'] })
// zip the tables
.zip()
// ensure we don't return any deleted communities
.filter(community => db.not(community.hasFields('deletedAt')))
.run();
};
export const getCommunitiesChannelCounts = (communityIds: Array<string>) => {
return db
.table('channels')
.getAll(...communityIds, { index: 'communityId' })
.filter(channel => db.not(channel.hasFields('deletedAt')))
.group('communityId')
.count()
.run();
};
export const getCommunitiesMemberCounts = (communityIds: Array<string>) => {
return db
.table('usersCommunities')
.getAll(...communityIds.map(id => [id, true]), {
index: 'communityIdAndIsMember',
})
.group('communityId')
.count()
.run();
};
export type EditCommunityInput = {
input: {
name: string,
slug: string,
description: string,
website: string,
file: Object,
coverFile: Object,
coverPhoto: string,
communityId: string,
watercoolerId?: boolean,
},
};
// prettier-ignore
export const editCommunity = async ({ input }: EditCommunityInput, userId: string): Promise<DBCommunity> => {
const { name, slug, description, website, watercoolerId, file, coverPhoto, coverFile, communityId } = input
let community = await db.table('communities').get(communityId).run()
// if the input comes in with a coverPhoto of length 0 (empty string), it means
// the user was trying to delete or reset their cover photo from the front end.
// in this case we can just set a new default. Otherwise, just keep their
// original cover photo
let updatedCoverPhoto = community.coverPhoto
if (input.coverPhoto.length === 0) {
({ coverPhoto: updatedCoverPhoto } = getRandomDefaultPhoto())
}
return db
.table('communities')
.get(communityId)
.update({
...community,
name,
slug,
description,
website,
watercoolerId: watercoolerId || community.watercoolerId,
coverPhoto: coverFile
? await uploadImage(coverFile, 'communities', community.id)
: updatedCoverPhoto,
profilePhoto: file
? await uploadImage(file, 'communities', community.id)
: community.profilePhoto,
modifiedAt: new Date()
}, { returnChanges: 'always' })
.run()
.then(result => {
if (result.replaced === 1) {
community = result.changes[0].new_val;
}
// an update was triggered from the client, but no data was changed
if (result.unchanged === 1) {
community = result.changes[0].old_val;
}
return community
})
};
export const toggleCommunityRedirect = async (communityId: string) => {
const community = await db.table('communities').get(communityId);
if (!community) return null;
return db
.table('communities')
.get(communityId)
.update(
{
redirect: !community.redirect,
},
{
returnChanges: true,
}
)
.then(result => {
if (!Array.isArray(result.changes) || result.changes.length === 0)
return getCommunityById(communityId);
return result.changes[0].new_val;
});
};
export const toggleCommunityNoindex = async (communityId: string) => {
const community = await db.table('communities').get(communityId);
if (!community) return null;
return db
.table('communities')
.get(communityId)
.update(
{
noindex: !community.noindex,
},
{
returnChanges: true,
}
)
.then(result => {
if (!Array.isArray(result.changes) || result.changes.length === 0)
return getCommunityById(communityId);
return result.changes[0].new_val;
});
};
// prettier-ignore
export const deleteCommunity = (communityId: string, userId: string): Promise<DBCommunity> => {
return db
.table('communities')
.get(communityId)
.update(
{
deletedBy: userId,
deletedAt: new Date(),
slug: db.uuid(),
},
{
returnChanges: 'always',
nonAtomic: true,
}
)
.run()
};
// prettier-ignore
export const userIsMemberOfAnyChannelInCommunity = (communityId: string, userId: string): Promise<Boolean> => {
return db('spectrum')
.table('channels')
.getAll(communityId, { index: 'communityId' })
.eqJoin('id', db.table('usersChannels'), { index: 'channelId' })
.zip()
.filter({ userId })
.pluck('isMember')
.run()
.then(channels => channels.some(channel => channel.isMember));
};
export const getRecentCommunities = (): Array<DBCommunity> => {
return db
.table('communities')
.orderBy({ index: db.desc('createdAt') })
.filter(community => db.not(community.hasFields('deletedAt')))
.limit(100)
.run();
};
export const getThreadCount = (communityId: string) => {
return db
.table('threads')
.getAll(communityId, { index: 'communityId' })
.filter(thread => db.not(thread.hasFields('deletedAt')))
.count()
.run();
};
export const setMemberCount = (
communityId: string,
value: number
): Promise<DBCommunity> => {
return db
.table('communities')
.get(communityId)
.update(
{
memberCount: value,
},
{ returnChanges: true }
)
.run()
.then(result => result.changes[0].new_val || result.changes[0].old_val);
};
export const decrementMemberCount = (
communityId: string
): Promise<DBCommunity> => {
return db
.table('communities')
.get(communityId)
.update(
{
memberCount: db
.row('memberCount')
.default(1)
.sub(1),
},
{ returnChanges: true }
)
.run()
.then(result => result.changes[0].new_val || result.changes[0].old_val);
};
|