File size: 6,585 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 |
// @flow
const { db } = require('shared/db');
import type { DBUsersCommunities } from 'shared/types';
import { decrementMemberCount, setMemberCount } from './community';
/*
===========================================================
MODIFYING AND CREATING DATA IN USERSCOMMUNITIES
===========================================================
*/
// removes all the user relationships to a community. will be invoked when a
// community is deleted, at which point we don't want any records in the
// database to show a user relationship to the deleted community
// prettier-ignore
export const removeMembersInCommunity = async (communityId: string): Promise<?Object> => {
const usersCommunities = await db
.table('usersCommunities')
.getAll(communityId, { index: 'communityId' })
.run()
if (!usersCommunities || usersCommunities.length === 0) return
const leavePromise = await db
.table('usersCommunities')
.getAll(communityId, { index: 'communityId' })
.update({
isMember: false,
receiveNotifications: false,
})
.run();
return await Promise.all([
setMemberCount(communityId, 0),
leavePromise
])
};
// invoked when a user is deleting their account or being banned
export const removeUsersCommunityMemberships = async (userId: string) => {
const memberships = await db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.run();
if (!memberships || memberships.length === 0) return;
const memberCountPromises = memberships.map(member => {
return decrementMemberCount(member.communityId);
});
const removeMembershipsPromise = db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.update({
isOwner: false,
isModerator: false,
isMember: false,
isPending: false,
receiveNotifications: false,
})
.run();
return Promise.all([memberCountPromises, removeMembershipsPromise]);
};
/*
===========================================================
GETTING DATA FROM USERSCOMMUNITIES
===========================================================
*/
type Options = { first: number, after: number };
// prettier-ignore
export const getMembersInCommunity = (communityId: string, options: Options): Promise<Array<string>> => {
const { first, after } = options
return db
.table('usersCommunities')
.between([communityId, true, db.minval], [communityId, true, db.maxval], {
index: 'communityIdAndIsMemberAndReputation',
leftBound: 'open',
rightBound: 'open',
})
.orderBy({ index: db.desc('communityIdAndIsMemberAndReputation') })
.skip(after || 0)
.limit(first || 25)
.map(userCommunity => userCommunity('userId'))
.run()
};
// prettier-ignore
export const getModeratorsInCommunity = (communityId: string, options: Options): Promise<Array<string>> => {
return (
db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsModerator' })
.skip(options.after || 0)
.limit(options.first || 25)
.map(userCommunity => userCommunity('userId'))
.run()
);
};
export const getOwnersInCommunity = (
communityId: string,
options: Options
): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsOwner' })
.skip(options.after || 0)
.limit(options.first || 25)
.map(userCommunity => userCommunity('userId'))
.run();
};
export const getTeamMembersInCommunity = (
communityId: string,
options: Options
): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsTeamMember' })
.skip(options.after || 0)
.limit(options.first || 25)
.map(userCommunity => userCommunity('userId'))
.run();
};
export const DEFAULT_USER_COMMUNITY_PERMISSIONS = {
isOwner: false,
isMember: false,
isModerator: false,
isBlocked: false,
isPending: false,
receiveNotifications: false,
reputation: 0,
};
// NOTE @BRIAN: DEPRECATED - DONT USE IN THE FUTURE
// prettier-ignore
export const getUserPermissionsInCommunity = (communityId: string, userId: string): Promise<Object> => {
return db
.table('usersCommunities')
.getAll([userId, communityId], {
index: 'userIdAndCommunityId',
})
.run()
.then(data => {
// if a record exists
if (data.length > 0) {
return data[0];
} else {
// if a record doesn't exist, we're creating a new relationship
// so default to false for everything
return {
...DEFAULT_USER_COMMUNITY_PERMISSIONS,
userId,
communityId,
};
}
});
};
// prettier-ignore
export const checkUserPermissionsInCommunity = (communityId: string, userId: string): Promise<DBUsersCommunities> => {
return db
.table('usersCommunities')
.getAll([userId, communityId], { index: 'userIdAndCommunityId' })
.run();
};
type UserIdAndCommunityId = [?string, string];
// prettier-ignore
export const getUsersPermissionsInCommunities = (input: Array<UserIdAndCommunityId>) => {
return db
.table('usersCommunities')
.getAll(...input, { index: 'userIdAndCommunityId' })
.run()
.then(data => {
if (!data)
return Array.from({ length: input.length }, (_, index) => ({
...DEFAULT_USER_COMMUNITY_PERMISSIONS,
userId: input[index][0],
communityId: input[index][1],
}));
return data.map(
(rec, index) =>
rec
? rec
: {
...DEFAULT_USER_COMMUNITY_PERMISSIONS,
userId: input[index][0],
communityId: input[index][1],
}
);
});
};
export const getReputationByUser = (userId: string): Promise<Number> => {
return db
.table('usersCommunities')
.getAll([userId, true], { index: 'userIdAndIsMember' })
.map(rec => rec('reputation'))
.count()
.default(0)
.run();
};
// prettier-ignore
export const getUsersTotalReputation = (userIds: Array<string>): Promise<Array<number>> => {
return db
.table('usersCommunities')
.getAll(...userIds.map(userId => ([userId, true])), { index: 'userIdAndIsMember' })
.group('userId')
.map(rec => rec('reputation'))
.reduce((l, r) => l.add(r))
.default(0)
.run()
.then(res =>
res.map(
res =>
res && {
reputation: res.reduction,
userId: res.group,
}
)
);
};
|