Implement like
This commit is contained in:
parent
321f61f1cb
commit
c5f23bce78
7 changed files with 131 additions and 137 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import { Object } from '../type';
|
||||
import { IRemoteUser } from '../../../models/user';
|
||||
import create from './create';
|
||||
import performDeleteActivity from './delete';
|
||||
import follow from './follow';
|
||||
import undo from './undo';
|
||||
import { Object } from '../type';
|
||||
import { IRemoteUser } from '../../../models/user';
|
||||
import like from './like';
|
||||
|
||||
const self = async (actor: IRemoteUser, activity: Object): Promise<void> => {
|
||||
switch (activity.type) {
|
||||
|
|
@ -23,6 +24,10 @@ const self = async (actor: IRemoteUser, activity: Object): Promise<void> => {
|
|||
// noop
|
||||
break;
|
||||
|
||||
case 'Like':
|
||||
await like(actor, activity);
|
||||
break;
|
||||
|
||||
case 'Undo':
|
||||
await undo(actor, activity);
|
||||
break;
|
||||
|
|
@ -33,7 +38,7 @@ const self = async (actor: IRemoteUser, activity: Object): Promise<void> => {
|
|||
break;
|
||||
|
||||
default:
|
||||
console.warn(`unknown activity type: ${activity.type}`);
|
||||
console.warn(`unknown activity type: ${(activity as any).type}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { MongoError } from 'mongodb';
|
||||
import Reaction, { IPostReaction } from '../../../models/post-reaction';
|
||||
import Post from '../../../models/post';
|
||||
import queue from '../../../queue';
|
||||
import { IRemoteUser } from '../../../models/user';
|
||||
import { ILike } from '../type';
|
||||
import create from '../../../services/post/reaction/create';
|
||||
|
||||
export default async (resolver, actor, activity, distribute) => {
|
||||
const id = activity.object.id || activity.object;
|
||||
export default async (actor: IRemoteUser, activity: ILike) => {
|
||||
const id = typeof activity.object == 'string' ? activity.object : activity.object.id;
|
||||
|
||||
// Transform:
|
||||
// https://misskey.ex/@syuilo/xxxx to
|
||||
|
|
@ -16,48 +16,5 @@ export default async (resolver, actor, activity, distribute) => {
|
|||
throw new Error();
|
||||
}
|
||||
|
||||
if (!distribute) {
|
||||
const { _id } = await Reaction.findOne({
|
||||
userId: actor._id,
|
||||
postId: post._id
|
||||
});
|
||||
|
||||
return {
|
||||
resolver,
|
||||
object: { $ref: 'postPeactions', $id: _id }
|
||||
};
|
||||
}
|
||||
|
||||
const promisedReaction = Reaction.insert({
|
||||
createdAt: new Date(),
|
||||
userId: actor._id,
|
||||
postId: post._id,
|
||||
reaction: 'pudding'
|
||||
}).then(reaction => new Promise<IPostReaction>((resolve, reject) => {
|
||||
queue.create('http', {
|
||||
type: 'reaction',
|
||||
reactionId: reaction._id
|
||||
}).save(error => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(reaction);
|
||||
}
|
||||
});
|
||||
}), async error => {
|
||||
// duplicate key error
|
||||
if (error instanceof MongoError && error.code === 11000) {
|
||||
return Reaction.findOne({
|
||||
userId: actor._id,
|
||||
postId: post._id
|
||||
});
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
|
||||
return promisedReaction.then(({ _id }) => ({
|
||||
resolver,
|
||||
object: { $ref: 'postPeactions', $id: _id }
|
||||
}));
|
||||
await create(actor, post, 'pudding');
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue