Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 1941778

Browse files
authored
Merge pull request #4320 from withspectrum/2.4.74
2.4.74
2 parents 892a831 + c31053d commit 1941778

File tree

66 files changed

+735
-539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+735
-539
lines changed

api/apollo-server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ const server = new ProtectedApolloServer({
141141
},
142142
maxFileSize: 25 * 1024 * 1024, // 25MB
143143
engine: false,
144-
tracing: true,
145-
cacheControl: true,
144+
tracing: false,
145+
cacheControl: false,
146146
validationRules: [depthLimit(10)],
147147
});
148148

api/index.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,7 @@ app.use(errorHandler);
7575
const httpServer = createServer(app);
7676
apolloServer.installSubscriptionHandlers(httpServer);
7777

78-
// Start API wrapped in Apollo Engine
79-
const engine = new ApolloEngine({
80-
logging: {
81-
level: 'WARN',
82-
},
83-
apiKey: process.env.APOLLO_ENGINE_API_KEY,
84-
// Only send perf data to the remote server in production
85-
reporting: {
86-
disabled: process.env.NODE_ENV !== 'production',
87-
hostname: process.env.NOW_URL || undefined,
88-
privateHeaders: ['authorization', 'Authorization', 'AUTHORIZATION'],
89-
},
90-
queryCache: {
91-
// Don't cache logged-in user responses
92-
privateFullQueryStore: 'disabled',
93-
},
94-
sessionAuth: {
95-
cookie: 'session',
96-
// TODO(@mxstbr): Ping Apollo to note that we need both of those
97-
// header: 'Authorization'
98-
},
99-
});
100-
101-
engine.listen({
102-
port: PORT,
103-
httpServer: httpServer,
104-
graphqlPaths: ['/api'],
105-
});
78+
httpServer.listen(PORT);
10679

10780
debug(`GraphQL API running at http://localhost:${PORT}/api`);
10881

api/loaders/channel.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getChannels,
44
getChannelsThreadCounts,
55
getChannelsMemberCounts,
6+
getChannelsOnlineMemberCounts,
67
} from '../models/channel';
78
import { getChannelsSettings } from '../models/channelSettings';
89
import createLoader from './create-loader';
@@ -27,6 +28,11 @@ export const __createChannelMemberCountLoader = createLoader(
2728
'group'
2829
);
2930

31+
export const __createChannelOnlineMemberCountLoader = createLoader(
32+
channelIds => getChannelsOnlineMemberCounts(channelIds),
33+
'group'
34+
);
35+
3036
export const __createChannelSettingsLoader = createLoader(
3137
channelIds => getChannelsSettings(channelIds),
3238
key => key.channelId

api/loaders/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
__createChannelLoader,
1818
__createChannelThreadCountLoader,
1919
__createChannelMemberCountLoader,
20+
__createChannelOnlineMemberCountLoader,
2021
__createChannelPendingMembersLoader,
2122
__createChannelSettingsLoader,
2223
} from './channel';
@@ -57,6 +58,7 @@ const createLoaders = (options?: DataLoaderOptions) => ({
5758
notification: __createNotificationLoader(options),
5859
channel: __createChannelLoader(options),
5960
channelMemberCount: __createChannelMemberCountLoader(options),
61+
channelOnlineMemberCount: __createChannelOnlineMemberCountLoader(options),
6062
channelThreadCount: __createChannelThreadCountLoader(options),
6163
channelPendingUsers: __createChannelPendingMembersLoader(options),
6264
channelSettings: __createChannelSettingsLoader(options),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
exports.up = function(r, conn) {
2+
return Promise.all([
3+
r
4+
.table('communities')
5+
.update(
6+
{
7+
memberCount: r
8+
.table('usersCommunities')
9+
.getAll(r.row('id'), { index: 'communityId' })
10+
.filter(row => row('isMember').eq(true))
11+
.count()
12+
.default(1),
13+
},
14+
{
15+
nonAtomic: true,
16+
}
17+
)
18+
.run(conn),
19+
r
20+
.table('channels')
21+
.update(
22+
{
23+
memberCount: r
24+
.table('usersChannels')
25+
.getAll(r.row('id'), { index: 'channelId' })
26+
.filter(row => row('isMember').eq(true))
27+
.count()
28+
.default(1),
29+
},
30+
{
31+
nonAtomic: true,
32+
}
33+
)
34+
.run(conn),
35+
]).catch(err => console.error(err));
36+
};
37+
exports.down = function(r, conn) {
38+
return Promise.resolve();
39+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.up = function(r, conn) {
2+
return r
3+
.table('usersCommunities')
4+
.indexCreate('userIdAndIsMember', [r.row('userId'), r.row('isMember')])
5+
.run(conn);
6+
};
7+
8+
exports.down = function(r, conn) {
9+
return r
10+
.table('usersCommunities')
11+
.indexDrop('userIdAndIsMember')
12+
.run(conn);
13+
};

api/models/channel.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,36 @@ const getMemberCount = (channelId: string): Promise<number> => {
435435
.run();
436436
};
437437

438+
const getChannelsOnlineMemberCounts = (channelIds: Array<string>) => {
439+
return db
440+
.table('usersChannels')
441+
.getAll(...channelIds, {
442+
index: 'channelId',
443+
})
444+
.filter({ isBlocked: false, isMember: true })
445+
.pluck(['channelId', 'userId'])
446+
.eqJoin('userId', db.table('users'))
447+
.pluck('left', { right: ['lastSeen', 'isOnline'] })
448+
.zip()
449+
.filter(rec =>
450+
rec('isOnline')
451+
.eq(true)
452+
.or(
453+
rec('lastSeen')
454+
.toEpochTime()
455+
.ge(
456+
db
457+
.now()
458+
.toEpochTime()
459+
.sub(86400)
460+
)
461+
)
462+
)
463+
.group('channelId')
464+
.count()
465+
.run();
466+
};
467+
438468
module.exports = {
439469
getChannelBySlug,
440470
getChannelById,
@@ -456,6 +486,7 @@ module.exports = {
456486
decrementMemberCount,
457487
setMemberCount,
458488
getMemberCount,
489+
getChannelsOnlineMemberCounts,
459490
__forQueryTests: {
460491
channelsByCommunitiesQuery,
461492
channelsByIdsQuery,

api/models/community.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export const getCommunitiesByUser = (userId: string): Promise<Array<DBCommunity>
6161
db
6262
.table('usersCommunities')
6363
// get all the user's communities
64-
.getAll(userId, { index: 'userId' })
65-
// only return communities the user is a member of
66-
.filter({ isMember: true })
64+
.getAll([userId, true], { index: 'userIdAndIsMember' })
6765
// get the community objects for each community
6866
.eqJoin('communityId', db.table('communities'))
6967
// get rid of unnecessary info from the usersCommunities object on the left
@@ -81,9 +79,7 @@ export const getVisibleCommunitiesByUser = async (evaluatingUserId: string, curr
8179
const evaluatingUserMemberships = await db
8280
.table('usersCommunities')
8381
// get all the user's communities
84-
.getAll(evaluatingUserId, { index: 'userId' })
85-
// only return communities the user is a member of
86-
.filter({ isMember: true })
82+
.getAll([evaluatingUserId, true], { index: 'userIdAndIsMember' })
8783
// get the community objects for each community
8884
.eqJoin('communityId', db.table('communities'))
8985
// get rid of unnecessary info from the usersCommunities object on the left
@@ -97,9 +93,7 @@ export const getVisibleCommunitiesByUser = async (evaluatingUserId: string, curr
9793
const currentUserMemberships = await db
9894
.table('usersCommunities')
9995
// get all the user's communities
100-
.getAll(currentUserId, { index: 'userId' })
101-
// only return communities the user is a member of
102-
.filter({ isMember: true })
96+
.getAll([currentUserId, true], { index: 'userIdAndIsMember' })
10397
// get the community objects for each community
10498
.eqJoin('communityId', db.table('communities'))
10599
// get rid of unnecessary info from the usersCommunities object on the left
@@ -130,9 +124,7 @@ export const getPublicCommunitiesByUser = async (userId: string) => {
130124
return await db
131125
.table('usersCommunities')
132126
// get all the user's communities
133-
.getAll(userId, { index: 'userId' })
134-
// only return communities the user is a member of
135-
.filter({ isMember: true })
127+
.getAll([userId, true], { index: 'userIdAndIsMember' })
136128
// get the community objects for each community
137129
.eqJoin('communityId', db.table('communities'))
138130
// only return public community ids
@@ -159,8 +151,9 @@ export const getCommunitiesChannelCounts = (communityIds: Array<string>) => {
159151
export const getCommunitiesMemberCounts = (communityIds: Array<string>) => {
160152
return db
161153
.table('usersCommunities')
162-
.getAll(...communityIds, { index: 'communityId' })
163-
.filter({ isBlocked: false, isMember: true })
154+
.getAll(...communityIds.map(id => [id, true]), {
155+
index: 'communityIdAndIsMember',
156+
})
164157
.group('communityId')
165158
.count()
166159
.run();
@@ -171,10 +164,9 @@ export const getCommunitiesOnlineMemberCounts = (
171164
) => {
172165
return db
173166
.table('usersCommunities')
174-
.getAll(...communityIds, {
175-
index: 'communityId',
167+
.getAll(...communityIds.map(id => [id, true]), {
168+
index: 'communityIdAndIsMember',
176169
})
177-
.filter({ isBlocked: false, isMember: true })
178170
.pluck(['communityId', 'userId'])
179171
.eqJoin('userId', db.table('users'))
180172
.pluck('left', { right: ['lastSeen', 'isOnline'] })
@@ -812,8 +804,7 @@ export const setMemberCount = (
812804
export const getMemberCount = (communityId: string): Promise<number> => {
813805
return db
814806
.table('usersCommunities')
815-
.getAll(communityId, { index: 'communityId' })
816-
.filter({ isMember: true })
807+
.getAll([communityId, true], { index: 'communityIdAndIsMember' })
817808
.count()
818809
.run();
819810
};

api/models/meta.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ const saveUserCommunityPermissions = (
1919
): Promise<Object> => {
2020
return db
2121
.table('usersCommunities')
22-
.getAll(userId, { index: 'userId' })
23-
.filter({ communityId })
22+
.getAll([userId, communityId], { index: 'userIdAndCommunityId' })
2423
.update(
2524
{
2625
...permissions,

api/models/search.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ export const getUsersJoinedChannels = (userId: string): Promise<Array<string>> =
102102
export const getUsersJoinedCommunities = (userId: string): Promise<Array<string>> => {
103103
return db
104104
.table('usersCommunities')
105-
.getAll(userId, { index: 'userId' })
106-
.filter({ isMember: true })
105+
.getAll([userId, true], { index: 'userIdAndIsMember' })
107106
.eqJoin('communityId', db.table('communities'))
108107
.filter(row => row('right').hasFields('deletedAt').not())
109108
.zip()
@@ -129,8 +128,7 @@ export const getUsersJoinedPrivateChannelIds = (userId: string): Promise<Array<s
129128
export const getUsersJoinedPrivateCommunityIds = (userId: string): Promise<Array<string>> => {
130129
return db
131130
.table('usersCommunities')
132-
.getAll(userId, { index: 'userId' })
133-
.filter({ isMember: true })
131+
.getAll([userId, true], { index: 'userIdAndIsMember' })
134132
.eqJoin('communityId', db.table('communities'))
135133
.filter(row => row('right')('isPrivate').eq(true).and(row('right').hasFields('deletedAt').not()))
136134
.without({ left: ['id'] })

0 commit comments

Comments
 (0)