diff --git a/UPGRADE_NOTES.md b/UPGRADE_NOTES.md new file mode 100644 index 0000000000..8bebd4eb34 --- /dev/null +++ b/UPGRADE_NOTES.md @@ -0,0 +1,41 @@ +# Upgrade Notes + +## 2024.9.0 + +### Following Feed + +When upgrading an existing instance to version 2024.9.0, the Following Feed will initially be empty. +The feed will gradually fill as new posts federate, but it may be desirable to back-fill the feed with existing data. +This database script will populate the feed with the latest post of each type for all users, ensuring that data is fully populated after the update. +Run this after migrations but before starting the instance. +Warning: the script may take a long time to execute! + +```postgresql +INSERT INTO latest_note (user_id, note_id, is_public, is_reply, is_quote) +SELECT + "userId" as user_id, + id as note_id, + visibility = 'public' AS is_public, + "replyId" IS NOT NULL AS is_reply, + ( + "renoteId" IS NOT NULL + AND ( + text IS NOT NULL + OR cw IS NOT NULL + OR "replyId" IS NOT NULL + OR "hasPoll" + OR "fileIds" != '{}' + ) + ) AS is_quote +FROM note +WHERE ( -- Exclude pure renotes (boosts) + "renoteId" IS NULL + OR text IS NOT NULL + OR cw IS NOT NULL + OR "replyId" IS NOT NULL + OR "hasPoll" + OR "fileIds" != '{}' + ) +ORDER BY id DESC -- This part is very important: it ensures that we only load the *latest* notes of each type. Do not remove it! +ON CONFLICT DO NOTHING; -- Any conflicts are guaranteed to be older notes that we can ignore. +``` diff --git a/locales/index.d.ts b/locales/index.d.ts index 8ccdaebcb9..f27793b860 100644 --- a/locales/index.d.ts +++ b/locales/index.d.ts @@ -10916,6 +10916,22 @@ export interface Locale extends ILocale { * Severing all follow relations with {host} queued. */ "severAllFollowRelationsQueued": ParameterizedString<"host">; + /** + * Pending follow requests + */ + "pendingFollowRequests": string; + /** + * Show quotes + */ + "showQuotes": string; + /** + * Show replies + */ + "showReplies": string; + /** + * Show non-public + */ + "showNonPublicNotes": string; "_mfm": { /** * This is not a widespread feature, it may not display properly on most other fedi software, including other Misskey forks diff --git a/locales/version.d.ts b/locales/version.d.ts new file mode 100644 index 0000000000..54ceec7443 --- /dev/null +++ b/locales/version.d.ts @@ -0,0 +1 @@ +export const localesVersion: string; diff --git a/locales/version.js b/locales/version.js new file mode 100644 index 0000000000..e84414b74d --- /dev/null +++ b/locales/version.js @@ -0,0 +1,14 @@ +import { createHash } from 'crypto'; +import locales from './index.js'; + +// MD5 is acceptable because we don't need cryptographic security. +const hash = createHash('md5'); + +// Derive the version hash from locale content exclusively. +// This avoids the problem of "stuck" translations after modifying locale files. +const localesText = JSON.stringify(locales); +hash.update(localesText, 'utf8'); + +// We can't use regular base64 since this becomes part of a filename. +// Base64URL avoids special characters that would cause an issue. +export const localesVersion = hash.digest().toString('base64url'); diff --git a/packages/backend/migration/1728420772835-track-latest-note-type.js b/packages/backend/migration/1728420772835-track-latest-note-type.js new file mode 100644 index 0000000000..4c9b4ca594 --- /dev/null +++ b/packages/backend/migration/1728420772835-track-latest-note-type.js @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +export class TrackLatestNoteType1728420772835 { + name = 'TrackLatestNoteType1728420772835' + + async up(queryRunner) { + await queryRunner.query(`ALTER TABLE "latest_note" DROP CONSTRAINT "PK_f619b62bfaafabe68f52fb50c9a"`); + await queryRunner.query(`ALTER TABLE "latest_note" ADD "is_public" boolean NOT NULL DEFAULT false`); + await queryRunner.query(`ALTER TABLE "latest_note" ADD "is_reply" boolean NOT NULL DEFAULT false`); + await queryRunner.query(`ALTER TABLE "latest_note" ADD "is_quote" boolean NOT NULL DEFAULT false`); + await queryRunner.query(`ALTER TABLE "latest_note" ADD CONSTRAINT "PK_a44ac8ca9cb916faeefc0912abd" PRIMARY KEY ("user_id", is_public, is_reply, is_quote)`); + } + + async down(queryRunner) { + await queryRunner.query(`ALTER TABLE "latest_note" DROP CONSTRAINT "PK_a44ac8ca9cb916faeefc0912abd"`); + await queryRunner.query(`ALTER TABLE "latest_note" DROP COLUMN is_quote`); + await queryRunner.query(`ALTER TABLE "latest_note" DROP COLUMN is_reply`); + await queryRunner.query(`ALTER TABLE "latest_note" DROP COLUMN is_public`); + await queryRunner.query(`ALTER TABLE "latest_note" ADD CONSTRAINT "PK_f619b62bfaafabe68f52fb50c9a" PRIMARY KEY ("user_id")`); + } +} diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index a192c2f270..c083068392 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -43,6 +43,7 @@ import { ModerationLogService } from './ModerationLogService.js'; import { NoteCreateService } from './NoteCreateService.js'; import { NoteEditService } from './NoteEditService.js'; import { NoteDeleteService } from './NoteDeleteService.js'; +import { LatestNoteService } from './LatestNoteService.js'; import { NotePiningService } from './NotePiningService.js'; import { NoteReadService } from './NoteReadService.js'; import { NotificationService } from './NotificationService.js'; @@ -187,6 +188,7 @@ const $ModerationLogService: Provider = { provide: 'ModerationLogService', useEx const $NoteCreateService: Provider = { provide: 'NoteCreateService', useExisting: NoteCreateService }; const $NoteEditService: Provider = { provide: 'NoteEditService', useExisting: NoteEditService }; const $NoteDeleteService: Provider = { provide: 'NoteDeleteService', useExisting: NoteDeleteService }; +const $LatestNoteService: Provider = { provide: 'LatestNoteService', useExisting: LatestNoteService }; const $NotePiningService: Provider = { provide: 'NotePiningService', useExisting: NotePiningService }; const $NoteReadService: Provider = { provide: 'NoteReadService', useExisting: NoteReadService }; const $NotificationService: Provider = { provide: 'NotificationService', useExisting: NotificationService }; @@ -339,6 +341,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp NoteCreateService, NoteEditService, NoteDeleteService, + LatestNoteService, NotePiningService, NoteReadService, NotificationService, @@ -487,6 +490,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp $NoteCreateService, $NoteEditService, $NoteDeleteService, + $LatestNoteService, $NotePiningService, $NoteReadService, $NotificationService, @@ -636,6 +640,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp NoteCreateService, NoteEditService, NoteDeleteService, + LatestNoteService, NotePiningService, NoteReadService, NotificationService, @@ -783,6 +788,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp $NoteCreateService, $NoteEditService, $NoteDeleteService, + $LatestNoteService, $NotePiningService, $NoteReadService, $NotificationService, diff --git a/packages/backend/src/core/LatestNoteService.ts b/packages/backend/src/core/LatestNoteService.ts new file mode 100644 index 0000000000..c379805506 --- /dev/null +++ b/packages/backend/src/core/LatestNoteService.ts @@ -0,0 +1,139 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { Not } from 'typeorm'; +import { MiNote } from '@/models/Note.js'; +import { isPureRenote } from '@/misc/is-renote.js'; +import { SkLatestNote } from '@/models/LatestNote.js'; +import { DI } from '@/di-symbols.js'; +import type { LatestNotesRepository, NotesRepository } from '@/models/_.js'; +import { LoggerService } from '@/core/LoggerService.js'; +import Logger from '@/logger.js'; + +@Injectable() +export class LatestNoteService { + private readonly logger: Logger; + + constructor( + @Inject(DI.notesRepository) + private notesRepository: NotesRepository, + + @Inject(DI.latestNotesRepository) + private latestNotesRepository: LatestNotesRepository, + + loggerService: LoggerService, + ) { + this.logger = loggerService.getLogger('LatestNoteService'); + } + + handleUpdatedNoteBG(before: MiNote, after: MiNote): void { + this + .handleUpdatedNote(before, after) + .catch(err => this.logger.error('Unhandled exception while updating latest_note (after update):', err)); + } + + async handleUpdatedNote(before: MiNote, after: MiNote): Promise { + // If the key didn't change, then there's nothing to update + if (SkLatestNote.areEquivalent(before, after)) return; + + // Simulate update as delete + create + await this.handleDeletedNote(before); + await this.handleCreatedNote(after); + } + + handleCreatedNoteBG(note: MiNote): void { + this + .handleCreatedNote(note) + .catch(err => this.logger.error('Unhandled exception while updating latest_note (after create):', err)); + } + + async handleCreatedNote(note: MiNote): Promise { + // Ignore DMs. + // Followers-only posts are *included*, as this table is used to back the "following" feed. + if (note.visibility === 'specified') return; + + // Ignore pure renotes + if (isPureRenote(note)) return; + + // Compute the compound key of the entry to check + const key = SkLatestNote.keyFor(note); + + // Make sure that this isn't an *older* post. + // We can get older posts through replies, lookups, updates, etc. + const currentLatest = await this.latestNotesRepository.findOneBy(key); + if (currentLatest != null && currentLatest.noteId >= note.id) return; + + // Record this as the latest note for the given user + const latestNote = new SkLatestNote({ + ...key, + noteId: note.id, + }); + await this.latestNotesRepository.upsert(latestNote, ['userId', 'isPublic', 'isReply', 'isQuote']); + } + + handleDeletedNoteBG(note: MiNote): void { + this + .handleDeletedNote(note) + .catch(err => this.logger.error('Unhandled exception while updating latest_note (after delete):', err)); + } + + async handleDeletedNote(note: MiNote): Promise { + // If it's a DM, then it can't possibly be the latest note so we can safely skip this. + if (note.visibility === 'specified') return; + + // If it's a pure renote, then it can't possibly be the latest note so we can safely skip this. + if (isPureRenote(note)) return; + + // Compute the compound key of the entry to check + const key = SkLatestNote.keyFor(note); + + // Check if the deleted note was possibly the latest for the user + const existingLatest = await this.latestNotesRepository.findOneBy(key); + if (existingLatest == null || existingLatest.noteId !== note.id) return; + + // Find the newest remaining note for the user. + // We exclude DMs and pure renotes. + const nextLatest = await this.notesRepository + .createQueryBuilder('note') + .select() + .where({ + userId: key.userId, + visibility: key.isPublic + ? 'public' + : Not('specified'), + replyId: key.isReply + ? Not(null) + : null, + renoteId: key.isQuote + ? Not(null) + : null, + }) + .andWhere(` + ( + note."renoteId" IS NULL + OR note.text IS NOT NULL + OR note.cw IS NOT NULL + OR note."replyId" IS NOT NULL + OR note."hasPoll" + OR note."fileIds" != '{}' + ) + `) + .orderBy({ id: 'DESC' }) + .getOne(); + if (!nextLatest) return; + + // Record it as the latest + const latestNote = new SkLatestNote({ + ...key, + noteId: nextLatest.id, + }); + + // When inserting the latest note, it's possible that another worker has "raced" the insert and already added a newer note. + // We must use orIgnore() to ensure that the query ignores conflicts, otherwise an exception may be thrown. + await this.latestNotesRepository + .createQueryBuilder('latest') + .insert() + .into(SkLatestNote) + .values(latestNote) + .orIgnore() + .execute(); + } +} diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 29f7dd917d..41c1e3f66f 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -14,7 +14,7 @@ import { extractHashtags } from '@/misc/extract-hashtags.js'; import type { IMentionedRemoteUsers } from '@/models/Note.js'; import { MiNote } from '@/models/Note.js'; import { LatestNote } from '@/models/LatestNote.js'; -import type { ChannelFollowingsRepository, ChannelsRepository, FollowingsRepository, InstancesRepository, LatestNotesRepository, MiFollowing, MiMeta, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserListMembershipsRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js'; +import type { ChannelFollowingsRepository, ChannelsRepository, FollowingsRepository, InstancesRepository, MiFollowing, MiMeta, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserListMembershipsRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js'; import type { MiDriveFile } from '@/models/DriveFile.js'; import type { MiApp } from '@/models/App.js'; import { concat } from '@/misc/prelude/array.js'; @@ -58,7 +58,7 @@ import { isReply } from '@/misc/is-reply.js'; import { trackPromise } from '@/misc/promise-tracker.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { IdentifiableError } from '@/misc/identifiable-error.js'; -import { isQuote, isRenote } from '@/misc/is-renote.js'; +import { LatestNoteService } from '@/core/LatestNoteService.js'; import { CollapsedQueue } from '@/misc/collapsed-queue.js'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; @@ -172,9 +172,6 @@ export class NoteCreateService implements OnApplicationShutdown { @Inject(DI.notesRepository) private notesRepository: NotesRepository, - @Inject(DI.latestNotesRepository) - private latestNotesRepository: LatestNotesRepository, - @Inject(DI.mutingsRepository) private mutingsRepository: MutingsRepository, @@ -225,6 +222,7 @@ export class NoteCreateService implements OnApplicationShutdown { private utilityService: UtilityService, private userBlockingService: UserBlockingService, private cacheService: CacheService, + private latestNoteService: LatestNoteService, ) { this.updateNotesCountQueue = new CollapsedQueue(60 * 1000 * 5, this.collapseNotesCount, this.performUpdateNotesCount); } @@ -530,8 +528,6 @@ export class NoteCreateService implements OnApplicationShutdown { await this.notesRepository.insert(insert); } - await this.updateLatestNote(insert); - return insert; } catch (e) { // duplicate key error @@ -812,6 +808,9 @@ export class NoteCreateService implements OnApplicationShutdown { }); } + // Update the Latest Note index / following feed + this.latestNoteService.handleCreatedNoteBG(note); + // Register to search database if (!user.noindex) this.index(note); } @@ -1151,25 +1150,4 @@ export class NoteCreateService implements OnApplicationShutdown { public async onApplicationShutdown(signal?: string | undefined): Promise { await this.dispose(); } - - private async updateLatestNote(note: MiNote) { - // Ignore DMs. - // Followers-only posts are *included*, as this table is used to back the "following" feed. - if (note.visibility === 'specified') return; - - // Ignore pure renotes - if (isRenote(note) && !isQuote(note)) return; - - // Make sure that this isn't an *older* post. - // We can get older posts through replies, lookups, etc. - const currentLatest = await this.latestNotesRepository.findOneBy({ userId: note.userId }); - if (currentLatest != null && currentLatest.noteId >= note.id) return; - - // Record this as the latest note for the given user - const latestNote = new LatestNote({ - userId: note.userId, - noteId: note.id, - }); - await this.latestNotesRepository.upsert(latestNote, ['userId']); - } } diff --git a/packages/backend/src/core/NoteDeleteService.ts b/packages/backend/src/core/NoteDeleteService.ts index 6ea400b03e..285db9f152 100644 --- a/packages/backend/src/core/NoteDeleteService.ts +++ b/packages/backend/src/core/NoteDeleteService.ts @@ -3,12 +3,11 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Brackets, In, Not } from 'typeorm'; +import { Brackets, In } from 'typeorm'; import { Injectable, Inject } from '@nestjs/common'; import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js'; -import type { MiNote, IMentionedRemoteUsers } from '@/models/Note.js'; -import { LatestNote } from '@/models/LatestNote.js'; -import type { InstancesRepository, MiMeta, LatestNotesRepository, NotesRepository, UsersRepository } from '@/models/_.js'; +import { MiNote, IMentionedRemoteUsers } from '@/models/Note.js'; +import type { InstancesRepository, MiMeta, NotesRepository, UsersRepository } from '@/models/_.js'; import { RelayService } from '@/core/RelayService.js'; import { FederatedInstanceService } from '@/core/FederatedInstanceService.js'; import { DI } from '@/di-symbols.js'; @@ -24,6 +23,7 @@ import { bindThis } from '@/decorators.js'; import { SearchService } from '@/core/SearchService.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; import { isQuote, isRenote } from '@/misc/is-renote.js'; +import { LatestNoteService } from '@/core/LatestNoteService.js'; @Injectable() export class NoteDeleteService { @@ -40,9 +40,6 @@ export class NoteDeleteService { @Inject(DI.notesRepository) private notesRepository: NotesRepository, - @Inject(DI.latestNotesRepository) - private latestNotesRepository: LatestNotesRepository, - @Inject(DI.instancesRepository) private instancesRepository: InstancesRepository, @@ -57,6 +54,7 @@ export class NoteDeleteService { private notesChart: NotesChart, private perUserNotesChart: PerUserNotesChart, private instanceChart: InstanceChart, + private latestNoteService: LatestNoteService, ) {} /** @@ -149,7 +147,7 @@ export class NoteDeleteService { userId: user.id, }); - await this.updateLatestNote(note); + this.latestNoteService.handleDeletedNoteBG(note); if (deleter && (note.userId !== deleter.id)) { const user = await this.usersRepository.findOneByOrFail({ id: note.userId }); @@ -232,52 +230,4 @@ export class NoteDeleteService { this.apDeliverManagerService.deliverToUser(user, content, remoteUser); } } - - private async updateLatestNote(note: MiNote) { - // If it's a DM, then it can't possibly be the latest note so we can safely skip this. - if (note.visibility === 'specified') return; - - // Check if the deleted note was possibly the latest for the user - const hasLatestNote = await this.latestNotesRepository.existsBy({ userId: note.userId }); - if (hasLatestNote) return; - - // Find the newest remaining note for the user. - // We exclude DMs and pure renotes. - const nextLatest = await this.notesRepository - .createQueryBuilder('note') - .select() - .where({ - userId: note.userId, - visibility: Not('specified'), - }) - .andWhere(` - ( - note."renoteId" IS NULL - OR note.text IS NOT NULL - OR note.cw IS NOT NULL - OR note."replyId" IS NOT NULL - OR note."hasPoll" - OR note."fileIds" != '{}' - ) - `) - .orderBy({ id: 'DESC' }) - .getOne(); - if (!nextLatest) return; - - // Record it as the latest - const latestNote = new LatestNote({ - userId: note.userId, - noteId: nextLatest.id, - }); - - // When inserting the latest note, it's possible that another worker has "raced" the insert and already added a newer note. - // We must use orIgnore() to ensure that the query ignores conflicts, otherwise an exception may be thrown. - await this.latestNotesRepository - .createQueryBuilder('latest') - .insert() - .into(LatestNote) - .values(latestNote) - .orIgnore() - .execute(); - } } diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index 9bccb6c756..4c2b88f8dc 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -50,6 +50,7 @@ import { isReply } from '@/misc/is-reply.js'; import { trackPromise } from '@/misc/promise-tracker.js'; import { isUserRelated } from '@/misc/is-user-related.js'; import { IdentifiableError } from '@/misc/identifiable-error.js'; +import { LatestNoteService } from '@/core/LatestNoteService.js'; import { CollapsedQueue } from '@/misc/collapsed-queue.js'; import { NoteCreateService } from '@/core/NoteCreateService.js'; @@ -217,7 +218,7 @@ export class NoteEditService implements OnApplicationShutdown { private utilityService: UtilityService, private userBlockingService: UserBlockingService, private cacheService: CacheService, - private noteCreateService: NoteCreateService, + private latestNoteService: LatestNoteService, ) { this.updateNotesCountQueue = new CollapsedQueue(60 * 1000 * 5, this.collapseNotesCount, this.performUpdateNotesCount); } @@ -563,7 +564,7 @@ export class NoteEditService implements OnApplicationShutdown { } setImmediate('post edited', { signal: this.#shutdownController.signal }).then( - () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!), + () => this.postNoteEdited(note, oldnote, user, data, silent, tags!, mentionedUsers!), () => { /* aborted, ignore this */ }, ); @@ -574,7 +575,7 @@ export class NoteEditService implements OnApplicationShutdown { } @bindThis - private async postNoteEdited(note: MiNote, user: { + private async postNoteEdited(note: MiNote, oldNote: MiNote, user: { id: MiUser['id']; username: MiUser['username']; host: MiUser['host']; @@ -771,6 +772,9 @@ export class NoteEditService implements OnApplicationShutdown { }); } + // Update the Latest Note index / following feed + this.latestNoteService.handleUpdatedNoteBG(oldNote, note); + // Register to search database if (!user.noindex) this.index(note); } diff --git a/packages/backend/src/core/activitypub/ApDbResolverService.ts b/packages/backend/src/core/activitypub/ApDbResolverService.ts index 062af39732..2cb558dbff 100644 --- a/packages/backend/src/core/activitypub/ApDbResolverService.ts +++ b/packages/backend/src/core/activitypub/ApDbResolverService.ts @@ -59,7 +59,7 @@ export class ApDbResolverService implements OnApplicationShutdown { } @bindThis - public parseUri(value: string | IObject): UriParseResult { + public parseUri(value: string | IObject | [string | IObject]): UriParseResult { const separator = '/'; const uri = new URL(getApId(value)); @@ -78,7 +78,7 @@ export class ApDbResolverService implements OnApplicationShutdown { * AP Note => Misskey Note in DB */ @bindThis - public async getNoteFromApId(value: string | IObject): Promise { + public async getNoteFromApId(value: string | IObject | [string | IObject]): Promise { const parsed = this.parseUri(value); if (parsed.local) { @@ -98,7 +98,7 @@ export class ApDbResolverService implements OnApplicationShutdown { * AP Person => Misskey User in DB */ @bindThis - public async getUserFromApId(value: string | IObject): Promise { + public async getUserFromApId(value: string | IObject | [string | IObject]): Promise { const parsed = this.parseUri(value); if (parsed.local) { diff --git a/packages/backend/src/core/activitypub/ApInboxService.ts b/packages/backend/src/core/activitypub/ApInboxService.ts index 9f300e9905..d54c9544c3 100644 --- a/packages/backend/src/core/activitypub/ApInboxService.ts +++ b/packages/backend/src/core/activitypub/ApInboxService.ts @@ -40,6 +40,7 @@ import { ApPersonService } from './models/ApPersonService.js'; import { ApQuestionService } from './models/ApQuestionService.js'; import type { Resolver } from './ApResolverService.js'; import type { IAccept, IAdd, IAnnounce, IBlock, ICreate, IDelete, IFlag, IFollow, ILike, IObject, IReject, IRemove, IUndo, IUpdate, IMove, IPost } from './type.js'; +import { fromTuple } from '@/misc/from-tuple.js'; @Injectable() export class ApInboxService { @@ -254,7 +255,8 @@ export class ApInboxService { } if (activity.target === actor.featured) { - const note = await this.apNoteService.resolveNote(activity.object); + const object = fromTuple(activity.object); + const note = await this.apNoteService.resolveNote(object); if (note == null) return 'note not found'; await this.notePiningService.addPinned(actor, note.id); return; @@ -271,11 +273,12 @@ export class ApInboxService { const resolver = this.apResolverService.createResolver(); - if (!activity.object) return 'skip: activity has no object property'; - const targetUri = getApId(activity.object); + const activityObject = fromTuple(activity.object); + if (!activityObject) return 'skip: activity has no object property'; + const targetUri = getApId(activityObject); if (targetUri.startsWith('bear:')) return 'skip: bearcaps url not supported.'; - const target = await resolver.resolve(activity.object).catch(e => { + const target = await resolver.resolve(activityObject).catch(e => { this.logger.error(`Resolution failed: ${e}`); return e; }); @@ -370,29 +373,30 @@ export class ApInboxService { this.logger.info(`Create: ${uri}`); - if (!activity.object) return 'skip: activity has no object property'; - const targetUri = getApId(activity.object); + const activityObject = fromTuple(activity.object); + if (!activityObject) return 'skip: activity has no object property'; + const targetUri = getApId(activityObject); if (targetUri.startsWith('bear:')) return 'skip: bearcaps url not supported.'; // copy audiences between activity <=> object. - if (typeof activity.object === 'object') { - const to = unique(concat([toArray(activity.to), toArray(activity.object.to)])); - const cc = unique(concat([toArray(activity.cc), toArray(activity.object.cc)])); + if (typeof activityObject === 'object') { + const to = unique(concat([toArray(activity.to), toArray(activityObject.to)])); + const cc = unique(concat([toArray(activity.cc), toArray(activityObject.cc)])); activity.to = to; activity.cc = cc; - activity.object.to = to; - activity.object.cc = cc; + activityObject.to = to; + activityObject.cc = cc; } // If there is no attributedTo, use Activity actor. - if (typeof activity.object === 'object' && !activity.object.attributedTo) { - activity.object.attributedTo = activity.actor; + if (typeof activityObject === 'object' && !activityObject.attributedTo) { + activityObject.attributedTo = activity.actor; } const resolver = this.apResolverService.createResolver(); - const object = await resolver.resolve(activity.object).catch(e => { + const object = await resolver.resolve(activityObject).catch(e => { this.logger.error(`Resolution failed: ${e}`); throw e; }); @@ -448,15 +452,15 @@ export class ApInboxService { // 削除対象objectのtype let formerType: string | undefined; - if (typeof activity.object === 'string') { + const activityObject = fromTuple(activity.object); + if (typeof activityObject === 'string') { // typeが不明だけど、どうせ消えてるのでremote resolveしない formerType = undefined; } else { - const object = activity.object; - if (isTombstone(object)) { - formerType = toSingle(object.formerType); + if (isTombstone(activityObject)) { + formerType = toSingle(activityObject.formerType); } else { - formerType = toSingle(object.type); + formerType = toSingle(activityObject.type); } } @@ -616,7 +620,8 @@ export class ApInboxService { } if (activity.target === actor.featured) { - const note = await this.apNoteService.resolveNote(activity.object); + const activityObject = fromTuple(activity.object); + const note = await this.apNoteService.resolveNote(activityObject); if (note == null) return 'note not found'; await this.notePiningService.removePinned(actor, note.id); return; diff --git a/packages/backend/src/core/activitypub/ApRendererService.ts b/packages/backend/src/core/activitypub/ApRendererService.ts index 107dfaa630..9e4ccc7019 100644 --- a/packages/backend/src/core/activitypub/ApRendererService.ts +++ b/packages/backend/src/core/activitypub/ApRendererService.ts @@ -200,7 +200,8 @@ export class ApRendererService { type: 'Flag', actor: this.userEntityService.genLocalUserUri(user.id), content, - object, + // This MUST be an array for Pleroma compatibility: https://activitypub.software/TransFem-org/Sharkey/-/issues/641#note_7301 + object: [object], }; } diff --git a/packages/backend/src/core/activitypub/ApResolverService.ts b/packages/backend/src/core/activitypub/ApResolverService.ts index f9411a1283..5d5c61ce2c 100644 --- a/packages/backend/src/core/activitypub/ApResolverService.ts +++ b/packages/backend/src/core/activitypub/ApResolverService.ts @@ -20,6 +20,7 @@ import { ApDbResolverService } from './ApDbResolverService.js'; import { ApRendererService } from './ApRendererService.js'; import { ApRequestService } from './ApRequestService.js'; import type { IObject, ICollection, IOrderedCollection } from './type.js'; +import { fromTuple } from '@/misc/from-tuple.js'; export class Resolver { private history: Set; @@ -66,7 +67,10 @@ export class Resolver { } @bindThis - public async resolve(value: string | IObject): Promise { + public async resolve(value: string | IObject | [string | IObject]): Promise { + // eslint-disable-next-line no-param-reassign + value = fromTuple(value); + if (typeof value !== 'string') { return value; } diff --git a/packages/backend/src/core/activitypub/type.ts b/packages/backend/src/core/activitypub/type.ts index bb9836fb4e..af5aba9c16 100644 --- a/packages/backend/src/core/activitypub/type.ts +++ b/packages/backend/src/core/activitypub/type.ts @@ -3,6 +3,8 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +import { fromTuple } from '@/misc/from-tuple.js'; + export type Obj = { [x: string]: any }; export type ApObject = IObject | string | (IObject | string)[]; @@ -53,10 +55,13 @@ export function getOneApId(value: ApObject): string { /** * Get ActivityStreams Object id */ -export function getApId(value: string | IObject): string { +export function getApId(value: string | IObject | [string | IObject]): string { + // eslint-disable-next-line no-param-reassign + value = fromTuple(value); + if (typeof value === 'string') return value; if (typeof value.id === 'string') return value.id; - throw new Error('cannot detemine id'); + throw new Error('cannot determine id'); } /** @@ -85,7 +90,9 @@ export function getApHrefNullable(value: string | IObject | undefined): string | export interface IActivity extends IObject { //type: 'Activity'; actor: IObject | string; - object: IObject | string; + // ActivityPub spec allows for arrays: https://www.w3.org/TR/activitystreams-vocabulary/#properties + // Misskey can only handle one value, so we use a tuple for that case. + object: IObject | string | [IObject | string] ; target?: IObject | string; /** LD-Signature */ signature?: { diff --git a/packages/backend/src/core/entities/UserEntityService.ts b/packages/backend/src/core/entities/UserEntityService.ts index bb05e8712f..703b07973e 100644 --- a/packages/backend/src/core/entities/UserEntityService.ts +++ b/packages/backend/src/core/entities/UserEntityService.ts @@ -374,6 +374,13 @@ export class UserEntityService implements OnModuleInit { return count > 0; } + @bindThis + public async getHasPendingSentFollowRequest(userId: MiUser['id']): Promise { + return this.followRequestsRepository.existsBy({ + followerId: userId, + }); + } + @bindThis public getOnlineStatus(user: MiUser): 'unknown' | 'online' | 'active' | 'offline' { if (user.hideOnlineStatus) return 'unknown'; @@ -643,6 +650,7 @@ export class UserEntityService implements OnModuleInit { hasUnreadChannel: false, // 後方互換性のため hasUnreadNotification: notificationsInfo?.hasUnread, // 後方互換性のため hasPendingReceivedFollowRequest: this.getHasPendingReceivedFollowRequest(user.id), + hasPendingSentFollowRequest: this.getHasPendingSentFollowRequest(user.id), unreadNotificationsCount: notificationsInfo?.unreadCount, mutedWords: profile!.mutedWords, hardMutedWords: profile!.hardMutedWords, diff --git a/packages/backend/src/misc/from-tuple.ts b/packages/backend/src/misc/from-tuple.ts new file mode 100644 index 0000000000..366b1e310f --- /dev/null +++ b/packages/backend/src/misc/from-tuple.ts @@ -0,0 +1,7 @@ +export function fromTuple(value: T | [T]): T { + if (Array.isArray(value)) { + return value[0]; + } + + return value; +} diff --git a/packages/backend/src/misc/is-renote.ts b/packages/backend/src/misc/is-renote.ts index 48f821806c..c128fded14 100644 --- a/packages/backend/src/misc/is-renote.ts +++ b/packages/backend/src/misc/is-renote.ts @@ -23,6 +23,17 @@ type Quote = hasPoll: true }); +type PureRenote = + Renote & { + text: null, + cw: null, + replyId: null, + hasPoll: false, + fileIds: { + length: 0, + }, + }; + export function isRenote(note: MiNote): note is Renote { return note.renoteId != null; } @@ -36,6 +47,10 @@ export function isQuote(note: Renote): note is Quote { note.fileIds.length > 0; } +export function isPureRenote(note: MiNote): note is PureRenote { + return isRenote(note) && !isQuote(note); +} + type PackedRenote = Packed<'Note'> & { renoteId: NonNullable['renoteId']> diff --git a/packages/backend/src/models/LatestNote.ts b/packages/backend/src/models/LatestNote.ts index 1163ff3bc0..064fcccc0a 100644 --- a/packages/backend/src/models/LatestNote.ts +++ b/packages/backend/src/models/LatestNote.ts @@ -6,6 +6,7 @@ import { PrimaryColumn, Entity, JoinColumn, Column, ManyToOne } from 'typeorm'; import { MiUser } from '@/models/User.js'; import { MiNote } from '@/models/Note.js'; +import { isQuote, isRenote } from '@/misc/is-renote.js'; /** * Maps a user to the most recent post by that user. @@ -13,7 +14,7 @@ import { MiNote } from '@/models/Note.js'; * DMs are not counted. */ @Entity('latest_note') -export class LatestNote { +export class SkLatestNote { @PrimaryColumn({ name: 'user_id', type: 'varchar' as const, @@ -21,6 +22,24 @@ export class LatestNote { }) public userId: string; + @PrimaryColumn('boolean', { + name: 'is_public', + default: false, + }) + public isPublic: boolean; + + @PrimaryColumn('boolean', { + name: 'is_reply', + default: false, + }) + public isReply: boolean; + + @PrimaryColumn('boolean', { + name: 'is_quote', + default: false, + }) + public isQuote: boolean; + @ManyToOne(() => MiUser, { onDelete: 'CASCADE', }) @@ -44,11 +63,38 @@ export class LatestNote { }) public note: MiNote | null; - constructor(data?: Partial) { + constructor(data?: Partial) { if (!data) return; for (const [k, v] of Object.entries(data)) { (this as Record)[k] = v; } } + + /** + * Generates a compound key matching a provided note. + */ + static keyFor(note: MiNote) { + return { + userId: note.userId, + isPublic: note.visibility === 'public', + isReply: note.replyId != null, + isQuote: isRenote(note) && isQuote(note), + }; + } + + /** + * Checks if two notes would produce equivalent compound keys. + */ + static areEquivalent(first: MiNote, second: MiNote): boolean { + const firstKey = SkLatestNote.keyFor(first); + const secondKey = SkLatestNote.keyFor(second); + + return ( + firstKey.userId === secondKey.userId && + firstKey.isPublic === secondKey.isPublic && + firstKey.isReply === secondKey.isReply && + firstKey.isQuote === secondKey.isQuote + ); + } } diff --git a/packages/backend/src/models/RepositoryModule.ts b/packages/backend/src/models/RepositoryModule.ts index f44334d84e..eb45b9a631 100644 --- a/packages/backend/src/models/RepositoryModule.ts +++ b/packages/backend/src/models/RepositoryModule.ts @@ -7,7 +7,7 @@ import type { Provider } from '@nestjs/common'; import { Module } from '@nestjs/common'; import { DI } from '@/di-symbols.js'; import { - LatestNote, + SkLatestNote, MiAbuseReportNotificationRecipient, MiAbuseUserReport, MiAccessToken, @@ -121,7 +121,7 @@ const $avatarDecorationsRepository: Provider = { const $latestNotesRepository: Provider = { provide: DI.latestNotesRepository, - useFactory: (db: DataSource) => db.getRepository(LatestNote).extend(miRepository as MiRepository), + useFactory: (db: DataSource) => db.getRepository(SkLatestNote).extend(miRepository as MiRepository), inject: [DI.db], }; diff --git a/packages/backend/src/models/_.ts b/packages/backend/src/models/_.ts index 9e01f4b6d7..ac2dd62aa2 100644 --- a/packages/backend/src/models/_.ts +++ b/packages/backend/src/models/_.ts @@ -10,7 +10,7 @@ import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLo import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js'; import { ObjectUtils } from 'typeorm/util/ObjectUtils.js'; import { OrmUtils } from 'typeorm/util/OrmUtils.js'; -import { LatestNote } from '@/models/LatestNote.js'; +import { SkLatestNote } from '@/models/LatestNote.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; import { MiAbuseReportNotificationRecipient } from '@/models/AbuseReportNotificationRecipient.js'; import { MiAccessToken } from '@/models/AccessToken.js'; @@ -127,7 +127,7 @@ export const miRepository = { } satisfies MiRepository; export { - LatestNote, + SkLatestNote, MiAbuseUserReport, MiAbuseReportNotificationRecipient, MiAccessToken, @@ -226,7 +226,7 @@ export type GalleryPostsRepository = Repository & MiRepository & MiRepository; export type InstancesRepository = Repository & MiRepository; export type MetasRepository = Repository & MiRepository; -export type LatestNotesRepository = Repository & MiRepository; +export type LatestNotesRepository = Repository & MiRepository; export type ModerationLogsRepository = Repository & MiRepository; export type MutingsRepository = Repository & MiRepository; export type RenoteMutingsRepository = Repository & MiRepository; diff --git a/packages/backend/src/models/json-schema/user.ts b/packages/backend/src/models/json-schema/user.ts index 9ed9ef828e..beb4dcc12c 100644 --- a/packages/backend/src/models/json-schema/user.ts +++ b/packages/backend/src/models/json-schema/user.ts @@ -590,6 +590,10 @@ export const packedMeDetailedOnlySchema = { type: 'boolean', nullable: false, optional: false, }, + hasPendingSentFollowRequest: { + type: 'boolean', + nullable: false, optional: false, + }, unreadNotificationsCount: { type: 'number', nullable: false, optional: false, diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index 0d17b3d046..2d66e6e445 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -83,7 +83,7 @@ import { MiReversiGame } from '@/models/ReversiGame.js'; import { Config } from '@/config.js'; import MisskeyLogger from '@/logger.js'; import { bindThis } from '@/decorators.js'; -import { LatestNote } from '@/models/LatestNote.js'; +import { SkLatestNote } from '@/models/LatestNote.js'; pg.types.setTypeParser(20, Number); @@ -131,7 +131,7 @@ class MyCustomLogger implements Logger { } export const entities = [ - LatestNote, + SkLatestNote, MiAnnouncement, MiAnnouncementRead, MiMeta, diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index d1df57190a..6d70f35883 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -190,6 +190,7 @@ import * as ep___following_invalidate from './endpoints/following/invalidate.js' import * as ep___following_requests_accept from './endpoints/following/requests/accept.js'; import * as ep___following_requests_cancel from './endpoints/following/requests/cancel.js'; import * as ep___following_requests_list from './endpoints/following/requests/list.js'; +import * as ep___following_requests_sent from './endpoints/following/requests/sent.js'; import * as ep___following_requests_reject from './endpoints/following/requests/reject.js'; import * as ep___gallery_featured from './endpoints/gallery/featured.js'; import * as ep___gallery_popular from './endpoints/gallery/popular.js'; @@ -589,6 +590,7 @@ const $following_invalidate: Provider = { provide: 'ep:following/invalidate', us const $following_requests_accept: Provider = { provide: 'ep:following/requests/accept', useClass: ep___following_requests_accept.default }; const $following_requests_cancel: Provider = { provide: 'ep:following/requests/cancel', useClass: ep___following_requests_cancel.default }; const $following_requests_list: Provider = { provide: 'ep:following/requests/list', useClass: ep___following_requests_list.default }; +const $following_requests_sent: Provider = { provide: 'ep:following/requests/sent', useClass: ep___following_requests_sent.default }; const $following_requests_reject: Provider = { provide: 'ep:following/requests/reject', useClass: ep___following_requests_reject.default }; const $gallery_featured: Provider = { provide: 'ep:gallery/featured', useClass: ep___gallery_featured.default }; const $gallery_popular: Provider = { provide: 'ep:gallery/popular', useClass: ep___gallery_popular.default }; @@ -992,6 +994,7 @@ const $reversi_verify: Provider = { provide: 'ep:reversi/verify', useClass: ep__ $following_requests_accept, $following_requests_cancel, $following_requests_list, + $following_requests_sent, $following_requests_reject, $gallery_featured, $gallery_popular, diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 3987464fcf..75a72eb063 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -196,6 +196,7 @@ import * as ep___following_invalidate from './endpoints/following/invalidate.js' import * as ep___following_requests_accept from './endpoints/following/requests/accept.js'; import * as ep___following_requests_cancel from './endpoints/following/requests/cancel.js'; import * as ep___following_requests_list from './endpoints/following/requests/list.js'; +import * as ep___following_requests_sent from './endpoints/following/requests/sent.js'; import * as ep___following_requests_reject from './endpoints/following/requests/reject.js'; import * as ep___gallery_featured from './endpoints/gallery/featured.js'; import * as ep___gallery_popular from './endpoints/gallery/popular.js'; @@ -593,6 +594,7 @@ const eps = [ ['following/requests/accept', ep___following_requests_accept], ['following/requests/cancel', ep___following_requests_cancel], ['following/requests/list', ep___following_requests_list], + ['following/requests/sent', ep___following_requests_sent], ['following/requests/reject', ep___following_requests_reject], ['gallery/featured', ep___gallery_featured], ['gallery/popular', ep___gallery_popular], diff --git a/packages/backend/src/server/api/endpoints/following/requests/sent.ts b/packages/backend/src/server/api/endpoints/following/requests/sent.ts new file mode 100644 index 0000000000..6325f01bb8 --- /dev/null +++ b/packages/backend/src/server/api/endpoints/following/requests/sent.ts @@ -0,0 +1,77 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { Inject, Injectable } from '@nestjs/common'; +import { Endpoint } from '@/server/api/endpoint-base.js'; +import { QueryService } from '@/core/QueryService.js'; +import type { FollowRequestsRepository } from '@/models/_.js'; +import { FollowRequestEntityService } from '@/core/entities/FollowRequestEntityService.js'; +import { DI } from '@/di-symbols.js'; + +export const meta = { + tags: ['following', 'account'], + + requireCredential: true, + + kind: 'read:following', + + res: { + type: 'array', + optional: false, nullable: false, + items: { + type: 'object', + optional: false, nullable: false, + properties: { + id: { + type: 'string', + optional: false, nullable: false, + format: 'id', + }, + follower: { + type: 'object', + optional: false, nullable: false, + ref: 'UserLite', + }, + followee: { + type: 'object', + optional: false, nullable: false, + ref: 'UserLite', + }, + }, + }, + }, +} as const; + +export const paramDef = { + type: 'object', + properties: { + sinceId: { type: 'string', format: 'misskey:id' }, + untilId: { type: 'string', format: 'misskey:id' }, + limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 }, + }, + required: [], +} as const; + +@Injectable() +export default class extends Endpoint { // eslint-disable-line import/no-default-export + constructor( + @Inject(DI.followRequestsRepository) + private followRequestsRepository: FollowRequestsRepository, + + private followRequestEntityService: FollowRequestEntityService, + private queryService: QueryService, + ) { + super(meta, paramDef, async (ps, me) => { + const query = this.queryService.makePaginationQuery(this.followRequestsRepository.createQueryBuilder('request'), ps.sinceId, ps.untilId) + .andWhere('request.followerId = :meId', { meId: me.id }); + + const requests = await query + .limit(ps.limit) + .getMany(); + + return await this.followRequestEntityService.packMany(requests, me); + }); + } +} diff --git a/packages/backend/src/server/api/endpoints/notes/following.ts b/packages/backend/src/server/api/endpoints/notes/following.ts index 436160f250..83e8f404e9 100644 --- a/packages/backend/src/server/api/endpoints/notes/following.ts +++ b/packages/backend/src/server/api/endpoints/notes/following.ts @@ -4,7 +4,7 @@ */ import { Inject, Injectable } from '@nestjs/common'; -import { LatestNote, MiFollowing } from '@/models/_.js'; +import { SkLatestNote, MiFollowing } from '@/models/_.js'; import type { NotesRepository } from '@/models/_.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; @@ -33,6 +33,12 @@ export const paramDef = { type: 'object', properties: { mutualsOnly: { type: 'boolean', default: false }, + filesOnly: { type: 'boolean', default: false }, + includeNonPublic: { type: 'boolean', default: false }, + includeReplies: { type: 'boolean', default: false }, + includeQuotes: { type: 'boolean', default: false }, + includeBots: { type: 'boolean', default: true }, + limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 }, sinceId: { type: 'string', format: 'misskey:id' }, untilId: { type: 'string', format: 'misskey:id' }, @@ -52,12 +58,12 @@ export default class extends Endpoint { // eslint- private queryService: QueryService, ) { super(meta, paramDef, async (ps, me) => { - let query = this.notesRepository + const query = this.notesRepository .createQueryBuilder('note') .setParameter('me', me.id) // Limit to latest notes - .innerJoin(LatestNote, 'latest', 'note.id = latest.note_id') + .innerJoin(SkLatestNote, 'latest', 'note.id = latest.note_id') // Avoid N+1 queries from the "pack" method .innerJoinAndSelect('note.user', 'user') @@ -73,8 +79,28 @@ export default class extends Endpoint { // eslint- // Limit to mutuals, if requested if (ps.mutualsOnly) { - query = query - .innerJoin(MiFollowing, 'mutuals', 'latest.user_id = mutuals."followerId" AND mutuals."followeeId" = :me'); + query.innerJoin(MiFollowing, 'mutuals', 'latest.user_id = mutuals."followerId" AND mutuals."followeeId" = :me'); + } + + // Limit to files, if requested + if (ps.filesOnly) { + query.andWhere('note."fileIds" != \'{}\''); + } + + // Match selected note types. + if (!ps.includeNonPublic) { + query.andWhere('latest.is_public'); + } + if (!ps.includeReplies) { + query.andWhere('latest.is_reply = false'); + } + if (!ps.includeQuotes) { + query.andWhere('latest.is_quote = false'); + } + + // Match selected user types. + if (!ps.includeBots) { + query.andWhere('"user"."isBot" = false'); } // Respect blocks and mutes @@ -82,7 +108,7 @@ export default class extends Endpoint { // eslint- this.queryService.generateMutedUserQuery(query, me); // Support pagination - query = this.queryService + this.queryService .makePaginationQuery(query, ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate) .orderBy('note.id', 'DESC') .take(ps.limit); diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts index 7fc11ba369..263d062961 100644 --- a/packages/backend/src/server/api/endpoints/users/notes.ts +++ b/packages/backend/src/server/api/endpoints/users/notes.ts @@ -16,6 +16,7 @@ import { MiLocalUser } from '@/models/User.js'; import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js'; import { FanoutTimelineName } from '@/core/FanoutTimelineService.js'; import { ApiError } from '@/server/api/error.js'; +import { isQuote, isRenote } from '@/misc/is-renote.js'; export const meta = { tags: ['users', 'notes'], @@ -50,7 +51,11 @@ export const paramDef = { properties: { userId: { type: 'string', format: 'misskey:id' }, withReplies: { type: 'boolean', default: false }, + withRepliesToSelf: { type: 'boolean', default: true }, + withQuotes: { type: 'boolean', default: true }, withRenotes: { type: 'boolean', default: true }, + withBots: { type: 'boolean', default: true }, + withNonPublic: { type: 'boolean', default: true }, withChannelNotes: { type: 'boolean', default: false }, limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 }, sinceId: { type: 'string', format: 'misskey:id' }, @@ -102,6 +107,11 @@ export default class extends Endpoint { // eslint- withChannelNotes: ps.withChannelNotes, withFiles: ps.withFiles, withRenotes: ps.withRenotes, + withQuotes: ps.withQuotes, + withBots: ps.withBots, + withNonPublic: ps.withNonPublic, + withRepliesToOthers: ps.withReplies, + withRepliesToSelf: ps.withRepliesToSelf, }, me); return await this.noteEntityService.packMany(timeline, me); @@ -126,11 +136,17 @@ export default class extends Endpoint { // eslint- excludeReplies: ps.withChannelNotes && !ps.withReplies, // userTimelineWithChannel may include replies excludeNoFiles: ps.withChannelNotes && ps.withFiles, // userTimelineWithChannel may include notes without files excludePureRenotes: !ps.withRenotes, + excludeBots: !ps.withBots, noteFilter: note => { if (note.channel?.isSensitive && !isSelf) return false; if (note.visibility === 'specified' && (!me || (me.id !== note.userId && !note.visibleUserIds.some(v => v === me.id)))) return false; if (note.visibility === 'followers' && !isFollowing && !isSelf) return false; + // These are handled by DB fallback, but we duplicate them here in case a timeline was already populated with notes + if (!ps.withRepliesToSelf && note.reply?.userId === note.userId) return false; + if (!ps.withQuotes && isRenote(note) && isQuote(note)) return false; + if (!ps.withNonPublic && note.visibility !== 'public') return false; + return true; }, dbFallback: async (untilId, sinceId, limit) => await this.getFromDb({ @@ -141,6 +157,11 @@ export default class extends Endpoint { // eslint- withChannelNotes: ps.withChannelNotes, withFiles: ps.withFiles, withRenotes: ps.withRenotes, + withQuotes: ps.withQuotes, + withBots: ps.withBots, + withNonPublic: ps.withNonPublic, + withRepliesToOthers: ps.withReplies, + withRepliesToSelf: ps.withRepliesToSelf, }, me), }); @@ -156,6 +177,11 @@ export default class extends Endpoint { // eslint- withChannelNotes: boolean, withFiles: boolean, withRenotes: boolean, + withQuotes: boolean, + withBots: boolean, + withNonPublic: boolean, + withRepliesToOthers: boolean, + withRepliesToSelf: boolean, }, me: MiLocalUser | null) { const isSelf = me && (me.id === ps.userId); @@ -187,7 +213,9 @@ export default class extends Endpoint { // eslint- query.andWhere('note.fileIds != \'{}\''); } - if (ps.withRenotes === false) { + if (!ps.withRenotes && !ps.withQuotes) { + query.andWhere('note.renoteId IS NULL'); + } else if (!ps.withRenotes) { query.andWhere(new Brackets(qb => { qb.orWhere('note.userId != :userId', { userId: ps.userId }); qb.orWhere('note.renoteId IS NULL'); @@ -195,6 +223,35 @@ export default class extends Endpoint { // eslint- qb.orWhere('note.fileIds != \'{}\''); qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)'); })); + } else if (!ps.withQuotes) { + query.andWhere(` + ( + note."renoteId" IS NULL + OR ( + note.text IS NULL + AND note.cw IS NULL + AND note."replyId" IS NULL + AND note."hasPoll" IS FALSE + AND note."fileIds" = '{}' + ) + ) + `); + } + + if (!ps.withRepliesToOthers && !ps.withRepliesToSelf) { + query.andWhere('reply.id IS NULL'); + } else if (!ps.withRepliesToOthers) { + query.andWhere('(reply.id IS NULL OR reply."userId" = note."userId")'); + } else if (!ps.withRepliesToSelf) { + query.andWhere('(reply.id IS NULL OR reply."userId" != note."userId")'); + } + + if (!ps.withNonPublic) { + query.andWhere('note.visibility = \'public\''); + } + + if (!ps.withBots) { + query.andWhere('"user"."isBot" = false'); } return await query.limit(ps.limit).getMany(); diff --git a/packages/backend/src/server/web/boot.js b/packages/backend/src/server/web/boot.js index 5bef0f1407..9063358eaa 100644 --- a/packages/backend/src/server/web/boot.js +++ b/packages/backend/src/server/web/boot.js @@ -22,8 +22,17 @@ return; } + // Force update when locales change + const langsVersion = LANGS_VERSION; + const localeVersion = localStorage.getItem('localeVersion'); + if (localeVersion !== langsVersion) { + console.info(`Updating locales from version ${localeVersion ?? 'N/A'} to ${langsVersion}`); + localStorage.removeItem('localeVersion'); + localStorage.removeItem('locale'); + } + //#region Detect language & fetch translations - if (!localStorage.hasOwnProperty('locale')) { + if (!localStorage.getItem('locale')) { const supportedLangs = LANGS; let lang = localStorage.getItem('lang'); if (lang == null || !supportedLangs.includes(lang)) { @@ -37,37 +46,17 @@ } } - const metaRes = await window.fetch('/api/meta', { - method: 'POST', - body: JSON.stringify({}), - credentials: 'omit', - cache: 'no-cache', - headers: { - 'Content-Type': 'application/json', - }, - }); - if (metaRes.status !== 200) { - renderError('META_FETCH'); - return; - } - const meta = await metaRes.json(); - const v = meta.version; - if (v == null) { - renderError('META_FETCH_V'); - return; - } - // for https://github.com/misskey-dev/misskey/issues/10202 if (lang == null || lang.toString == null || lang.toString() === 'null') { console.error('invalid lang value detected!!!', typeof lang, lang); lang = 'en-US'; } - const localRes = await window.fetch(`/assets/locales/${lang}.${v}.json`); + const localRes = await window.fetch(`/assets/locales/${lang}.${langsVersion}.json`); if (localRes.status === 200) { localStorage.setItem('lang', lang); localStorage.setItem('locale', await localRes.text()); - localStorage.setItem('localeVersion', v); + localStorage.setItem('localeVersion', langsVersion); } else { renderError('LOCALE_FETCH'); return; diff --git a/packages/backend/test/e2e/users.ts b/packages/backend/test/e2e/users.ts index 7097bd31a1..7d2e14f85d 100644 --- a/packages/backend/test/e2e/users.ts +++ b/packages/backend/test/e2e/users.ts @@ -141,6 +141,7 @@ describe('ユーザー', () => { hasUnreadNotification: user.hasUnreadNotification, unreadNotificationsCount: user.unreadNotificationsCount, hasPendingReceivedFollowRequest: user.hasPendingReceivedFollowRequest, + hasPendingSentFollowRequest: user.hasPendingSentFollowRequest, unreadAnnouncements: user.unreadAnnouncements, mutedWords: user.mutedWords, hardMutedWords: user.hardMutedWords, @@ -381,6 +382,7 @@ describe('ユーザー', () => { assert.strictEqual(response.hasUnreadNotification, false); assert.strictEqual(response.unreadNotificationsCount, 0); assert.strictEqual(response.hasPendingReceivedFollowRequest, false); + assert.strictEqual(response.hasPendingSentFollowRequest, false); assert.deepStrictEqual(response.unreadAnnouncements, []); assert.deepStrictEqual(response.mutedWords, []); assert.deepStrictEqual(response.mutedInstances, []); diff --git a/packages/backend/test/unit/misc/from-tuple.ts b/packages/backend/test/unit/misc/from-tuple.ts new file mode 100644 index 0000000000..b523cb5782 --- /dev/null +++ b/packages/backend/test/unit/misc/from-tuple.ts @@ -0,0 +1,13 @@ +import { fromTuple } from '@/misc/from-tuple.js'; + +describe(fromTuple, () => { + it('should return value when value is not an array', () => { + const value = fromTuple('abc'); + expect(value).toBe('abc'); + }); + + it('should return first element when value is an array', () => { + const value = fromTuple(['abc']); + expect(value).toBe('abc'); + }); +}); diff --git a/packages/backend/test/unit/misc/is-renote.ts b/packages/backend/test/unit/misc/is-renote.ts index 080271e404..1baa995f59 100644 --- a/packages/backend/test/unit/misc/is-renote.ts +++ b/packages/backend/test/unit/misc/is-renote.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { isQuote, isRenote } from '@/misc/is-renote.js'; +import { isPureRenote, isQuote, isRenote } from '@/misc/is-renote.js'; import { MiNote } from '@/models/Note.js'; const base: MiNote = { @@ -86,4 +86,24 @@ describe('misc:is-renote', () => { expect(isRenote(note)).toBe(true); expect(isQuote(note as any)).toBe(true); }); + + describe('isPureRenote', () => { + it('should return true when note is pure renote', () => { + const note = new MiNote({ renoteId: 'abc123', fileIds: [] }); + const result = isPureRenote(note); + expect(result).toBeTruthy(); + }); + + it('should return false when note is quote', () => { + const note = new MiNote({ renoteId: 'abc123', text: 'text', fileIds: [] }); + const result = isPureRenote(note); + expect(result).toBeFalsy(); + }); + + it('should return false when note is not renote', () => { + const note = new MiNote({ renoteId: null, fileIds: [] }); + const result = isPureRenote(note); + expect(result).toBeFalsy(); + }); + }); }); diff --git a/packages/backend/test/unit/models/LatestNote.ts b/packages/backend/test/unit/models/LatestNote.ts new file mode 100644 index 0000000000..129094ceff --- /dev/null +++ b/packages/backend/test/unit/models/LatestNote.ts @@ -0,0 +1,149 @@ +import { SkLatestNote } from '@/models/LatestNote.js'; +import { MiNote } from '@/models/Note.js'; + +describe(SkLatestNote, () => { + describe('keyFor', () => { + it('should include userId', () => { + const note = new MiNote({ userId: 'abc123', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.userId).toBe(note.userId); + }); + + it('should include isPublic when is public', () => { + const note = new MiNote({ visibility: 'public', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isPublic).toBeTruthy(); + }); + + it('should include isPublic when is home-only', () => { + const note = new MiNote({ visibility: 'home', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isPublic).toBeFalsy(); + }); + + it('should include isPublic when is followers-only', () => { + const note = new MiNote({ visibility: 'followers', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isPublic).toBeFalsy(); + }); + + it('should include isPublic when is specified', () => { + const note = new MiNote({ visibility: 'specified', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isPublic).toBeFalsy(); + }); + + it('should include isReply when is reply', () => { + const note = new MiNote({ replyId: 'abc123', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isReply).toBeTruthy(); + }); + + it('should include isReply when is not reply', () => { + const note = new MiNote({ replyId: null, fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isReply).toBeFalsy(); + }); + + it('should include isQuote when is quote', () => { + const note = new MiNote({ renoteId: 'abc123', text: 'text', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isQuote).toBeTruthy(); + }); + + it('should include isQuote when is reblog', () => { + const note = new MiNote({ renoteId: 'abc123', fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isQuote).toBeFalsy(); + }); + + it('should include isQuote when is neither quote nor reblog', () => { + const note = new MiNote({ renoteId: null, fileIds: [] }); + const key = SkLatestNote.keyFor(note); + expect(key.isQuote).toBeFalsy(); + }); + }); + + describe('areEquivalent', () => { + it('should return true when keys match', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeTruthy(); + }); + + it('should return true when keys match with different reply IDs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: '3', renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: '4', renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeTruthy(); + }); + + it('should return true when keys match with different renote IDs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: '3', fileIds: ['1'] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: null, renoteId: '4', fileIds: ['1'] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeTruthy(); + }); + + it('should return true when keys match with different file counts', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: ['1'] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: ['1','2'] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeTruthy(); + }); + + it('should return true when keys match with different private visibilities', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'home', replyId: null, renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'followers', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeTruthy(); + }); + + it('should return false when user ID differs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'def456', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeFalsy(); + }); + + it('should return false when visibility differs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'home', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeFalsy(); + }); + + it('should return false when reply differs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: '1', renoteId: null, fileIds: [] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeFalsy(); + }); + + it('should return false when quote differs', () => { + const first = new MiNote({ id: '1', userId: 'abc123', visibility: 'public', replyId: null, renoteId: '3', fileIds: ['1'] }); + const second = new MiNote({ id: '2', userId: 'abc123', visibility: 'public', replyId: null, renoteId: null, fileIds: [] }); + + const result = SkLatestNote.areEquivalent(first, second); + + expect(result).toBeFalsy(); + }); + }); +}); diff --git a/packages/frontend-embed/@types/global.d.ts b/packages/frontend-embed/@types/global.d.ts index 1025d1bedb..15373cbd2d 100644 --- a/packages/frontend-embed/@types/global.d.ts +++ b/packages/frontend-embed/@types/global.d.ts @@ -6,6 +6,7 @@ type FIXME = any; declare const _LANGS_: string[][]; +declare const _LANGS_VERSION_: string; declare const _VERSION_: string; declare const _ENV_: string; declare const _DEV_: boolean; diff --git a/packages/frontend-embed/vite.config.ts b/packages/frontend-embed/vite.config.ts index 5c9d0164f6..739c31cbc0 100644 --- a/packages/frontend-embed/vite.config.ts +++ b/packages/frontend-embed/vite.config.ts @@ -1,7 +1,7 @@ import path from 'path'; import pluginVue from '@vitejs/plugin-vue'; import { type UserConfig, defineConfig } from 'vite'; - +import { localesVersion } from '../../locales/version.js'; import locales from '../../locales/index.js'; import meta from '../../package.json'; import packageInfo from './package.json' with { type: 'json' }; @@ -95,6 +95,7 @@ export function getConfig(): UserConfig { define: { _VERSION_: JSON.stringify(meta.version), _LANGS_: JSON.stringify(Object.entries(locales).map(([k, v]) => [k, v._lang_])), + _LANGS_VERSION_: JSON.stringify(localesVersion), _ENV_: JSON.stringify(process.env.NODE_ENV), _DEV_: process.env.NODE_ENV !== 'production', _PERF_PREFIX_: JSON.stringify('Misskey:'), diff --git a/packages/frontend-shared/@types/global.d.ts b/packages/frontend-shared/@types/global.d.ts index 4b8d679e75..308515564b 100644 --- a/packages/frontend-shared/@types/global.d.ts +++ b/packages/frontend-shared/@types/global.d.ts @@ -7,6 +7,7 @@ type FIXME = any; declare const _LANGS_: string[][]; +declare const _LANGS_VERSION_: string; declare const _VERSION_: string; declare const _ENV_: string; declare const _DEV_: boolean; diff --git a/packages/frontend-shared/js/config.ts b/packages/frontend-shared/js/config.ts index af8dba4a8d..9a23e0e7f3 100644 --- a/packages/frontend-shared/js/config.ts +++ b/packages/frontend-shared/js/config.ts @@ -16,6 +16,7 @@ export const apiUrl = location.origin + '/api'; export const wsOrigin = location.origin; export const lang = localStorage.getItem('lang') ?? 'en-US'; export const langs = _LANGS_; +export const langsVersion = _LANGS_VERSION_; const preParseLocale = localStorage.getItem('locale'); export let locale: Locale = preParseLocale ? JSON.parse(preParseLocale) : null; export const version = _VERSION_; diff --git a/packages/frontend/@types/global.d.ts b/packages/frontend/@types/global.d.ts index 1025d1bedb..15373cbd2d 100644 --- a/packages/frontend/@types/global.d.ts +++ b/packages/frontend/@types/global.d.ts @@ -6,6 +6,7 @@ type FIXME = any; declare const _LANGS_: string[][]; +declare const _LANGS_VERSION_: string; declare const _VERSION_: string; declare const _ENV_: string; declare const _DEV_: boolean; diff --git a/packages/frontend/src/boot/common.ts b/packages/frontend/src/boot/common.ts index 487eefe60a..af8bbf57d2 100644 --- a/packages/frontend/src/boot/common.ts +++ b/packages/frontend/src/boot/common.ts @@ -5,7 +5,7 @@ import { computed, watch, version as vueVersion, App } from 'vue'; import { compareVersions } from 'compare-versions'; -import { version, lang, updateLocale, locale } from '@@/js/config.js'; +import { version, lang, langsVersion, updateLocale, locale } from '@@/js/config.js'; import widgets from '@/widgets/index.js'; import directives from '@/directives/index.js'; import components from '@/components/index.js'; @@ -81,14 +81,15 @@ export async function common(createVue: () => App) { //#region Detect language & fetch translations const localeVersion = miLocalStorage.getItem('localeVersion'); - const localeOutdated = (localeVersion == null || localeVersion !== version || locale == null); + const localeOutdated = (localeVersion == null || localeVersion !== langsVersion || locale == null); if (localeOutdated) { - const res = await window.fetch(`/assets/locales/${lang}.${version}.json`); + console.info(`Updating locales from version ${localeVersion ?? 'N/A'} to ${langsVersion}`); + const res = await window.fetch(`/assets/locales/${lang}.${langsVersion}.json`); if (res.status === 200) { const newLocale = await res.text(); const parsedNewLocale = JSON.parse(newLocale); miLocalStorage.setItem('locale', newLocale); - miLocalStorage.setItem('localeVersion', version); + miLocalStorage.setItem('localeVersion', langsVersion); updateLocale(parsedNewLocale); updateI18n(parsedNewLocale); } diff --git a/packages/frontend/src/components/SkUserRecentNotes.vue b/packages/frontend/src/components/SkUserRecentNotes.vue index 1d124b4932..2cdb4b6586 100644 --- a/packages/frontend/src/components/SkUserRecentNotes.vue +++ b/packages/frontend/src/components/SkUserRecentNotes.vue @@ -24,16 +24,14 @@ import MkPullToRefresh from '@/components/MkPullToRefresh.vue'; import { Paging } from '@/components/MkPagination.vue'; import { misskeyApi } from '@/scripts/misskey-api.js'; -const props = withDefaults(defineProps<{ +const props = defineProps<{ userId: string; - withRenotes?: boolean; - withReplies?: boolean; - onlyFiles?: boolean; -}>(), { - withRenotes: false, - withReplies: true, - onlyFiles: false, -}); + withNonPublic: boolean; + withQuotes: boolean; + withReplies: boolean; + withBots: boolean; + onlyFiles: boolean; +}>(); const loadError: Ref = ref(null); const user: Ref = ref(null); @@ -43,9 +41,13 @@ const pagination: Paging<'users/notes'> = { limit: 10, params: computed(() => ({ userId: props.userId, - withRenotes: props.withRenotes, + withNonPublic: props.withNonPublic, + withRenotes: false, + withQuotes: props.withQuotes, withReplies: props.withReplies, + withRepliesToSelf: props.withReplies, withFiles: props.onlyFiles, + allowPartial: true, })), }; diff --git a/packages/frontend/src/navbar.ts b/packages/frontend/src/navbar.ts index b92fdb17b9..8b45eb50e7 100644 --- a/packages/frontend/src/navbar.ts +++ b/packages/frontend/src/navbar.ts @@ -41,7 +41,7 @@ export const navbarItemDef = reactive({ followRequests: { title: i18n.ts.followRequests, icon: 'ti ti-user-plus', - show: computed(() => $i != null && $i.isLocked), + show: computed(() => $i != null && ($i.isLocked || $i.hasPendingReceivedFollowRequest || $i.hasPendingSentFollowRequest)), indicated: computed(() => $i != null && $i.hasPendingReceivedFollowRequest), to: '/my/follow-requests', }, diff --git a/packages/frontend/src/pages/follow-requests.vue b/packages/frontend/src/pages/follow-requests.vue index d50887b2e9..400dfdbe6d 100644 --- a/packages/frontend/src/pages/follow-requests.vue +++ b/packages/frontend/src/pages/follow-requests.vue @@ -5,39 +5,43 @@ SPDX-License-Identifier: AGPL-3.0-only -