Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(r, conn) {
return r
.table('usersCommunities')
.indexCreate('userIdAndIsMember', [r.row('userId'), r.row('isMember')])
.run(conn);
};

exports.down = function(r, conn) {
return r
.table('usersCommunities')
.indexDrop('userIdAndIsMember')
.run(conn);
};
29 changes: 10 additions & 19 deletions api/models/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ export const getCommunitiesByUser = (userId: string): Promise<Array<DBCommunity>
db
.table('usersCommunities')
// get all the user's communities
.getAll(userId, { index: 'userId' })
// only return communities the user is a member of
.filter({ isMember: true })
.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
Expand All @@ -81,9 +79,7 @@ export const getVisibleCommunitiesByUser = async (evaluatingUserId: string, curr
const evaluatingUserMemberships = await db
.table('usersCommunities')
// get all the user's communities
.getAll(evaluatingUserId, { index: 'userId' })
// only return communities the user is a member of
.filter({ isMember: true })
.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
Expand All @@ -97,9 +93,7 @@ export const getVisibleCommunitiesByUser = async (evaluatingUserId: string, curr
const currentUserMemberships = await db
.table('usersCommunities')
// get all the user's communities
.getAll(currentUserId, { index: 'userId' })
// only return communities the user is a member of
.filter({ isMember: true })
.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
Expand Down Expand Up @@ -130,9 +124,7 @@ export const getPublicCommunitiesByUser = async (userId: string) => {
return await db
.table('usersCommunities')
// get all the user's communities
.getAll(userId, { index: 'userId' })
// only return communities the user is a member of
.filter({ isMember: true })
.getAll([userId, true], { index: 'userIdAndIsMember' })
// get the community objects for each community
.eqJoin('communityId', db.table('communities'))
// only return public community ids
Expand All @@ -159,8 +151,9 @@ export const getCommunitiesChannelCounts = (communityIds: Array<string>) => {
export const getCommunitiesMemberCounts = (communityIds: Array<string>) => {
return db
.table('usersCommunities')
.getAll(...communityIds, { index: 'communityId' })
.filter({ isBlocked: false, isMember: true })
.getAll(...communityIds.map(id => [id, true]), {
index: 'communityIdAndIsMember',
})
.group('communityId')
.count()
.run();
Expand All @@ -171,10 +164,9 @@ export const getCommunitiesOnlineMemberCounts = (
) => {
return db
.table('usersCommunities')
.getAll(...communityIds, {
index: 'communityId',
.getAll(...communityIds.map(id => [id, true]), {
index: 'communityIdAndIsMember',
})
.filter({ isBlocked: false, isMember: true })
.pluck(['communityId', 'userId'])
.eqJoin('userId', db.table('users'))
.pluck('left', { right: ['lastSeen', 'isOnline'] })
Expand Down Expand Up @@ -812,8 +804,7 @@ export const setMemberCount = (
export const getMemberCount = (communityId: string): Promise<number> => {
return db
.table('usersCommunities')
.getAll(communityId, { index: 'communityId' })
.filter({ isMember: true })
.getAll([communityId, true], { index: 'communityIdAndIsMember' })
.count()
.run();
};
3 changes: 1 addition & 2 deletions api/models/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const saveUserCommunityPermissions = (
): Promise<Object> => {
return db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.filter({ communityId })
.getAll([userId, communityId], { index: 'userIdAndCommunityId' })
.update(
{
...permissions,
Expand Down
6 changes: 2 additions & 4 deletions api/models/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export const getUsersJoinedChannels = (userId: string): Promise<Array<string>> =
export const getUsersJoinedCommunities = (userId: string): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.getAll([userId, true], { index: 'userIdAndIsMember' })
.eqJoin('communityId', db.table('communities'))
.filter(row => row('right').hasFields('deletedAt').not())
.zip()
Expand All @@ -129,8 +128,7 @@ export const getUsersJoinedPrivateChannelIds = (userId: string): Promise<Array<s
export const getUsersJoinedPrivateCommunityIds = (userId: string): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.getAll([userId, true], { index: 'userIdAndIsMember' })
.eqJoin('communityId', db.table('communities'))
.filter(row => row('right')('isPrivate').eq(true).and(row('right').hasFields('deletedAt').not()))
.without({ left: ['id'] })
Expand Down
6 changes: 2 additions & 4 deletions api/models/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ export const getViewableThreadsByUser = async (

const getCurrentUserCommunityIds = db
.table('usersCommunities')
.getAll(currentUser, { index: 'userId' })
.filter({ isMember: true })
.getAll([currentUser, true], { index: 'userIdAndIsMember' })
.map(userCommunity => userCommunity('communityId'))
.run();

Expand Down Expand Up @@ -281,8 +280,7 @@ export const getViewableParticipantThreadsByUser = async (

const getCurrentUserCommunityIds = db
.table('usersCommunities')
.getAll(currentUser, { index: 'userId' })
.filter({ isMember: true })
.getAll([currentUser, true], { index: 'userIdAndIsMember' })
.map(userCommunity => userCommunity('communityId'))
.run();

Expand Down
10 changes: 4 additions & 6 deletions api/models/usersCommunities.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,9 @@ export const getUsersPermissionsInCommunities = (input: Array<UserIdAndCommunity
export const getReputationByUser = (userId: string): Promise<Number> => {
return db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.getAll([userId, true], { index: 'userIdAndIsMember' })
.map(rec => rec('reputation'))
.reduce((l, r) => l.add(r))
.count()
.default(0)
.run();
};
Expand All @@ -691,11 +690,10 @@ export const getReputationByUser = (userId: string): Promise<Number> => {
export const getUsersTotalReputation = (userIds: Array<string>): Promise<Array<number>> => {
return db
.table('usersCommunities')
.getAll(...userIds, { index: 'userId' })
.filter({ isMember: true })
.getAll(...userIds.map(userId => ([userId, true])), { index: 'userIdAndIsMember' })
.group('userId')
.map(rec => rec('reputation'))
.reduce((l, r) => l.add(r))
.count()
.default(0)
.run()
.then(res =>
Expand Down
20 changes: 9 additions & 11 deletions athena/models/usersCommunities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ export const getMembersInCommunity = (
return db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsMember' })
.filter({ receiveNotifications: true })
.run()
.then(users => users.map(user => user.userId));
.filter({ receiveNotifications: true })('userId')
.run();
};

export const getOwnersInCommunity = (
communityId: string
): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsOwner' })
.run()
.then(users => users.map(user => user.userId));
.getAll([communityId, true], { index: 'communityIdAndIsOwner' })('userId')
.run();
};

export const getModeratorsInCommunity = (
communityId: string
): Promise<Array<string>> => {
return db
.table('usersCommunities')
.getAll([communityId, true], { index: 'communityIdAndIsModerator' })
.run()
.then(users => users.map(user => user.userId));
.getAll([communityId, true], { index: 'communityIdAndIsModerator' })(
'userId'
)
.run();
};

export const getUserPermissionsInCommunity = (
Expand All @@ -38,8 +37,7 @@ export const getUserPermissionsInCommunity = (
): Promise<Object> => {
return db
.table('usersCommunities')
.getAll(communityId, { index: 'communityId' })
.filter({ userId })
.getAll([userId, communityId], { index: 'userIdAndCommunityId' })
.run()
.then(data => {
// if a record exists
Expand Down
17 changes: 4 additions & 13 deletions chronos/models/usersCommunities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ export const getUsersCommunityIds = (
userId: string
): Promise<Array<string>> => {
debug(userId);
return (
db
.table('usersCommunities')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.run()
// return an array of the userIds to be loaded by gql
.then(
communities =>
debug(communities) ||
communities.map(community => community.communityId)
)
);
return db
.table('usersCommunities')
.getAll([userId, true], { index: 'userIdAndIsMember' })('communityId')
.run();
};