Correct Like id generation (#5852)

This commit is contained in:
MeiMei 2020-02-06 17:07:37 +09:00 committed by GitHub
parent 1c7c72181e
commit 988ac80087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 11 deletions

View File

@ -1,10 +1,12 @@
import config from '../../../config'; import config from '../../../config';
import { ILocalUser } from '../../../models/entities/user'; import { NoteReaction } from '../../../models/entities/note-reaction';
import { Note } from '../../../models/entities/note'; import { Note } from '../../../models/entities/note';
export default (user: ILocalUser, note: Note, reaction: string) => ({ export const renderLike = (noteReaction: NoteReaction, note: Note) => ({
type: 'Like', type: 'Like',
actor: `${config.url}/users/${user.id}`, id: `${config.url}/likes/${noteReaction.id}`,
object: note.uri ? note.uri : `${config.url}/notes/${note.id}`, actor: `${config.url}/users/${noteReaction.userId}`,
_misskey_reaction: reaction object: note.uri ? note.uri : `${config.url}/notes/${noteReaction.noteId}`,
content: noteReaction.reaction,
_misskey_reaction: noteReaction.reaction
}); });

View File

@ -13,10 +13,11 @@ import Following from './activitypub/following';
import Featured from './activitypub/featured'; import Featured from './activitypub/featured';
import { inbox as processInbox } from '../queue'; import { inbox as processInbox } from '../queue';
import { isSelfHost } from '../misc/convert-host'; 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 { ILocalUser, User } from '../models/entities/user';
import { In } from 'typeorm'; import { In } from 'typeorm';
import { ensure } from '../prelude/ensure'; import { ensure } from '../prelude/ensure';
import { renderLike } from '../remote/activitypub/renderer/like';
// Init router // Init router
const router = new Router(); const router = new Router();
@ -202,4 +203,25 @@ router.get('/emojis/:emoji', async ctx => {
setResponseType(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; export default router;

View File

@ -1,6 +1,6 @@
import { publishNoteStream } from '../../stream'; import { publishNoteStream } from '../../stream';
import watch from '../watch'; 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 DeliverManager from '../../../remote/activitypub/deliver-manager';
import { renderActivity } from '../../../remote/activitypub/renderer'; import { renderActivity } from '../../../remote/activitypub/renderer';
import { IdentifiableError } from '../../../misc/identifiable-error'; import { IdentifiableError } from '../../../misc/identifiable-error';
@ -38,7 +38,7 @@ export default async (user: User, note: Note, reaction?: string) => {
} }
// Create reaction // Create reaction
await NoteReactions.save({ const inserted = await NoteReactions.save({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
noteId: note.id, noteId: note.id,
@ -94,7 +94,7 @@ export default async (user: User, note: Note, reaction?: string) => {
//#region 配信 //#region 配信
if (Users.isLocalUser(user) && !note.localOnly) { 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); const dm = new DeliverManager(user, content);
if (note.userHost !== null) { if (note.userHost !== null) {
const reactee = await Users.findOne(note.userId) const reactee = await Users.findOne(note.userId)

View File

@ -1,5 +1,5 @@
import { publishNoteStream } from '../../stream'; 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 renderUndo from '../../../remote/activitypub/renderer/undo';
import { renderActivity } from '../../../remote/activitypub/renderer'; import { renderActivity } from '../../../remote/activitypub/renderer';
import DeliverManager from '../../../remote/activitypub/deliver-manager'; import DeliverManager from '../../../remote/activitypub/deliver-manager';
@ -40,7 +40,7 @@ export default async (user: User, note: Note) => {
//#region 配信 //#region 配信
if (Users.isLocalUser(user) && !note.localOnly) { 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); const dm = new DeliverManager(user, content);
if (note.userHost !== null) { if (note.userHost !== null) {
const reactee = await Users.findOne(note.userId) const reactee = await Users.findOne(note.userId)