2022-02-26 20:07:39 -06:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import renderAdd from '@/remote/activitypub/renderer/add.js';
|
|
|
|
import renderRemove from '@/remote/activitypub/renderer/remove.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { Notes, UserNotePinings, Users } from '@/models/index.js';
|
|
|
|
import { UserNotePining } from '@/models/entities/user-note-pining.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { deliverToFollowers } from '@/remote/activitypub/deliver-manager.js';
|
|
|
|
import { deliverToRelays } from '../relay.js';
|
2018-09-17 23:08:27 -05:00
|
|
|
|
2018-10-02 02:27:36 -05:00
|
|
|
/**
|
|
|
|
* 指定した投稿をピン留めします
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2021-03-23 21:05:37 -05:00
|
|
|
export async function addPinned(user: { id: User['id']; host: User['host']; }, noteId: Note['id']) {
|
2018-10-02 02:27:36 -05:00
|
|
|
// Fetch pinee
|
2019-04-07 07:50:36 -05:00
|
|
|
const note = await Notes.findOne({
|
|
|
|
id: noteId,
|
2021-12-09 08:58:30 -06:00
|
|
|
userId: user.id,
|
2018-10-02 02:27:36 -05:00
|
|
|
});
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (note == null) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new IdentifiableError('70c4e51f-5bea-449c-a030-53bee3cce202', 'No such note.');
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const pinings = await UserNotePinings.find({ userId: user.id });
|
2018-10-07 05:58:00 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (pinings.length >= 5) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new IdentifiableError('15a018eb-58e5-4da1-93be-330fcc5e4e1a', 'You can not pin notes any more.');
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (pinings.some(pining => pining.noteId === note.id)) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new IdentifiableError('23f0cf4e-59a3-4276-a91d-61a5891c1514', 'That note has already been pinned.');
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
|
2021-03-21 07:27:09 -05:00
|
|
|
await UserNotePinings.insert({
|
2019-04-07 07:50:36 -05:00
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user.id,
|
2021-12-09 08:58:30 -06:00
|
|
|
noteId: note.id,
|
2019-04-07 07:50:36 -05:00
|
|
|
} as UserNotePining);
|
2018-10-02 02:27:36 -05:00
|
|
|
|
|
|
|
// Deliver to remote followers
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user.id, note.id, true);
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した投稿のピン留めを解除します
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2021-03-23 21:05:37 -05:00
|
|
|
export async function removePinned(user: { id: User['id']; host: User['host']; }, noteId: Note['id']) {
|
2018-10-02 02:27:36 -05:00
|
|
|
// Fetch unpinee
|
2019-04-07 07:50:36 -05:00
|
|
|
const note = await Notes.findOne({
|
|
|
|
id: noteId,
|
2021-12-09 08:58:30 -06:00
|
|
|
userId: user.id,
|
2018-10-02 02:27:36 -05:00
|
|
|
});
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (note == null) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new IdentifiableError('b302d4cf-c050-400a-bbb3-be208681f40c', 'No such note.');
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
UserNotePinings.delete({
|
|
|
|
userId: user.id,
|
2021-12-09 08:58:30 -06:00
|
|
|
noteId: note.id,
|
2018-10-02 02:27:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Deliver to remote followers
|
2019-04-07 07:50:36 -05:00
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user.id, noteId, false);
|
2018-10-02 02:27:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
export async function deliverPinnedChange(userId: User['id'], noteId: Note['id'], isAddition: boolean) {
|
2019-04-12 11:43:22 -05:00
|
|
|
const user = await Users.findOne(userId);
|
2019-04-13 14:17:24 -05:00
|
|
|
if (user == null) throw new Error('user not found');
|
2018-09-17 23:08:27 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (!Users.isLocalUser(user)) return;
|
2018-09-17 23:08:27 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const target = `${config.url}/users/${user.id}/collections/featured`;
|
2018-09-24 02:26:12 -05:00
|
|
|
const item = `${config.url}/notes/${noteId}`;
|
2019-01-30 11:29:36 -06:00
|
|
|
const content = renderActivity(isAddition ? renderAdd(user, target, item) : renderRemove(user, target, item));
|
2018-09-17 23:08:27 -05:00
|
|
|
|
2019-11-09 03:51:54 -06:00
|
|
|
deliverToFollowers(user, content);
|
2020-05-10 04:42:31 -05:00
|
|
|
deliverToRelays(user, content);
|
2018-09-17 23:08:27 -05:00
|
|
|
}
|