summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/notes')
-rw-r--r--src/server/api/endpoints/notes/create.ts2
-rw-r--r--src/server/api/endpoints/notes/delete.ts26
-rw-r--r--src/server/api/endpoints/notes/global-timeline.ts36
-rw-r--r--src/server/api/endpoints/notes/local-timeline.ts40
-rw-r--r--src/server/api/endpoints/notes/replies.ts26
-rw-r--r--src/server/api/endpoints/notes/search_by_tag.ts329
-rw-r--r--src/server/api/endpoints/notes/timeline.ts10
-rw-r--r--src/server/api/endpoints/notes/user-list-timeline.ts10
8 files changed, 431 insertions, 48 deletions
diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts
index 182359f637..446764e1d6 100644
--- a/src/server/api/endpoints/notes/create.ts
+++ b/src/server/api/endpoints/notes/create.ts
@@ -140,7 +140,7 @@ module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res
}
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
- if (text === undefined && files === null && renote === null && poll === undefined) {
+ if ((text === undefined || text === null) && files === null && renote === null && poll === undefined) {
return rej('text, mediaIds, renoteId or poll is required');
}
diff --git a/src/server/api/endpoints/notes/delete.ts b/src/server/api/endpoints/notes/delete.ts
new file mode 100644
index 0000000000..9bbb1541d6
--- /dev/null
+++ b/src/server/api/endpoints/notes/delete.ts
@@ -0,0 +1,26 @@
+import $ from 'cafy'; import ID from '../../../../cafy-id';
+import Note from '../../../../models/note';
+import deleteNote from '../../../../services/note/delete';
+
+/**
+ * Delete a note
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'noteId' parameter
+ const [noteId, noteIdErr] = $.type(ID).get(params.noteId);
+ if (noteIdErr) return rej('invalid noteId param');
+
+ // Fetch note
+ const note = await Note.findOne({
+ _id: noteId,
+ userId: user._id
+ });
+
+ if (note === null) {
+ return rej('note not found');
+ }
+
+ await deleteNote(user, note);
+
+ res();
+});
diff --git a/src/server/api/endpoints/notes/global-timeline.ts b/src/server/api/endpoints/notes/global-timeline.ts
index d22a1763de..2d4f2b6368 100644
--- a/src/server/api/endpoints/notes/global-timeline.ts
+++ b/src/server/api/endpoints/notes/global-timeline.ts
@@ -9,7 +9,7 @@ import { pack } from '../../../../models/note';
/**
* Get timeline of global
*/
-module.exports = async (params, user, app) => {
+module.exports = async (params, user) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit);
if (limitErr) throw 'invalid limit param';
@@ -35,10 +35,14 @@ module.exports = async (params, user, app) => {
throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified';
}
+ // Get 'mediaOnly' parameter
+ const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly);
+ if (mediaOnlyErr) throw 'invalid mediaOnly param';
+
// ミュートしているユーザーを取得
- const mutedUserIds = (await Mute.find({
+ const mutedUserIds = user ? (await Mute.find({
muterId: user._id
- })).map(m => m.muteeId);
+ })).map(m => m.muteeId) : null;
//#region Construct query
const sort = {
@@ -46,17 +50,27 @@ module.exports = async (params, user, app) => {
};
const query = {
- // mute
- userId: {
+ // public only
+ visibility: 'public'
+ } as any;
+
+ if (mutedUserIds && mutedUserIds.length > 0) {
+ query.userId = {
$nin: mutedUserIds
- },
- '_reply.userId': {
+ };
+
+ query['_reply.userId'] = {
$nin: mutedUserIds
- },
- '_renote.userId': {
+ };
+
+ query['_renote.userId'] = {
$nin: mutedUserIds
- }
- } as any;
+ };
+ }
+
+ if (mediaOnly) {
+ query.mediaIds = { $exists: true, $ne: [] };
+ }
if (sinceId) {
sort._id = 1;
diff --git a/src/server/api/endpoints/notes/local-timeline.ts b/src/server/api/endpoints/notes/local-timeline.ts
index e7ebe5d960..734cc9af0a 100644
--- a/src/server/api/endpoints/notes/local-timeline.ts
+++ b/src/server/api/endpoints/notes/local-timeline.ts
@@ -9,7 +9,7 @@ import { pack } from '../../../../models/note';
/**
* Get timeline of local
*/
-module.exports = async (params, user, app) => {
+module.exports = async (params, user) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit);
if (limitErr) throw 'invalid limit param';
@@ -35,10 +35,14 @@ module.exports = async (params, user, app) => {
throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified';
}
+ // Get 'mediaOnly' parameter
+ const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly);
+ if (mediaOnlyErr) throw 'invalid mediaOnly param';
+
// ミュートしているユーザーを取得
- const mutedUserIds = (await Mute.find({
+ const mutedUserIds = user ? (await Mute.find({
muterId: user._id
- })).map(m => m.muteeId);
+ })).map(m => m.muteeId) : null;
//#region Construct query
const sort = {
@@ -46,21 +50,31 @@ module.exports = async (params, user, app) => {
};
const query = {
- // mute
- userId: {
- $nin: mutedUserIds
- },
- '_reply.userId': {
- $nin: mutedUserIds
- },
- '_renote.userId': {
- $nin: mutedUserIds
- },
+ // public only
+ visibility: 'public',
// local
'_user.host': null
} as any;
+ if (mutedUserIds && mutedUserIds.length > 0) {
+ query.userId = {
+ $nin: mutedUserIds
+ };
+
+ query['_reply.userId'] = {
+ $nin: mutedUserIds
+ };
+
+ query['_renote.userId'] = {
+ $nin: mutedUserIds
+ };
+ }
+
+ if (mediaOnly) {
+ query.mediaIds = { $exists: true, $ne: [] };
+ }
+
if (sinceId) {
sort._id = 1;
query._id = {
diff --git a/src/server/api/endpoints/notes/replies.ts b/src/server/api/endpoints/notes/replies.ts
index 11d221d8f7..608027f6b1 100644
--- a/src/server/api/endpoints/notes/replies.ts
+++ b/src/server/api/endpoints/notes/replies.ts
@@ -1,15 +1,8 @@
-/**
- * Module dependencies
- */
import $ from 'cafy'; import ID from '../../../../cafy-id';
import Note, { pack } from '../../../../models/note';
/**
- * Show a replies of a note
- *
- * @param {any} params
- * @param {any} user
- * @return {Promise<any>}
+ * Get replies of a note
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'noteId' parameter
@@ -24,10 +17,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset);
if (offsetErr) return rej('invalid offset param');
- // Get 'sort' parameter
- const [sort = 'desc', sortError] = $.str.optional().or('desc asc').get(params.sort);
- if (sortError) return rej('invalid sort param');
-
// Lookup note
const note = await Note.findOne({
_id: noteId
@@ -37,17 +26,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('note not found');
}
- // Issue query
- const replies = await Note
- .find({ replyId: note._id }, {
- limit: limit,
- skip: offset,
- sort: {
- _id: sort == 'asc' ? 1 : -1
- }
- });
+ const ids = (note._replyIds || []).slice(offset, offset + limit);
// Serialize
- res(await Promise.all(replies.map(async note =>
- await pack(note, user))));
+ res(await Promise.all(ids.map(id => pack(id, user))));
});
diff --git a/src/server/api/endpoints/notes/search_by_tag.ts b/src/server/api/endpoints/notes/search_by_tag.ts
new file mode 100644
index 0000000000..4cf070f4ce
--- /dev/null
+++ b/src/server/api/endpoints/notes/search_by_tag.ts
@@ -0,0 +1,329 @@
+import $ from 'cafy'; import ID from '../../../../cafy-id';
+import Note from '../../../../models/note';
+import User from '../../../../models/user';
+import Mute from '../../../../models/mute';
+import { getFriendIds } from '../../common/get-friends';
+import { pack } from '../../../../models/note';
+
+/**
+ * Search notes by tag
+ */
+module.exports = (params, me) => new Promise(async (res, rej) => {
+ // Get 'tag' parameter
+ const [tag, tagError] = $.str.get(params.tag);
+ if (tagError) return rej('invalid tag param');
+
+ // Get 'includeUserIds' parameter
+ const [includeUserIds = [], includeUserIdsErr] = $.arr($.type(ID)).optional().get(params.includeUserIds);
+ if (includeUserIdsErr) return rej('invalid includeUserIds param');
+
+ // Get 'excludeUserIds' parameter
+ const [excludeUserIds = [], excludeUserIdsErr] = $.arr($.type(ID)).optional().get(params.excludeUserIds);
+ if (excludeUserIdsErr) return rej('invalid excludeUserIds param');
+
+ // Get 'includeUserUsernames' parameter
+ const [includeUserUsernames = [], includeUserUsernamesErr] = $.arr($.str).optional().get(params.includeUserUsernames);
+ if (includeUserUsernamesErr) return rej('invalid includeUserUsernames param');
+
+ // Get 'excludeUserUsernames' parameter
+ const [excludeUserUsernames = [], excludeUserUsernamesErr] = $.arr($.str).optional().get(params.excludeUserUsernames);
+ if (excludeUserUsernamesErr) return rej('invalid excludeUserUsernames param');
+
+ // Get 'following' parameter
+ const [following = null, followingErr] = $.bool.optional().nullable().get(params.following);
+ if (followingErr) return rej('invalid following param');
+
+ // Get 'mute' parameter
+ const [mute = 'mute_all', muteErr] = $.str.optional().get(params.mute);
+ if (muteErr) return rej('invalid mute param');
+
+ // Get 'reply' parameter
+ const [reply = null, replyErr] = $.bool.optional().nullable().get(params.reply);
+ if (replyErr) return rej('invalid reply param');
+
+ // Get 'renote' parameter
+ const [renote = null, renoteErr] = $.bool.optional().nullable().get(params.renote);
+ if (renoteErr) return rej('invalid renote param');
+
+ // Get 'media' parameter
+ const [media = null, mediaErr] = $.bool.optional().nullable().get(params.media);
+ if (mediaErr) return rej('invalid media param');
+
+ // Get 'poll' parameter
+ const [poll = null, pollErr] = $.bool.optional().nullable().get(params.poll);
+ if (pollErr) return rej('invalid poll param');
+
+ // Get 'sinceDate' parameter
+ const [sinceDate, sinceDateErr] = $.num.optional().get(params.sinceDate);
+ if (sinceDateErr) throw 'invalid sinceDate param';
+
+ // Get 'untilDate' parameter
+ const [untilDate, untilDateErr] = $.num.optional().get(params.untilDate);
+ if (untilDateErr) throw 'invalid untilDate param';
+
+ // Get 'offset' parameter
+ const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset);
+ if (offsetErr) return rej('invalid offset param');
+
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $.num.optional().range(1, 30).get(params.limit);
+ if (limitErr) return rej('invalid limit param');
+
+ let includeUsers = includeUserIds;
+ if (includeUserUsernames != null) {
+ const ids = (await Promise.all(includeUserUsernames.map(async (username) => {
+ const _user = await User.findOne({
+ usernameLower: username.toLowerCase()
+ });
+ return _user ? _user._id : null;
+ }))).filter(id => id != null);
+ includeUsers = includeUsers.concat(ids);
+ }
+
+ let excludeUsers = excludeUserIds;
+ if (excludeUserUsernames != null) {
+ const ids = (await Promise.all(excludeUserUsernames.map(async (username) => {
+ const _user = await User.findOne({
+ usernameLower: username.toLowerCase()
+ });
+ return _user ? _user._id : null;
+ }))).filter(id => id != null);
+ excludeUsers = excludeUsers.concat(ids);
+ }
+
+ search(res, rej, me, tag, includeUsers, excludeUsers, following,
+ mute, reply, renote, media, poll, sinceDate, untilDate, offset, limit);
+});
+
+async function search(
+ res, rej, me, tag, includeUserIds, excludeUserIds, following,
+ mute, reply, renote, media, poll, sinceDate, untilDate, offset, max) {
+
+ let q: any = {
+ $and: [{
+ tags: tag
+ }]
+ };
+
+ const push = x => q.$and.push(x);
+
+ if (includeUserIds && includeUserIds.length != 0) {
+ push({
+ userId: {
+ $in: includeUserIds
+ }
+ });
+ } else if (excludeUserIds && excludeUserIds.length != 0) {
+ push({
+ userId: {
+ $nin: excludeUserIds
+ }
+ });
+ }
+
+ if (following != null && me != null) {
+ const ids = await getFriendIds(me._id, false);
+ push({
+ userId: following ? {
+ $in: ids
+ } : {
+ $nin: ids.concat(me._id)
+ }
+ });
+ }
+
+ if (me != null) {
+ const mutes = await Mute.find({
+ muterId: me._id,
+ deletedAt: { $exists: false }
+ });
+ const mutedUserIds = mutes.map(m => m.muteeId);
+
+ switch (mute) {
+ case 'mute_all':
+ push({
+ userId: {
+ $nin: mutedUserIds
+ },
+ '_reply.userId': {
+ $nin: mutedUserIds
+ },
+ '_renote.userId': {
+ $nin: mutedUserIds
+ }
+ });
+ break;
+ case 'mute_related':
+ push({
+ '_reply.userId': {
+ $nin: mutedUserIds
+ },
+ '_renote.userId': {
+ $nin: mutedUserIds
+ }
+ });
+ break;
+ case 'mute_direct':
+ push({
+ userId: {
+ $nin: mutedUserIds
+ }
+ });
+ break;
+ case 'direct_only':
+ push({
+ userId: {
+ $in: mutedUserIds
+ }
+ });
+ break;
+ case 'related_only':
+ push({
+ $or: [{
+ '_reply.userId': {
+ $in: mutedUserIds
+ }
+ }, {
+ '_renote.userId': {
+ $in: mutedUserIds
+ }
+ }]
+ });
+ break;
+ case 'all_only':
+ push({
+ $or: [{
+ userId: {
+ $in: mutedUserIds
+ }
+ }, {
+ '_reply.userId': {
+ $in: mutedUserIds
+ }
+ }, {
+ '_renote.userId': {
+ $in: mutedUserIds
+ }
+ }]
+ });
+ break;
+ }
+ }
+
+ if (reply != null) {
+ if (reply) {
+ push({
+ replyId: {
+ $exists: true,
+ $ne: null
+ }
+ });
+ } else {
+ push({
+ $or: [{
+ replyId: {
+ $exists: false
+ }
+ }, {
+ replyId: null
+ }]
+ });
+ }
+ }
+
+ if (renote != null) {
+ if (renote) {
+ push({
+ renoteId: {
+ $exists: true,
+ $ne: null
+ }
+ });
+ } else {
+ push({
+ $or: [{
+ renoteId: {
+ $exists: false
+ }
+ }, {
+ renoteId: null
+ }]
+ });
+ }
+ }
+
+ if (media != null) {
+ if (media) {
+ push({
+ mediaIds: {
+ $exists: true,
+ $ne: null
+ }
+ });
+ } else {
+ push({
+ $or: [{
+ mediaIds: {
+ $exists: false
+ }
+ }, {
+ mediaIds: null
+ }]
+ });
+ }
+ }
+
+ if (poll != null) {
+ if (poll) {
+ push({
+ poll: {
+ $exists: true,
+ $ne: null
+ }
+ });
+ } else {
+ push({
+ $or: [{
+ poll: {
+ $exists: false
+ }
+ }, {
+ poll: null
+ }]
+ });
+ }
+ }
+
+ if (sinceDate) {
+ push({
+ createdAt: {
+ $gt: new Date(sinceDate)
+ }
+ });
+ }
+
+ if (untilDate) {
+ push({
+ createdAt: {
+ $lt: new Date(untilDate)
+ }
+ });
+ }
+
+ if (q.$and.length == 0) {
+ q = {};
+ }
+
+ // Search notes
+ const notes = await Note
+ .find(q, {
+ sort: {
+ _id: -1
+ },
+ limit: max,
+ skip: offset
+ });
+
+ // Serialize
+ res(await Promise.all(notes.map(note => pack(note, me))));
+}
diff --git a/src/server/api/endpoints/notes/timeline.ts b/src/server/api/endpoints/notes/timeline.ts
index 9f32555649..f1d741d5ee 100644
--- a/src/server/api/endpoints/notes/timeline.ts
+++ b/src/server/api/endpoints/notes/timeline.ts
@@ -44,6 +44,10 @@ module.exports = async (params, user, app) => {
const [includeRenotedMyNotes = true, includeRenotedMyNotesErr] = $.bool.optional().get(params.includeRenotedMyNotes);
if (includeRenotedMyNotesErr) throw 'invalid includeRenotedMyNotes param';
+ // Get 'mediaOnly' parameter
+ const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly);
+ if (mediaOnlyErr) throw 'invalid mediaOnly param';
+
const [followings, mutedUserIds] = await Promise.all([
// フォローを取得
// Fetch following
@@ -137,6 +141,12 @@ module.exports = async (params, user, app) => {
});
}
+ if (mediaOnly) {
+ query.$and.push({
+ mediaIds: { $exists: true, $ne: [] }
+ });
+ }
+
if (sinceId) {
sort._id = 1;
query._id = {
diff --git a/src/server/api/endpoints/notes/user-list-timeline.ts b/src/server/api/endpoints/notes/user-list-timeline.ts
index 9f8397d679..a74a5141f9 100644
--- a/src/server/api/endpoints/notes/user-list-timeline.ts
+++ b/src/server/api/endpoints/notes/user-list-timeline.ts
@@ -44,6 +44,10 @@ module.exports = async (params, user, app) => {
const [includeRenotedMyNotes = true, includeRenotedMyNotesErr] = $.bool.optional().get(params.includeRenotedMyNotes);
if (includeRenotedMyNotesErr) throw 'invalid includeRenotedMyNotes param';
+ // Get 'mediaOnly' parameter
+ const [mediaOnly, mediaOnlyErr] = $.bool.optional().get(params.mediaOnly);
+ if (mediaOnlyErr) throw 'invalid mediaOnly param';
+
// Get 'listId' parameter
const [listId, listIdErr] = $.type(ID).get(params.listId);
if (listIdErr) throw 'invalid listId param';
@@ -146,6 +150,12 @@ module.exports = async (params, user, app) => {
});
}
+ if (mediaOnly) {
+ query.$and.push({
+ mediaIds: { $exists: true, $ne: [] }
+ });
+ }
+
if (sinceId) {
sort._id = 1;
query._id = {