unoffical wafrn mirror wafrn.net
atproto social-network activitypub
at angular21 52 lines 1.4 kB view raw
1/* eslint-disable guard-for-in */ 2import { Post } from '../models/index.js' 3import { Op } from 'sequelize' 4import { Privacy } from '../models/post.js' 5 6export default async function getPosstGroupDetails(postGroup: any[]) { 7 const getPostFirstParentId = (post: any) => { 8 if (!post?.ancestors) { 9 return post.id 10 } else { 11 let furthestDate = new Date() 12 let id = post.id 13 post.ancestors.forEach((ancestor: any) => { 14 if (furthestDate > ancestor.createdAt) { 15 furthestDate = ancestor.createdAt 16 id = ancestor.id 17 } 18 }) 19 return id 20 } 21 } 22 const postIds: string[] = postGroup.map((elem) => getPostFirstParentId(elem)) 23 // TODO optimize this! I feel like this might be more optimizable. This is one of those things 24 const fullPostTree = await Post.findAll({ 25 where: { 26 id: { [Op.in]: postIds } 27 }, 28 attributes: ['id'], 29 include: [ 30 { 31 model: Post, 32 as: 'descendents', 33 attributes: ['id'], 34 where: { 35 privacy: { 36 [Op.ne]: Privacy.DirectMessage 37 } 38 } 39 } 40 ] 41 }) 42 return postGroup.map((elem) => { 43 let notes = 0 44 fullPostTree.forEach((elementWithNotes: any) => { 45 const idtoCheck = getPostFirstParentId(elem) 46 if (idtoCheck === elementWithNotes.id) { 47 notes = elementWithNotes.descendents.length 48 } 49 }) 50 return { ...elem.dataValues, notes } 51 }) 52}