add `latest_note` table

This commit is contained in:
Hazel K 2024-09-29 21:27:38 -04:00
parent c224ed031f
commit f8b55d357d
6 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,15 @@
export class AddLatestNote1727659258948 {
name = 'AddLatestNote1727659258948';
async up(queryRunner) {
await queryRunner.query('CREATE TABLE "latest_note" ("user_id" character varying(32) NOT NULL, "note_id" character varying(32) NOT NULL, "userId" character varying(32), "noteId" character varying(32), CONSTRAINT "PK_f619b62bfaafabe68f52fb50c9a" PRIMARY KEY ("user_id"))');
await queryRunner.query('ALTER TABLE "latest_note" ADD CONSTRAINT "FK_20e346fffe4a2174585005d6d80" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION');
await queryRunner.query('ALTER TABLE "latest_note" ADD CONSTRAINT "FK_47a38b1c13de6ce4e5090fb1acd" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION');
}
async down(queryRunner) {
await queryRunner.query('ALTER TABLE "latest_note" DROP CONSTRAINT "FK_47a38b1c13de6ce4e5090fb1acd"');
await queryRunner.query('ALTER TABLE "latest_note" DROP CONSTRAINT "FK_20e346fffe4a2174585005d6d80"');
await queryRunner.query('DROP TABLE "latest_note"');
}
}

View File

@ -19,6 +19,7 @@ export const DI = {
announcementReadsRepository: Symbol('announcementReadsRepository'),
appsRepository: Symbol('appsRepository'),
avatarDecorationsRepository: Symbol('avatarDecorationsRepository'),
latestNotesRepository: Symbol('latestNotesRepository'),
noteFavoritesRepository: Symbol('noteFavoritesRepository'),
noteThreadMutingsRepository: Symbol('noteThreadMutingsRepository'),
noteReactionsRepository: Symbol('noteReactionsRepository'),

View File

@ -0,0 +1,37 @@
import { PrimaryColumn, Entity, JoinColumn, Column, ManyToOne } from 'typeorm';
import { MiUser } from '@/models/User.js';
import { MiNote } from '@/models/Note.js';
/**
* Maps a user to the most recent post by that user.
* Public, home-only, and followers-only posts are included.
* DMs are not counted.
*/
@Entity('latest_note')
export class LatestNote {
@PrimaryColumn({
name: 'user_id',
type: 'varchar' as const,
length: 32,
})
public userId: string;
@ManyToOne(() => MiUser, {
onDelete: 'CASCADE',
})
@JoinColumn()
public user: MiUser | null;
@Column({
name: 'note_id',
type: 'varchar' as const,
length: 32,
})
public noteId: string;
@ManyToOne(() => MiNote, {
onDelete: 'CASCADE',
})
@JoinColumn()
public note: MiNote | null;
}

View File

@ -7,6 +7,7 @@ import type { Provider } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import {
LatestNote,
MiAbuseReportNotificationRecipient,
MiAbuseUserReport,
MiAccessToken,
@ -118,6 +119,12 @@ const $avatarDecorationsRepository: Provider = {
inject: [DI.db],
};
const $latestNotesRepository: Provider = {
provide: DI.latestNotesRepository,
useFactory: (db: DataSource) => db.getRepository(LatestNote).extend(miRepository as MiRepository<LatestNote>),
inject: [DI.db],
};
const $noteFavoritesRepository: Provider = {
provide: DI.noteFavoritesRepository,
useFactory: (db: DataSource) => db.getRepository(MiNoteFavorite).extend(miRepository as MiRepository<MiNoteFavorite>),
@ -511,6 +518,7 @@ const $reversiGamesRepository: Provider = {
$announcementReadsRepository,
$appsRepository,
$avatarDecorationsRepository,
$latestNotesRepository,
$noteFavoritesRepository,
$noteThreadMutingsRepository,
$noteReactionsRepository,
@ -583,6 +591,7 @@ const $reversiGamesRepository: Provider = {
$announcementReadsRepository,
$appsRepository,
$avatarDecorationsRepository,
$latestNotesRepository,
$noteFavoritesRepository,
$noteThreadMutingsRepository,
$noteReactionsRepository,

View File

@ -10,6 +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 { MiAbuseUserReport } from '@/models/AbuseUserReport.js';
import { MiAbuseReportNotificationRecipient } from '@/models/AbuseReportNotificationRecipient.js';
import { MiAccessToken } from '@/models/AccessToken.js';
@ -126,6 +127,7 @@ export const miRepository = {
} satisfies MiRepository<ObjectLiteral>;
export {
LatestNote,
MiAbuseUserReport,
MiAbuseReportNotificationRecipient,
MiAccessToken,
@ -224,6 +226,7 @@ export type GalleryPostsRepository = Repository<MiGalleryPost> & MiRepository<Mi
export type HashtagsRepository = Repository<MiHashtag> & MiRepository<MiHashtag>;
export type InstancesRepository = Repository<MiInstance> & MiRepository<MiInstance>;
export type MetasRepository = Repository<MiMeta> & MiRepository<MiMeta>;
export type LatestNoteRepository = Repository<LatestNote> & MiRepository<LatestNote>;
export type ModerationLogsRepository = Repository<MiModerationLog> & MiRepository<MiModerationLog>;
export type MutingsRepository = Repository<MiMuting> & MiRepository<MiMuting>;
export type RenoteMutingsRepository = Repository<MiRenoteMuting> & MiRepository<MiRenoteMuting>;

View File

@ -83,6 +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";
pg.types.setTypeParser(20, Number);
@ -130,6 +131,7 @@ class MyCustomLogger implements Logger {
}
export const entities = [
LatestNote,
MiAnnouncement,
MiAnnouncementRead,
MiMeta,