summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes.ts
blob: 4f5a2112404e34dc038cc9d71ce2f20bd213233c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import $ from 'cafy'; import ID from '../../../misc/cafy-id';
import Note, { packMany } from '../../../models/note';
import getParams from '../get-params';

export const meta = {
	desc: {
		'ja-JP': '投稿を取得します。'
	},

	params: {
		local: $.bool.optional.note({
			desc: {
				'ja-JP': 'ローカルの投稿に限定するか否か'
			}
		}),

		reply: $.bool.optional.note({
			desc: {
				'ja-JP': '返信に限定するか否か'
			}
		}),

		renote: $.bool.optional.note({
			desc: {
				'ja-JP': 'Renoteに限定するか否か'
			}
		}),

		withFiles: $.bool.optional.note({
			desc: {
				'ja-JP': 'ファイルが添付された投稿に限定するか否か'
			}
		}),

		media: $.bool.optional.note({
			desc: {
				'ja-JP': 'ファイルが添付された投稿に限定するか否か (このパラメータは廃止予定です。代わりに withFiles を使ってください。)'
			}
		}),

		poll: $.bool.optional.note({
			desc: {
				'ja-JP': 'アンケートが添付された投稿に限定するか否か'
			}
		}),

		limit: $.num.optional.range(1, 100).note({
			default: 10
		}),

		sinceId: $.type(ID).optional.note({}),

		untilId: $.type(ID).optional.note({}),
	}
};

export default (params: any) => new Promise(async (res, rej) => {
	const [ps, psErr] = getParams(meta, params);
	if (psErr) return rej(psErr);

	// Check if both of sinceId and untilId is specified
	if (ps.sinceId && ps.untilId) {
		return rej('cannot set sinceId and untilId');
	}

	// Construct query
	const sort = {
		_id: -1
	};
	const query = {
		visibility: 'public'
	} as any;
	if (ps.sinceId) {
		sort._id = 1;
		query._id = {
			$gt: ps.sinceId
		};
	} else if (ps.untilId) {
		query._id = {
			$lt: ps.untilId
		};
	}

	if (ps.local) {
		query['_user.host'] = null;
	}

	if (ps.reply != undefined) {
		query.replyId = ps.reply ? { $exists: true, $ne: null } : null;
	}

	if (ps.renote != undefined) {
		query.renoteId = ps.renote ? { $exists: true, $ne: null } : null;
	}

	const withFiles = ps.withFiles != undefined ? ps.withFiles : ps.media;

	if (withFiles) {
		query.fileIds = withFiles ? { $exists: true, $ne: null } : [];
	}

	if (ps.poll != undefined) {
		query.poll = ps.poll ? { $exists: true, $ne: null } : null;
	}

	// TODO
	//if (bot != undefined) {
	//	query.isBot = bot;
	//}

	// Issue query
	const notes = await Note
		.find(query, {
			limit: ps.limit,
			sort: sort
		});

	// Serialize
	res(await packMany(notes));
});