unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Op } from 'sequelize'
2import {
3 Notification,
4 Post,
5 PostMentionsUserRelation,
6 PostTag,
7 Quotes,
8 User,
9 UserLikesPostRelations
10} from '../models/index.js'
11import { getDeletedUser } from './cacheGetters/getDeletedUser.js'
12
13async function deletePostCommon(id: string) {
14 const postToDelete = await Post.findByPk(id)
15 if (postToDelete) {
16 if (postToDelete.isReblog) {
17 await Notification.destroy({
18 where: {
19 postId: postToDelete.parentId,
20 userId: postToDelete.userId
21 }
22 })
23 } else {
24 await Notification.destroy({
25 where: {
26 postId: id
27 }
28 })
29 }
30
31 const quotesToDelete = await Quotes.findAll({
32 where: {
33 [Op.or]: [
34 {
35 quotedPostId: id
36 },
37 {
38 quoterPostId: id
39 }
40 ]
41 }
42 })
43 if (quotesToDelete) {
44 Promise.all(quotesToDelete.map((qte: any) => qte.destroy()))
45 }
46 postToDelete.removeMedias(await postToDelete.getMedias())
47 await PostTag.destroy({
48 where: {
49 postId: postToDelete.id
50 }
51 })
52 await UserLikesPostRelations.destroy({
53 where: {
54 postId: postToDelete.id
55 }
56 })
57
58 await PostMentionsUserRelation.destroy({
59 where: {
60 postId: postToDelete.id
61 }
62 })
63
64 postToDelete.content_warning = ''
65 postToDelete.content = '<p>This post has been deleted</p>'
66 postToDelete.isDeleted = true
67 const deletedUser = (await getDeletedUser()) as User
68 postToDelete.userId = deletedUser.id
69 await postToDelete.save()
70
71 }
72}
73
74export { deletePostCommon }