summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/is-renote.ts
blob: d6872de46a4fe7a1f3402600c58bf6c3031ca36b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import type { MiNote } from '@/models/Note.js';
import type { Packed } from '@/misc/json-schema.js';

// NoteEntityService.isPureRenote とよしなにリンク

type Renote =
	MiNote & {
		renoteId: NonNullable<MiNote['renoteId']>
	};

type Quote =
	Renote & ({
		text: NonNullable<MiNote['text']>
	} | {
		cw: NonNullable<MiNote['cw']>
	} | {
		replyId: NonNullable<MiNote['replyId']>
		reply: NonNullable<MiNote['reply']>
	} | {
		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;
}

export function isQuote(note: Renote): note is Quote {
	// NOTE: SYNC WITH NoteCreateService.isQuote
	return note.text != null ||
		note.cw != null ||
		note.replyId != null ||
		note.hasPoll ||
		note.fileIds.length > 0;
}

export function isPureRenote(note: MiNote): note is PureRenote {
	return isRenote(note) && !isQuote(note);
}

type PackedRenote =
	Packed<'Note'> & {
		renoteId: NonNullable<Packed<'Note'>['renoteId']>
	};

type PackedQuote =
	PackedRenote & ({
		text: NonNullable<Packed<'Note'>['text']>
	} | {
		cw: NonNullable<Packed<'Note'>['cw']>
	} | {
		replyId: NonNullable<Packed<'Note'>['replyId']>
	} | {
		poll: NonNullable<Packed<'Note'>['poll']>
	} | {
		fileIds: NonNullable<Packed<'Note'>['fileIds']>
	});

type PackedPureRenote = PackedRenote & {
	text: NonNullable<Packed<'Note'>['text']>;
	cw: NonNullable<Packed<'Note'>['cw']>;
	replyId: NonNullable<Packed<'Note'>['replyId']>;
	poll: NonNullable<Packed<'Note'>['poll']>;
	fileIds: NonNullable<Packed<'Note'>['fileIds']>;
}

export function isRenotePacked(note: Packed<'Note'>): note is PackedRenote {
	return note.renoteId != null;
}

export function isQuotePacked(note: PackedRenote): note is PackedQuote {
	return note.text != null ||
		note.cw != null ||
		note.replyId != null ||
		note.poll != null ||
		(note.fileIds != null && note.fileIds.length > 0);
}

export function isPackedPureRenote(note: Packed<'Note'>): note is PackedPureRenote {
	return isRenotePacked(note) && !isQuotePacked(note);
}