diff --git a/Dockerfile b/Dockerfile index a01aae254..14d606c0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ ENV TALK_PORT 5000 EXPOSE 5000 # Install app dependencies -COPY package.json /usr/src/app/ +COPY package.json yarn.lock /usr/src/app/ RUN yarn install --production # Bundle app source diff --git a/client/coral-admin/src/actions/auth.js b/client/coral-admin/src/actions/auth.js index 8bc0d4e4c..bdb101362 100644 --- a/client/coral-admin/src/actions/auth.js +++ b/client/coral-admin/src/actions/auth.js @@ -16,7 +16,7 @@ export const checkLogin = () => dispatch => { }) .catch(error => { console.error(error); - dispatch(checkLoginFailure(`${error.message}`)); + dispatch(checkLoginFailure(`${error.translation_key}`)); }); }; diff --git a/client/coral-admin/src/actions/install.js b/client/coral-admin/src/actions/install.js index a74a1929f..c1d05d253 100644 --- a/client/coral-admin/src/actions/install.js +++ b/client/coral-admin/src/actions/install.js @@ -81,7 +81,7 @@ export const submitUser = () => (dispatch, getState) => { }) .catch(error => { console.error(error); - dispatch(installFailure(`${error.message}`)); + dispatch(installFailure(`${error.translation_key}`)); }); }); }; @@ -104,6 +104,6 @@ export const checkInstall = next => dispatch => { }) .catch(error => { console.error(error); - dispatch(checkInstallFailure(`${error.message}`)); + dispatch(checkInstallFailure(`${error.translation_key}`)); }); }; diff --git a/client/coral-embed-stream/src/Comment.js b/client/coral-embed-stream/src/Comment.js index 034c44f6a..9d679473e 100644 --- a/client/coral-embed-stream/src/Comment.js +++ b/client/coral-embed-stream/src/Comment.js @@ -142,7 +142,6 @@ class Comment extends React.Component { ? { setActiveReplyBox(''); - refetch(); }} setActiveReplyBox={setActiveReplyBox} parentId={parentId || comment.id} diff --git a/client/coral-embed-stream/src/Embed.js b/client/coral-embed-stream/src/Embed.js index 8b420836e..a7c4dcb38 100644 --- a/client/coral-embed-stream/src/Embed.js +++ b/client/coral-embed-stream/src/Embed.js @@ -128,7 +128,6 @@ class Embed extends Component { { user ? (dispatch) => { dispatch(signUpSuccess(user)); }) .catch(error => { - dispatch(signUpFailure(lang.t(`error.${error.translation_key}`))); + dispatch(signUpFailure(lang.t(`error.${error.message}`))); }); }; @@ -177,7 +177,7 @@ export const checkLogin = () => dispatch => { }) .catch(error => { console.error(error); - dispatch(checkLoginFailure(`${error.message}`)); + dispatch(checkLoginFailure(`${error.translation_key}`)); }); }; diff --git a/client/coral-framework/components/SuspendedAccount.js b/client/coral-framework/components/SuspendedAccount.js index 00adc9952..446169b37 100644 --- a/client/coral-framework/components/SuspendedAccount.js +++ b/client/coral-framework/components/SuspendedAccount.js @@ -26,7 +26,7 @@ class SuspendedAccount extends Component { editName(username) .then(() => location.reload()) .catch((error) => { - this.setState({alert: lang.t(`error.${error.message}`)}); + this.setState({alert: lang.t(`error.${error.translation_key}`)}); }); } else { this.setState({alert: lang.t('editName.error')}); diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index 40ca0a7a3..a9d08da85 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -10,16 +10,72 @@ export const postComment = graphql(POST_COMMENT, { options: () => ({ fragments: commentView }), - props: ({mutate}) => ({ - postItem: ({asset_id, body, parent_id} /* , type */ ) => { - return mutate({ + props: ({ownProps, mutate}) => ({ + postItem: ({asset_id, body, parent_id}) => + mutate({ variables: { asset_id, body, parent_id + }, + optimisticResponse: { + createComment: { + comment: { + user: { + id: ownProps.auth.user.id, + name: ownProps.auth.user.username + }, + created_at: new Date().toISOString(), + body, + parent_id, + asset_id, + action_summaries: [], + tags: [], + status: null, + id: `${Date.now()}_temp_id` + } + } + }, + updateQueries: { + AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => { + + if (oldData.asset.moderation === 'PRE') { + return oldData; + } + + let updatedAsset; + + // If posting a reply + if (parent_id) { + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + comments: oldData.asset.comments.map((oldComment) => { + return oldComment.id === parent_id + ? {...oldComment, replies: [...oldComment.replies, comment]} + : oldComment; + }) + } + }; + } else { + + // If posting a top-level comment + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + commentCount: oldData.asset.commentCount + 1, + comments: [comment, ...oldData.asset.comments] + } + }; + } + + return updatedAsset; + } } - }); - }}), + }) + }), }); export const postLike = graphql(POST_LIKE, { diff --git a/client/coral-framework/graphql/mutations/postComment.graphql b/client/coral-framework/graphql/mutations/postComment.graphql index 6ce46eaa8..110ab4b4e 100644 --- a/client/coral-framework/graphql/mutations/postComment.graphql +++ b/client/coral-framework/graphql/mutations/postComment.graphql @@ -4,6 +4,10 @@ mutation CreateComment ($asset_id: ID!, $parent_id: ID, $body: String!) { createComment(asset_id:$asset_id, parent_id:$parent_id, body:$body) { comment { ...commentView + replyCount + replies { + ...commentView + } } errors { translation_key diff --git a/package.json b/package.json index 09ce50aa9..70b0e17b1 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ }, "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { - "apollo-client": "^0.8.3", "bcrypt": "^0.8.7", "body-parser": "^1.15.2", "cli-table": "^0.3.1", @@ -66,7 +65,6 @@ "graphql": "^0.8.2", "graphql-errors": "^2.1.0", "graphql-server-express": "^0.5.0", - "graphql-tag": "^1.2.3", "graphql-tools": "^0.9.0", "helmet": "^3.1.0", "inquirer": "^3.0.1", @@ -88,6 +86,7 @@ "uuid": "^2.0.3" }, "devDependencies": { + "apollo-client": "^0.8.3", "autoprefixer": "^6.5.2", "babel-core": "^6.21.0", "babel-eslint": "^7.1.0", @@ -122,6 +121,7 @@ "exports-loader": "^0.6.3", "fetch-mock": "^5.5.0", "graphql-docs": "^0.2.0", + "graphql-tag": "^1.2.3", "hammerjs": "^2.0.8", "ignore-styles": "^5.0.1", "immutable": "^3.8.1", diff --git a/routes/api/account/index.js b/routes/api/account/index.js index 4eab8b920..ee10636bf 100644 --- a/routes/api/account/index.js +++ b/routes/api/account/index.js @@ -102,15 +102,13 @@ router.put('/password/reset', (req, res, next) => { UsersService .verifyPasswordResetToken(token) - .then(user => { + .then((user) => { return UsersService.changePassword(user.id, password); }) .then(() => { res.status(204).end(); }) - .catch(error => { - console.error(error); - + .catch(() => { next(authorization.ErrNotAuthorized); }); }); @@ -121,12 +119,8 @@ router.put('/username', authorization.needed(), (req, res, next) => { .then(() => { res.status(204).end(); }) - .catch(error => { - if (error.code === 11000) { - next(errors.ErrUsernameTaken); - } else { - next(error); - } + .catch((err) => { + next(err); }); }); diff --git a/routes/api/assets/index.js b/routes/api/assets/index.js index 0b6762de8..3bb3ef778 100644 --- a/routes/api/assets/index.js +++ b/routes/api/assets/index.js @@ -79,7 +79,8 @@ router.get('/:asset_id', (req, res, next) => { .findById(req.params.asset_id) .then((asset) => { if (!asset) { - return res.status(404).end(); + res.status(404).end(); + return; } res.json(asset); @@ -97,15 +98,17 @@ router.post('/:asset_id/scrape', (req, res, next) => { .findById(req.params.asset_id) .then((asset) => { if (!asset) { - return res.status(404).end(); + res.status(404).end(); + return; } - return scraper.create(asset); - }) - .then((job) => { + return scraper + .create(asset) + .then((job) => { - // Send the job back for monitoring. - res.status(201).json(job); + // Send the job back for monitoring. + res.status(201).json(job); + }); }) .catch((err) => { next(err); @@ -117,8 +120,12 @@ router.put('/:asset_id/settings', (req, res, next) => { // Override the settings for the asset. AssetsService .overrideSettings(req.params.asset_id, req.body) - .then(() => res.status(204).end()) - .catch((err) => next(err)); + .then(() => { + res.status(204).end(); + }) + .catch((err) => { + next(err); + }); }); router.put('/:asset_id/status', (req, res, next) => { diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index f1a5fe9f7..5c4b03568 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -5,7 +5,8 @@ const router = express.Router(); router.get('/', (req, res, next) => { SettingsService - .retrieve().then((settings) => { + .retrieve() + .then((settings) => { res.json(settings); }) .catch((err) => { diff --git a/routes/api/setup/index.js b/routes/api/setup/index.js index e325769cd..dfddc64d9 100644 --- a/routes/api/setup/index.js +++ b/routes/api/setup/index.js @@ -43,7 +43,7 @@ router.post('/', (req, res, next) => { res.status(204).end(); }) .catch((err) => { - return next(err); + next(err); }); }); diff --git a/routes/api/users/index.js b/routes/api/users/index.js index 4182beb49..2af6bc2ee 100644 --- a/routes/api/users/index.js +++ b/routes/api/users/index.js @@ -136,7 +136,7 @@ router.post('/', (req, res, next) => { res.status(201).json(user); }); }) - .catch(err => { + .catch((err) => { next(err); }); }); @@ -152,7 +152,7 @@ router.post('/:user_id/actions', authorization.needed(), (req, res, next) => { .then((action) => { // Set the user status to "pending" for review by moderators - if (action_type.slice(0, 4) === 'FLAG') { + if (action_type === 'FLAG') { return UsersService.setStatus(req.params.user_id, 'PENDING') .then(() => action); } else { @@ -163,7 +163,6 @@ router.post('/:user_id/actions', authorization.needed(), (req, res, next) => { res.status(201).json(action); }) .catch((err) => { - console.log('Error', err); next(err); }); }); diff --git a/services/users.js b/services/users.js index b09a7d399..cf3a0063d 100644 --- a/services/users.js +++ b/services/users.js @@ -686,9 +686,20 @@ module.exports = class UsersService { canEditName: false, status: 'PENDING' } - }).then((result) => { - return result.nModified > 0 ? result : - Promise.reject(errors.ErrPermissionUpdateUsername); + }) + .then((result) => { + if (result.nModified <= 0) { + return Promise.reject(errors.ErrPermissionUpdateUsername); + } + + return result; + }) + .catch((err) => { + if (err.code === 11000) { + throw errors.ErrUsernameTaken; + } + + throw err; }); } };