| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const winston = require('winston'); | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | const nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | const db = require('../database'); | 
					
						
							| 
									
										
										
										
											2024-02-27 15:25:13 -05:00
										 |  |  | const meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2024-02-26 11:39:32 -05:00
										 |  |  | const privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2024-03-14 13:17:26 -04:00
										 |  |  | const categories = require('../categories'); | 
					
						
							| 
									
										
										
										
											2024-02-12 14:34:37 -05:00
										 |  |  | const user = require('../user'); | 
					
						
							| 
									
										
										
										
											2024-01-17 11:54:20 -05:00
										 |  |  | const topics = require('../topics'); | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | const posts = require('../posts'); | 
					
						
							| 
									
										
										
										
											2024-01-18 16:20:37 -05:00
										 |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | const activitypub = module.parent.exports; | 
					
						
							|  |  |  | const Notes = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | async function lock(value) { | 
					
						
							|  |  |  | 	const count = await db.incrObjectField('locks', value); | 
					
						
							|  |  |  | 	return count <= 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function unlock(value) { | 
					
						
							|  |  |  | 	await db.deleteObjectField('locks', value); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | Notes.assert = async (uid, input, options = { skipChecks: false }) => { | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-03-22 16:21:32 -04:00
										 |  |  | 	 * Given the id or object of any as:Note, traverses up to cache the entire threaded context | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 	 * | 
					
						
							|  |  |  | 	 * Unfortunately, due to limitations and fragmentation of the existing ActivityPub landscape, | 
					
						
							|  |  |  | 	 * retrieving the entire reply tree is not possible at this time. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 16:21:32 -04:00
										 |  |  | 	const object = !activitypub.helpers.isUri(input) && input; | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 	const id = object ? object.id : input; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const lockStatus = await lock(id, '[[error:activitypub.already-asserting]]'); | 
					
						
							|  |  |  | 	if (!lockStatus) { // unable to achieve lock, stop processing.
 | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 16:21:32 -04:00
										 |  |  | 	const chain = Array.from(await Notes.getParentChain(uid, input)); | 
					
						
							| 
									
										
										
										
											2024-02-12 15:25:49 -05:00
										 |  |  | 	if (!chain.length) { | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 		unlock(id); | 
					
						
							| 
									
										
										
										
											2024-02-12 15:25:49 -05:00
										 |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-29 11:19:56 -05:00
										 |  |  | 	const mainPost = chain[chain.length - 1]; | 
					
						
							|  |  |  | 	let { pid: mainPid, tid, uid: authorId, timestamp, name, content } = mainPost; | 
					
						
							| 
									
										
										
										
											2024-02-28 13:29:21 -05:00
										 |  |  | 	const hasTid = !!tid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-29 11:19:56 -05:00
										 |  |  | 	const members = await db.isSortedSetMembers(`tid:${tid}:posts`, chain.slice(0, -1).map(p => p.pid)); | 
					
						
							|  |  |  | 	members.push(await posts.exists(mainPid)); | 
					
						
							| 
									
										
										
										
											2024-01-19 11:31:04 -05:00
										 |  |  | 	if (tid && members.every(Boolean)) { | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 		// All cached, return early.
 | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | 		winston.verbose('[notes/assert] No new notes to process.'); | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 		unlock(id); | 
					
						
							| 
									
										
										
										
											2024-05-07 10:40:47 +02:00
										 |  |  | 		return { tid, count: 0 }; | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 13:29:21 -05:00
										 |  |  | 	let cid; | 
					
						
							|  |  |  | 	let title; | 
					
						
							|  |  |  | 	if (hasTid) { | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 		({ cid, mainPid } = await topics.getTopicFields(tid, ['tid', 'cid', 'mainPid'])); | 
					
						
							| 
									
										
										
										
											2024-04-16 14:17:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (options.cid && cid === -1) { | 
					
						
							|  |  |  | 			// Move topic
 | 
					
						
							| 
									
										
										
										
											2024-04-16 14:27:21 -04:00
										 |  |  | 			await topics.tools.move(tid, { cid: options.cid, uid: 'system' }); | 
					
						
							| 
									
										
										
										
											2024-04-16 14:17:47 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-28 13:29:21 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		// mainPid ok to leave as-is
 | 
					
						
							| 
									
										
										
										
											2024-04-16 14:00:01 -04:00
										 |  |  | 		cid = options.cid || -1; | 
					
						
							| 
									
										
										
										
											2024-04-29 16:16:07 -04:00
										 |  |  | 		title = name || activitypub.helpers.generateTitle(utils.decodeHTMLEntities(content)); | 
					
						
							| 
									
										
										
										
											2024-02-28 13:29:21 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-29 00:06:59 -05:00
										 |  |  | 	mainPid = utils.isNumber(mainPid) ? parseInt(mainPid, 10) : mainPid; | 
					
						
							| 
									
										
										
										
											2024-02-26 11:39:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | 	// Relation & privilege check for local categories
 | 
					
						
							| 
									
										
										
										
											2024-05-06 15:54:45 -04:00
										 |  |  | 	const hasRelation = uid || options.skipChecks || options.cid || hasTid || await assertRelation(chain[0]); | 
					
						
							| 
									
										
										
										
											2024-04-16 13:38:05 -04:00
										 |  |  | 	const privilege = `topics:${tid ? 'reply' : 'create'}`; | 
					
						
							| 
									
										
										
										
											2024-02-26 11:39:32 -05:00
										 |  |  | 	const allowed = await privileges.categories.can(privilege, cid, activitypub._constants.uid); | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | 	if (!hasRelation || !allowed) { | 
					
						
							|  |  |  | 		if (!hasRelation) { | 
					
						
							|  |  |  | 			winston.info(`[activitypub/notes.assert] Not asserting ${id} as it has no relation to existing tracked content.`); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 		unlock(id); | 
					
						
							| 
									
										
										
										
											2024-02-26 11:39:32 -05:00
										 |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 15:25:13 -05:00
										 |  |  | 	tid = tid || utils.generateUUID(); | 
					
						
							| 
									
										
										
										
											2024-02-29 11:19:56 -05:00
										 |  |  | 	mainPost.tid = tid; | 
					
						
							| 
									
										
										
										
											2024-02-27 15:25:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 14:24:13 -05:00
										 |  |  | 	const unprocessed = chain.map((post) => { | 
					
						
							|  |  |  | 		post.tid = tid; // add tid to post hash
 | 
					
						
							|  |  |  | 		return post; | 
					
						
							|  |  |  | 	}).filter((p, idx) => !members[idx]); | 
					
						
							| 
									
										
										
										
											2024-03-13 11:03:08 -04:00
										 |  |  | 	const count = unprocessed.length; | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | 	winston.verbose(`[notes/assert] ${count} new note(s) found.`); | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 	const [ids, timestamps] = [ | 
					
						
							| 
									
										
										
										
											2024-02-28 21:50:43 -05:00
										 |  |  | 		unprocessed.map(n => (utils.isNumber(n.pid) ? parseInt(n.pid, 10) : n.pid)), | 
					
						
							| 
									
										
										
										
											2024-01-16 10:44:47 -05:00
										 |  |  | 		unprocessed.map(n => n.timestamp), | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-07 12:28:16 -05:00
										 |  |  | 	// mainPid doesn't belong in posts zset
 | 
					
						
							| 
									
										
										
										
											2024-02-29 00:06:59 -05:00
										 |  |  | 	if (ids.includes(mainPid)) { | 
					
						
							|  |  |  | 		const idx = ids.indexOf(mainPid); | 
					
						
							| 
									
										
										
										
											2024-02-28 13:29:21 -05:00
										 |  |  | 		ids.splice(idx, 1); | 
					
						
							|  |  |  | 		timestamps.splice(idx, 1); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-07 12:28:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-29 11:19:56 -05:00
										 |  |  | 	let tags; | 
					
						
							|  |  |  | 	if (!hasTid) { | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		const { to, cc, attachment } = mainPost._activitypub; | 
					
						
							| 
									
										
										
										
											2024-03-14 13:17:26 -04:00
										 |  |  | 		const systemTags = (meta.config.systemTags || '').split(','); | 
					
						
							| 
									
										
										
										
											2024-03-14 14:48:35 -04:00
										 |  |  | 		const maxTags = await categories.getCategoryField(cid, 'maxTags'); | 
					
						
							| 
									
										
										
										
											2024-03-07 15:11:43 -05:00
										 |  |  | 		tags = (mainPost._activitypub.tag || []) | 
					
						
							| 
									
										
										
										
											2024-03-14 13:41:04 -04:00
										 |  |  | 			.filter(o => o.type === 'Hashtag' && !systemTags.includes(o.name.slice(1))) | 
					
						
							| 
									
										
										
										
											2024-03-07 15:11:43 -05:00
										 |  |  | 			.map(o => o.name.slice(1)); | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 14:48:35 -04:00
										 |  |  | 		if (maxTags && tags.length > maxTags) { | 
					
						
							| 
									
										
										
										
											2024-03-14 13:17:26 -04:00
										 |  |  | 			tags.length = maxTags; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		await Promise.all([ | 
					
						
							|  |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				tid, | 
					
						
							|  |  |  | 				uid: authorId, | 
					
						
							|  |  |  | 				cid, | 
					
						
							|  |  |  | 				pid: mainPid, | 
					
						
							|  |  |  | 				title, | 
					
						
							|  |  |  | 				timestamp, | 
					
						
							|  |  |  | 				tags, | 
					
						
							|  |  |  | 				content: mainPost.content, | 
					
						
							|  |  |  | 				_activitypub: mainPost._activitypub, | 
					
						
							|  |  |  | 			}), | 
					
						
							|  |  |  | 			Notes.updateLocalRecipients(mainPid, { to, cc }), | 
					
						
							| 
									
										
										
										
											2024-04-10 22:01:44 -04:00
										 |  |  | 			posts.attachments.update(mainPid, attachment), | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		]); | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 		unprocessed.pop(); | 
					
						
							| 
									
										
										
										
											2024-02-29 11:19:56 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 	unprocessed.reverse(); | 
					
						
							|  |  |  | 	for (const post of unprocessed) { | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		const { to, cc, attachment } = post._activitypub; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 		// eslint-disable-next-line no-await-in-loop
 | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		await Promise.all([ | 
					
						
							|  |  |  | 			topics.reply(post), | 
					
						
							|  |  |  | 			Notes.updateLocalRecipients(post.pid, { to, cc }), | 
					
						
							| 
									
										
										
										
											2024-04-10 22:01:44 -04:00
										 |  |  | 			posts.attachments.update(post.pid, attachment), | 
					
						
							| 
									
										
										
										
											2024-03-15 16:38:00 -04:00
										 |  |  | 		]); | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Category announce
 | 
					
						
							| 
									
										
										
										
											2024-03-22 16:21:32 -04:00
										 |  |  | 		if (object && object.id === post.pid) { | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | 			// eslint-disable-next-line no-await-in-loop
 | 
					
						
							|  |  |  | 			const followers = await activitypub.notes.getCategoryFollowers(cid); | 
					
						
							|  |  |  | 			// eslint-disable-next-line no-await-in-loop
 | 
					
						
							|  |  |  | 			await activitypub.send('cid', cid, followers, { | 
					
						
							| 
									
										
										
										
											2024-05-03 17:15:55 +02:00
										 |  |  | 				id: `${object.id}#activity/announce`, | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | 				type: 'Announce', | 
					
						
							|  |  |  | 				to: [`${nconf.get('url')}/category/${cid}/followers`], | 
					
						
							|  |  |  | 				cc: [activitypub._constants.publicAddress], | 
					
						
							|  |  |  | 				object, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-03-12 13:27:29 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 	await Promise.all([ | 
					
						
							| 
									
										
										
										
											2024-05-06 15:54:45 -04:00
										 |  |  | 		Notes.syncUserInboxes(tid, uid), | 
					
						
							| 
									
										
										
										
											2024-03-25 14:55:25 -04:00
										 |  |  | 		unlock(id), | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-13 11:03:08 -04:00
										 |  |  | 	return { tid, count }; | 
					
						
							| 
									
										
										
										
											2024-01-12 15:23:30 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | async function assertRelation(post) { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Given a mocked post object, ensures that it is related to some other object in database | 
					
						
							|  |  |  | 	 * This check ensures that random content isn't added to the database just because it is received. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Is followed by at least one local user
 | 
					
						
							| 
									
										
										
										
											2024-04-16 14:00:01 -04:00
										 |  |  | 	const numFollowers = await activitypub.actors.getLocalFollowersCount(post.uid); | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Local user is mentioned
 | 
					
						
							|  |  |  | 	const { tag } = post._activitypub; | 
					
						
							|  |  |  | 	let uids = []; | 
					
						
							| 
									
										
										
										
											2024-04-03 13:49:27 -04:00
										 |  |  | 	if (tag && tag.length) { | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | 		const slugs = tag.reduce((slugs, tag) => { | 
					
						
							|  |  |  | 			if (tag.type === 'Mention') { | 
					
						
							|  |  |  | 				const [slug, hostname] = tag.name.slice(1).split('@'); | 
					
						
							|  |  |  | 				if (hostname === nconf.get('url_parsed').hostname) { | 
					
						
							|  |  |  | 					slugs.push(slug); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return slugs; | 
					
						
							|  |  |  | 		}, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uids = slugs.length ? await db.sortedSetScores('userslug:uid', slugs) : []; | 
					
						
							|  |  |  | 		uids = uids.filter(Boolean); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 14:00:01 -04:00
										 |  |  | 	return numFollowers > 0 || uids.length; | 
					
						
							| 
									
										
										
										
											2024-03-26 10:22:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | Notes.updateLocalRecipients = async (id, { to, cc }) => { | 
					
						
							|  |  |  | 	const recipients = new Set([...(to || []), ...(cc || [])]); | 
					
						
							|  |  |  | 	const uids = new Set(); | 
					
						
							|  |  |  | 	await Promise.all(Array.from(recipients).map(async (recipient) => { | 
					
						
							|  |  |  | 		const { type, id } = await activitypub.helpers.resolveLocalId(recipient); | 
					
						
							|  |  |  | 		if (type === 'user' && await user.exists(id)) { | 
					
						
							|  |  |  | 			uids.add(parseInt(id, 10)); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const followedUid = await db.getObjectField('followersUrl:uid', recipient); | 
					
						
							|  |  |  | 		if (followedUid) { | 
					
						
							| 
									
										
										
										
											2024-04-16 14:00:01 -04:00
										 |  |  | 			const { uids: followers } = await activitypub.actors.getLocalFollowers(followedUid); | 
					
						
							|  |  |  | 			if (followers.size > 0) { | 
					
						
							|  |  |  | 				followers.forEach((uid) => { | 
					
						
							|  |  |  | 					uids.add(uid); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (uids.size > 0) { | 
					
						
							|  |  |  | 		await db.setAdd(`post:${id}:recipients`, Array.from(uids)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes.getParentChain = async (uid, input) => { | 
					
						
							|  |  |  | 	// Traverse upwards via `inReplyTo` until you find the root-level Note
 | 
					
						
							|  |  |  | 	const id = activitypub.helpers.isUri(input) ? input : input.id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const chain = new Set(); | 
					
						
							|  |  |  | 	const traverse = async (uid, id) => { | 
					
						
							|  |  |  | 		// Handle remote reference to local post
 | 
					
						
							|  |  |  | 		const { type, id: localId } = await activitypub.helpers.resolveLocalId(id); | 
					
						
							|  |  |  | 		if (type === 'post' && localId) { | 
					
						
							|  |  |  | 			return await traverse(uid, localId); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const exists = await db.exists(`post:${id}`); | 
					
						
							|  |  |  | 		if (exists) { | 
					
						
							|  |  |  | 			const postData = await posts.getPostData(id); | 
					
						
							|  |  |  | 			chain.add(postData); | 
					
						
							|  |  |  | 			if (postData.toPid) { | 
					
						
							|  |  |  | 				await traverse(uid, postData.toPid); | 
					
						
							|  |  |  | 			} else if (utils.isNumber(id)) { // local pid without toPid, could be OP or reply to OP
 | 
					
						
							|  |  |  | 				const mainPid = await topics.getTopicField(postData.tid, 'mainPid'); | 
					
						
							|  |  |  | 				if (mainPid !== parseInt(id, 10)) { | 
					
						
							|  |  |  | 					await traverse(uid, mainPid); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | 			let object = !activitypub.helpers.isUri(input) && input.id === id ? input : undefined; | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | 			try { | 
					
						
							| 
									
										
										
										
											2024-03-22 15:28:01 -04:00
										 |  |  | 				object = object || await activitypub.get('uid', uid, id); | 
					
						
							| 
									
										
										
										
											2024-03-22 14:54:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// Handle incorrect id passed in
 | 
					
						
							|  |  |  | 				if (id !== object.id) { | 
					
						
							|  |  |  | 					return await traverse(uid, object.id); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				object = await activitypub.mocks.post(object); | 
					
						
							|  |  |  | 				if (object) { | 
					
						
							|  |  |  | 					chain.add(object); | 
					
						
							|  |  |  | 					if (object.toPid) { | 
					
						
							|  |  |  | 						await traverse(uid, object.toPid); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} catch (e) { | 
					
						
							|  |  |  | 				winston.warn(`[activitypub/notes/getParentChain] Cannot retrieve ${id}, terminating here.`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	await traverse(uid, id); | 
					
						
							|  |  |  | 	return chain; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 15:54:45 -04:00
										 |  |  | Notes.syncUserInboxes = async function (tid, uid) { | 
					
						
							| 
									
										
										
										
											2024-02-12 14:51:21 -05:00
										 |  |  | 	const [pids, { cid, mainPid }] = await Promise.all([ | 
					
						
							|  |  |  | 		db.getSortedSetMembers(`tid:${tid}:posts`), | 
					
						
							| 
									
										
										
										
											2024-02-12 14:59:13 -05:00
										 |  |  | 		topics.getTopicFields(tid, ['tid', 'cid', 'mainPid']), | 
					
						
							| 
									
										
										
										
											2024-02-12 14:51:21 -05:00
										 |  |  | 	]); | 
					
						
							|  |  |  | 	pids.unshift(mainPid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 16:41:51 -04:00
										 |  |  | 	const recipients = await db.getSetsMembers(pids.map(id => `post:${id}:recipients`)); | 
					
						
							|  |  |  | 	const uids = recipients.reduce((set, uids) => new Set([...set, ...uids.map(u => parseInt(u, 10))]), new Set()); | 
					
						
							| 
									
										
										
										
											2024-05-06 15:54:45 -04:00
										 |  |  | 	if (uid) { | 
					
						
							|  |  |  | 		uids.add(parseInt(uid, 10)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 16:41:51 -04:00
										 |  |  | 	const keys = Array.from(uids).map(uid => `uid:${uid}:inbox`); | 
					
						
							| 
									
										
										
										
											2024-02-12 14:34:37 -05:00
										 |  |  | 	const score = await db.sortedSetScore(`cid:${cid}:tids`, tid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 16:41:51 -04:00
										 |  |  | 	winston.verbose(`[activitypub/syncUserInboxes] Syncing tid ${tid} with ${uids.size} inboxes`); | 
					
						
							| 
									
										
										
										
											2024-02-12 15:01:10 -05:00
										 |  |  | 	await db.sortedSetsAdd(keys, keys.map(() => score || Date.now()), tid); | 
					
						
							| 
									
										
										
										
											2024-02-12 14:34:37 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2024-03-22 14:39:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Notes.getCategoryFollowers = async (cid) => { | 
					
						
							|  |  |  | 	// Retrieves remote users who have followed a category; used to build recipient list
 | 
					
						
							|  |  |  | 	let uids = await db.getSortedSetRangeByScore(`cid:${cid}:uid:watch:state`, 0, -1, categories.watchStates.tracking, categories.watchStates.tracking); | 
					
						
							|  |  |  | 	uids = uids.filter(uid => !utils.isNumber(uid)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return uids; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2024-05-01 14:44:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Notes.announce = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes.announce.list = async ({ pid, tid }) => { | 
					
						
							|  |  |  | 	let pids = []; | 
					
						
							|  |  |  | 	if (pid) { | 
					
						
							|  |  |  | 		pids = [pid]; | 
					
						
							|  |  |  | 	} else if (tid) { | 
					
						
							|  |  |  | 		let mainPid; | 
					
						
							|  |  |  | 		([pids, mainPid] = await Promise.all([ | 
					
						
							|  |  |  | 			db.getSortedSetMembers(`tid:${tid}:posts`), | 
					
						
							|  |  |  | 			topics.getTopicField(tid, 'mainPid'), | 
					
						
							|  |  |  | 		])); | 
					
						
							|  |  |  | 		pids.unshift(mainPid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!pids.length) { | 
					
						
							|  |  |  | 		return []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const keys = pids.map(pid => `pid:${pid}:announces`); | 
					
						
							|  |  |  | 	let announces = await db.getSortedSetsMembersWithScores(keys); | 
					
						
							|  |  |  | 	announces = announces.reduce((memo, cur, idx) => { | 
					
						
							|  |  |  | 		if (cur.length) { | 
					
						
							|  |  |  | 			const pid = pids[idx]; | 
					
						
							|  |  |  | 			cur.forEach(({ value: actor, score: timestamp }) => { | 
					
						
							|  |  |  | 				memo.push({ pid, actor, timestamp }); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return memo; | 
					
						
							|  |  |  | 	}, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return announces; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes.announce.add = async (pid, actor, timestamp = Date.now()) => { | 
					
						
							|  |  |  | 	await db.sortedSetAdd(`pid:${pid}:announces`, timestamp, actor); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes.announce.remove = async (pid, actor) => { | 
					
						
							|  |  |  | 	await db.sortedSetRemove(`pid:${pid}:announces`, actor); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes.announce.removeAll = async (pid) => { | 
					
						
							|  |  |  | 	await db.delete(`pid:${pid}:announces`); | 
					
						
							|  |  |  | }; |