mirror of
https://github.com/wassname/talk.git
synced 2026-08-12 12:30:39 +08:00
Implement live updates for mod actions
This commit is contained in:
@@ -326,7 +326,7 @@ const createPublicComment = async (context, commentInput) => {
|
||||
* @param {String} id identifier of the comment (uuid)
|
||||
* @param {String} status the new status of the comment
|
||||
*/
|
||||
const setStatus = async ({user, loaders: {Comments}}, {id, status}) => {
|
||||
const setStatus = async ({user, loaders: {Comments}, pubsub}, {id, status}) => {
|
||||
let comment = await CommentsService.pushStatus(id, status, user ? user.id : null);
|
||||
|
||||
// If the loaders are present, clear the caches for these values because we
|
||||
@@ -370,6 +370,9 @@ const edit = async (context, {id, asset_id, edit: {body}}) => {
|
||||
|
||||
// Publish the edited comment via the subscription.
|
||||
context.pubsub.publish('commentEdited', comment);
|
||||
|
||||
// Publish the comment status change via the subscription.
|
||||
context.pubsub.publish('commentStatusChanged', comment);
|
||||
}
|
||||
|
||||
return comment;
|
||||
|
||||
@@ -31,7 +31,13 @@ const RootMutation = {
|
||||
stopIgnoringUser(_, {id}, {mutators: {User}}) {
|
||||
return wrapResponse(null)(User.stopIgnoringUser({id}));
|
||||
},
|
||||
setCommentStatus(_, {id, status}, {mutators: {Comment}}) {
|
||||
setCommentStatus: async (_, {id, status}, {loaders: {Comments}, mutators: {Comment}, user, pubsub}) => {
|
||||
const previous = await Comments.get.load(id);
|
||||
const comment = await Comment.setStatus({id, status});
|
||||
|
||||
// Publish the comment status change via the subscription.
|
||||
pubsub.publish('commentStatusChanged', {user, comment, previous});
|
||||
|
||||
return wrapResponse(null)(Comment.setStatus({id, status}));
|
||||
},
|
||||
addTag(_, {tag}, {mutators: {Tag}}) {
|
||||
|
||||
@@ -4,7 +4,10 @@ const Subscription = {
|
||||
},
|
||||
commentEdited(comment) {
|
||||
return comment;
|
||||
}
|
||||
},
|
||||
commentStatusChanged(data) {
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = Subscription;
|
||||
|
||||
@@ -10,6 +10,10 @@ const plugins = require('../services/plugins');
|
||||
|
||||
const {deserializeUser} = require('../services/subscriptions');
|
||||
|
||||
const {
|
||||
SUBSCRIBE_COMMENT_STATUS,
|
||||
} = require('../perms/constants');
|
||||
|
||||
/**
|
||||
* Plugin support requires that we merge in existing setupFunctions with our new
|
||||
* plugin based ones. This allows plugins to extend existing setupFunctions as well
|
||||
@@ -30,6 +34,16 @@ const setupFunctions = plugins.get('server', 'setupFunctions').reduce((acc, {plu
|
||||
filter: (comment) => comment.asset_id === args.asset_id
|
||||
},
|
||||
}),
|
||||
commentStatusChanged: (options, args) => ({
|
||||
commentStatusChanged: {
|
||||
filter: ({comment}, context) => {
|
||||
if (!context.user || !context.user.can(SUBSCRIBE_COMMENT_STATUS)) {
|
||||
return false;
|
||||
}
|
||||
return !args.asset_id || comment.asset_id === args.asset_id;
|
||||
}
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -936,9 +936,17 @@ type RootMutation {
|
||||
## Subscriptions
|
||||
################################################################################
|
||||
|
||||
# Response to ignoreUser mutation
|
||||
type CommentStatusChangedUpdate {
|
||||
user: User
|
||||
comment: Comment
|
||||
previous: Comment
|
||||
}
|
||||
|
||||
type Subscription {
|
||||
commentAdded(asset_id: ID!): Comment
|
||||
commentEdited(asset_id: ID!): Comment
|
||||
commentStatusChanged(asset_id: ID): CommentStatusChangedUpdate
|
||||
}
|
||||
|
||||
################################################################################
|
||||
|
||||
Reference in New Issue
Block a user