forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from anatawa12/vmimi-relay-timeline
virtual kemomimi relay timeline
- Loading branch information
Showing
22 changed files
with
511 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { Brackets, SelectQueryBuilder } from 'typeorm'; | ||
import { bindThis } from '@/decorators.js'; | ||
import { HttpRequestService } from '@/core/HttpRequestService.js'; | ||
import { LoggerService } from '@/core/LoggerService.js'; | ||
import type Logger from '@/logger.js'; | ||
|
||
type VmimiInstanceList = { Url: string; }[]; | ||
|
||
// one day | ||
const UpdateInterval = 1000 * 60 * 60 * 24; | ||
const RetryInterval = 1000 * 60 * 60 * 6; | ||
|
||
@Injectable() | ||
export class VmimiRelayTimelineService { | ||
instanceHosts: Set<string>; | ||
instanceHostsArray: string[]; | ||
nextUpdate: number; | ||
updatePromise: Promise<void> | null; | ||
private logger: Logger; | ||
|
||
constructor( | ||
private httpRequestService: HttpRequestService, | ||
private loggerService: LoggerService, | ||
) { | ||
// Initialize with | ||
this.instanceHosts = new Set<string>([]); | ||
this.instanceHostsArray = []; | ||
this.nextUpdate = 0; | ||
this.updatePromise = null; | ||
|
||
this.logger = this.loggerService.getLogger('vmimi'); | ||
|
||
this.checkForUpdateInstanceList(); | ||
} | ||
|
||
@bindThis | ||
checkForUpdateInstanceList() { | ||
if (this.updatePromise == null && this.nextUpdate < Date.now()) { | ||
this.updatePromise = this.updateInstanceList().finally(() => this.updatePromise = null); | ||
} | ||
} | ||
|
||
@bindThis | ||
async updateInstanceList() { | ||
try { | ||
this.logger.info('Updating instance list'); | ||
const instanceList = await this.httpRequestService.getJson<VmimiInstanceList>('https://relay.virtualkemomimi.net/api/servers'); | ||
this.instanceHostsArray = instanceList.map(i => new URL(i.Url).host); | ||
this.instanceHosts = new Set<string>(this.instanceHostsArray); | ||
this.nextUpdate = Date.now() + UpdateInterval; | ||
this.logger.info(`Got instance list: ${this.instanceHostsArray}`); | ||
} catch (e) { | ||
this.logger.error('Failed to update instance list', e as any); | ||
this.nextUpdate = Date.now() + RetryInterval; | ||
setTimeout(() => this.checkForUpdateInstanceList(), RetryInterval + 5); | ||
} | ||
} | ||
|
||
@bindThis | ||
isRelayedInstance(host: string | null): boolean { | ||
this.checkForUpdateInstanceList(); | ||
// assuming the current instance is joined to the i relay | ||
if (host == null) return true; | ||
return this.instanceHosts.has(host); | ||
} | ||
|
||
get hostNames (): string[] { | ||
this.checkForUpdateInstanceList(); | ||
return this.instanceHostsArray; | ||
} | ||
|
||
@bindThis | ||
generateFilterQuery(query: SelectQueryBuilder<any>, excludeReplies: boolean) { | ||
const names = this.hostNames; | ||
query.andWhere(new Brackets(qb => { | ||
qb.where('note.userHost IS NULL'); | ||
if (names.length !== 0) { | ||
qb.orWhere('note.userHost IN (:...vmimiRelayInstances)', { vmimiRelayInstances: names }); | ||
} | ||
})); | ||
if (excludeReplies) { | ||
query.andWhere(new Brackets(qb => { | ||
qb.where('note.replyUserHost IS NULL'); | ||
if (names.length !== 0) { | ||
qb.orWhere('note.replyUserHost IN (:...vmimiRelayInstances)', { vmimiRelayInstances: names }); | ||
} | ||
})); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/backend/src/server/api/endpoints/notes/vmimi-relay-timeline.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* SPDX-FileCopyrightText: anatawa12 | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Brackets } from 'typeorm'; | ||
import type { NotesRepository } from '@/models/_.js'; | ||
import { Endpoint } from '@/server/api/endpoint-base.js'; | ||
import { QueryService } from '@/core/QueryService.js'; | ||
import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; | ||
import ActiveUsersChart from '@/core/chart/charts/active-users.js'; | ||
import { DI } from '@/di-symbols.js'; | ||
import { RoleService } from '@/core/RoleService.js'; | ||
import { VmimiRelayTimelineService } from '@/core/VmimiRelayTimelineService.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: { | ||
gtlDisabled: { | ||
message: 'Global timeline has been disabled.', | ||
code: 'GTL_DISABLED', | ||
id: '0332fc13-6ab2-4427-ae80-a9fadffd1a6b', | ||
}, | ||
}, | ||
} 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' }, | ||
sinceDate: { type: 'integer' }, | ||
untilDate: { type: 'integer' }, | ||
}, | ||
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 noteEntityService: NoteEntityService, | ||
private queryService: QueryService, | ||
private roleService: RoleService, | ||
private activeUsersChart: ActiveUsersChart, | ||
private vmimiRelayTimelineService: VmimiRelayTimelineService, | ||
) { | ||
super(meta, paramDef, async (ps, me) => { | ||
const policies = await this.roleService.getUserPolicies(me ? me.id : null); | ||
if (!policies.gtlAvailable) { | ||
throw new ApiError(meta.errors.gtlDisabled); | ||
} | ||
|
||
//#region Construct query | ||
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), | ||
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) | ||
.andWhere('note.visibility = \'public\'') | ||
.andWhere('note.channelId IS NULL') | ||
.innerJoinAndSelect('note.user', 'user') | ||
.leftJoinAndSelect('note.reply', 'reply') | ||
.leftJoinAndSelect('note.renote', 'renote') | ||
.leftJoinAndSelect('reply.user', 'replyUser') | ||
.leftJoinAndSelect('renote.user', 'renoteUser'); | ||
|
||
this.vmimiRelayTimelineService.generateFilterQuery(query, !ps.withReplies); | ||
|
||
if (me) { | ||
this.queryService.generateMutedUserQuery(query, me); | ||
this.queryService.generateBlockedUserQuery(query, me); | ||
this.queryService.generateMutedUserRenotesQueryForNotes(query, me); | ||
} | ||
|
||
if (ps.withFiles) { | ||
query.andWhere('note.fileIds != \'{}\''); | ||
} | ||
|
||
if (ps.withRenotes === false) { | ||
query.andWhere(new Brackets(qb => { | ||
qb.where('note.renoteId IS NULL'); | ||
qb.orWhere(new Brackets(qb => { | ||
qb.where('note.text IS NOT NULL'); | ||
qb.orWhere('note.fileIds != \'{}\''); | ||
})); | ||
})); | ||
} | ||
//#endregion | ||
|
||
const timeline = await query.limit(ps.limit).getMany(); | ||
|
||
process.nextTick(() => { | ||
if (me) { | ||
this.activeUsersChart.read(me); | ||
} | ||
}); | ||
|
||
return await this.noteEntityService.packMany(timeline, me); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.