diff options
Diffstat (limited to 'src/server/api/endpoints')
26 files changed, 772 insertions, 236 deletions
diff --git a/src/server/api/endpoints/following/create.ts b/src/server/api/endpoints/following/create.ts index 766a8c03d0..b9610658d1 100644 --- a/src/server/api/endpoints/following/create.ts +++ b/src/server/api/endpoints/following/create.ts @@ -2,7 +2,7 @@ * Module dependencies */ import $ from 'cafy'; import ID from '../../../../cafy-id'; -import User from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Following from '../../../../models/following'; import create from '../../../../services/following/create'; @@ -49,5 +49,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => { create(follower, followee); // Send response - res(); + res(await pack(followee._id, user)); }); diff --git a/src/server/api/endpoints/following/delete.ts b/src/server/api/endpoints/following/delete.ts index 396b19a6f6..4fcdaf5a82 100644 --- a/src/server/api/endpoints/following/delete.ts +++ b/src/server/api/endpoints/following/delete.ts @@ -2,7 +2,7 @@ * Module dependencies */ import $ from 'cafy'; import ID from '../../../../cafy-id'; -import User from '../../../../models/user'; +import User, { pack } from '../../../../models/user'; import Following from '../../../../models/following'; import deleteFollowing from '../../../../services/following/delete'; @@ -49,5 +49,5 @@ module.exports = (params, user) => new Promise(async (res, rej) => { deleteFollowing(follower, followee); // Send response - res(); + res(await pack(followee._id, user)); }); diff --git a/src/server/api/endpoints/following/requests/accept.ts b/src/server/api/endpoints/following/requests/accept.ts new file mode 100644 index 0000000000..705d3b161a --- /dev/null +++ b/src/server/api/endpoints/following/requests/accept.ts @@ -0,0 +1,26 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import acceptFollowRequest from '../../../../../services/following/requests/accept'; +import User from '../../../../../models/user'; + +/** + * Accept a follow request + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'userId' parameter + const [followerId, followerIdErr] = $.type(ID).get(params.userId); + if (followerIdErr) return rej('invalid userId param'); + + // Fetch follower + const follower = await User.findOne({ + _id: followerId + }); + + if (follower === null) { + return rej('follower not found'); + } + + await acceptFollowRequest(user, follower); + + // Send response + res(); +}); diff --git a/src/server/api/endpoints/following/requests/cancel.ts b/src/server/api/endpoints/following/requests/cancel.ts new file mode 100644 index 0000000000..388a54890b --- /dev/null +++ b/src/server/api/endpoints/following/requests/cancel.ts @@ -0,0 +1,26 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import cancelFollowRequest from '../../../../../services/following/requests/cancel'; +import User, { pack } from '../../../../../models/user'; + +/** + * Cancel a follow request + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'userId' parameter + const [followeeId, followeeIdErr] = $.type(ID).get(params.userId); + if (followeeIdErr) return rej('invalid userId param'); + + // Fetch followee + const followee = await User.findOne({ + _id: followeeId + }); + + if (followee === null) { + return rej('followee not found'); + } + + await cancelFollowRequest(followee, user); + + // Send response + res(await pack(followee._id, user)); +}); diff --git a/src/server/api/endpoints/following/requests/list.ts b/src/server/api/endpoints/following/requests/list.ts new file mode 100644 index 0000000000..e8364335d1 --- /dev/null +++ b/src/server/api/endpoints/following/requests/list.ts @@ -0,0 +1,14 @@ +//import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import FollowRequest, { pack } from '../../../../../models/follow-request'; + +/** + * Get all pending received follow requests + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + const reqs = await FollowRequest.find({ + followeeId: user._id + }); + + // Send response + res(await Promise.all(reqs.map(req => pack(req)))); +}); diff --git a/src/server/api/endpoints/following/requests/reject.ts b/src/server/api/endpoints/following/requests/reject.ts new file mode 100644 index 0000000000..1cfb562b55 --- /dev/null +++ b/src/server/api/endpoints/following/requests/reject.ts @@ -0,0 +1,26 @@ +import $ from 'cafy'; import ID from '../../../../../cafy-id'; +import rejectFollowRequest from '../../../../../services/following/requests/reject'; +import User from '../../../../../models/user'; + +/** + * Reject a follow request + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + // Get 'userId' parameter + const [followerId, followerIdErr] = $.type(ID).get(params.userId); + if (followerIdErr) return rej('invalid userId param'); + + // Fetch follower + const follower = await User.findOne({ + _id: followerId + }); + + if (follower === null) { + return rej('follower not found'); + } + + await rejectFollowRequest(user, follower); + + // Send response + res(); +}); diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts new file mode 100644 index 0000000000..07317d1db2 --- /dev/null +++ b/src/server/api/endpoints/hashtags/trend.ts @@ -0,0 +1,87 @@ +import Note from '../../../../models/note'; + +/** + * Get trends of hashtags + */ +module.exports = (params, user) => new Promise(async (res, rej) => { + const data = await Note.aggregate([{ + $match: { + createdAt: { + $gt: new Date(Date.now() - 1000 * 60 * 60) + }, + tags: { + $exists: true, + $ne: [] + } + } + }, { + $unwind: '$tags' + }, { + $group: { + _id: { tags: '$tags', userId: '$userId' } + } + }]) as Array<{ + _id: { + tags: string; + userId: any; + } + }>; + + if (data.length == 0) { + return res([]); + } + + const tags = []; + + data.map(x => x._id).forEach(x => { + const i = tags.findIndex(tag => tag.name == x.tags); + if (i != -1) { + tags[i].count++; + } else { + tags.push({ + name: x.tags, + count: 1 + }); + } + }); + + const hots = tags + .sort((a, b) => b.count - a.count) + .map(tag => tag.name) + .slice(0, 5); + + const countPromises: Array<Promise<any[]>> = []; + + const range = 20; + + // 10分 + const interval = 1000 * 60 * 10; + + for (let i = 0; i < range; i++) { + + countPromises.push(Promise.all(hots.map(tag => Note.distinct('userId', { + tags: tag, + createdAt: { + $lt: new Date(Date.now() - (interval * i)), + $gt: new Date(Date.now() - (interval * (i + 1))) + } + })))); + } + + const countsLog = await Promise.all(countPromises); + + const totalCounts: any = await Promise.all(hots.map(tag => Note.distinct('userId', { + tags: tag, + createdAt: { + $gt: new Date(Date.now() - (interval * range)) + } + }))); + + const stats = hots.map((tag, i) => ({ + tag, + chart: countsLog.map(counts => counts[i].length), + usersCount: totalCounts[i].length + })); + + res(stats); +}); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 6e0c5b8515..1a1da997c9 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -5,6 +5,7 @@ import $ from 'cafy'; import ID from '../../../../cafy-id'; import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user'; import event from '../../../../publishers/stream'; import DriveFile from '../../../../models/drive-file'; +import acceptAllFollowRequests from '../../../../services/following/requests/accept-all'; /** * Update myself @@ -12,50 +13,62 @@ import DriveFile from '../../../../models/drive-file'; module.exports = async (params, user, app) => new Promise(async (res, rej) => { const isSecure = user != null && app == null; + const updates = {} as any; + // Get 'name' parameter const [name, nameErr] = $.str.optional().nullable().pipe(isValidName).get(params.name); if (nameErr) return rej('invalid name param'); - if (name) user.name = name; + if (name) updates.name = name; // Get 'description' parameter const [description, descriptionErr] = $.str.optional().nullable().pipe(isValidDescription).get(params.description); if (descriptionErr) return rej('invalid description param'); - if (description !== undefined) user.description = description; + if (description !== undefined) updates.description = description; // Get 'location' parameter const [location, locationErr] = $.str.optional().nullable().pipe(isValidLocation).get(params.location); if (locationErr) return rej('invalid location param'); - if (location !== undefined) user.profile.location = location; + if (location !== undefined) updates['profile.location'] = location; // Get 'birthday' parameter const [birthday, birthdayErr] = $.str.optional().nullable().pipe(isValidBirthday).get(params.birthday); if (birthdayErr) return rej('invalid birthday param'); - if (birthday !== undefined) user.profile.birthday = birthday; + if (birthday !== undefined) updates['profile.birthday'] = birthday; // Get 'avatarId' parameter - const [avatarId, avatarIdErr] = $.type(ID).optional().get(params.avatarId); + const [avatarId, avatarIdErr] = $.type(ID).optional().nullable().get(params.avatarId); if (avatarIdErr) return rej('invalid avatarId param'); - if (avatarId) user.avatarId = avatarId; + if (avatarId !== undefined) updates.avatarId = avatarId; // Get 'bannerId' parameter - const [bannerId, bannerIdErr] = $.type(ID).optional().get(params.bannerId); + const [bannerId, bannerIdErr] = $.type(ID).optional().nullable().get(params.bannerId); if (bannerIdErr) return rej('invalid bannerId param'); - if (bannerId) user.bannerId = bannerId; + if (bannerId !== undefined) updates.bannerId = bannerId; + + // Get 'wallpaperId' parameter + const [wallpaperId, wallpaperIdErr] = $.type(ID).optional().nullable().get(params.wallpaperId); + if (wallpaperIdErr) return rej('invalid wallpaperId param'); + if (wallpaperId !== undefined) updates.wallpaperId = wallpaperId; + + // Get 'isLocked' parameter + const [isLocked, isLockedErr] = $.bool.optional().get(params.isLocked); + if (isLockedErr) return rej('invalid isLocked param'); + if (isLocked != null) updates.isLocked = isLocked; // Get 'isBot' parameter const [isBot, isBotErr] = $.bool.optional().get(params.isBot); if (isBotErr) return rej('invalid isBot param'); - if (isBot != null) user.isBot = isBot; + if (isBot != null) updates.isBot = isBot; // Get 'isCat' parameter const [isCat, isCatErr] = $.bool.optional().get(params.isCat); if (isCatErr) return rej('invalid isCat param'); - if (isCat != null) user.isCat = isCat; + if (isCat != null) updates.isCat = isCat; // Get 'autoWatch' parameter const [autoWatch, autoWatchErr] = $.bool.optional().get(params.autoWatch); if (autoWatchErr) return rej('invalid autoWatch param'); - if (autoWatch != null) user.settings.autoWatch = autoWatch; + if (autoWatch != null) updates['settings.autoWatch'] = autoWatch; if (avatarId) { const avatar = await DriveFile.findOne({ @@ -63,7 +76,7 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => { }); if (avatar != null && avatar.metadata.properties.avgColor) { - user.avatarColor = avatar.metadata.properties.avgColor; + updates.avatarColor = avatar.metadata.properties.avgColor; } } @@ -73,27 +86,26 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => { }); if (banner != null && banner.metadata.properties.avgColor) { - user.bannerColor = banner.metadata.properties.avgColor; + updates.bannerColor = banner.metadata.properties.avgColor; } } - await User.update(user._id, { - $set: { - name: user.name, - description: user.description, - avatarId: user.avatarId, - avatarColor: user.avatarColor, - bannerId: user.bannerId, - bannerColor: user.bannerColor, - profile: user.profile, - isBot: user.isBot, - isCat: user.isCat, - settings: user.settings + if (wallpaperId) { + const wallpaper = await DriveFile.findOne({ + _id: wallpaperId + }); + + if (wallpaper != null && wallpaper.metadata.properties.avgColor) { + updates.wallpaperColor = wallpaper.metadata.properties.avgColor; } + } + + await User.update(user._id, { + $set: updates }); // Serialize - const iObj = await pack(user, user, { + const iObj = await pack(user._id, user, { detail: true, includeSecrets: isSecure }); @@ -101,6 +113,11 @@ module.exports = async (params, user, app) => new Promise(async (res, rej) => { // Send response res(iObj); - // Publish i updated event - event(user._id, 'i_updated', iObj); + // Publish meUpdated event + event(user._id, 'meUpdated', iObj); + + // 鍵垢を解除したとき、溜まっていたフォローリクエストがあるならすべて承認 + if (user.isLocked && isLocked === false) { + acceptAllFollowRequests(user); + } }); diff --git a/src/server/api/endpoints/i/update_home.ts b/src/server/api/endpoints/i/update_home.ts index 8ce551957e..48f6dbbb7a 100644 --- a/src/server/api/endpoints/i/update_home.ts +++ b/src/server/api/endpoints/i/update_home.ts @@ -1,6 +1,3 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import User from '../../../../models/user'; import event from '../../../../publishers/stream'; @@ -13,50 +10,16 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { .have('id', $.str) .have('place', $.str) .have('data', $.obj)) - .optional() .get(params.home); if (homeErr) return rej('invalid home param'); - // Get 'id' parameter - const [id, idErr] = $.str.optional().get(params.id); - if (idErr) return rej('invalid id param'); + await User.update(user._id, { + $set: { + 'clientSettings.home': home + } + }); - // Get 'data' parameter - const [data, dataErr] = $.obj.optional().get(params.data); - if (dataErr) return rej('invalid data param'); + res(); - if (home) { - await User.update(user._id, { - $set: { - 'clientSettings.home': home - } - }); - - res(); - - event(user._id, 'home_updated', { - home - }); - } else { - if (id == null && data == null) return rej('you need to set id and data params if home param unset'); - - const _home = user.clientSettings.home; - const widget = _home.find(w => w.id == id); - - if (widget == null) return rej('widget not found'); - - widget.data = data; - - await User.update(user._id, { - $set: { - 'clientSettings.home': _home - } - }); - - res(); - - event(user._id, 'home_updated', { - id, data - }); - } + event(user._id, 'home_updated', home); }); diff --git a/src/server/api/endpoints/i/update_mobile_home.ts b/src/server/api/endpoints/i/update_mobile_home.ts index d79a77072b..d285a0a72d 100644 --- a/src/server/api/endpoints/i/update_mobile_home.ts +++ b/src/server/api/endpoints/i/update_mobile_home.ts @@ -1,6 +1,3 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import User from '../../../../models/user'; import event from '../../../../publishers/stream'; @@ -12,49 +9,16 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { .have('name', $.str) .have('id', $.str) .have('data', $.obj)) - .optional().get(params.home); + .get(params.home); if (homeErr) return rej('invalid home param'); - // Get 'id' parameter - const [id, idErr] = $.str.optional().get(params.id); - if (idErr) return rej('invalid id param'); + await User.update(user._id, { + $set: { + 'clientSettings.mobileHome': home + } + }); - // Get 'data' parameter - const [data, dataErr] = $.obj.optional().get(params.data); - if (dataErr) return rej('invalid data param'); + res(); - if (home) { - await User.update(user._id, { - $set: { - 'clientSettings.mobileHome': home - } - }); - - res(); - - event(user._id, 'mobile_home_updated', { - home - }); - } else { - if (id == null && data == null) return rej('you need to set id and data params if home param unset'); - - const _home = user.clientSettings.mobileHome || []; - const widget = _home.find(w => w.id == id); - - if (widget == null) return rej('widget not found'); - - widget.data = data; - - await User.update(user._id, { - $set: { - 'clientSettings.mobileHome': _home - } - }); - - res(); - - event(user._id, 'mobile_home_updated', { - id, data - }); - } + event(user._id, 'mobile_home_updated', home); }); diff --git a/src/server/api/endpoints/i/update_widget.ts b/src/server/api/endpoints/i/update_widget.ts new file mode 100644 index 0000000000..b37761bde1 --- /dev/null +++ b/src/server/api/endpoints/i/update_widget.ts @@ -0,0 +1,79 @@ +import $ from 'cafy'; +import User from '../../../../models/user'; +import event from '../../../../publishers/stream'; + +module.exports = async (params, user) => new Promise(async (res, rej) => { + // Get 'id' parameter + const [id, idErr] = $.str.get(params.id); + if (idErr) return rej('invalid id param'); + + // Get 'data' parameter + const [data, dataErr] = $.obj.get(params.data); + if (dataErr) return rej('invalid data param'); + + if (id == null && data == null) return rej('you need to set id and data params if home param unset'); + + let widget; + + //#region Desktop home + if (widget == null && user.clientSettings.home) { + const desktopHome = user.clientSettings.home; + widget = desktopHome.find(w => w.id == id); + if (widget) { + widget.data = data; + + await User.update(user._id, { + $set: { + 'clientSettings.home': desktopHome + } + }); + } + } + //#endregion + + //#region Mobile home + if (widget == null && user.clientSettings.mobileHome) { + const mobileHome = user.clientSettings.mobileHome; + widget = mobileHome.find(w => w.id == id); + if (widget) { + widget.data = data; + + await User.update(user._id, { + $set: { + 'clientSettings.mobileHome': mobileHome + } + }); + } + } + //#endregion + + //#region Deck + if (widget == null && user.clientSettings.deck && user.clientSettings.deck.columns) { + const deck = user.clientSettings.deck; + deck.columns.filter(c => c.type == 'widgets').forEach(c => { + c.widgets.forEach(w => { + if (w.id == id) widget = w; + }); + }); + if (widget) { + widget.data = data; + + await User.update(user._id, { + $set: { + 'clientSettings.deck': deck + } + }); + } + } + //#endregion + + if (widget) { + event(user._id, 'widgetUpdated', { + id, data + }); + + res(); + } else { + rej('widget not found'); + } +}); diff --git a/src/server/api/endpoints/messaging/messages.ts b/src/server/api/endpoints/messaging/messages.ts index 0338aba68a..9c3a48334b 100644 --- a/src/server/api/endpoints/messaging/messages.ts +++ b/src/server/api/endpoints/messaging/messages.ts @@ -1,6 +1,3 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Message from '../../../../models/messaging-message'; import User from '../../../../models/user'; @@ -9,10 +6,6 @@ import read from '../../common/read-messaging-message'; /** * Get messages - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'userId' parameter diff --git a/src/server/api/endpoints/messaging/messages/create.ts b/src/server/api/endpoints/messaging/messages/create.ts index db471839e7..41238de1e1 100644 --- a/src/server/api/endpoints/messaging/messages/create.ts +++ b/src/server/api/endpoints/messaging/messages/create.ts @@ -91,6 +91,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => { publishMessagingIndexStream(message.recipientId, 'message', messageObj); publishUserStream(message.recipientId, 'messaging_message', messageObj); + // Update flag + User.update({ _id: recipient._id }, { + $set: { + hasUnreadMessagingMessage: true + } + }); + // 3秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する setTimeout(async () => { const freshMessage = await Message.findOne({ _id: message._id }, { isRead: true }); diff --git a/src/server/api/endpoints/messaging/unread.ts b/src/server/api/endpoints/messaging/unread.ts deleted file mode 100644 index 1d83af501d..0000000000 --- a/src/server/api/endpoints/messaging/unread.ts +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Module dependencies - */ -import Message from '../../../../models/messaging-message'; -import Mute from '../../../../models/mute'; - -/** - * Get count of unread messages - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const mute = await Mute.find({ - muterId: user._id, - deletedAt: { $exists: false } - }); - const mutedUserIds = mute.map(m => m.muteeId); - - const count = await Message - .count({ - userId: { - $nin: mutedUserIds - }, - recipientId: user._id, - isRead: false - }); - - res({ - count: count - }); -}); diff --git a/src/server/api/endpoints/notes.ts b/src/server/api/endpoints/notes.ts index 21946d1bd3..e6fe80ac8a 100644 --- a/src/server/api/endpoints/notes.ts +++ b/src/server/api/endpoints/notes.ts @@ -53,7 +53,9 @@ module.exports = (params) => new Promise(async (res, rej) => { const sort = { _id: -1 }; - const query = {} as any; + const query = { + visibility: 'public' + } as any; if (sinceId) { sort._id = 1; query._id = { 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 = { diff --git a/src/server/api/endpoints/notifications/get_unread_count.ts b/src/server/api/endpoints/notifications/get_unread_count.ts deleted file mode 100644 index 9766366ff1..0000000000 --- a/src/server/api/endpoints/notifications/get_unread_count.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Module dependencies - */ -import Notification from '../../../../models/notification'; -import Mute from '../../../../models/mute'; - -/** - * Get count of unread notifications - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const mute = await Mute.find({ - muterId: user._id - }); - const mutedUserIds = mute.map(m => m.muteeId); - - const count = await Notification - .count({ - notifieeId: user._id, - notifierId: { - $nin: mutedUserIds - }, - isRead: false - }); - - res({ - count: count - }); -}); diff --git a/src/server/api/endpoints/notifications/mark_as_read_all.ts b/src/server/api/endpoints/notifications/mark_as_read_all.ts index dce3cb4663..7a48ca3e62 100644 --- a/src/server/api/endpoints/notifications/mark_as_read_all.ts +++ b/src/server/api/endpoints/notifications/mark_as_read_all.ts @@ -1,8 +1,6 @@ -/** - * Module dependencies - */ import Notification from '../../../../models/notification'; import event from '../../../../publishers/stream'; +import User from '../../../../models/user'; /** * Mark as read all notifications @@ -23,6 +21,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Response res(); + // Update flag + User.update({ _id: user._id }, { + $set: { + hasUnreadNotification: false + } + }); + // 全ての通知を読みましたよというイベントを発行 event(user._id, 'read_all_notifications'); }); diff --git a/src/server/api/endpoints/users/recommendation.ts b/src/server/api/endpoints/users/recommendation.ts index 620ae17ca2..23821a552f 100644 --- a/src/server/api/endpoints/users/recommendation.ts +++ b/src/server/api/endpoints/users/recommendation.ts @@ -36,6 +36,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => { _id: { $nin: followingIds.concat(mutedUserIds) }, + isLocked: false, $or: [{ lastUsedAt: { $gte: new Date(Date.now() - ms('7days')) |