type-graphql#PubSubEngine TypeScript Examples
The following examples show how to use
type-graphql#PubSubEngine.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: message-resolver.ts From convoychat with GNU General Public License v3.0 | 6 votes |
@Authorized()
@UseMiddleware(RateLimit({ limit: 1000 }))
@Mutation(() => Message)
async deleteMessage(
@Arg("messageId") messageId: ObjectID,
@Ctx() context: Context,
@PubSub() pubsub: PubSubEngine
) {
try {
const message = await MessageModel.findOneAndDelete({
_id: messageId,
author: context.currentUser.id,
});
if (!message) throw new ApolloError("Cannot find message with ID");
await message.populate("author").execPopulate();
pubsub.publish(CONSTANTS.DELETE_MESSAGE, message.toObject());
return message;
} catch (err) {
throw new ApolloError(err);
}
}
Example #2
Source File: message-resolver.ts From convoychat with GNU General Public License v3.0 | 6 votes |
@Authorized()
@UseMiddleware(RateLimit({ limit: 2000 }))
@Mutation(() => Message)
async editMessage(
@Args() { messageId, content }: editMessageArgs,
@Ctx() context: Context,
@PubSub() pubsub: PubSubEngine
) {
try {
const message = await MessageModel.findOneAndUpdate(
{
_id: messageId,
author: context.currentUser.id,
},
{ content: content },
{ new: true }
);
if (!message) throw new ApolloError("Cannot find message with ID");
await message.populate("author").execPopulate();
pubsub.publish(CONSTANTS.UPDATE_MESSAGE, message.toObject());
return message;
} catch (err) {
throw new ApolloError(err);
}
}
Example #3
Source File: message-resolver.ts From convoychat with GNU General Public License v3.0 | 5 votes |
@Authorized()
@UseMiddleware(RateLimit({ limit: 1000 }))
@Mutation(() => Message)
async sendMessage(
@Args() { roomId, content }: sendMessageArgs,
@Ctx() context: Context,
@PubSub() pubsub: PubSubEngine
) {
try {
const room = await RoomModel.findOne({
_id: roomId,
members: { $in: [context.currentUser.id] },
}).populate("members");
if (!room) {
throw new ApolloError(
"Room not found or you are not a member of this room"
);
}
// parse mentions
const mentions = parseMentions(content);
// check if mentioned users are member of the room
const mentioned_users = mentions
.map(m => {
const found = room?.members.find((i: Member) => i.username === m);
if (found) {
return (found as any)._id;
}
return null;
})
// remove null values & current user if mentioned
.filter(userId => {
if (!userId) return false;
return `${userId}` !== `${context.currentUser.id}`;
});
const message = new MessageModel({
content: content,
roomId: roomId,
author: context.currentUser.id,
mentions: mentioned_users,
});
message.populate("author").execPopulate();
// filter out the current User id to prevent self notification sending
const mentionNotifications = message.mentions.map(async id => {
return sendNotification({
context: context,
sender: context.currentUser.id,
receiver: id as any,
type: NOTIFICATION_TYPE.MENTION,
payload: {
roomName: room?.name,
message: message.content,
messageId: message._id,
roomId: room?._id,
},
});
});
await Promise.all(mentionNotifications);
(message as any).$roomId = roomId;
const savedMessage = await message.save();
pubsub.publish(CONSTANTS.NEW_MESSAGE, savedMessage.toObject());
return savedMessage;
} catch (err) {
throw new ApolloError(err);
}
}