2018-09-18 04:08:27 +00:00
|
|
|
import config from '../../config';
|
|
|
|
import renderAdd from '../../remote/activitypub/renderer/add';
|
|
|
|
import renderRemove from '../../remote/activitypub/renderer/remove';
|
2019-01-30 17:29:36 +00:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-09-18 04:08:27 +00:00
|
|
|
import { deliver } from '../../queue';
|
2019-02-22 02:46:58 +00:00
|
|
|
import { IdentifiableError } from '../../misc/identifiable-error';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { User, ILocalUser } from '../../models/entities/user';
|
|
|
|
import { Note } from '../../models/entities/note';
|
|
|
|
import { Notes, UserNotePinings, Users, Followings } from '../../models';
|
|
|
|
import { UserNotePining } from '../../models/entities/user-note-pinings';
|
|
|
|
import { genId } from '../../misc/gen-id';
|
2018-09-18 04:08:27 +00:00
|
|
|
|
2018-10-02 07:27:36 +00:00
|
|
|
/**
|
|
|
|
* 指定した投稿をピン留めします
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2019-04-07 12:50:36 +00:00
|
|
|
export async function addPinned(user: User, noteId: Note['id']) {
|
2018-10-02 07:27:36 +00:00
|
|
|
// Fetch pinee
|
2019-04-07 12:50:36 +00:00
|
|
|
const note = await Notes.findOne({
|
|
|
|
id: noteId,
|
|
|
|
userId: user.id
|
2018-10-02 07:27:36 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (note == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new IdentifiableError('70c4e51f-5bea-449c-a030-53bee3cce202', 'No such note.');
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const pinings = await UserNotePinings.find({ userId: user.id });
|
2018-10-07 10:58:00 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (pinings.length >= 5) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new IdentifiableError('15a018eb-58e5-4da1-93be-330fcc5e4e1a', 'You can not pin notes any more.');
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (pinings.some(pining => pining.noteId === note.id)) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new IdentifiableError('23f0cf4e-59a3-4276-a91d-61a5891c1514', 'That note has already been pinned.');
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
await UserNotePinings.save({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user.id,
|
|
|
|
noteId: note.id
|
|
|
|
} as UserNotePining);
|
2018-10-02 07:27:36 +00:00
|
|
|
|
|
|
|
// Deliver to remote followers
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user.id, note.id, true);
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した投稿のピン留めを解除します
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
2019-04-07 12:50:36 +00:00
|
|
|
export async function removePinned(user: User, noteId: Note['id']) {
|
2018-10-02 07:27:36 +00:00
|
|
|
// Fetch unpinee
|
2019-04-07 12:50:36 +00:00
|
|
|
const note = await Notes.findOne({
|
|
|
|
id: noteId,
|
|
|
|
userId: user.id
|
2018-10-02 07:27:36 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (note == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new IdentifiableError('b302d4cf-c050-400a-bbb3-be208681f40c', 'No such note.');
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
UserNotePinings.delete({
|
|
|
|
userId: user.id,
|
|
|
|
noteId: note.id
|
2018-10-02 07:27:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Deliver to remote followers
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user.id, noteId, false);
|
2018-10-02 07:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
export async function deliverPinnedChange(userId: User['id'], noteId: Note['id'], isAddition: boolean) {
|
|
|
|
const user = await Users.findOne({
|
|
|
|
id: userId
|
2018-09-18 04:08:27 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!Users.isLocalUser(user)) return;
|
2018-09-18 04:08:27 +00:00
|
|
|
|
|
|
|
const queue = await CreateRemoteInboxes(user);
|
|
|
|
|
|
|
|
if (queue.length < 1) return;
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const target = `${config.url}/users/${user.id}/collections/featured`;
|
2018-09-18 04:08:27 +00:00
|
|
|
|
2018-09-24 07:26:12 +00:00
|
|
|
const item = `${config.url}/notes/${noteId}`;
|
2019-01-30 17:29:36 +00:00
|
|
|
const content = renderActivity(isAddition ? renderAdd(user, target, item) : renderRemove(user, target, item));
|
2018-12-11 11:36:55 +00:00
|
|
|
for (const inbox of queue) {
|
2018-09-24 07:26:12 +00:00
|
|
|
deliver(user, content, inbox);
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2018-09-18 04:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ローカルユーザーのリモートフォロワーのinboxリストを作成する
|
|
|
|
* @param user ローカルユーザー
|
|
|
|
*/
|
|
|
|
async function CreateRemoteInboxes(user: ILocalUser): Promise<string[]> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const followers = await Followings.find({
|
|
|
|
followeeId: user.id
|
2018-09-18 04:08:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const queue: string[] = [];
|
|
|
|
|
2018-11-30 14:30:28 +00:00
|
|
|
for (const following of followers) {
|
2019-04-07 12:50:36 +00:00
|
|
|
const follower = {
|
|
|
|
host: following.followerHost,
|
|
|
|
inbox: following.followerInbox,
|
|
|
|
sharedInbox: following.followerSharedInbox,
|
|
|
|
};
|
2018-09-18 04:08:27 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (follower.host !== null) {
|
2018-09-18 04:08:27 +00:00
|
|
|
const inbox = follower.sharedInbox || follower.inbox;
|
|
|
|
if (!queue.includes(inbox)) queue.push(inbox);
|
|
|
|
}
|
2018-11-30 14:30:28 +00:00
|
|
|
}
|
2018-09-18 04:08:27 +00:00
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|