Feat: remote local timeline
This commit is contained in:
parent
35361f933e
commit
eee41cc6ba
7 changed files with 652 additions and 151 deletions
|
|
@ -283,6 +283,7 @@ import * as ep___notes_featured from './endpoints/notes/featured.js';
|
|||
import * as ep___notes_globalTimeline from './endpoints/notes/global-timeline.js';
|
||||
import * as ep___notes_hybridTimeline from './endpoints/notes/hybrid-timeline.js';
|
||||
import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js';
|
||||
import * as ep___notes_anyLocalTimeline from './endpoints/notes/any-local-timeline.js';
|
||||
import * as ep___notes_mentions from './endpoints/notes/mentions.js';
|
||||
import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js';
|
||||
import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js';
|
||||
|
|
@ -600,7 +601,7 @@ const $i_importMuting: Provider = { provide: 'ep:i/import-muting', useClass: ep_
|
|||
const $i_importUserLists: Provider = { provide: 'ep:i/import-user-lists', useClass: ep___i_importUserLists.default };
|
||||
const $i_importAntennas: Provider = { provide: 'ep:i/import-antennas', useClass: ep___i_importAntennas.default };
|
||||
const $i_notifications: Provider = { provide: 'ep:i/notifications', useClass: ep___i_notifications.default };
|
||||
const $i_userstats: Provider = { provide: 'ep:i/stats', useClass: ep___users_user_stats.default }
|
||||
const $i_userstats: Provider = { provide: 'ep:i/stats', useClass: ep___users_user_stats.default };
|
||||
const $i_notificationsGrouped: Provider = { provide: 'ep:i/notifications-grouped', useClass: ep___i_notificationsGrouped.default };
|
||||
const $i_pageLikes: Provider = { provide: 'ep:i/page-likes', useClass: ep___i_pageLikes.default };
|
||||
const $i_pages: Provider = { provide: 'ep:i/pages', useClass: ep___i_pages.default };
|
||||
|
|
@ -658,6 +659,7 @@ const $notes_featured: Provider = { provide: 'ep:notes/featured', useClass: ep__
|
|||
const $notes_globalTimeline: Provider = { provide: 'ep:notes/global-timeline', useClass: ep___notes_globalTimeline.default };
|
||||
const $notes_hybridTimeline: Provider = { provide: 'ep:notes/hybrid-timeline', useClass: ep___notes_hybridTimeline.default };
|
||||
const $notes_localTimeline: Provider = { provide: 'ep:notes/local-timeline', useClass: ep___notes_localTimeline.default };
|
||||
const $notes_anyLocalTimeline: Provider = { provide: 'ep:notes/any-local-timeline', useClass: ep___notes_anyLocalTimeline.default };
|
||||
const $notes_mentions: Provider = { provide: 'ep:notes/mentions', useClass: ep___notes_mentions.default };
|
||||
const $notes_polls_recommendation: Provider = { provide: 'ep:notes/polls/recommendation', useClass: ep___notes_polls_recommendation.default };
|
||||
const $notes_polls_vote: Provider = { provide: 'ep:notes/polls/vote', useClass: ep___notes_polls_vote.default };
|
||||
|
|
@ -783,8 +785,8 @@ const $bubbleGame_ranking: Provider = { provide: 'ep:bubble-game/ranking', useCl
|
|||
$admin_drive_files,
|
||||
$admin_drive_showFile,
|
||||
$admin_emoji_addAliasesBulk,
|
||||
$admin_emoji_setlocalOnlyBulk,
|
||||
$admin_emoji_setisSensitiveBulk,
|
||||
$admin_emoji_setlocalOnlyBulk,
|
||||
$admin_emoji_setisSensitiveBulk,
|
||||
$admin_emoji_add,
|
||||
$admin_emoji_addRequest,
|
||||
$admin_emoji_copy,
|
||||
|
|
@ -866,7 +868,7 @@ const $bubbleGame_ranking: Provider = { provide: 'ep:bubble-game/ranking', useCl
|
|||
$channels_timeline,
|
||||
$channels_unfollow,
|
||||
$channels_update,
|
||||
$i_userstats,
|
||||
$i_userstats,
|
||||
$channels_favorite,
|
||||
$channels_unfavorite,
|
||||
$channels_myFavorites,
|
||||
|
|
@ -1036,6 +1038,7 @@ const $bubbleGame_ranking: Provider = { provide: 'ep:bubble-game/ranking', useCl
|
|||
$notes_globalTimeline,
|
||||
$notes_hybridTimeline,
|
||||
$notes_localTimeline,
|
||||
$notes_anyLocalTimeline,
|
||||
$notes_mentions,
|
||||
$notes_polls_recommendation,
|
||||
$notes_polls_vote,
|
||||
|
|
@ -1168,12 +1171,12 @@ const $bubbleGame_ranking: Provider = { provide: 'ep:bubble-game/ranking', useCl
|
|||
$admin_emoji_setAliasesBulk,
|
||||
$admin_emoji_setCategoryBulk,
|
||||
$admin_emoji_setLicenseBulk,
|
||||
$admin_emoji_setlocalOnlyBulk,
|
||||
$admin_emoji_setisSensitiveBulk,
|
||||
$admin_emoji_setlocalOnlyBulk,
|
||||
$admin_emoji_setisSensitiveBulk,
|
||||
$admin_emoji_update,
|
||||
$admin_emoji_updateRequest,
|
||||
$admin_federation_deleteAllFiles,
|
||||
$i_userstats,
|
||||
$i_userstats,
|
||||
$admin_federation_refreshRemoteInstanceMetadata,
|
||||
$admin_federation_removeAllFollowing,
|
||||
$admin_federation_updateInstance,
|
||||
|
|
@ -1408,6 +1411,7 @@ const $bubbleGame_ranking: Provider = { provide: 'ep:bubble-game/ranking', useCl
|
|||
$notes_globalTimeline,
|
||||
$notes_hybridTimeline,
|
||||
$notes_localTimeline,
|
||||
$notes_anyLocalTimeline,
|
||||
$notes_mentions,
|
||||
$notes_polls_recommendation,
|
||||
$notes_polls_vote,
|
||||
|
|
|
|||
|
|
@ -284,6 +284,7 @@ import * as ep___notes_featured from './endpoints/notes/featured.js';
|
|||
import * as ep___notes_globalTimeline from './endpoints/notes/global-timeline.js';
|
||||
import * as ep___notes_hybridTimeline from './endpoints/notes/hybrid-timeline.js';
|
||||
import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js';
|
||||
import * as ep___notes_anyLocalTimeline from './endpoints/notes/any-local-timeline.js';
|
||||
import * as ep___notes_mentions from './endpoints/notes/mentions.js';
|
||||
import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js';
|
||||
import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js';
|
||||
|
|
@ -656,6 +657,7 @@ const eps = [
|
|||
['notes/global-timeline', ep___notes_globalTimeline],
|
||||
['notes/hybrid-timeline', ep___notes_hybridTimeline],
|
||||
['notes/local-timeline', ep___notes_localTimeline],
|
||||
['notes/any-local-timeline', ep___notes_anyLocalTimeline],
|
||||
['notes/mentions', ep___notes_mentions],
|
||||
['notes/polls/recommendation', ep___notes_polls_recommendation],
|
||||
['notes/polls/vote', ep___notes_polls_vote],
|
||||
|
|
|
|||
|
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { In } from 'typeorm';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
||||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||
import { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
|
||||
import { ApResolverService } from '@/core/activitypub/ApResolverService.js';
|
||||
import { getApId, isActor, isPost } from '@/core/activitypub/type.js';
|
||||
import { ApNoteService } from '@/core/activitypub/models/ApNoteService.js';
|
||||
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { NotesRepository } from '@/models/_.js';
|
||||
import { MiNote, MiUser } from '@/models/_.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { SchemaType } from '@/misc/json-schema.js';
|
||||
import { UtilityService } from '@/core/UtilityService.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
import { MetaService } from '@/core/MetaService.js';
|
||||
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['notes'],
|
||||
|
||||
res: {
|
||||
type: 'array',
|
||||
optional: false, nullable: false,
|
||||
items: {
|
||||
type: 'object',
|
||||
optional: false, nullable: false,
|
||||
ref: 'Note',
|
||||
},
|
||||
},
|
||||
|
||||
errors: {
|
||||
hostIsNull: {
|
||||
message: 'Host is null',
|
||||
code: 'HOST_NULL',
|
||||
id: 'PRSMSK-ANY-LTL-0001',
|
||||
},
|
||||
|
||||
bothWithRepliesAndWithFiles: {
|
||||
message: 'Specifying both withReplies and withFiles is not supported',
|
||||
code: 'BOTH_WITH_REPLIES_AND_WITH_FILES',
|
||||
id: 'dd9c8400-1cb5-4eef-8a31-200c5f933793',
|
||||
},
|
||||
remoteTokenIsNull: {
|
||||
message: 'remoteToken is null',
|
||||
code: 'REMOTE_TOKEN_NULL',
|
||||
id: 'PRSMSK-ANY-LTL-0002',
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const paramDef = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
withFiles: { type: 'boolean', default: false },
|
||||
withRenotes: { type: 'boolean', default: true },
|
||||
withReplies: { type: 'boolean', default: false },
|
||||
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
||||
sinceId: { type: 'string', format: 'misskey:id' },
|
||||
untilId: { type: 'string', format: 'misskey:id' },
|
||||
allowPartial: { type: 'boolean', default: false }, // true is recommended but for compatibility false by default
|
||||
sinceDate: { type: 'integer' },
|
||||
untilDate: { type: 'integer' },
|
||||
host: { type: 'string' },
|
||||
remoteToken: { type: 'string' },
|
||||
},
|
||||
required: [],
|
||||
} as const;
|
||||
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.notesRepository)
|
||||
private notesRepository: NotesRepository,
|
||||
private idService: IdService,
|
||||
private federatedInstanceService: FederatedInstanceService,
|
||||
private httpRequestService: HttpRequestService,
|
||||
private utilityService: UtilityService,
|
||||
private userEntityService: UserEntityService,
|
||||
private noteEntityService: NoteEntityService,
|
||||
private metaService: MetaService,
|
||||
private apResolverService: ApResolverService,
|
||||
private apDbResolverService: ApDbResolverService,
|
||||
private apPersonService: ApPersonService,
|
||||
private apNoteService: ApNoteService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
|
||||
const sinceId = ps.sinceId ?? (ps.sinceDate ? this.idService.gen(ps.sinceDate!) : null);
|
||||
if (ps.host === undefined) throw new ApiError(meta.errors.hostIsNull);
|
||||
if (ps.remoteToken === undefined) throw new ApiError(meta.errors.remoteTokenIsNull);
|
||||
const i = await this.federatedInstanceService.fetch(ps.host);
|
||||
const noteIds = [];
|
||||
|
||||
if (i.softwareName === 'misskey') {
|
||||
const remoteTimeline: string[] = await (await this.httpRequestService.send('https://' + ps.host + '/api/notes/local-timeline', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
i: ps.remoteToken,
|
||||
withFiles: ps.withFiles,
|
||||
withRenotes: ps.withRenotes,
|
||||
withReplies: ps.withReplies,
|
||||
limit: 30,
|
||||
}),
|
||||
})).json() as string[];
|
||||
|
||||
if (remoteTimeline.length > 0) {
|
||||
for (const note of remoteTimeline) {
|
||||
const uri = `https://${ps.host}/notes/${note.id}`;
|
||||
const note_ = await this.fetchAny(uri, me);
|
||||
if (note_ == null) continue;
|
||||
noteIds.push(note_.id);
|
||||
}
|
||||
}
|
||||
|
||||
let notes = await this.notesRepository.findBy({ id: In(noteIds) });
|
||||
let packedNote: any[] = await this.noteEntityService.packMany(notes, me, { detail: true });
|
||||
if (untilId) {
|
||||
let lastRemoteId;
|
||||
const lastUri = packedNote[packedNote.length - 1].uri;
|
||||
lastRemoteId = lastUri.split('/')[lastUri.split('/').length - 1];
|
||||
do {
|
||||
const remoteTimeline: string[] = await (await this.httpRequestService.send('https://' + ps.host + '/api/notes/local-timeline', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
i: ps.remoteToken,
|
||||
withFiles: ps.withFiles,
|
||||
withRenotes: ps.withRenotes,
|
||||
withReplies: ps.withReplies,
|
||||
untilId: lastRemoteId,
|
||||
limit: 30,
|
||||
}),
|
||||
})).json() as string[];
|
||||
|
||||
if (remoteTimeline.length > 0) {
|
||||
for (const note of remoteTimeline) {
|
||||
const uri = `https://${ps.host}/notes/${note.id}`;
|
||||
const note_ = await this.fetchAny(uri, me);
|
||||
if (note_ == null) continue;
|
||||
//noteIds.push(note_.id);
|
||||
lastRemoteId = note_.id;
|
||||
if (lastRemoteId === ps.untilId) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (lastRemoteId !== ps.untilId);
|
||||
const remoteTimeline: string[] = await (await this.httpRequestService.send('https://' + ps.host + '/api/notes/local-timeline', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
i: ps.remoteToken,
|
||||
withFiles: ps.withFiles,
|
||||
withRenotes: ps.withRenotes,
|
||||
withReplies: ps.withReplies,
|
||||
untilId: lastRemoteId,
|
||||
limit: 30,
|
||||
}),
|
||||
})).json() as string[];
|
||||
|
||||
if (remoteTimeline.length > 0) {
|
||||
for (const note of remoteTimeline) {
|
||||
const uri = `https://${ps.host}/notes/${note.id}`;
|
||||
const note_ = await this.fetchAny(uri, me);
|
||||
if (note_ == null) continue;
|
||||
noteIds.push(note_.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
notes = await this.notesRepository.findBy({ id: In(noteIds) });
|
||||
packedNote = await this.noteEntityService.packMany(notes, me, { detail: true });
|
||||
return packedNote.reverse();
|
||||
}
|
||||
});
|
||||
}
|
||||
@bindThis
|
||||
private async fetchAny(uri: string, me: MiLocalUser | null | undefined) {
|
||||
// ブロックしてたら中断
|
||||
const fetchedMeta = await this.metaService.fetch();
|
||||
if (this.utilityService.isBlockedHost(fetchedMeta.blockedHosts, this.utilityService.extractDbHost(uri))) return null;
|
||||
|
||||
let local = await this.mergePack(me, ...await Promise.all([
|
||||
this.apDbResolverService.getUserFromApId(uri),
|
||||
this.apDbResolverService.getNoteFromApId(uri),
|
||||
]));
|
||||
if (local != null) return local;
|
||||
|
||||
// リモートから一旦オブジェクトフェッチ
|
||||
let object;
|
||||
try {
|
||||
const resolver = this.apResolverService.createResolver();
|
||||
object = await resolver.resolve(uri) as any;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
if (!object) return null;
|
||||
// /@user のような正規id以外で取得できるURIが指定されていた場合、ここで初めて正規URIが確定する
|
||||
// これはDBに存在する可能性があるため再度DB検索
|
||||
if (uri !== object.id) {
|
||||
local = await this.mergePack(me, ...await Promise.all([
|
||||
this.apDbResolverService.getUserFromApId(object.id),
|
||||
this.apDbResolverService.getNoteFromApId(object.id),
|
||||
]));
|
||||
if (local != null) return local;
|
||||
}
|
||||
|
||||
return await this.mergePack(
|
||||
me,
|
||||
isActor(object) ? await this.apPersonService.createPerson(getApId(object)) : null,
|
||||
isPost(object) ? await this.apNoteService.createNote(getApId(object), undefined, true) : null,
|
||||
);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private async mergePack(me: MiLocalUser | null | undefined, user: MiUser | null | undefined, note: MiNote | null | undefined) {
|
||||
if (note != null) {
|
||||
try {
|
||||
const object = await this.noteEntityService.pack(note, me, { detail: true });
|
||||
|
||||
return object;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue