unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at testPDSNotExplode 135 lines 3.3 kB view raw
1import { Op } from 'sequelize' 2import { Ask, Emoji, EmojiReaction, Media, Post, PostTag, User, UserLikesPostRelations } from '../../models/index.js' 3import { redisCache } from '../redis.js' 4import { Privacy } from '../../models/post.js' 5 6async function getPostAndUserFromPostId(postId: string): Promise<{ found: boolean; data?: any }> { 7 const cacheResult = await redisCache.get('postAndUser:' + postId) 8 let res: { found: boolean; data?: any } = cacheResult ? JSON.parse(cacheResult) : { found: false } 9 if (!cacheResult) { 10 const dbQuery = await Post.findOne({ 11 include: [ 12 { 13 model: Ask 14 }, 15 { 16 model: User, 17 as: 'user', 18 required: true, 19 where: { 20 banned: false 21 } 22 }, 23 { 24 model: Post, 25 include: [ 26 { 27 model: User, 28 as: 'user' 29 } 30 ], 31 as: 'quoted', 32 where: { 33 isDeleted: false 34 }, 35 required: false 36 }, 37 { 38 model: Post, 39 as: 'parent', 40 required: false, 41 where: { 42 isDeleted: false 43 }, 44 include: [ 45 { 46 model: Media, 47 required: false 48 }, 49 { 50 model: PostTag, 51 required: false 52 } 53 ] 54 }, 55 { 56 model: User, 57 as: 'mentionPost', 58 required: false 59 }, 60 { 61 model: Media, 62 required: false 63 }, 64 { 65 model: PostTag, 66 required: false 67 }, 68 { 69 model: Emoji, 70 required: false 71 } 72 ], 73 where: { 74 id: postId, 75 isDeleted: false, 76 privacy: { 77 [Op.notIn]: [Privacy.LocalOnly] 78 } 79 } 80 }) 81 if (dbQuery) { 82 // check if its a bsky post because we dont enjoy bsky posts going to fedi! 83 const parents = await dbQuery.getAncestors({ 84 include: [ 85 { 86 model: User, 87 as: 'user' 88 } 89 ] 90 }) 91 const isBskyPost = parents.some((elem) => elem.isRemoteBlueskyPost) 92 if (isBskyPost) { 93 res = { found: false } 94 return res 95 } 96 97 let likes = UserLikesPostRelations.findAll({ 98 where: { 99 postId: postId 100 } 101 }) 102 let shares = Post.findAll({ 103 where: { 104 parentId: postId, 105 isReblog: true 106 } 107 }) 108 let reacts = EmojiReaction.findAll({ 109 where: { 110 postId: postId 111 } 112 }) 113 Promise.all([likes, shares, reacts]) 114 115 res = { found: true, data: dbQuery.dataValues } 116 if (res.data.ask) { 117 const userAsker = await User.findByPk(res.data.ask.userAsker) 118 res.data.ask.asker = userAsker 119 } 120 res.data.shares = await shares 121 res.data.likes = await likes 122 res.data.reacts = await reacts 123 } else { 124 res = { found: false } 125 } 126 if (res.found) { 127 await redisCache.set('postAndUser:' + postId, JSON.stringify(res), 'EX', 300) 128 } else { 129 // await redisCache.set('postAndUser:' + postId, JSON.stringify(res), 'EX', 60) 130 } 131 } 132 return res 133} 134 135export { getPostAndUserFromPostId }