MCPcopy Create free account
hub / github.com/async-labs/async / delete

Method delete

api/server/models/Chat.ts:185–234  ·  view source on GitHub ↗
({ userId, chatId })

Source from the content-addressed store, hash-verified

183
184 // review
185 public static async delete({ userId, chatId }): Promise<{ teamId: string }> {
186 if (!chatId) {
187 throw new Error('Bad data');
188 }
189
190 const existingChat = await this.findById(chatId).setOptions({ lean: true });
191
192 if (!existingChat) {
193 throw new Error('Not found');
194 }
195
196 await this.checkPermission({
197 userId,
198 teamId: existingChat.teamId,
199 chat: existingChat,
200 });
201
202 const messagesToBeRemoved = await Message.find({
203 chatId: existingChat._id.toString(),
204 }).setOptions({ lean: true });
205
206 const messageIdsToBeRemoved = messagesToBeRemoved.map((m) => {
207 return m['_id'].toString();
208 });
209
210 // remove all messages and files if any
211 for (const messageId of messageIdsToBeRemoved) {
212 await Message.delete({ userId, id: messageId });
213 }
214
215 // remove chat document
216 await this.deleteOne({ _id: chatId });
217
218 // remove messageId from each user's unreadByUserMessageIds
219 for (const id of existingChat.chatParticipantIds.filter((id) => id !== userId)) {
220 const user = await User.findOne({ _id: id }).setOptions({ lean: true });
221
222 const unreadByUserMessageIds = user.unreadByUserMessageIds.filter((uci) => {
223 return !messageIdsToBeRemoved.includes(uci);
224 });
225
226 await User.findOneAndUpdate(
227 { _id: id },
228 { $set: { unreadByUserMessageIds } },
229 { runValidators: true },
230 ).setOptions({ lean: true });
231 }
232
233 return { teamId: existingChat.teamId };
234 }
235
236 public static async clearHistory({ userId, chatId }): Promise<{ teamId: string }> {
237 if (!chatId) {

Callers

nothing calls this directly

Calls 2

checkPermissionMethod · 0.95
deleteMethod · 0.65

Tested by

no test coverage detected