Sharkey/packages/backend/src/models/LatestNote.ts

101 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-09-30 16:14:00 +00:00
/*
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2024-09-30 01:27:38 +00:00
import { PrimaryColumn, Entity, JoinColumn, Column, ManyToOne } from 'typeorm';
import { MiUser } from '@/models/User.js';
import { MiNote } from '@/models/Note.js';
2024-10-09 19:09:55 +00:00
import { isQuote, isRenote } from '@/misc/is-renote.js';
2024-09-30 01:27:38 +00:00
/**
* 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')
2024-10-08 20:37:44 +00:00
export class SkLatestNote {
2024-09-30 01:27:38 +00:00
@PrimaryColumn({
name: 'user_id',
type: 'varchar' as const,
length: 32,
})
public userId: string;
2024-10-08 21:02:31 +00:00
@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;
2024-09-30 01:27:38 +00:00
@ManyToOne(() => MiUser, {
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'user_id',
})
2024-09-30 01:27:38 +00:00
public user: MiUser | null;
@Column({
name: 'note_id',
type: 'varchar' as const,
length: 32,
})
public noteId: string;
@ManyToOne(() => MiNote, {
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'note_id',
})
2024-09-30 01:27:38 +00:00
public note: MiNote | null;
2024-09-30 01:52:57 +00:00
2024-10-08 20:37:44 +00:00
constructor(data?: Partial<SkLatestNote>) {
2024-09-30 03:22:58 +00:00
if (!data) return;
2024-09-30 01:52:57 +00:00
for (const [k, v] of Object.entries(data)) {
2024-09-30 02:50:39 +00:00
(this as Record<string, unknown>)[k] = v;
2024-09-30 01:52:57 +00:00
}
}
2024-10-09 19:09:55 +00:00
/**
* 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
);
}
2024-09-30 01:27:38 +00:00
}