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

Method delete

api/server/models/Message.ts:369–431  ·  view source on GitHub ↗
({ userId, id })

Source from the content-addressed store, hash-verified

367 }
368
369 public static async delete({ userId, id }) {
370 if (!id) {
371 throw new Error('Bad data');
372 }
373
374 const messageToBeDeleted = await this.findOne({ _id: id }).setOptions({ lean: true });
375
376 if (!messageToBeDeleted) {
377 throw new Error(id);
378 }
379
380 const { chat } = await this.checkPermission({ userId, chatId: messageToBeDeleted.chatId });
381
382 //
383 await User.markMessageAsReadForParticipantsOfChatAfterMessageIsDeletedByUser({
384 userIdsToNotify: chat.chatParticipantIds.filter((id) => id !== userId),
385 messageId: id,
386 });
387
388 // call markMessageAsReadBySomeoneAfterMessageIsDeletedByUser
389 await User.updateOne(
390 { _id: userId },
391 {
392 $pull: {
393 unreadBySomeoneMessageIds: id,
394 },
395 },
396 );
397
398 const allThreadMessages = await this.find({
399 parentMessageId: id,
400 }).setOptions({ lean: true });
401
402 if (allThreadMessages.length > 0) {
403 for (const threadMessage of allThreadMessages) {
404 await this.deleteOne({ _id: threadMessage._id.toString() });
405
406 const arrayOfFileUrls = threadMessage.files.map((f) => {
407 return f['fileUrl'];
408 });
409
410 if (arrayOfFileUrls.length > 0) {
411 deleteFiles(arrayOfFileUrls).catch((err) => console.log(err));
412 }
413 }
414 }
415
416 await this.deleteOne({ _id: id });
417
418 const arrayOfFileUrls = messageToBeDeleted.files.map((f) => {
419 return f['fileUrl'];
420 });
421
422 if (arrayOfFileUrls.length > 0) {
423 deleteFiles(arrayOfFileUrls).catch((err) => console.log(err));
424 }
425
426 // 2 browsers, same user fix

Callers

nothing calls this directly

Tested by

no test coverage detected