Sharkey/src/server/api/endpoints/notes/user-list-timeline.ts

247 lines
5.6 KiB
TypeScript
Raw Normal View History

2018-07-07 13:19:00 +03:00
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
2018-04-25 13:53:16 +03:00
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import UserList from '../../../../models/user-list';
2018-06-18 03:54:53 +03:00
import { ILocalUser } from '../../../../models/user';
2018-08-16 17:59:22 +03:00
import getParams from '../../get-params';
2018-04-25 13:53:16 +03:00
2018-07-16 22:36:44 +03:00
export const meta = {
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '指定したユーザーリストのタイムラインを取得します。',
'en-US': 'Get timeline of a user list.'
2018-07-16 22:36:44 +03:00
},
2018-08-16 17:59:22 +03:00
requireCredential: true,
2018-07-16 22:36:44 +03:00
2018-08-16 17:59:22 +03:00
params: {
listId: $.type(ID).note({
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': 'リストのID'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
limit: $.num.optional.range(1, 100).note({
default: 10,
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '最大数'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
sinceId: $.type(ID).optional.note({
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '指定すると、この投稿を基点としてより新しい投稿を取得します'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
untilId: $.type(ID).optional.note({
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '指定すると、この投稿を基点としてより古い投稿を取得します'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
sinceDate: $.num.optional.note({
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '指定した時間を基点としてより新しい投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
untilDate: $.num.optional.note({
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '指定した時間を基点としてより古い投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
includeMyRenotes: $.bool.optional.note({
default: true,
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': '自分の行ったRenoteを含めるかどうか'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
includeRenotedMyNotes: $.bool.optional.note({
default: true,
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': 'Renoteされた自分の投稿を含めるかどうか'
2018-08-16 17:59:22 +03:00
}
}),
2018-04-25 13:53:16 +03:00
2018-08-16 17:59:22 +03:00
includeLocalRenotes: $.bool.optional.note({
default: true,
desc: {
2018-08-29 00:59:43 +03:00
'ja-JP': 'Renoteされたローカルの投稿を含めるかどうか'
2018-08-16 17:59:22 +03:00
}
}),
2018-06-07 00:13:57 +03:00
2018-09-05 13:32:46 +03:00
withFiles: $.bool.optional.note({
2018-08-16 17:59:22 +03:00
desc: {
'ja-JP': 'true にすると、ファイルが添付された投稿だけ取得します'
}
}),
mediaOnly: $.bool.optional.note({
desc: {
'ja-JP': 'true にすると、ファイルが添付された投稿だけ取得します (このパラメータは廃止予定です。代わりに withFiles を使ってください。)'
2018-08-16 17:59:22 +03:00
}
}),
}
};
export default async (params: any, user: ILocalUser) => {
const [ps, psErr] = getParams(meta, params);
if (psErr) throw psErr;
2018-04-25 13:53:16 +03:00
const [list, mutedUserIds] = await Promise.all([
// リストを取得
// Fetch the list
UserList.findOne({
2018-08-16 17:59:22 +03:00
_id: ps.listId,
2018-04-25 13:53:16 +03:00
userId: user._id
}),
// ミュートしているユーザーを取得
Mute.find({
muterId: user._id
}).then(ms => ms.map(m => m.muteeId))
]);
if (list.userIds.length == 0) {
return [];
}
//#region Construct query
const sort = {
_id: -1
};
const listQuery = list.userIds.map(u => ({
userId: u,
// リプライは含めない(ただし投稿者自身の投稿へのリプライ、自分の投稿へのリプライ、自分のリプライは含める)
$or: [{
// リプライでない
replyId: null
}, { // または
// リプライだが返信先が投稿者自身の投稿
$expr: {
$eq: ['$_reply.userId', '$userId']
}
}, { // または
// リプライだが返信先が自分(フォロワー)の投稿
'_reply.userId': user._id
}, { // または
// 自分(フォロワー)が送信したリプライ
userId: user._id
}]
}));
const query = {
$and: [{
// リストに入っている人のタイムラインへの投稿
$or: listQuery,
// mute
userId: {
$nin: mutedUserIds
},
'_reply.userId': {
$nin: mutedUserIds
},
'_renote.userId': {
$nin: mutedUserIds
},
}]
} as any;
// MongoDBではトップレベルで否定ができないため、De Morganの法則を利用してクエリします。
// つまり、「『自分の投稿かつRenote』ではない」を「『自分の投稿ではない』または『Renoteではない』」と表現します。
// for details: https://en.wikipedia.org/wiki/De_Morgan%27s_laws
2018-08-16 17:59:22 +03:00
if (ps.includeMyRenotes === false) {
2018-04-25 13:53:16 +03:00
query.$and.push({
$or: [{
userId: { $ne: user._id }
}, {
renoteId: null
}, {
text: { $ne: null }
}, {
2018-09-05 13:32:46 +03:00
fileIds: { $ne: [] }
2018-04-25 13:53:16 +03:00
}, {
poll: { $ne: null }
}]
});
}
2018-08-16 17:59:22 +03:00
if (ps.includeRenotedMyNotes === false) {
2018-04-25 13:53:16 +03:00
query.$and.push({
$or: [{
'_renote.userId': { $ne: user._id }
}, {
renoteId: null
}, {
text: { $ne: null }
}, {
2018-09-05 13:32:46 +03:00
fileIds: { $ne: [] }
2018-04-25 13:53:16 +03:00
}, {
poll: { $ne: null }
}]
});
}
2018-08-16 17:59:22 +03:00
if (ps.includeLocalRenotes === false) {
query.$and.push({
$or: [{
'_renote.user.host': { $ne: null }
}, {
renoteId: null
}, {
text: { $ne: null }
}, {
2018-09-05 13:32:46 +03:00
fileIds: { $ne: [] }
2018-08-16 17:59:22 +03:00
}, {
poll: { $ne: null }
}]
});
}
const withFiles = ps.withFiles != null ? ps.withFiles : ps.mediaOnly;
if (withFiles) {
2018-06-07 00:13:57 +03:00
query.$and.push({
2018-09-05 13:32:46 +03:00
fileIds: { $exists: true, $ne: [] }
2018-06-07 00:13:57 +03:00
});
}
2018-08-16 17:59:22 +03:00
if (ps.sinceId) {
2018-04-25 13:53:16 +03:00
sort._id = 1;
query._id = {
2018-08-16 17:59:22 +03:00
$gt: ps.sinceId
2018-04-25 13:53:16 +03:00
};
2018-08-16 17:59:22 +03:00
} else if (ps.untilId) {
2018-04-25 13:53:16 +03:00
query._id = {
2018-08-16 17:59:22 +03:00
$lt: ps.untilId
2018-04-25 13:53:16 +03:00
};
2018-08-16 17:59:22 +03:00
} else if (ps.sinceDate) {
2018-04-25 13:53:16 +03:00
sort._id = 1;
query.createdAt = {
2018-08-16 17:59:22 +03:00
$gt: new Date(ps.sinceDate)
2018-04-25 13:53:16 +03:00
};
2018-08-16 17:59:22 +03:00
} else if (ps.untilDate) {
2018-04-25 13:53:16 +03:00
query.createdAt = {
2018-08-16 17:59:22 +03:00
$lt: new Date(ps.untilDate)
2018-04-25 13:53:16 +03:00
};
}
//#endregion
// Issue query
const timeline = await Note
.find(query, {
2018-08-16 17:59:22 +03:00
limit: ps.limit,
2018-04-25 13:53:16 +03:00
sort: sort
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
};