リモートのピン留め投稿取得対応 (#2798)
* Fetch featured * Handle featured change * Fix typo
This commit is contained in:
@@ -1,12 +1,83 @@
|
||||
import config from '../../config';
|
||||
import * as mongo from 'mongodb';
|
||||
import User, { isLocalUser, isRemoteUser, ILocalUser } from '../../models/user';
|
||||
import User, { isLocalUser, isRemoteUser, ILocalUser, IUser } from '../../models/user';
|
||||
import Note from '../../models/note';
|
||||
import Following from '../../models/following';
|
||||
import renderAdd from '../../remote/activitypub/renderer/add';
|
||||
import renderRemove from '../../remote/activitypub/renderer/remove';
|
||||
import packAp from '../../remote/activitypub/renderer';
|
||||
import { deliver } from '../../queue';
|
||||
|
||||
/**
|
||||
* 指定した投稿をピン留めします
|
||||
* @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) {
|
||||
throw new Error('note not found');
|
||||
}
|
||||
|
||||
const pinnedNoteIds = user.pinnedNoteIds || [];
|
||||
|
||||
if (pinnedNoteIds.length > 5) {
|
||||
throw new Error('cannot pin more notes');
|
||||
}
|
||||
|
||||
if (pinnedNoteIds.some(id => id.equals(note._id))) {
|
||||
throw new Error('already exists');
|
||||
}
|
||||
|
||||
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) {
|
||||
throw new Error('note not found');
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
export async function deliverPinnedChange(userId: mongo.ObjectID, noteId: mongo.ObjectID, isAddition: boolean) {
|
||||
const user = await User.findOne({
|
||||
_id: userId
|
||||
|
Reference in New Issue
Block a user