unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at angular21 109 lines 3.1 kB view raw
1import { Op, Sequelize } from 'sequelize' 2import { 3 Emoji, 4 FederatedHost, 5 Post, 6 QuestionPoll, 7 QuestionPollAnswer, 8 QuestionPollQuestion, 9 User, 10 sequelize 11} from '../../models/index.js' 12import { completeEnvironment } from '../backendOptions.js' 13import { activityPubObject } from '../../interfaces/fediverse/activityPubObject.js' 14import { postPetitionSigned } from './postPetitionSigned.js' 15import { logger } from '../logger.js' 16import { Queue, QueueEvents } from 'bullmq' 17import _ from 'underscore' 18import { emojiToAPTag } from './emojiToAPTag.js' 19import { wait } from '../wait.js' 20import { loadPoll } from './loadPollFromPost.js' 21import { getPostThreadRecursive } from './getPostThreadRecursive.js' 22 23const sendPostQueue = new Queue('sendPostToInboxes', { 24 connection: completeEnvironment.bullmqConnection, 25 defaultJobOptions: { 26 removeOnComplete: true, 27 attempts: 3, 28 backoff: { 29 type: 'exponential', 30 delay: 1000 31 }, 32 removeOnFail: true 33 } 34}) 35 36const queueEvents = new QueueEvents('sendPostToInboxes', { 37 connection: completeEnvironment.bullmqConnection 38}) 39async function voteInPoll(userId: string, pollId: number) { 40 const user = await User.findByPk(userId) 41 if (!user) return 42 43 const votesToSend = await QuestionPollQuestion.findAll({ 44 include: [ 45 { 46 model: QuestionPollAnswer, 47 where: { 48 userId: userId 49 } 50 }, 51 { 52 model: QuestionPoll, 53 include: [ 54 { 55 model: Post, 56 include: [ 57 { 58 model: User, 59 as: 'user' 60 } 61 ] 62 } 63 ] 64 } 65 ], 66 where: { 67 questionPollId: pollId 68 } 69 }) 70 71 for await (const vote of votesToSend) { 72 const voteObject = { 73 '@context': [ 74 'https://www.w3.org/ns/activitystreams', 75 `${completeEnvironment.frontendUrl}/contexts/litepub-0.1.jsonld` 76 ], 77 actor: `${completeEnvironment.frontendUrl}/fediverse/blog/${user.url.toLowerCase()}`, 78 id: `${completeEnvironment.frontendUrl}/fediverse/voteActivity/${userId}/${vote.id}`, 79 object: { 80 attributedTo: `${completeEnvironment.frontendUrl}/fediverse/blog/${user.url.toLowerCase()}`, 81 id: `${completeEnvironment.frontendUrl}/fediverse/vote/${userId}/${vote.id}`, 82 inReplyTo: vote.questionPoll.post.remotePostId, 83 name: vote.questionText, 84 to: vote.questionPoll.post.user.remoteId, 85 type: `Note` 86 }, 87 to: vote.questionPoll.post.user.remoteId, 88 type: 'Create' 89 } 90 const inboxes = vote.questionPoll.post.user.remoteInbox 91 const sendVoteJob = await sendPostQueue.add( 92 'sendChunk', 93 { 94 objectToSend: voteObject, 95 petitionBy: user.dataValues, 96 inboxList: inboxes 97 }, 98 { 99 priority: 2097152, 100 delay: 2500 101 } 102 ) 103 sendVoteJob.waitUntilFinished(queueEvents).then(async () => { 104 await getPostThreadRecursive(user, vote.questionPoll.post.remotePostId, undefined, vote.questionPoll.postId) 105 }) 106 } 107} 108 109export { voteInPoll }