[wip] Implement like activity
This commit is contained in:
		
							parent
							
								
									45fb451111
								
							
						
					
					
						commit
						7da1913964
					
				
					 3 changed files with 50 additions and 2 deletions
				
			
		| 
						 | 
					@ -5,12 +5,12 @@ import Reaction from './post-reaction';
 | 
				
			||||||
import { pack as packUser } from './user';
 | 
					import { pack as packUser } from './user';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const PostReaction = db.get<IPostReaction>('postReactions');
 | 
					const PostReaction = db.get<IPostReaction>('postReactions');
 | 
				
			||||||
 | 
					PostReaction.createIndex(['userId', 'postId'], { unique: true });
 | 
				
			||||||
export default PostReaction;
 | 
					export default PostReaction;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface IPostReaction {
 | 
					export interface IPostReaction {
 | 
				
			||||||
	_id: mongo.ObjectID;
 | 
						_id: mongo.ObjectID;
 | 
				
			||||||
	createdAt: Date;
 | 
						createdAt: Date;
 | 
				
			||||||
	deletedAt: Date;
 | 
					 | 
				
			||||||
	postId: mongo.ObjectID;
 | 
						postId: mongo.ObjectID;
 | 
				
			||||||
	userId: mongo.ObjectID;
 | 
						userId: mongo.ObjectID;
 | 
				
			||||||
	reaction: string;
 | 
						reaction: string;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										48
									
								
								src/remote/activitypub/act/like.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/remote/activitypub/act/like.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,48 @@
 | 
				
			||||||
 | 
					import { MongoError } from 'mongodb';
 | 
				
			||||||
 | 
					import Post from '../../../models/post';
 | 
				
			||||||
 | 
					import Reaction from '../../../models/post-reaction';
 | 
				
			||||||
 | 
					import config from '../../../config';
 | 
				
			||||||
 | 
					import queue from '../../../queue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default async (actor, activity) => {
 | 
				
			||||||
 | 
						const prefix = config.url + '/posts';
 | 
				
			||||||
 | 
						const id = activity.object.id || activity.object;
 | 
				
			||||||
 | 
						let reaction;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!id.startsWith(prefix)) {
 | 
				
			||||||
 | 
							return null;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const postId = id.slice(prefix.length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const post = await Post.findOne({ _id: postId });
 | 
				
			||||||
 | 
						if (post === null) {
 | 
				
			||||||
 | 
							throw new Error();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						try {
 | 
				
			||||||
 | 
							reaction = await Reaction.insert({
 | 
				
			||||||
 | 
								createdAt: new Date(),
 | 
				
			||||||
 | 
								postId,
 | 
				
			||||||
 | 
								userId: actor._id,
 | 
				
			||||||
 | 
								reaction: 'pudding'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						} catch (exception) {
 | 
				
			||||||
 | 
							// duplicate key error
 | 
				
			||||||
 | 
							if (exception instanceof MongoError && exception.code === 11000) {
 | 
				
			||||||
 | 
								return null;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							throw exception;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						await new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
							queue.create('http', { type: 'like', reaction: reaction._id }).save(error => {
 | 
				
			||||||
 | 
								if (error) {
 | 
				
			||||||
 | 
									reject(error);
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									resolve(null);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -30,7 +30,7 @@ export default async (user, post) => {
 | 
				
			||||||
	const attributedTo = `${config.url}/@${user.username}`;
 | 
						const attributedTo = `${config.url}/@${user.username}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return {
 | 
						return {
 | 
				
			||||||
		id: `${attributedTo}/${post._id}`,
 | 
							id: `${config.url}/posts/${post._id}}`,
 | 
				
			||||||
		type: 'Note',
 | 
							type: 'Note',
 | 
				
			||||||
		attributedTo,
 | 
							attributedTo,
 | 
				
			||||||
		content: post.textHtml,
 | 
							content: post.textHtml,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue