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