unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at testPDSNotExplode 84 lines 2.8 kB view raw
1import { Job } from 'bullmq' 2import { Media, Post, PostTag, Quotes, User, Notification } from '../../models/index.js' 3import { completeEnvironment } from '../backendOptions.js' 4import { Privacy } from '../../models/post.js' 5import { getAtProtoSession } from '../../atproto/utils/getAtProtoSession.js' 6import { postToAtproto } from '../../atproto/utils/postToAtproto.js' 7import { wait } from '../wait.js' 8import { logger } from '../logger.js' 9async function sendPostBsky(job: Job) { 10 const post = await Post.findByPk(job.data.postId) 11 if (!post || post.bskyUri) { 12 // post non existing or post already in bsky! 13 return 14 } 15 const parent = post.parentId ? await Post.findByPk(post.parentId) : undefined 16 const parentPoster = parent ? await User.findByPk(parent.userId) : undefined 17 const localUser = await User.findByPk(post.userId) 18 if (post.privacy === Privacy.Public && localUser?.enableBsky && completeEnvironment.enableBsky) { 19 if (!parent || parent.bskyUri) { 20 // ok the user has bluesky time to send the post 21 let isReblog = false 22 if (post.content == '' && post.content_warning == '' && post.parentId) { 23 const mediaCount = await Media.count({ 24 where: { 25 postId: post.id 26 } 27 }) 28 const quotesCount = await Quotes.count({ 29 where: { 30 quoterPostId: post.id 31 } 32 }) 33 const tagsCount = await PostTag.count({ 34 where: { 35 postId: post.id 36 } 37 }) 38 if (mediaCount + quotesCount + tagsCount === 0) { 39 isReblog = true 40 if (parent?.bskyUri) { 41 let agent = await getAtProtoSession(localUser) 42 const { uri } = await agent.repost(parent.bskyUri, parent.bskyCid as string) 43 post.bskyUri = uri 44 await post.save() 45 } 46 } 47 } 48 if (!isReblog) { 49 let agent = await getAtProtoSession(localUser) 50 const bskyPost = await agent.post(await postToAtproto(post, agent)) 51 await wait(750) 52 const duplicatedPost = await Post.findOne({ 53 where: { 54 bskyCid: bskyPost.cid 55 } 56 }) 57 if (duplicatedPost) { 58 logger.debug({ 59 message: `Bluesky duplicated post in database already. Cleaning up` 60 }) 61 await Notification.destroy({ 62 where: { 63 postId: duplicatedPost.id 64 } 65 }) 66 try { 67 await duplicatedPost.destroy() 68 } catch (err) { 69 duplicatedPost.isDeleted = true 70 await duplicatedPost.save() 71 } 72 } 73 post.bskyUri = bskyPost.uri 74 post.bskyCid = bskyPost.cid 75 if (post.parentId) { 76 post.replyControl = 100 77 } 78 await post.save() 79 } 80 } 81 } 82} 83 84export { sendPostBsky }