2018-09-18 04:08:27 +00:00
|
|
|
import config from '../../config';
|
|
|
|
import * as mongo from 'mongodb';
|
2018-10-02 07:27:36 +00:00
|
|
|
import User, { isLocalUser, isRemoteUser, ILocalUser, IUser } from '../../models/user';
|
2018-10-13 14:12:48 +00:00
|
|
|
import Note, { packMany } from '../../models/note';
|
2018-09-18 04:08:27 +00:00
|
|
|
import Following from '../../models/following';
|
|
|
|
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';
|
2018-09-18 04:08:27 +00:00
|
|
|
|
2018-10-02 07:27:36 +00:00
|
|
|
/**
|
|
|
|
* 指定した投稿をピン留めします
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
|
|
|
export async function addPinned(user: IUser, noteId: mongo.ObjectID) {
|
|
|
|
// Fetch pinee
|
|
|
|
const note = await Note.findOne({
|
|
|
|
_id: noteId,
|
|
|
|
userId: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-07 10:58:00 +00:00
|
|
|
let pinnedNoteIds = user.pinnedNoteIds || [];
|
|
|
|
|
|
|
|
//#region 現在ピン留め投稿している投稿が実際にデータベースに存在しているのかチェック
|
2018-10-13 14:12:48 +00:00
|
|
|
// データベースの欠損などで存在していない(または破損している)場合があるので。
|
2018-10-07 10:58:00 +00:00
|
|
|
// 存在していなかったらピン留め投稿から外す
|
2018-10-13 14:12:48 +00:00
|
|
|
const pinnedNotes = await packMany(pinnedNoteIds, null, { detail: true });
|
2018-10-07 10:58:00 +00:00
|
|
|
|
2018-10-13 14:12:48 +00:00
|
|
|
pinnedNoteIds = pinnedNoteIds.filter(id => pinnedNotes.some(n => n.id.toString() === id.toHexString()));
|
2018-10-07 10:58:00 +00:00
|
|
|
//#endregion
|
2018-10-02 07:27:36 +00:00
|
|
|
|
2018-10-03 03:37:23 +00:00
|
|
|
if (pinnedNoteIds.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
|
|
|
}
|
|
|
|
|
|
|
|
if (pinnedNoteIds.some(id => id.equals(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
|
|
|
}
|
|
|
|
|
|
|
|
pinnedNoteIds.unshift(note._id);
|
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
|
|
|
pinnedNoteIds: pinnedNoteIds
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Deliver to remote followers
|
|
|
|
if (isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user._id, note._id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した投稿のピン留めを解除します
|
|
|
|
* @param user
|
|
|
|
* @param noteId
|
|
|
|
*/
|
|
|
|
export async function removePinned(user: IUser, noteId: mongo.ObjectID) {
|
|
|
|
// Fetch unpinee
|
|
|
|
const note = await Note.findOne({
|
|
|
|
_id: noteId,
|
|
|
|
userId: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const pinnedNoteIds = (user.pinnedNoteIds || []).filter(id => !id.equals(note._id));
|
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
|
|
|
pinnedNoteIds: pinnedNoteIds
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Deliver to remote followers
|
|
|
|
if (isLocalUser(user)) {
|
|
|
|
deliverPinnedChange(user._id, noteId, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 07:26:12 +00:00
|
|
|
export async function deliverPinnedChange(userId: mongo.ObjectID, noteId: mongo.ObjectID, isAddition: boolean) {
|
2018-09-18 04:08:27 +00:00
|
|
|
const user = await User.findOne({
|
|
|
|
_id: userId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isLocalUser(user)) return;
|
|
|
|
|
|
|
|
const queue = await CreateRemoteInboxes(user);
|
|
|
|
|
|
|
|
if (queue.length < 1) return;
|
|
|
|
|
|
|
|
const target = `${config.url}/users/${user._id}/collections/featured`;
|
|
|
|
|
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[]> {
|
|
|
|
const followers = await Following.find({
|
|
|
|
followeeId: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
const queue: string[] = [];
|
|
|
|
|
2018-11-30 14:30:28 +00:00
|
|
|
for (const following of followers) {
|
2018-09-18 04:08:27 +00:00
|
|
|
const follower = following._follower;
|
|
|
|
|
|
|
|
if (isRemoteUser(follower)) {
|
|
|
|
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;
|
|
|
|
}
|