summaryrefslogtreecommitdiff
path: root/src/services/i/pin.ts
blob: 1ff5476b40ad7cc71a6c1eab3e4d39c7182bcd71 (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
import config from '../../config';
import renderAdd from '../../remote/activitypub/renderer/add';
import renderRemove from '../../remote/activitypub/renderer/remove';
import { renderActivity } from '../../remote/activitypub/renderer';
import { IdentifiableError } from '../../misc/identifiable-error';
import { User } from '../../models/entities/user';
import { Note } from '../../models/entities/note';
import { Notes, UserNotePinings, Users } from '../../models';
import { UserNotePining } from '../../models/entities/user-note-pining';
import { genId } from '../../misc/gen-id';
import { deliverToFollowers } from '../../remote/activitypub/deliver-manager';
import { deliverToRelays } from '../relay';

/**
 * 指定した投稿をピン留めします
 * @param user
 * @param noteId
 */
export async function addPinned(user: User, noteId: Note['id']) {
	// Fetch pinee
	const note = await Notes.findOne({
		id: noteId,
		userId: user.id
	});

	if (note == null) {
		throw new IdentifiableError('70c4e51f-5bea-449c-a030-53bee3cce202', 'No such note.');
	}

	const pinings = await UserNotePinings.find({ userId: user.id });

	if (pinings.length >= 5) {
		throw new IdentifiableError('15a018eb-58e5-4da1-93be-330fcc5e4e1a', 'You can not pin notes any more.');
	}

	if (pinings.some(pining => pining.noteId === note.id)) {
		throw new IdentifiableError('23f0cf4e-59a3-4276-a91d-61a5891c1514', 'That note has already been pinned.');
	}

	await UserNotePinings.save({
		id: genId(),
		createdAt: new Date(),
		userId: user.id,
		noteId: note.id
	} as UserNotePining);

	// Deliver to remote followers
	if (Users.isLocalUser(user)) {
		deliverPinnedChange(user.id, note.id, true);
	}
}

/**
 * 指定した投稿のピン留めを解除します
 * @param user
 * @param noteId
 */
export async function removePinned(user: User, noteId: Note['id']) {
	// Fetch unpinee
	const note = await Notes.findOne({
		id: noteId,
		userId: user.id
	});

	if (note == null) {
		throw new IdentifiableError('b302d4cf-c050-400a-bbb3-be208681f40c', 'No such note.');
	}

	UserNotePinings.delete({
		userId: user.id,
		noteId: note.id
	});

	// Deliver to remote followers
	if (Users.isLocalUser(user)) {
		deliverPinnedChange(user.id, noteId, false);
	}
}

export async function deliverPinnedChange(userId: User['id'], noteId: Note['id'], isAddition: boolean) {
	const user = await Users.findOne(userId);
	if (user == null) throw new Error('user not found');

	if (!Users.isLocalUser(user)) return;

	const target = `${config.url}/users/${user.id}/collections/featured`;
	const item = `${config.url}/notes/${noteId}`;
	const content = renderActivity(isAddition ? renderAdd(user, target, item) : renderRemove(user, target, item));

	deliverToFollowers(user, content);
	deliverToRelays(user, content);
}