From 988ac80087a116173ab36043a7a9dd2228d2986c Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Thu, 6 Feb 2020 17:07:37 +0900 Subject: [PATCH] Correct Like id generation (#5852) --- src/remote/activitypub/renderer/like.ts | 12 +++++++----- src/server/activitypub.ts | 24 +++++++++++++++++++++++- src/services/note/reaction/create.ts | 6 +++--- src/services/note/reaction/delete.ts | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/remote/activitypub/renderer/like.ts b/src/remote/activitypub/renderer/like.ts index 01f10ec0a..e36a3ab0d 100644 --- a/src/remote/activitypub/renderer/like.ts +++ b/src/remote/activitypub/renderer/like.ts @@ -1,10 +1,12 @@ import config from '../../../config'; -import { ILocalUser } from '../../../models/entities/user'; +import { NoteReaction } from '../../../models/entities/note-reaction'; import { Note } from '../../../models/entities/note'; -export default (user: ILocalUser, note: Note, reaction: string) => ({ +export const renderLike = (noteReaction: NoteReaction, note: Note) => ({ type: 'Like', - actor: `${config.url}/users/${user.id}`, - object: note.uri ? note.uri : `${config.url}/notes/${note.id}`, - _misskey_reaction: reaction + id: `${config.url}/likes/${noteReaction.id}`, + actor: `${config.url}/users/${noteReaction.userId}`, + object: note.uri ? note.uri : `${config.url}/notes/${noteReaction.noteId}`, + content: noteReaction.reaction, + _misskey_reaction: noteReaction.reaction }); diff --git a/src/server/activitypub.ts b/src/server/activitypub.ts index be8a118b9..c665fe28c 100644 --- a/src/server/activitypub.ts +++ b/src/server/activitypub.ts @@ -13,10 +13,11 @@ import Following from './activitypub/following'; import Featured from './activitypub/featured'; import { inbox as processInbox } from '../queue'; import { isSelfHost } from '../misc/convert-host'; -import { Notes, Users, Emojis, UserKeypairs } from '../models'; +import { Notes, Users, Emojis, UserKeypairs, NoteReactions } from '../models'; import { ILocalUser, User } from '../models/entities/user'; import { In } from 'typeorm'; import { ensure } from '../prelude/ensure'; +import { renderLike } from '../remote/activitypub/renderer/like'; // Init router const router = new Router(); @@ -202,4 +203,25 @@ router.get('/emojis/:emoji', async ctx => { setResponseType(ctx); }); +// like +router.get('/likes/:like', async ctx => { + const reaction = await NoteReactions.findOne(ctx.params.like); + + if (reaction == null) { + ctx.status = 404; + return; + } + + const note = await Notes.findOne(reaction.noteId); + + if (note == null) { + ctx.status = 404; + return; + } + + ctx.body = renderActivity(await renderLike(reaction, note)); + ctx.set('Cache-Control', 'public, max-age=180'); + setResponseType(ctx); +}); + export default router; diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index aee31813a..166c4e1f8 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -1,6 +1,6 @@ import { publishNoteStream } from '../../stream'; import watch from '../watch'; -import renderLike from '../../../remote/activitypub/renderer/like'; +import { renderLike } from '../../../remote/activitypub/renderer/like'; import DeliverManager from '../../../remote/activitypub/deliver-manager'; import { renderActivity } from '../../../remote/activitypub/renderer'; import { IdentifiableError } from '../../../misc/identifiable-error'; @@ -38,7 +38,7 @@ export default async (user: User, note: Note, reaction?: string) => { } // Create reaction - await NoteReactions.save({ + const inserted = await NoteReactions.save({ id: genId(), createdAt: new Date(), noteId: note.id, @@ -94,7 +94,7 @@ export default async (user: User, note: Note, reaction?: string) => { //#region 配信 if (Users.isLocalUser(user) && !note.localOnly) { - const content = renderActivity(renderLike(user, note, reaction)); + const content = renderActivity(renderLike(inserted, note)); const dm = new DeliverManager(user, content); if (note.userHost !== null) { const reactee = await Users.findOne(note.userId) diff --git a/src/services/note/reaction/delete.ts b/src/services/note/reaction/delete.ts index 533c3b406..620856c2d 100644 --- a/src/services/note/reaction/delete.ts +++ b/src/services/note/reaction/delete.ts @@ -1,5 +1,5 @@ import { publishNoteStream } from '../../stream'; -import renderLike from '../../../remote/activitypub/renderer/like'; +import { renderLike } from '../../../remote/activitypub/renderer/like'; import renderUndo from '../../../remote/activitypub/renderer/undo'; import { renderActivity } from '../../../remote/activitypub/renderer'; import DeliverManager from '../../../remote/activitypub/deliver-manager'; @@ -40,7 +40,7 @@ export default async (user: User, note: Note) => { //#region 配信 if (Users.isLocalUser(user) && !note.localOnly) { - const content = renderActivity(renderUndo(renderLike(user, note, exist.reaction), user)); + const content = renderActivity(renderUndo(renderLike(exist, note), user)); const dm = new DeliverManager(user, content); if (note.userHost !== null) { const reactee = await Users.findOne(note.userId)