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

Commit 8ad89a0

Browse files
authored
Merge pull request #2520 from withspectrum/weekly-merge
Weekly merge
2 parents 4ac60e0 + 8a6ad1f commit 8ad89a0

File tree

22 files changed

+192
-151
lines changed

22 files changed

+192
-151
lines changed

config-overrides.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module.exports = function override(config, env) {
114114
// Don't return the cached index.html for API requests or /auth pages
115115
if (url.pathname.indexOf('/api') === 0) return;
116116
if (url.pathname.indexOf('/auth') === 0) return;
117-
return new URL('/index.html', url);
117+
return new URL('https://spectrum.chat/index.html');
118118
},
119119
requestType: ['navigate'],
120120
},

iris/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ debug('logging with debug enabled!');
99
import { createServer } from 'http';
1010
import express from 'express';
1111
import Raven from 'shared/raven';
12+
import { ApolloEngine } from 'apollo-engine';
1213
import { init as initPassport } from './authentication.js';
13-
import engine from './routes/middlewares/engine';
14-
const PORT = 3001;
1514
import type { DBUser } from 'shared/types';
1615

16+
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
17+
18+
const engine = new ApolloEngine({
19+
logging: {
20+
level: 'WARN',
21+
},
22+
apiKey: process.env.APOLLO_ENGINE_API_KEY,
23+
// Only send perf data to the remote server in production
24+
reporting: {
25+
disabled: process.env.NODE_ENV !== 'production',
26+
hostname: process.env.NOW_URL || undefined,
27+
privateHeaders: ['authorization', 'Authorization', 'AUTHORIZATION'],
28+
},
29+
});
30+
1731
// Initialize authentication
1832
initPassport();
1933

@@ -49,16 +63,14 @@ const server = createServer(app);
4963
import createSubscriptionsServer from './routes/create-subscription-server';
5064
const subscriptionsServer = createSubscriptionsServer(server, '/websocket');
5165

52-
// Start webserver
53-
server.listen(PORT);
66+
// Start API wrapped in Apollo Engine
67+
engine.listen({
68+
port: PORT,
69+
httpServer: server,
70+
graphqlPaths: ['/api'],
71+
});
5472
console.log(`GraphQL server running at http://localhost:${PORT}/api`);
5573

56-
if (process.env.NODE_ENV === 'production') {
57-
// Start Apollo Engine
58-
console.log('Apollo Engine starting...');
59-
engine.start();
60-
}
61-
6274
process.on('unhandledRejection', async err => {
6375
console.error('Unhandled rejection', err);
6476
try {

iris/models/thread.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ export const publishThread = (
324324

325325
export const setThreadLock = (
326326
threadId: string,
327-
value: boolean
327+
value: boolean,
328+
userId: string
328329
): Promise<DBThread> => {
329330
return (
330331
db
@@ -335,6 +336,8 @@ export const setThreadLock = (
335336
.update(
336337
{
337338
isLocked: value,
339+
lockedBy: value === true ? userId : db.literal(),
340+
lockedAt: value === true ? new Date() : db.literal(),
338341
},
339342
{ returnChanges: true }
340343
)

iris/mutations/message/addMessage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export default async (
5050

5151
const thread = await loaders.thread.load(message.threadId);
5252

53+
if (thread.isLocked) throw new UserError("Can't reply in a locked thread.");
54+
5355
let contextPermissions;
5456
// Make sure that we have permission to send a message in the community
5557
if (message.threadType === 'story') {

iris/mutations/thread/setThreadLock.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import UserError from '../../utils/UserError';
44
import { getThreads, setThreadLock } from '../../models/thread';
55
import { getUserPermissionsInChannel } from '../../models/usersChannels';
66
import { getUserPermissionsInCommunity } from '../../models/usersCommunities';
7+
import type { DBThread } from 'shared/types';
78

89
export default async (
910
_: any,
@@ -17,13 +18,23 @@ export default async (
1718
return new UserError('You must be signed in to make changes.');
1819
}
1920

20-
const thread = await loaders.thread.load(threadId);
21+
const thread: DBThread = await loaders.thread.load(threadId);
2122

2223
// if the thread doesn't exist
2324
if (!thread || thread.deletedAt) {
2425
return new UserError(`Could not find thread with ID '${threadId}'.`);
2526
}
2627

28+
// A threads author can always lock their thread, but only unlock it if
29+
// it was locked by themselves. (if a mod locks a thread an author cannot
30+
// unlock it anymore)
31+
const isAuthor = thread.creatorId === currentUser.id;
32+
const authorCanLock =
33+
!thread.isLocked || thread.lockedBy === thread.creatorId;
34+
if (isAuthor && authorCanLock) {
35+
return setThreadLock(threadId, value, currentUser.id);
36+
}
37+
2738
// get the channel permissions
2839
let [
2940
currentUserChannelPermissions,
@@ -46,10 +57,15 @@ export default async (
4657
currentUserCommunityPermissions.isOwner ||
4758
currentUserCommunityPermissions.isModerator
4859
) {
49-
return setThreadLock(threadId, value);
60+
return setThreadLock(threadId, value, currentUser.id);
5061
}
5162

5263
// if the user is not a channel or community owner, the thread can't be locked
64+
if (isAuthor) {
65+
return new UserError(
66+
"You don't have permission to unlock this thread as it was locked by a moderator."
67+
);
68+
}
5369
return new UserError(
5470
"You don't have permission to make changes to this thread."
5571
);

iris/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"algoliasearch": "^3.24.7",
4-
"apollo-engine": "0.8.4",
4+
"apollo-engine": "1.x",
55
"apollo-local-query": "^0.3.0",
66
"apollo-upload-client": "^5.1.0",
77
"apollo-upload-server": "^2.0.4",

iris/routes/api/graphql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import schema from '../../schema';
1212
export default graphqlExpress(req => ({
1313
schema,
1414
formatError: createErrorFormatter(req),
15+
tracing: true,
1516
context: {
1617
user: req.user,
1718
loaders: createLoaders(),
@@ -30,5 +31,4 @@ export default graphqlExpress(req => ({
3031
},
3132
}),
3233
],
33-
tracing: true,
3434
}));

iris/routes/middlewares/engine.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

iris/routes/middlewares/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ if (process.env.NODE_ENV === 'development') {
88
middlewares.use(logging);
99
}
1010

11-
// Start apollo engine
12-
if (process.env.NODE_ENV === 'production' && !process.env.FORCE_DEV) {
13-
const engine = require('./engine').default;
14-
middlewares.use(engine.expressMiddleware());
15-
}
16-
1711
if (process.env.NODE_ENV === 'production' && !process.env.FORCE_DEV) {
1812
// Raven (Sentry client) needs to come before everything else
1913
const raven = require('shared/middlewares/raven').default;

iris/subscriptions/directMessageThread.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,20 @@ module.exports = {
3232
);
3333

3434
debug(`@${user.username || user.id} listening to updated DM threads`);
35-
return asyncify(listenToUpdatedDirectMessageThreads(user.id), err => {
36-
// Don't crash the whole API server on error in the listener
37-
console.error(err);
38-
Raven.captureException(err);
35+
return asyncify(listenToUpdatedDirectMessageThreads(user.id), {
36+
onError: err => {
37+
// Don't crash the whole API server on error in the listener
38+
console.error(err);
39+
Raven.captureException(err);
40+
},
41+
onClose: cursor => {
42+
if (cursor) {
43+
/* ignore errors that happen when closing the cursor */
44+
try {
45+
cursor.close(() => {});
46+
} catch (err) {}
47+
}
48+
},
3949
});
4050
},
4151
},

0 commit comments

Comments
 (0)