diff options
| author | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-03-29 01:20:40 +0900 |
|---|---|---|
| committer | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-03-29 01:54:41 +0900 |
| commit | 90f8fe7e538bb7e52d2558152a0390e693f39b11 (patch) | |
| tree | 0f830887053c8f352b1cd0c13ca715fd14c1f030 /src/api/endpoints | |
| parent | Implement remote account resolution (diff) | |
| download | sharkey-90f8fe7e538bb7e52d2558152a0390e693f39b11.tar.gz sharkey-90f8fe7e538bb7e52d2558152a0390e693f39b11.tar.bz2 sharkey-90f8fe7e538bb7e52d2558152a0390e693f39b11.zip | |
Introduce processor
Diffstat (limited to 'src/api/endpoints')
102 files changed, 0 insertions, 7525 deletions
diff --git a/src/api/endpoints/aggregation/posts.ts b/src/api/endpoints/aggregation/posts.ts deleted file mode 100644 index 9d8bccbdb2..0000000000 --- a/src/api/endpoints/aggregation/posts.ts +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../models/post'; - -/** - * Aggregate posts - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = params => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$; - if (limitErr) return rej('invalid limit param'); - - const datas = await Post - .aggregate([ - { $project: { - repost_id: '$repost_id', - reply_id: '$reply_id', - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - }, - type: { - $cond: { - if: { $ne: ['$repost_id', null] }, - then: 'repost', - else: { - $cond: { - if: { $ne: ['$reply_id', null] }, - then: 'reply', - else: 'post' - } - } - } - }} - }, - { $group: { _id: { - date: '$date', - type: '$type' - }, count: { $sum: 1 } } }, - { $group: { - _id: '$_id.date', - data: { $addToSet: { - type: '$_id.type', - count: '$count' - }} - } } - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; - data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; - - delete data.data; - }); - - const graph = []; - - for (let i = 0; i < limit; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - posts: 0, - reposts: 0, - replies: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/reaction.ts b/src/api/endpoints/aggregation/posts/reaction.ts deleted file mode 100644 index eb99b9d088..0000000000 --- a/src/api/endpoints/aggregation/posts/reaction.ts +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../models/post'; -import Reaction from '../../../models/post-reaction'; - -/** - * Aggregate reaction of a post - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Reaction - .aggregate([ - { $match: { post_id: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/reactions.ts b/src/api/endpoints/aggregation/posts/reactions.ts deleted file mode 100644 index 790b523be9..0000000000 --- a/src/api/endpoints/aggregation/posts/reactions.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../models/post'; -import Reaction from '../../../models/post-reaction'; - -/** - * Aggregate reactions of a post - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const reactions = await Reaction - .find({ - post_id: post._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - sort: { - _id: -1 - }, - fields: { - _id: false, - post_id: false - } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - // day = day.getTime(); - - const count = reactions.filter(r => - r.created_at < day && (r.deleted_at == null || r.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/reply.ts b/src/api/endpoints/aggregation/posts/reply.ts deleted file mode 100644 index b114c34e1e..0000000000 --- a/src/api/endpoints/aggregation/posts/reply.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../models/post'; - -/** - * Aggregate reply of a post - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { reply: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/repost.ts b/src/api/endpoints/aggregation/posts/repost.ts deleted file mode 100644 index 217159caa7..0000000000 --- a/src/api/endpoints/aggregation/posts/repost.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../models/post'; - -/** - * Aggregate repost of a post - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { repost_id: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users.ts b/src/api/endpoints/aggregation/users.ts deleted file mode 100644 index e38ce92ff9..0000000000 --- a/src/api/endpoints/aggregation/users.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; - -/** - * Aggregate users - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = params => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$; - if (limitErr) return rej('invalid limit param'); - - const users = await User - .find({}, { - sort: { - _id: -1 - }, - fields: { - _id: false, - created_at: true, - deleted_at: true - } - }); - - const graph = []; - - for (let i = 0; i < limit; i++) { - let dayStart = new Date(new Date().setDate(new Date().getDate() - i)); - dayStart = new Date(dayStart.setMilliseconds(0)); - dayStart = new Date(dayStart.setSeconds(0)); - dayStart = new Date(dayStart.setMinutes(0)); - dayStart = new Date(dayStart.setHours(0)); - - let dayEnd = new Date(new Date().setDate(new Date().getDate() - i)); - dayEnd = new Date(dayEnd.setMilliseconds(999)); - dayEnd = new Date(dayEnd.setSeconds(59)); - dayEnd = new Date(dayEnd.setMinutes(59)); - dayEnd = new Date(dayEnd.setHours(23)); - // day = day.getTime(); - - const total = users.filter(u => - u.created_at < dayEnd && (u.deleted_at == null || u.deleted_at > dayEnd) - ).length; - - const created = users.filter(u => - u.created_at < dayEnd && u.created_at > dayStart - ).length; - - graph.push({ - total: total, - created: created - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/activity.ts b/src/api/endpoints/aggregation/users/activity.ts deleted file mode 100644 index 102a71d7cb..0000000000 --- a/src/api/endpoints/aggregation/users/activity.ts +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../../models/user'; -import Post from '../../../models/post'; - -// TODO: likeやfollowも集計 - -/** - * Aggregate activity of a user - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const datas = await Post - .aggregate([ - { $match: { user_id: user._id } }, - { $project: { - repost_id: '$repost_id', - reply_id: '$reply_id', - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - }, - type: { - $cond: { - if: { $ne: ['$repost_id', null] }, - then: 'repost', - else: { - $cond: { - if: { $ne: ['$reply_id', null] }, - then: 'reply', - else: 'post' - } - } - } - }} - }, - { $group: { _id: { - date: '$date', - type: '$type' - }, count: { $sum: 1 } } }, - { $group: { - _id: '$_id.date', - data: { $addToSet: { - type: '$_id.type', - count: '$count' - }} - } } - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; - data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; - - delete data.data; - }); - - const graph = []; - - for (let i = 0; i < limit; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - posts: 0, - reposts: 0, - replies: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/followers.ts b/src/api/endpoints/aggregation/users/followers.ts deleted file mode 100644 index 3022b2b002..0000000000 --- a/src/api/endpoints/aggregation/users/followers.ts +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../../models/user'; -import Following from '../../../models/following'; - -/** - * Aggregate followers of a user - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const following = await Following - .find({ - followee_id: user._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - _id: false, - follower_id: false, - followee_id: false - }, { - sort: { created_at: -1 } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - // day = day.getTime(); - - const count = following.filter(f => - f.created_at < day && (f.deleted_at == null || f.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/following.ts b/src/api/endpoints/aggregation/users/following.ts deleted file mode 100644 index 92da7e6921..0000000000 --- a/src/api/endpoints/aggregation/users/following.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../../models/user'; -import Following from '../../../models/following'; - -/** - * Aggregate following of a user - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const following = await Following - .find({ - follower_id: user._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - _id: false, - follower_id: false, - followee_id: false - }, { - sort: { created_at: -1 } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - - const count = following.filter(f => - f.created_at < day && (f.deleted_at == null || f.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/post.ts b/src/api/endpoints/aggregation/users/post.ts deleted file mode 100644 index c6a75eee39..0000000000 --- a/src/api/endpoints/aggregation/users/post.ts +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../../models/user'; -import Post from '../../../models/post'; - -/** - * Aggregate post of a user - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const datas = await Post - .aggregate([ - { $match: { user_id: user._id } }, - { $project: { - repost_id: '$repost_id', - reply_id: '$reply_id', - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - }, - type: { - $cond: { - if: { $ne: ['$repost_id', null] }, - then: 'repost', - else: { - $cond: { - if: { $ne: ['$reply_id', null] }, - then: 'reply', - else: 'post' - } - } - } - }} - }, - { $group: { _id: { - date: '$date', - type: '$type' - }, count: { $sum: 1 } } }, - { $group: { - _id: '$_id.date', - data: { $addToSet: { - type: '$_id.type', - count: '$count' - }} - } } - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; - data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; - - delete data.data; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - posts: 0, - reposts: 0, - replies: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/reaction.ts b/src/api/endpoints/aggregation/users/reaction.ts deleted file mode 100644 index 0a082ed1b7..0000000000 --- a/src/api/endpoints/aggregation/users/reaction.ts +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../../models/user'; -import Reaction from '../../../models/post-reaction'; - -/** - * Aggregate reaction of a user - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const datas = await Reaction - .aggregate([ - { $match: { user_id: user._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }); - } - } - - res(graph); -}); diff --git a/src/api/endpoints/app/create.ts b/src/api/endpoints/app/create.ts deleted file mode 100644 index 0f688792a7..0000000000 --- a/src/api/endpoints/app/create.ts +++ /dev/null @@ -1,108 +0,0 @@ -/** - * Module dependencies - */ -import rndstr from 'rndstr'; -import $ from 'cafy'; -import App, { isValidNameId, pack } from '../../models/app'; - -/** - * @swagger - * /app/create: - * post: - * summary: Create an application - * parameters: - * - $ref: "#/parameters/AccessToken" - * - - * name: name_id - * description: Application unique name - * in: formData - * required: true - * type: string - * - - * name: name - * description: Application name - * in: formData - * required: true - * type: string - * - - * name: description - * description: Application description - * in: formData - * required: true - * type: string - * - - * name: permission - * description: Permissions that application has - * in: formData - * required: true - * type: array - * items: - * type: string - * collectionFormat: csv - * - - * name: callback_url - * description: URL called back after authentication - * in: formData - * required: false - * type: string - * - * responses: - * 200: - * description: Created application's information - * schema: - * $ref: "#/definitions/Application" - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Create an app - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'name_id' parameter - const [nameId, nameIdErr] = $(params.name_id).string().pipe(isValidNameId).$; - if (nameIdErr) return rej('invalid name_id param'); - - // Get 'name' parameter - const [name, nameErr] = $(params.name).string().$; - if (nameErr) return rej('invalid name param'); - - // Get 'description' parameter - const [description, descriptionErr] = $(params.description).string().$; - if (descriptionErr) return rej('invalid description param'); - - // Get 'permission' parameter - const [permission, permissionErr] = $(params.permission).array('string').unique().$; - if (permissionErr) return rej('invalid permission param'); - - // Get 'callback_url' parameter - // TODO: Check it is valid url - const [callbackUrl = null, callbackUrlErr] = $(params.callback_url).optional.nullable.string().$; - if (callbackUrlErr) return rej('invalid callback_url param'); - - // Generate secret - const secret = rndstr('a-zA-Z0-9', 32); - - // Create account - const app = await App.insert({ - created_at: new Date(), - user_id: user._id, - name: name, - name_id: nameId, - name_id_lower: nameId.toLowerCase(), - description: description, - permission: permission, - callback_url: callbackUrl, - secret: secret - }); - - // Response - res(await pack(app)); -}); diff --git a/src/api/endpoints/app/name_id/available.ts b/src/api/endpoints/app/name_id/available.ts deleted file mode 100644 index 3d2c710322..0000000000 --- a/src/api/endpoints/app/name_id/available.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import App from '../../../models/app'; -import { isValidNameId } from '../../../models/app'; - -/** - * @swagger - * /app/name_id/available: - * post: - * summary: Check available name_id on creation an application - * parameters: - * - - * name: name_id - * description: Application unique name - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * available: - * description: Whether name_id is available - * type: boolean - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Check available name_id of app - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = async (params) => new Promise(async (res, rej) => { - // Get 'name_id' parameter - const [nameId, nameIdErr] = $(params.name_id).string().pipe(isValidNameId).$; - if (nameIdErr) return rej('invalid name_id param'); - - // Get exist - const exist = await App - .count({ - name_id_lower: nameId.toLowerCase() - }, { - limit: 1 - }); - - // Reply - res({ - available: exist === 0 - }); -}); diff --git a/src/api/endpoints/app/show.ts b/src/api/endpoints/app/show.ts deleted file mode 100644 index 8bc3dda42c..0000000000 --- a/src/api/endpoints/app/show.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import App, { pack } from '../../models/app'; - -/** - * @swagger - * /app/show: - * post: - * summary: Show an application's information - * description: Require app_id or name_id - * parameters: - * - - * name: app_id - * description: Application ID - * in: formData - * type: string - * - - * name: name_id - * description: Application unique name - * in: formData - * type: string - * - * responses: - * 200: - * description: Success - * schema: - * $ref: "#/definitions/Application" - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show an app - * - * @param {any} params - * @param {any} user - * @param {any} _ - * @param {any} isSecure - * @return {Promise<any>} - */ -module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => { - // Get 'app_id' parameter - const [appId, appIdErr] = $(params.app_id).optional.id().$; - if (appIdErr) return rej('invalid app_id param'); - - // Get 'name_id' parameter - const [nameId, nameIdErr] = $(params.name_id).optional.string().$; - if (nameIdErr) return rej('invalid name_id param'); - - if (appId === undefined && nameId === undefined) { - return rej('app_id or name_id is required'); - } - - // Lookup app - const app = appId !== undefined - ? await App.findOne({ _id: appId }) - : await App.findOne({ name_id_lower: nameId.toLowerCase() }); - - if (app === null) { - return rej('app not found'); - } - - // Send response - res(await pack(app, user, { - includeSecret: isSecure && app.user_id.equals(user._id) - })); -}); diff --git a/src/api/endpoints/auth/accept.ts b/src/api/endpoints/auth/accept.ts deleted file mode 100644 index 4ee20a6d25..0000000000 --- a/src/api/endpoints/auth/accept.ts +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Module dependencies - */ -import rndstr from 'rndstr'; -const crypto = require('crypto'); -import $ from 'cafy'; -import App from '../../models/app'; -import AuthSess from '../../models/auth-session'; -import AccessToken from '../../models/access-token'; - -/** - * @swagger - * /auth/accept: - * post: - * summary: Accept a session - * parameters: - * - $ref: "#/parameters/NativeToken" - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * responses: - * 204: - * description: OK - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Accept - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'token' parameter - const [token, tokenErr] = $(params.token).string().$; - if (tokenErr) return rej('invalid token param'); - - // Fetch token - const session = await AuthSess - .findOne({ token: token }); - - if (session === null) { - return rej('session not found'); - } - - // Generate access token - const accessToken = rndstr('a-zA-Z0-9', 32); - - // Fetch exist access token - const exist = await AccessToken.findOne({ - app_id: session.app_id, - user_id: user._id, - }); - - if (exist === null) { - // Lookup app - const app = await App.findOne({ - _id: session.app_id - }); - - // Generate Hash - const sha256 = crypto.createHash('sha256'); - sha256.update(accessToken + app.secret); - const hash = sha256.digest('hex'); - - // Insert access token doc - await AccessToken.insert({ - created_at: new Date(), - app_id: session.app_id, - user_id: user._id, - token: accessToken, - hash: hash - }); - } - - // Update session - await AuthSess.update(session._id, { - $set: { - user_id: user._id - } - }); - - // Response - res(); -}); diff --git a/src/api/endpoints/auth/session/generate.ts b/src/api/endpoints/auth/session/generate.ts deleted file mode 100644 index 510382247e..0000000000 --- a/src/api/endpoints/auth/session/generate.ts +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Module dependencies - */ -import * as uuid from 'uuid'; -import $ from 'cafy'; -import App from '../../../models/app'; -import AuthSess from '../../../models/auth-session'; -import config from '../../../../conf'; - -/** - * @swagger - * /auth/session/generate: - * post: - * summary: Generate a session - * parameters: - * - - * name: app_secret - * description: App Secret - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * token: - * type: string - * description: Session Token - * url: - * type: string - * description: Authentication form's URL - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Generate a session - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'app_secret' parameter - const [appSecret, appSecretErr] = $(params.app_secret).string().$; - if (appSecretErr) return rej('invalid app_secret param'); - - // Lookup app - const app = await App.findOne({ - secret: appSecret - }); - - if (app == null) { - return rej('app not found'); - } - - // Generate token - const token = uuid.v4(); - - // Create session token document - const doc = await AuthSess.insert({ - created_at: new Date(), - app_id: app._id, - token: token - }); - - // Response - res({ - token: doc.token, - url: `${config.auth_url}/${doc.token}` - }); -}); diff --git a/src/api/endpoints/auth/session/show.ts b/src/api/endpoints/auth/session/show.ts deleted file mode 100644 index 73ac3185f6..0000000000 --- a/src/api/endpoints/auth/session/show.ts +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import AuthSess, { pack } from '../../../models/auth-session'; - -/** - * @swagger - * /auth/session/show: - * post: - * summary: Show a session information - * parameters: - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * created_at: - * type: string - * format: date-time - * description: Date and time of the session creation - * app_id: - * type: string - * description: Application ID - * token: - * type: string - * description: Session Token - * user_id: - * type: string - * description: ID of user who create the session - * app: - * $ref: "#/definitions/Application" - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show a session - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'token' parameter - const [token, tokenErr] = $(params.token).string().$; - if (tokenErr) return rej('invalid token param'); - - // Lookup session - const session = await AuthSess.findOne({ - token: token - }); - - if (session == null) { - return rej('session not found'); - } - - // Response - res(await pack(session, user)); -}); diff --git a/src/api/endpoints/auth/session/userkey.ts b/src/api/endpoints/auth/session/userkey.ts deleted file mode 100644 index fc989bf8c2..0000000000 --- a/src/api/endpoints/auth/session/userkey.ts +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import App from '../../../models/app'; -import AuthSess from '../../../models/auth-session'; -import AccessToken from '../../../models/access-token'; -import { pack } from '../../../models/user'; - -/** - * @swagger - * /auth/session/userkey: - * post: - * summary: Get an access token(userkey) - * parameters: - * - - * name: app_secret - * description: App Secret - * in: formData - * required: true - * type: string - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * userkey: - * type: string - * description: Access Token - * user: - * $ref: "#/definitions/User" - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Generate a session - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'app_secret' parameter - const [appSecret, appSecretErr] = $(params.app_secret).string().$; - if (appSecretErr) return rej('invalid app_secret param'); - - // Lookup app - const app = await App.findOne({ - secret: appSecret - }); - - if (app == null) { - return rej('app not found'); - } - - // Get 'token' parameter - const [token, tokenErr] = $(params.token).string().$; - if (tokenErr) return rej('invalid token param'); - - // Fetch token - const session = await AuthSess - .findOne({ - token: token, - app_id: app._id - }); - - if (session === null) { - return rej('session not found'); - } - - if (session.user_id == null) { - return rej('this session is not allowed yet'); - } - - // Lookup access token - const accessToken = await AccessToken.findOne({ - app_id: app._id, - user_id: session.user_id - }); - - // Delete session - - /* https://github.com/Automattic/monk/issues/178 - AuthSess.deleteOne({ - _id: session._id - }); - */ - AuthSess.remove({ - _id: session._id - }); - - // Response - res({ - access_token: accessToken.token, - user: await pack(session.user_id, null, { - detail: true - }) - }); -}); diff --git a/src/api/endpoints/channels.ts b/src/api/endpoints/channels.ts deleted file mode 100644 index b9a7d1b788..0000000000 --- a/src/api/endpoints/channels.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel, { pack } from '../models/channel'; - -/** - * Get all channels - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = {} as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const channels = await Channel - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(channels.map(async channel => - await pack(channel, me)))); -}); diff --git a/src/api/endpoints/channels/create.ts b/src/api/endpoints/channels/create.ts deleted file mode 100644 index 695b4515b3..0000000000 --- a/src/api/endpoints/channels/create.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel from '../../models/channel'; -import Watching from '../../models/channel-watching'; -import { pack } from '../../models/channel'; - -/** - * Create a channel - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'title' parameter - const [title, titleErr] = $(params.title).string().range(1, 100).$; - if (titleErr) return rej('invalid title param'); - - // Create a channel - const channel = await Channel.insert({ - created_at: new Date(), - user_id: user._id, - title: title, - index: 0, - watching_count: 1 - }); - - // Response - res(await pack(channel)); - - // Create Watching - await Watching.insert({ - created_at: new Date(), - user_id: user._id, - channel_id: channel._id - }); -}); diff --git a/src/api/endpoints/channels/posts.ts b/src/api/endpoints/channels/posts.ts deleted file mode 100644 index d722589c20..0000000000 --- a/src/api/endpoints/channels/posts.ts +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import { default as Channel, IChannel } from '../../models/channel'; -import Post, { pack } from '../../models/post'; - -/** - * Show a posts of a channel - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 1000, limitErr] = $(params.limit).optional.number().range(1, 1000).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - // Fetch channel - const channel: IChannel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - - //#region Construct query - const sort = { - _id: -1 - }; - - const query = { - channel_id: channel._id - } as any; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - //#endregion Construct query - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async (post) => - await pack(post, user) - ))); -}); diff --git a/src/api/endpoints/channels/show.ts b/src/api/endpoints/channels/show.ts deleted file mode 100644 index 332da64675..0000000000 --- a/src/api/endpoints/channels/show.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel, { IChannel, pack } from '../../models/channel'; - -/** - * Show a channel - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - // Fetch channel - const channel: IChannel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - - // Serialize - res(await pack(channel, user)); -}); diff --git a/src/api/endpoints/channels/unwatch.ts b/src/api/endpoints/channels/unwatch.ts deleted file mode 100644 index 19d3be118a..0000000000 --- a/src/api/endpoints/channels/unwatch.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel from '../../models/channel'; -import Watching from '../../models/channel-watching'; - -/** - * Unwatch a channel - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - //#region Fetch channel - const channel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - //#endregion - - //#region Check whether not watching - const exist = await Watching.findOne({ - user_id: user._id, - channel_id: channel._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not watching'); - } - //#endregion - - // Delete watching - await Watching.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - // Decrement watching count - Channel.update(channel._id, { - $inc: { - watching_count: -1 - } - }); -}); diff --git a/src/api/endpoints/channels/watch.ts b/src/api/endpoints/channels/watch.ts deleted file mode 100644 index 030e0dd411..0000000000 --- a/src/api/endpoints/channels/watch.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel from '../../models/channel'; -import Watching from '../../models/channel-watching'; - -/** - * Watch a channel - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - //#region Fetch channel - const channel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - //#endregion - - //#region Check whether already watching - const exist = await Watching.findOne({ - user_id: user._id, - channel_id: channel._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already watching'); - } - //#endregion - - // Create Watching - await Watching.insert({ - created_at: new Date(), - user_id: user._id, - channel_id: channel._id - }); - - // Send response - res(); - - // Increment watching count - Channel.update(channel._id, { - $inc: { - watching_count: 1 - } - }); -}); diff --git a/src/api/endpoints/drive.ts b/src/api/endpoints/drive.ts deleted file mode 100644 index d92473633a..0000000000 --- a/src/api/endpoints/drive.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Module dependencies - */ -import DriveFile from '../models/drive-file'; - -/** - * Get drive information - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Calculate drive usage - const usage = ((await DriveFile - .aggregate([ - { $match: { 'metadata.user_id': user._id } }, - { - $project: { - length: true - } - }, - { - $group: { - _id: null, - usage: { $sum: '$length' } - } - } - ]))[0] || { - usage: 0 - }).usage; - - res({ - capacity: user.drive_capacity, - usage: usage - }); -}); diff --git a/src/api/endpoints/drive/files.ts b/src/api/endpoints/drive/files.ts deleted file mode 100644 index 89915331ea..0000000000 --- a/src/api/endpoints/drive/files.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFile, { pack } from '../../models/drive-file'; - -/** - * Get drive files - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise<any>} - */ -module.exports = async (params, user, app) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) throw 'invalid limit param'; - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) throw 'invalid since_id param'; - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) throw 'invalid until_id param'; - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - throw 'cannot set since_id and until_id'; - } - - // Get 'folder_id' parameter - const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) throw 'invalid folder_id param'; - - // Get 'type' parameter - const [type, typeErr] = $(params.type).optional.string().match(/^[a-zA-Z\/\-\*]+$/).$; - if (typeErr) throw 'invalid type param'; - - // Construct query - const sort = { - _id: -1 - }; - const query = { - 'metadata.user_id': user._id, - 'metadata.folder_id': folderId - } as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - if (type) { - query.contentType = new RegExp(`^${type.replace(/\*/g, '.+?')}$`); - } - - // Issue query - const files = await DriveFile - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - const _files = await Promise.all(files.map(file => pack(file))); - return _files; -}; diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts deleted file mode 100644 index db801b61fe..0000000000 --- a/src/api/endpoints/drive/files/create.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import { validateFileName, pack } from '../../../models/drive-file'; -import create from '../../../common/drive/add-file'; - -/** - * Create a file - * - * @param {any} file - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (file, params, user): Promise<any> => { - if (file == null) { - throw 'file is required'; - } - - // Get 'name' parameter - let name = file.originalname; - if (name !== undefined && name !== null) { - name = name.trim(); - if (name.length === 0) { - name = null; - } else if (name === 'blob') { - name = null; - } else if (!validateFileName(name)) { - throw 'invalid name'; - } - } else { - name = null; - } - - // Get 'folder_id' parameter - const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) throw 'invalid folder_id param'; - - try { - // Create file - const driveFile = await create(user, file.path, name, null, folderId); - - // Serialize - return pack(driveFile); - } catch (e) { - console.error(e); - - throw e; - } -}; diff --git a/src/api/endpoints/drive/files/find.ts b/src/api/endpoints/drive/files/find.ts deleted file mode 100644 index e026afe936..0000000000 --- a/src/api/endpoints/drive/files/find.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFile, { pack } from '../../../models/drive-file'; - -/** - * Find a file(s) - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name, nameErr] = $(params.name).string().$; - if (nameErr) return rej('invalid name param'); - - // Get 'folder_id' parameter - const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) return rej('invalid folder_id param'); - - // Issue query - const files = await DriveFile - .find({ - filename: name, - 'metadata.user_id': user._id, - 'metadata.folder_id': folderId - }); - - // Serialize - res(await Promise.all(files.map(async file => - await pack(file)))); -}); diff --git a/src/api/endpoints/drive/files/show.ts b/src/api/endpoints/drive/files/show.ts deleted file mode 100644 index 21664f7ba4..0000000000 --- a/src/api/endpoints/drive/files/show.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFile, { pack } from '../../../models/drive-file'; - -/** - * Show a file - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => { - // Get 'file_id' parameter - const [fileId, fileIdErr] = $(params.file_id).id().$; - if (fileIdErr) throw 'invalid file_id param'; - - // Fetch file - const file = await DriveFile - .findOne({ - _id: fileId, - 'metadata.user_id': user._id - }); - - if (file === null) { - throw 'file-not-found'; - } - - // Serialize - const _file = await pack(file, { - detail: true - }); - - return _file; -}; diff --git a/src/api/endpoints/drive/files/update.ts b/src/api/endpoints/drive/files/update.ts deleted file mode 100644 index 83da462113..0000000000 --- a/src/api/endpoints/drive/files/update.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder from '../../../models/drive-folder'; -import DriveFile, { validateFileName, pack } from '../../../models/drive-file'; -import { publishDriveStream } from '../../../event'; - -/** - * Update a file - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'file_id' parameter - const [fileId, fileIdErr] = $(params.file_id).id().$; - if (fileIdErr) return rej('invalid file_id param'); - - // Fetch file - const file = await DriveFile - .findOne({ - _id: fileId, - 'metadata.user_id': user._id - }); - - if (file === null) { - return rej('file-not-found'); - } - - // Get 'name' parameter - const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$; - if (nameErr) return rej('invalid name param'); - if (name) file.filename = name; - - // Get 'folder_id' parameter - const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) return rej('invalid folder_id param'); - - if (folderId !== undefined) { - if (folderId === null) { - file.metadata.folder_id = null; - } else { - // Fetch folder - const folder = await DriveFolder - .findOne({ - _id: folderId, - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - file.metadata.folder_id = folder._id; - } - } - - await DriveFile.update(file._id, { - $set: { - filename: file.filename, - 'metadata.folder_id': file.metadata.folder_id - } - }); - - // Serialize - const fileObj = await pack(file); - - // Response - res(fileObj); - - // Publish file_updated event - publishDriveStream(user._id, 'file_updated', fileObj); -}); diff --git a/src/api/endpoints/drive/files/upload_from_url.ts b/src/api/endpoints/drive/files/upload_from_url.ts deleted file mode 100644 index 346633c616..0000000000 --- a/src/api/endpoints/drive/files/upload_from_url.ts +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import { pack } from '../../../models/drive-file'; -import uploadFromUrl from '../../../common/drive/upload_from_url'; - -/** - * Create a file from a URL - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user): Promise<any> => { - // Get 'url' parameter - // TODO: Validate this url - const [url, urlErr] = $(params.url).string().$; - if (urlErr) throw 'invalid url param'; - - // Get 'folder_id' parameter - const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) throw 'invalid folder_id param'; - - return pack(await uploadFromUrl(url, user, folderId)); -}; diff --git a/src/api/endpoints/drive/folders.ts b/src/api/endpoints/drive/folders.ts deleted file mode 100644 index 428bde3507..0000000000 --- a/src/api/endpoints/drive/folders.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder, { pack } from '../../models/drive-folder'; - -/** - * Get drive folders - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise<any>} - */ -module.exports = (params, user, app) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Get 'folder_id' parameter - const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; - if (folderIdErr) return rej('invalid folder_id param'); - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: user._id, - parent_id: folderId - } as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const folders = await DriveFolder - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(folders.map(async folder => - await pack(folder)))); -}); diff --git a/src/api/endpoints/drive/folders/create.ts b/src/api/endpoints/drive/folders/create.ts deleted file mode 100644 index 03f396ddc9..0000000000 --- a/src/api/endpoints/drive/folders/create.ts +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder, { isValidFolderName, pack } from '../../../models/drive-folder'; -import { publishDriveStream } from '../../../event'; - -/** - * Create drive folder - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name = '無題のフォルダー', nameErr] = $(params.name).optional.string().pipe(isValidFolderName).$; - if (nameErr) return rej('invalid name param'); - - // Get 'parent_id' parameter - const [parentId = null, parentIdErr] = $(params.parent_id).optional.nullable.id().$; - if (parentIdErr) return rej('invalid parent_id param'); - - // If the parent folder is specified - let parent = null; - if (parentId) { - // Fetch parent folder - parent = await DriveFolder - .findOne({ - _id: parentId, - user_id: user._id - }); - - if (parent === null) { - return rej('parent-not-found'); - } - } - - // Create folder - const folder = await DriveFolder.insert({ - created_at: new Date(), - name: name, - parent_id: parent !== null ? parent._id : null, - user_id: user._id - }); - - // Serialize - const folderObj = await pack(folder); - - // Response - res(folderObj); - - // Publish folder_created event - publishDriveStream(user._id, 'folder_created', folderObj); -}); diff --git a/src/api/endpoints/drive/folders/find.ts b/src/api/endpoints/drive/folders/find.ts deleted file mode 100644 index fc84766bc8..0000000000 --- a/src/api/endpoints/drive/folders/find.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder, { pack } from '../../../models/drive-folder'; - -/** - * Find a folder(s) - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name, nameErr] = $(params.name).string().$; - if (nameErr) return rej('invalid name param'); - - // Get 'parent_id' parameter - const [parentId = null, parentIdErr] = $(params.parent_id).optional.nullable.id().$; - if (parentIdErr) return rej('invalid parent_id param'); - - // Issue query - const folders = await DriveFolder - .find({ - name: name, - user_id: user._id, - parent_id: parentId - }); - - // Serialize - res(await Promise.all(folders.map(folder => pack(folder)))); -}); diff --git a/src/api/endpoints/drive/folders/show.ts b/src/api/endpoints/drive/folders/show.ts deleted file mode 100644 index e07d14d20d..0000000000 --- a/src/api/endpoints/drive/folders/show.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder, { pack } from '../../../models/drive-folder'; - -/** - * Show a folder - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'folder_id' parameter - const [folderId, folderIdErr] = $(params.folder_id).id().$; - if (folderIdErr) return rej('invalid folder_id param'); - - // Get folder - const folder = await DriveFolder - .findOne({ - _id: folderId, - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - // Serialize - res(await pack(folder, { - detail: true - })); -}); diff --git a/src/api/endpoints/drive/folders/update.ts b/src/api/endpoints/drive/folders/update.ts deleted file mode 100644 index d3df8bdae5..0000000000 --- a/src/api/endpoints/drive/folders/update.ts +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFolder, { isValidFolderName, pack } from '../../../models/drive-folder'; -import { publishDriveStream } from '../../../event'; - -/** - * Update a folder - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'folder_id' parameter - const [folderId, folderIdErr] = $(params.folder_id).id().$; - if (folderIdErr) return rej('invalid folder_id param'); - - // Fetch folder - const folder = await DriveFolder - .findOne({ - _id: folderId, - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - // Get 'name' parameter - const [name, nameErr] = $(params.name).optional.string().pipe(isValidFolderName).$; - if (nameErr) return rej('invalid name param'); - if (name) folder.name = name; - - // Get 'parent_id' parameter - const [parentId, parentIdErr] = $(params.parent_id).optional.nullable.id().$; - if (parentIdErr) return rej('invalid parent_id param'); - if (parentId !== undefined) { - if (parentId === null) { - folder.parent_id = null; - } else { - // Get parent folder - const parent = await DriveFolder - .findOne({ - _id: parentId, - user_id: user._id - }); - - if (parent === null) { - return rej('parent-folder-not-found'); - } - - // Check if the circular reference will occur - async function checkCircle(folderId) { - // Fetch folder - const folder2 = await DriveFolder.findOne({ - _id: folderId - }, { - _id: true, - parent_id: true - }); - - if (folder2._id.equals(folder._id)) { - return true; - } else if (folder2.parent_id) { - return await checkCircle(folder2.parent_id); - } else { - return false; - } - } - - if (parent.parent_id !== null) { - if (await checkCircle(parent.parent_id)) { - return rej('detected-circular-definition'); - } - } - - folder.parent_id = parent._id; - } - } - - // Update - DriveFolder.update(folder._id, { - $set: { - name: folder.name, - parent_id: folder.parent_id - } - }); - - // Serialize - const folderObj = await pack(folder); - - // Response - res(folderObj); - - // Publish folder_updated event - publishDriveStream(user._id, 'folder_updated', folderObj); -}); diff --git a/src/api/endpoints/drive/stream.ts b/src/api/endpoints/drive/stream.ts deleted file mode 100644 index 8352c7dd4c..0000000000 --- a/src/api/endpoints/drive/stream.ts +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import DriveFile, { pack } from '../../models/drive-file'; - -/** - * Get drive stream - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Get 'type' parameter - const [type, typeErr] = $(params.type).optional.string().match(/^[a-zA-Z\/\-\*]+$/).$; - if (typeErr) return rej('invalid type param'); - - // Construct query - const sort = { - _id: -1 - }; - const query = { - 'metadata.user_id': user._id - } as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - if (type) { - query.contentType = new RegExp(`^${type.replace(/\*/g, '.+?')}$`); - } - - // Issue query - const files = await DriveFile - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(files.map(async file => - await pack(file)))); -}); diff --git a/src/api/endpoints/following/create.ts b/src/api/endpoints/following/create.ts deleted file mode 100644 index 767b837b35..0000000000 --- a/src/api/endpoints/following/create.ts +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { pack as packUser } from '../../models/user'; -import Following from '../../models/following'; -import notify from '../../common/notify'; -import event from '../../event'; - -/** - * Follow a user - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const follower = user; - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // 自分自身 - if (user._id.equals(userId)) { - return rej('followee is yourself'); - } - - // Get followee - const followee = await User.findOne({ - _id: userId - }, { - fields: { - data: false, - 'account.profile': false - } - }); - - if (followee === null) { - return rej('user not found'); - } - - // Check if already following - const exist = await Following.findOne({ - follower_id: follower._id, - followee_id: followee._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already following'); - } - - // Create following - await Following.insert({ - created_at: new Date(), - follower_id: follower._id, - followee_id: followee._id - }); - - // Send response - res(); - - // Increment following count - User.update(follower._id, { - $inc: { - following_count: 1 - } - }); - - // Increment followers count - User.update({ _id: followee._id }, { - $inc: { - followers_count: 1 - } - }); - - // Publish follow event - event(follower._id, 'follow', await packUser(followee, follower)); - event(followee._id, 'followed', await packUser(follower, followee)); - - // Notify - notify(followee._id, follower._id, 'follow'); -}); diff --git a/src/api/endpoints/following/delete.ts b/src/api/endpoints/following/delete.ts deleted file mode 100644 index 64b9a8cecb..0000000000 --- a/src/api/endpoints/following/delete.ts +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { pack as packUser } from '../../models/user'; -import Following from '../../models/following'; -import event from '../../event'; - -/** - * Unfollow a user - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const follower = user; - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Check if the followee is yourself - if (user._id.equals(userId)) { - return rej('followee is yourself'); - } - - // Get followee - const followee = await User.findOne({ - _id: userId - }, { - fields: { - data: false, - 'account.profile': false - } - }); - - if (followee === null) { - return rej('user not found'); - } - - // Check not following - const exist = await Following.findOne({ - follower_id: follower._id, - followee_id: followee._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not following'); - } - - // Delete following - await Following.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - // Decrement following count - User.update({ _id: follower._id }, { - $inc: { - following_count: -1 - } - }); - - // Decrement followers count - User.update({ _id: followee._id }, { - $inc: { - followers_count: -1 - } - }); - - // Publish follow event - event(follower._id, 'unfollow', await packUser(followee, follower)); -}); diff --git a/src/api/endpoints/i.ts b/src/api/endpoints/i.ts deleted file mode 100644 index 32b0382faf..0000000000 --- a/src/api/endpoints/i.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Module dependencies - */ -import User, { pack } from '../models/user'; - -/** - * Show myself - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise<any>} - */ -module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => { - // Serialize - res(await pack(user, user, { - detail: true, - includeSecrets: isSecure - })); - - // Update lastUsedAt - User.update({ _id: user._id }, { - $set: { - 'account.last_used_at': new Date() - } - }); -}); diff --git a/src/api/endpoints/i/2fa/done.ts b/src/api/endpoints/i/2fa/done.ts deleted file mode 100644 index 0f1db73829..0000000000 --- a/src/api/endpoints/i/2fa/done.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import * as speakeasy from 'speakeasy'; -import User from '../../../models/user'; - -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'token' parameter - const [token, tokenErr] = $(params.token).string().$; - if (tokenErr) return rej('invalid token param'); - - const _token = token.replace(/\s/g, ''); - - if (user.two_factor_temp_secret == null) { - return rej('二段階認証の設定が開始されていません'); - } - - const verified = (speakeasy as any).totp.verify({ - secret: user.two_factor_temp_secret, - encoding: 'base32', - token: _token - }); - - if (!verified) { - return rej('not verified'); - } - - await User.update(user._id, { - $set: { - 'account.two_factor_secret': user.two_factor_temp_secret, - 'account.two_factor_enabled': true - } - }); - - res(); -}); diff --git a/src/api/endpoints/i/2fa/register.ts b/src/api/endpoints/i/2fa/register.ts deleted file mode 100644 index 24abfcdfc5..0000000000 --- a/src/api/endpoints/i/2fa/register.ts +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import * as bcrypt from 'bcryptjs'; -import * as speakeasy from 'speakeasy'; -import * as QRCode from 'qrcode'; -import User from '../../../models/user'; -import config from '../../../../conf'; - -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'password' parameter - const [password, passwordErr] = $(params.password).string().$; - if (passwordErr) return rej('invalid password param'); - - // Compare password - const same = await bcrypt.compare(password, user.account.password); - - if (!same) { - return rej('incorrect password'); - } - - // Generate user's secret key - const secret = speakeasy.generateSecret({ - length: 32 - }); - - await User.update(user._id, { - $set: { - two_factor_temp_secret: secret.base32 - } - }); - - // Get the data URL of the authenticator URL - QRCode.toDataURL(speakeasy.otpauthURL({ - secret: secret.base32, - encoding: 'base32', - label: user.username, - issuer: config.host - }), (err, data_url) => { - res({ - qr: data_url, - secret: secret.base32, - label: user.username, - issuer: config.host - }); - }); -}); diff --git a/src/api/endpoints/i/2fa/unregister.ts b/src/api/endpoints/i/2fa/unregister.ts deleted file mode 100644 index c43f9ccc44..0000000000 --- a/src/api/endpoints/i/2fa/unregister.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import * as bcrypt from 'bcryptjs'; -import User from '../../../models/user'; - -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'password' parameter - const [password, passwordErr] = $(params.password).string().$; - if (passwordErr) return rej('invalid password param'); - - // Compare password - const same = await bcrypt.compare(password, user.account.password); - - if (!same) { - return rej('incorrect password'); - } - - await User.update(user._id, { - $set: { - 'account.two_factor_secret': null, - 'account.two_factor_enabled': false - } - }); - - res(); -}); diff --git a/src/api/endpoints/i/appdata/get.ts b/src/api/endpoints/i/appdata/get.ts deleted file mode 100644 index 571208d46c..0000000000 --- a/src/api/endpoints/i/appdata/get.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Appdata from '../../../models/appdata'; - -/** - * Get app data - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise<any>} - */ -module.exports = (params, user, app) => new Promise(async (res, rej) => { - if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます'); - - // Get 'key' parameter - const [key = null, keyError] = $(params.key).optional.nullable.string().match(/[a-z_]+/).$; - if (keyError) return rej('invalid key param'); - - const select = {}; - if (key !== null) { - select[`data.${key}`] = true; - } - const appdata = await Appdata.findOne({ - app_id: app._id, - user_id: user._id - }, { - fields: select - }); - - if (appdata) { - res(appdata.data); - } else { - res(); - } -}); diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts deleted file mode 100644 index 2804a14cb3..0000000000 --- a/src/api/endpoints/i/appdata/set.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Appdata from '../../../models/appdata'; - -/** - * Set app data - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise<any>} - */ -module.exports = (params, user, app) => new Promise(async (res, rej) => { - if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます'); - - // Get 'data' parameter - const [data, dataError] = $(params.data).optional.object() - .pipe(obj => { - const hasInvalidData = Object.entries(obj).some(([k, v]) => - $(k).string().match(/^[a-z_]+$/).nok() && $(v).string().nok()); - return !hasInvalidData; - }).$; - if (dataError) return rej('invalid data param'); - - // Get 'key' parameter - const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$; - if (keyError) return rej('invalid key param'); - - // Get 'value' parameter - const [value, valueError] = $(params.value).optional.string().$; - if (valueError) return rej('invalid value param'); - - const set = {}; - if (data) { - Object.entries(data).forEach(([k, v]) => { - set[`data.${k}`] = v; - }); - } else { - set[`data.${key}`] = value; - } - - await Appdata.update({ - app_id: app._id, - user_id: user._id - }, Object.assign({ - app_id: app._id, - user_id: user._id - }, { - $set: set - }), { - upsert: true - }); - - res(204); -}); diff --git a/src/api/endpoints/i/authorized_apps.ts b/src/api/endpoints/i/authorized_apps.ts deleted file mode 100644 index 40ce7a68c8..0000000000 --- a/src/api/endpoints/i/authorized_apps.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import AccessToken from '../../models/access-token'; -import { pack } from '../../models/app'; - -/** - * Get authorized apps of my account - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'sort' parameter - const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$; - if (sortError) return rej('invalid sort param'); - - // Get tokens - const tokens = await AccessToken - .find({ - user_id: user._id - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(tokens.map(async token => - await pack(token.app_id)))); -}); diff --git a/src/api/endpoints/i/change_password.ts b/src/api/endpoints/i/change_password.ts deleted file mode 100644 index 88fb36b1fb..0000000000 --- a/src/api/endpoints/i/change_password.ts +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import * as bcrypt from 'bcryptjs'; -import User from '../../models/user'; - -/** - * Change password - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'current_password' parameter - const [currentPassword, currentPasswordErr] = $(params.current_password).string().$; - if (currentPasswordErr) return rej('invalid current_password param'); - - // Get 'new_password' parameter - const [newPassword, newPasswordErr] = $(params.new_password).string().$; - if (newPasswordErr) return rej('invalid new_password param'); - - // Compare password - const same = await bcrypt.compare(currentPassword, user.account.password); - - if (!same) { - return rej('incorrect password'); - } - - // Generate hash of password - const salt = await bcrypt.genSalt(8); - const hash = await bcrypt.hash(newPassword, salt); - - await User.update(user._id, { - $set: { - 'account.password': hash - } - }); - - res(); -}); diff --git a/src/api/endpoints/i/favorites.ts b/src/api/endpoints/i/favorites.ts deleted file mode 100644 index eb464cf0f0..0000000000 --- a/src/api/endpoints/i/favorites.ts +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Favorite from '../../models/favorite'; -import { pack } from '../../models/post'; - -/** - * Get followers of a user - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'sort' parameter - const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$; - if (sortError) return rej('invalid sort param'); - - // Get favorites - const favorites = await Favorite - .find({ - user_id: user._id - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(favorites.map(async favorite => - await pack(favorite.post) - ))); -}); diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts deleted file mode 100644 index 688039a0dd..0000000000 --- a/src/api/endpoints/i/notifications.ts +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Notification from '../../models/notification'; -import Mute from '../../models/mute'; -import { pack } from '../../models/notification'; -import getFriends from '../../common/get-friends'; -import read from '../../common/read-notification'; - -/** - * Get notifications - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'following' parameter - const [following = false, followingError] = - $(params.following).optional.boolean().$; - if (followingError) return rej('invalid following param'); - - // Get 'mark_as_read' parameter - const [markAsRead = true, markAsReadErr] = $(params.mark_as_read).optional.boolean().$; - if (markAsReadErr) return rej('invalid mark_as_read param'); - - // Get 'type' parameter - const [type, typeErr] = $(params.type).optional.array('string').unique().$; - if (typeErr) return rej('invalid type param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - const mute = await Mute.find({ - muter_id: user._id, - deleted_at: { $exists: false } - }); - - const query = { - notifiee_id: user._id, - $and: [{ - notifier_id: { - $nin: mute.map(m => m.mutee_id) - } - }] - } as any; - - const sort = { - _id: -1 - }; - - if (following) { - // ID list of the user itself and other users who the user follows - const followingIds = await getFriends(user._id); - - query.$and.push({ - notifier_id: { - $in: followingIds - } - }); - } - - if (type) { - query.type = { - $in: type - }; - } - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const notifications = await Notification - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(notifications.map(async notification => - await pack(notification)))); - - // Mark as read all - if (notifications.length > 0 && markAsRead) { - read(user._id, notifications); - } -}); diff --git a/src/api/endpoints/i/pin.ts b/src/api/endpoints/i/pin.ts deleted file mode 100644 index ff546fc2bd..0000000000 --- a/src/api/endpoints/i/pin.ts +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import Post from '../../models/post'; -import { pack } from '../../models/user'; - -/** - * Pin post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Fetch pinee - const post = await Post.findOne({ - _id: postId, - user_id: user._id - }); - - if (post === null) { - return rej('post not found'); - } - - await User.update(user._id, { - $set: { - pinned_post_id: post._id - } - }); - - // Serialize - const iObj = await pack(user, user, { - detail: true - }); - - // Send response - res(iObj); -}); diff --git a/src/api/endpoints/i/regenerate_token.ts b/src/api/endpoints/i/regenerate_token.ts deleted file mode 100644 index 9ac7b55071..0000000000 --- a/src/api/endpoints/i/regenerate_token.ts +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import * as bcrypt from 'bcryptjs'; -import User from '../../models/user'; -import event from '../../event'; -import generateUserToken from '../../common/generate-native-user-token'; - -/** - * Regenerate native token - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'password' parameter - const [password, passwordErr] = $(params.password).string().$; - if (passwordErr) return rej('invalid password param'); - - // Compare password - const same = await bcrypt.compare(password, user.account.password); - - if (!same) { - return rej('incorrect password'); - } - - // Generate secret - const secret = generateUserToken(); - - await User.update(user._id, { - $set: { - 'account.token': secret - } - }); - - res(); - - // Publish event - event(user._id, 'my_token_regenerated'); -}); diff --git a/src/api/endpoints/i/signin_history.ts b/src/api/endpoints/i/signin_history.ts deleted file mode 100644 index 859e81653d..0000000000 --- a/src/api/endpoints/i/signin_history.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Signin, { pack } from '../../models/signin'; - -/** - * Get signin history of my account - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - const query = { - user_id: user._id - } as any; - - const sort = { - _id: -1 - }; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const history = await Signin - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(history.map(async record => - await pack(record)))); -}); diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts deleted file mode 100644 index db8a3f25bd..0000000000 --- a/src/api/endpoints/i/update.ts +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../models/user'; -import event from '../../event'; -import config from '../../../conf'; - -/** - * Update myself - * - * @param {any} params - * @param {any} user - * @param {any} _ - * @param {boolean} isSecure - * @return {Promise<any>} - */ -module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name, nameErr] = $(params.name).optional.string().pipe(isValidName).$; - if (nameErr) return rej('invalid name param'); - if (name) user.name = name; - - // Get 'description' parameter - const [description, descriptionErr] = $(params.description).optional.nullable.string().pipe(isValidDescription).$; - if (descriptionErr) return rej('invalid description param'); - if (description !== undefined) user.description = description; - - // Get 'location' parameter - const [location, locationErr] = $(params.location).optional.nullable.string().pipe(isValidLocation).$; - if (locationErr) return rej('invalid location param'); - if (location !== undefined) user.account.profile.location = location; - - // Get 'birthday' parameter - const [birthday, birthdayErr] = $(params.birthday).optional.nullable.string().pipe(isValidBirthday).$; - if (birthdayErr) return rej('invalid birthday param'); - if (birthday !== undefined) user.account.profile.birthday = birthday; - - // Get 'avatar_id' parameter - const [avatarId, avatarIdErr] = $(params.avatar_id).optional.id().$; - if (avatarIdErr) return rej('invalid avatar_id param'); - if (avatarId) user.avatar_id = avatarId; - - // Get 'banner_id' parameter - const [bannerId, bannerIdErr] = $(params.banner_id).optional.id().$; - if (bannerIdErr) return rej('invalid banner_id param'); - if (bannerId) user.banner_id = bannerId; - - // Get 'is_bot' parameter - const [isBot, isBotErr] = $(params.is_bot).optional.boolean().$; - if (isBotErr) return rej('invalid is_bot param'); - if (isBot != null) user.account.is_bot = isBot; - - // Get 'auto_watch' parameter - const [autoWatch, autoWatchErr] = $(params.auto_watch).optional.boolean().$; - if (autoWatchErr) return rej('invalid auto_watch param'); - if (autoWatch != null) user.account.settings.auto_watch = autoWatch; - - await User.update(user._id, { - $set: { - name: user.name, - description: user.description, - avatar_id: user.avatar_id, - banner_id: user.banner_id, - 'account.profile': user.account.profile, - 'account.is_bot': user.account.is_bot, - 'account.settings': user.account.settings - } - }); - - // Serialize - const iObj = await pack(user, user, { - detail: true, - includeSecrets: isSecure - }); - - // Send response - res(iObj); - - // Publish i updated event - event(user._id, 'i_updated', iObj); - - // Update search index - if (config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'user', - id: user._id.toString(), - body: { - name: user.name, - bio: user.bio - } - }); - } -}); diff --git a/src/api/endpoints/i/update_client_setting.ts b/src/api/endpoints/i/update_client_setting.ts deleted file mode 100644 index c772ed5dc3..0000000000 --- a/src/api/endpoints/i/update_client_setting.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { pack } from '../../models/user'; -import event from '../../event'; - -/** - * Update myself - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name, nameErr] = $(params.name).string().$; - if (nameErr) return rej('invalid name param'); - - // Get 'value' parameter - const [value, valueErr] = $(params.value).nullable.any().$; - if (valueErr) return rej('invalid value param'); - - const x = {}; - x[`account.client_settings.${name}`] = value; - - await User.update(user._id, { - $set: x - }); - - // Serialize - user.account.client_settings[name] = value; - const iObj = await pack(user, user, { - detail: true, - includeSecrets: true - }); - - // Send response - res(iObj); - - // Publish i updated event - event(user._id, 'i_updated', iObj); -}); diff --git a/src/api/endpoints/i/update_home.ts b/src/api/endpoints/i/update_home.ts deleted file mode 100644 index 9ce44e25ee..0000000000 --- a/src/api/endpoints/i/update_home.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import event from '../../event'; - -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'home' parameter - const [home, homeErr] = $(params.home).optional.array().each( - $().strict.object() - .have('name', $().string()) - .have('id', $().string()) - .have('place', $().string()) - .have('data', $().object())).$; - if (homeErr) return rej('invalid home param'); - - // Get 'id' parameter - const [id, idErr] = $(params.id).optional.string().$; - if (idErr) return rej('invalid id param'); - - // Get 'data' parameter - const [data, dataErr] = $(params.data).optional.object().$; - if (dataErr) return rej('invalid data param'); - - if (home) { - await User.update(user._id, { - $set: { - 'account.client_settings.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.account.client_settings.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: { - 'account.client_settings.home': _home - } - }); - - res(); - - event(user._id, 'home_updated', { - id, data - }); - } -}); diff --git a/src/api/endpoints/i/update_mobile_home.ts b/src/api/endpoints/i/update_mobile_home.ts deleted file mode 100644 index 1daddf42b9..0000000000 --- a/src/api/endpoints/i/update_mobile_home.ts +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import event from '../../event'; - -module.exports = async (params, user) => new Promise(async (res, rej) => { - // Get 'home' parameter - const [home, homeErr] = $(params.home).optional.array().each( - $().strict.object() - .have('name', $().string()) - .have('id', $().string()) - .have('data', $().object())).$; - if (homeErr) return rej('invalid home param'); - - // Get 'id' parameter - const [id, idErr] = $(params.id).optional.string().$; - if (idErr) return rej('invalid id param'); - - // Get 'data' parameter - const [data, dataErr] = $(params.data).optional.object().$; - if (dataErr) return rej('invalid data param'); - - if (home) { - await User.update(user._id, { - $set: { - 'account.client_settings.mobile_home': 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.account.client_settings.mobile_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: { - 'account.client_settings.mobile_home': _home - } - }); - - res(); - - event(user._id, 'mobile_home_updated', { - id, data - }); - } -}); diff --git a/src/api/endpoints/messaging/history.ts b/src/api/endpoints/messaging/history.ts deleted file mode 100644 index 1683ca7a89..0000000000 --- a/src/api/endpoints/messaging/history.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import History from '../../models/messaging-history'; -import Mute from '../../models/mute'; -import { pack } from '../../models/messaging-message'; - -/** - * Show messaging history - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - const mute = await Mute.find({ - muter_id: user._id, - deleted_at: { $exists: false } - }); - - // Get history - const history = await History - .find({ - user_id: user._id, - partner: { - $nin: mute.map(m => m.mutee_id) - } - }, { - limit: limit, - sort: { - updated_at: -1 - } - }); - - // Serialize - res(await Promise.all(history.map(async h => - await pack(h.message, user)))); -}); diff --git a/src/api/endpoints/messaging/messages.ts b/src/api/endpoints/messaging/messages.ts deleted file mode 100644 index 67ba5e9d6d..0000000000 --- a/src/api/endpoints/messaging/messages.ts +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Message from '../../models/messaging-message'; -import User from '../../models/user'; -import { pack } from '../../models/messaging-message'; -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 'user_id' parameter - const [recipientId, recipientIdErr] = $(params.user_id).id().$; - if (recipientIdErr) return rej('invalid user_id param'); - - // Fetch recipient - const recipient = await User.findOne({ - _id: recipientId - }, { - fields: { - _id: true - } - }); - - if (recipient === null) { - return rej('user not found'); - } - - // Get 'mark_as_read' parameter - const [markAsRead = true, markAsReadErr] = $(params.mark_as_read).optional.boolean().$; - if (markAsReadErr) return rej('invalid mark_as_read param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - const query = { - $or: [{ - user_id: user._id, - recipient_id: recipient._id - }, { - user_id: recipient._id, - recipient_id: user._id - }] - } as any; - - const sort = { - _id: -1 - }; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const messages = await Message - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(messages.map(async message => - await pack(message, user, { - populateRecipient: false - })))); - - if (messages.length === 0) { - return; - } - - // Mark as read all - if (markAsRead) { - read(user._id, recipient._id, messages); - } -}); diff --git a/src/api/endpoints/messaging/messages/create.ts b/src/api/endpoints/messaging/messages/create.ts deleted file mode 100644 index 1b8a5f59e6..0000000000 --- a/src/api/endpoints/messaging/messages/create.ts +++ /dev/null @@ -1,156 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Message from '../../../models/messaging-message'; -import { isValidText } from '../../../models/messaging-message'; -import History from '../../../models/messaging-history'; -import User from '../../../models/user'; -import Mute from '../../../models/mute'; -import DriveFile from '../../../models/drive-file'; -import { pack } from '../../../models/messaging-message'; -import publishUserStream from '../../../event'; -import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event'; -import config from '../../../../conf'; - -/** - * Create a message - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [recipientId, recipientIdErr] = $(params.user_id).id().$; - if (recipientIdErr) return rej('invalid user_id param'); - - // Myself - if (recipientId.equals(user._id)) { - return rej('cannot send message to myself'); - } - - // Fetch recipient - const recipient = await User.findOne({ - _id: recipientId - }, { - fields: { - _id: true - } - }); - - if (recipient === null) { - return rej('user not found'); - } - - // Get 'text' parameter - const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$; - if (textErr) return rej('invalid text'); - - // Get 'file_id' parameter - const [fileId, fileIdErr] = $(params.file_id).optional.id().$; - if (fileIdErr) return rej('invalid file_id param'); - - let file = null; - if (fileId !== undefined) { - file = await DriveFile.findOne({ - _id: fileId, - 'metadata.user_id': user._id - }); - - if (file === null) { - return rej('file not found'); - } - } - - // テキストが無いかつ添付ファイルも無かったらエラー - if (text === undefined && file === null) { - return rej('text or file is required'); - } - - // メッセージを作成 - const message = await Message.insert({ - created_at: new Date(), - file_id: file ? file._id : undefined, - recipient_id: recipient._id, - text: text ? text : undefined, - user_id: user._id, - is_read: false - }); - - // Serialize - const messageObj = await pack(message); - - // Reponse - res(messageObj); - - // 自分のストリーム - publishMessagingStream(message.user_id, message.recipient_id, 'message', messageObj); - publishMessagingIndexStream(message.user_id, 'message', messageObj); - publishUserStream(message.user_id, 'messaging_message', messageObj); - - // 相手のストリーム - publishMessagingStream(message.recipient_id, message.user_id, 'message', messageObj); - publishMessagingIndexStream(message.recipient_id, 'message', messageObj); - publishUserStream(message.recipient_id, 'messaging_message', messageObj); - - // 3秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する - setTimeout(async () => { - const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true }); - if (!freshMessage.is_read) { - //#region ただしミュートされているなら発行しない - const mute = await Mute.find({ - muter_id: recipient._id, - deleted_at: { $exists: false } - }); - const mutedUserIds = mute.map(m => m.mutee_id.toString()); - if (mutedUserIds.indexOf(user._id.toString()) != -1) { - return; - } - //#endregion - - publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj); - pushSw(message.recipient_id, 'unread_messaging_message', messageObj); - } - }, 3000); - - // Register to search database - if (message.text && config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'messaging_message', - id: message._id.toString(), - body: { - text: message.text - } - }); - } - - // 履歴作成(自分) - History.update({ - user_id: user._id, - partner: recipient._id - }, { - updated_at: new Date(), - user_id: user._id, - partner: recipient._id, - message: message._id - }, { - upsert: true - }); - - // 履歴作成(相手) - History.update({ - user_id: recipient._id, - partner: user._id - }, { - updated_at: new Date(), - user_id: recipient._id, - partner: user._id, - message: message._id - }, { - upsert: true - }); -}); diff --git a/src/api/endpoints/messaging/unread.ts b/src/api/endpoints/messaging/unread.ts deleted file mode 100644 index c4326e1d22..0000000000 --- a/src/api/endpoints/messaging/unread.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Module dependencies - */ -import Message from '../../models/messaging-message'; -import Mute from '../../models/mute'; - -/** - * Get count of unread messages - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const mute = await Mute.find({ - muter_id: user._id, - deleted_at: { $exists: false } - }); - const mutedUserIds = mute.map(m => m.mutee_id); - - const count = await Message - .count({ - user_id: { - $nin: mutedUserIds - }, - recipient_id: user._id, - is_read: false - }); - - res({ - count: count - }); -}); diff --git a/src/api/endpoints/meta.ts b/src/api/endpoints/meta.ts deleted file mode 100644 index 1370ead3c5..0000000000 --- a/src/api/endpoints/meta.ts +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Module dependencies - */ -import * as os from 'os'; -import version from '../../version'; -import config from '../../conf'; -import Meta from '../models/meta'; - -/** - * @swagger - * /meta: - * post: - * summary: Show the misskey's information - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * maintainer: - * description: maintainer's name - * type: string - * commit: - * description: latest commit's hash - * type: string - * secure: - * description: whether the server supports secure protocols - * type: boolean - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show core info - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - const meta = (await Meta.findOne()) || {}; - - res({ - maintainer: config.maintainer, - version: version, - secure: config.https != null, - machine: os.hostname(), - os: os.platform(), - node: process.version, - cpu: { - model: os.cpus()[0].model, - cores: os.cpus().length - }, - top_image: meta.top_image, - broadcasts: meta.broadcasts - }); -}); diff --git a/src/api/endpoints/mute/create.ts b/src/api/endpoints/mute/create.ts deleted file mode 100644 index f99b40d32e..0000000000 --- a/src/api/endpoints/mute/create.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import Mute from '../../models/mute'; - -/** - * Mute a user - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const muter = user; - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // 自分自身 - if (user._id.equals(userId)) { - return rej('mutee is yourself'); - } - - // Get mutee - const mutee = await User.findOne({ - _id: userId - }, { - fields: { - data: false, - 'account.profile': false - } - }); - - if (mutee === null) { - return rej('user not found'); - } - - // Check if already muting - const exist = await Mute.findOne({ - muter_id: muter._id, - mutee_id: mutee._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already muting'); - } - - // Create mute - await Mute.insert({ - created_at: new Date(), - muter_id: muter._id, - mutee_id: mutee._id, - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/mute/delete.ts b/src/api/endpoints/mute/delete.ts deleted file mode 100644 index 36e2fd101a..0000000000 --- a/src/api/endpoints/mute/delete.ts +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import Mute from '../../models/mute'; - -/** - * Unmute a user - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const muter = user; - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Check if the mutee is yourself - if (user._id.equals(userId)) { - return rej('mutee is yourself'); - } - - // Get mutee - const mutee = await User.findOne({ - _id: userId - }, { - fields: { - data: false, - 'account.profile': false - } - }); - - if (mutee === null) { - return rej('user not found'); - } - - // Check not muting - const exist = await Mute.findOne({ - muter_id: muter._id, - mutee_id: mutee._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not muting'); - } - - // Delete mute - await Mute.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/mute/list.ts b/src/api/endpoints/mute/list.ts deleted file mode 100644 index 19e3b157e6..0000000000 --- a/src/api/endpoints/mute/list.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Mute from '../../models/mute'; -import { pack } from '../../models/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get muted users of a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'iknow' parameter - const [iknow = false, iknowErr] = $(params.iknow).optional.boolean().$; - if (iknowErr) return rej('invalid iknow param'); - - // Get 'limit' parameter - const [limit = 30, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'cursor' parameter - const [cursor = null, cursorErr] = $(params.cursor).optional.id().$; - if (cursorErr) return rej('invalid cursor param'); - - // Construct query - const query = { - muter_id: me._id, - deleted_at: { $exists: false } - } as any; - - if (iknow) { - // Get my friends - const myFriends = await getFriends(me._id); - - query.mutee_id = { - $in: myFriends - }; - } - - // カーソルが指定されている場合 - if (cursor) { - query._id = { - $lt: cursor - }; - } - - // Get mutes - const mutes = await Mute - .find(query, { - limit: limit + 1, - sort: { _id: -1 } - }); - - // 「次のページ」があるかどうか - const inStock = mutes.length === limit + 1; - if (inStock) { - mutes.pop(); - } - - // Serialize - const users = await Promise.all(mutes.map(async m => - await pack(m.mutee_id, me, { detail: true }))); - - // Response - res({ - users: users, - next: inStock ? mutes[mutes.length - 1]._id : null, - }); -}); diff --git a/src/api/endpoints/my/apps.ts b/src/api/endpoints/my/apps.ts deleted file mode 100644 index b236190506..0000000000 --- a/src/api/endpoints/my/apps.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import App, { pack } from '../../models/app'; - -/** - * Get my apps - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - const query = { - user_id: user._id - }; - - // Execute query - const apps = await App - .find(query, { - limit: limit, - skip: offset, - sort: { - _id: -1 - } - }); - - // Reply - res(await Promise.all(apps.map(async app => - await pack(app)))); -}); diff --git a/src/api/endpoints/notifications/get_unread_count.ts b/src/api/endpoints/notifications/get_unread_count.ts deleted file mode 100644 index 845d6b29ce..0000000000 --- a/src/api/endpoints/notifications/get_unread_count.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Module dependencies - */ -import Notification from '../../models/notification'; -import Mute from '../../models/mute'; - -/** - * Get count of unread notifications - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - const mute = await Mute.find({ - muter_id: user._id, - deleted_at: { $exists: false } - }); - const mutedUserIds = mute.map(m => m.mutee_id); - - const count = await Notification - .count({ - notifiee_id: user._id, - notifier_id: { - $nin: mutedUserIds - }, - is_read: false - }); - - res({ - count: count - }); -}); diff --git a/src/api/endpoints/notifications/mark_as_read_all.ts b/src/api/endpoints/notifications/mark_as_read_all.ts deleted file mode 100644 index 3550e344c4..0000000000 --- a/src/api/endpoints/notifications/mark_as_read_all.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Module dependencies - */ -import Notification from '../../models/notification'; -import event from '../../event'; - -/** - * Mark as read all notifications - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Update documents - await Notification.update({ - notifiee_id: user._id, - is_read: false - }, { - $set: { - is_read: true - } - }, { - multi: true - }); - - // Response - res(); - - // 全ての通知を読みましたよというイベントを発行 - event(user._id, 'read_all_notifications'); -}); diff --git a/src/api/endpoints/othello/games.ts b/src/api/endpoints/othello/games.ts deleted file mode 100644 index 2a6bbb4043..0000000000 --- a/src/api/endpoints/othello/games.ts +++ /dev/null @@ -1,62 +0,0 @@ -import $ from 'cafy'; -import Game, { pack } from '../../models/othello-game'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'my' parameter - const [my = false, myErr] = $(params.my).optional.boolean().$; - if (myErr) return rej('invalid my param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - const q: any = my ? { - is_started: true, - $or: [{ - user1_id: user._id - }, { - user2_id: user._id - }] - } : { - is_started: true - }; - - const sort = { - _id: -1 - }; - - if (sinceId) { - sort._id = 1; - q._id = { - $gt: sinceId - }; - } else if (untilId) { - q._id = { - $lt: untilId - }; - } - - // Fetch games - const games = await Game.find(q, { - sort, - limit - }); - - // Reponse - res(Promise.all(games.map(async (g) => await pack(g, user, { - detail: false - })))); -}); diff --git a/src/api/endpoints/othello/games/show.ts b/src/api/endpoints/othello/games/show.ts deleted file mode 100644 index 2b0db4dd00..0000000000 --- a/src/api/endpoints/othello/games/show.ts +++ /dev/null @@ -1,32 +0,0 @@ -import $ from 'cafy'; -import Game, { pack } from '../../../models/othello-game'; -import Othello from '../../../../common/othello/core'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'game_id' parameter - const [gameId, gameIdErr] = $(params.game_id).id().$; - if (gameIdErr) return rej('invalid game_id param'); - - const game = await Game.findOne({ _id: gameId }); - - if (game == null) { - return rej('game not found'); - } - - const o = new Othello(game.settings.map, { - isLlotheo: game.settings.is_llotheo, - canPutEverywhere: game.settings.can_put_everywhere, - loopedBoard: game.settings.looped_board - }); - - game.logs.forEach(log => { - o.put(log.color, log.pos); - }); - - const packed = await pack(game, user); - - res(Object.assign({ - board: o.board, - turn: o.turn - }, packed)); -}); diff --git a/src/api/endpoints/othello/invitations.ts b/src/api/endpoints/othello/invitations.ts deleted file mode 100644 index 02fb421fbc..0000000000 --- a/src/api/endpoints/othello/invitations.ts +++ /dev/null @@ -1,15 +0,0 @@ -import Matching, { pack as packMatching } from '../../models/othello-matching'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - // Find session - const invitations = await Matching.find({ - child_id: user._id - }, { - sort: { - _id: -1 - } - }); - - // Reponse - res(Promise.all(invitations.map(async (i) => await packMatching(i, user)))); -}); diff --git a/src/api/endpoints/othello/match.ts b/src/api/endpoints/othello/match.ts deleted file mode 100644 index b73e105ef0..0000000000 --- a/src/api/endpoints/othello/match.ts +++ /dev/null @@ -1,95 +0,0 @@ -import $ from 'cafy'; -import Matching, { pack as packMatching } from '../../models/othello-matching'; -import Game, { pack as packGame } from '../../models/othello-game'; -import User from '../../models/user'; -import publishUserStream, { publishOthelloStream } from '../../event'; -import { eighteight } from '../../../common/othello/maps'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [childId, childIdErr] = $(params.user_id).id().$; - if (childIdErr) return rej('invalid user_id param'); - - // Myself - if (childId.equals(user._id)) { - return rej('invalid user_id param'); - } - - // Find session - const exist = await Matching.findOne({ - parent_id: childId, - child_id: user._id - }); - - if (exist) { - // Destroy session - Matching.remove({ - _id: exist._id - }); - - // Create game - const game = await Game.insert({ - created_at: new Date(), - user1_id: exist.parent_id, - user2_id: user._id, - user1_accepted: false, - user2_accepted: false, - is_started: false, - is_ended: false, - logs: [], - settings: { - map: eighteight.data, - bw: 'random', - is_llotheo: false - } - }); - - // Reponse - res(await packGame(game, user)); - - publishOthelloStream(exist.parent_id, 'matched', await packGame(game, exist.parent_id)); - - const other = await Matching.count({ - child_id: user._id - }); - - if (other == 0) { - publishUserStream(user._id, 'othello_no_invites'); - } - } else { - // Fetch child - const child = await User.findOne({ - _id: childId - }, { - fields: { - _id: true - } - }); - - if (child === null) { - return rej('user not found'); - } - - // 以前のセッションはすべて削除しておく - await Matching.remove({ - parent_id: user._id - }); - - // セッションを作成 - const matching = await Matching.insert({ - created_at: new Date(), - parent_id: user._id, - child_id: child._id - }); - - // Reponse - res(); - - const packed = await packMatching(matching, child); - - // 招待 - publishOthelloStream(child._id, 'invited', packed); - - publishUserStream(child._id, 'othello_invited', packed); - } -}); diff --git a/src/api/endpoints/othello/match/cancel.ts b/src/api/endpoints/othello/match/cancel.ts deleted file mode 100644 index 6f751ef835..0000000000 --- a/src/api/endpoints/othello/match/cancel.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Matching from '../../../models/othello-matching'; - -module.exports = (params, user) => new Promise(async (res, rej) => { - await Matching.remove({ - parent_id: user._id - }); - - res(); -}); diff --git a/src/api/endpoints/posts.ts b/src/api/endpoints/posts.ts deleted file mode 100644 index 7df744d2a3..0000000000 --- a/src/api/endpoints/posts.ts +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post, { pack } from '../models/post'; - -/** - * Lists all posts - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'reply' parameter - const [reply, replyErr] = $(params.reply).optional.boolean().$; - if (replyErr) return rej('invalid reply param'); - - // Get 'repost' parameter - const [repost, repostErr] = $(params.repost).optional.boolean().$; - if (repostErr) return rej('invalid repost param'); - - // Get 'media' parameter - const [media, mediaErr] = $(params.media).optional.boolean().$; - if (mediaErr) return rej('invalid media param'); - - // Get 'poll' parameter - const [poll, pollErr] = $(params.poll).optional.boolean().$; - if (pollErr) return rej('invalid poll param'); - - // Get 'bot' parameter - //const [bot, botErr] = $(params.bot).optional.boolean().$; - //if (botErr) return rej('invalid bot param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = {} as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - if (reply != undefined) { - query.reply_id = reply ? { $exists: true, $ne: null } : null; - } - - if (repost != undefined) { - query.repost_id = repost ? { $exists: true, $ne: null } : null; - } - - if (media != undefined) { - query.media_ids = media ? { $exists: true, $ne: null } : null; - } - - if (poll != undefined) { - query.poll = poll ? { $exists: true, $ne: null } : null; - } - - // TODO - //if (bot != undefined) { - // query.is_bot = bot; - //} - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async post => await pack(post)))); -}); diff --git a/src/api/endpoints/posts/categorize.ts b/src/api/endpoints/posts/categorize.ts deleted file mode 100644 index 0c85c2b4e0..0000000000 --- a/src/api/endpoints/posts/categorize.ts +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../models/post'; - -/** - * Categorize a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - if (!user.account.is_pro) { - return rej('This endpoint is available only from a Pro account'); - } - - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get categorizee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - if (post.is_category_verified) { - return rej('This post already has the verified category'); - } - - // Get 'category' parameter - const [category, categoryErr] = $(params.category).string().or([ - 'music', 'game', 'anime', 'it', 'gadgets', 'photography' - ]).$; - if (categoryErr) return rej('invalid category param'); - - // Set category - Post.update({ _id: post._id }, { - $set: { - category: category, - is_category_verified: true - } - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts deleted file mode 100644 index 5ba3758975..0000000000 --- a/src/api/endpoints/posts/context.ts +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post, { pack } from '../../models/post'; - -/** - * Show a context of a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const context = []; - let i = 0; - - async function get(id) { - i++; - const p = await Post.findOne({ _id: id }); - - if (i > offset) { - context.push(p); - } - - if (context.length == limit) { - return; - } - - if (p.reply_id) { - await get(p.reply_id); - } - } - - if (post.reply_id) { - await get(post.reply_id); - } - - // Serialize - res(await Promise.all(context.map(async post => - await pack(post, user)))); -}); diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts deleted file mode 100644 index 286e18bb76..0000000000 --- a/src/api/endpoints/posts/create.ts +++ /dev/null @@ -1,536 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import deepEqual = require('deep-equal'); -import parse from '../../common/text'; -import { default as Post, IPost, isValidText } from '../../models/post'; -import { default as User, ILocalAccount, IUser } from '../../models/user'; -import { default as Channel, IChannel } from '../../models/channel'; -import Following from '../../models/following'; -import Mute from '../../models/mute'; -import DriveFile from '../../models/drive-file'; -import Watching from '../../models/post-watching'; -import ChannelWatching from '../../models/channel-watching'; -import { pack } from '../../models/post'; -import notify from '../../common/notify'; -import watch from '../../common/watch-post'; -import event, { pushSw, publishChannelStream } from '../../event'; -import getAcct from '../../../common/user/get-acct'; -import parseAcct from '../../../common/user/parse-acct'; -import config from '../../../conf'; - -/** - * Create a post - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise<any>} - */ -module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => { - // Get 'text' parameter - const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$; - if (textErr) return rej('invalid text'); - - // Get 'via_mobile' parameter - const [viaMobile = false, viaMobileErr] = $(params.via_mobile).optional.boolean().$; - if (viaMobileErr) return rej('invalid via_mobile'); - - // Get 'tags' parameter - const [tags = [], tagsErr] = $(params.tags).optional.array('string').unique().eachQ(t => t.range(1, 32)).$; - if (tagsErr) return rej('invalid tags'); - - // Get 'geo' parameter - const [geo, geoErr] = $(params.geo).optional.nullable.strict.object() - .have('latitude', $().number().range(-90, 90)) - .have('longitude', $().number().range(-180, 180)) - .have('altitude', $().nullable.number()) - .have('accuracy', $().nullable.number()) - .have('altitudeAccuracy', $().nullable.number()) - .have('heading', $().nullable.number().range(0, 360)) - .have('speed', $().nullable.number()) - .$; - if (geoErr) return rej('invalid geo'); - - // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = $(params.media_ids).optional.array('id').unique().range(1, 4).$; - if (mediaIdsErr) return rej('invalid media_ids'); - - let files = []; - if (mediaIds !== undefined) { - // Fetch files - // forEach だと途中でエラーなどがあっても return できないので - // 敢えて for を使っています。 - for (const mediaId of mediaIds) { - // Fetch file - // SELECT _id - const entity = await DriveFile.findOne({ - _id: mediaId, - 'metadata.user_id': user._id - }); - - if (entity === null) { - return rej('file not found'); - } else { - files.push(entity); - } - } - } else { - files = null; - } - - // Get 'repost_id' parameter - const [repostId, repostIdErr] = $(params.repost_id).optional.id().$; - if (repostIdErr) return rej('invalid repost_id'); - - let repost: IPost = null; - let isQuote = false; - if (repostId !== undefined) { - // Fetch repost to post - repost = await Post.findOne({ - _id: repostId - }); - - if (repost == null) { - return rej('repostee is not found'); - } else if (repost.repost_id && !repost.text && !repost.media_ids) { - return rej('cannot repost to repost'); - } - - // Fetch recently post - const latestPost = await Post.findOne({ - user_id: user._id - }, { - sort: { - _id: -1 - } - }); - - isQuote = text != null || files != null; - - // 直近と同じRepost対象かつ引用じゃなかったらエラー - if (latestPost && - latestPost.repost_id && - latestPost.repost_id.equals(repost._id) && - !isQuote) { - return rej('cannot repost same post that already reposted in your latest post'); - } - - // 直近がRepost対象かつ引用じゃなかったらエラー - if (latestPost && - latestPost._id.equals(repost._id) && - !isQuote) { - return rej('cannot repost your latest post'); - } - } - - // Get 'reply_id' parameter - const [replyId, replyIdErr] = $(params.reply_id).optional.id().$; - if (replyIdErr) return rej('invalid reply_id'); - - let reply: IPost = null; - if (replyId !== undefined) { - // Fetch reply - reply = await Post.findOne({ - _id: replyId - }); - - if (reply === null) { - return rej('in reply to post is not found'); - } - - // 返信対象が引用でないRepostだったらエラー - if (reply.repost_id && !reply.text && !reply.media_ids) { - return rej('cannot reply to repost'); - } - } - - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).optional.id().$; - if (channelIdErr) return rej('invalid channel_id'); - - let channel: IChannel = null; - if (channelId !== undefined) { - // Fetch channel - channel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - - // 返信対象の投稿がこのチャンネルじゃなかったらダメ - if (reply && !channelId.equals(reply.channel_id)) { - return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません'); - } - - // Repost対象の投稿がこのチャンネルじゃなかったらダメ - if (repost && !channelId.equals(repost.channel_id)) { - return rej('チャンネル内部からチャンネル外部の投稿をRepostすることはできません'); - } - - // 引用ではないRepostはダメ - if (repost && !isQuote) { - return rej('チャンネル内部では引用ではないRepostをすることはできません'); - } - } else { - // 返信対象の投稿がチャンネルへの投稿だったらダメ - if (reply && reply.channel_id != null) { - return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません'); - } - - // Repost対象の投稿がチャンネルへの投稿だったらダメ - if (repost && repost.channel_id != null) { - return rej('チャンネル外部からチャンネル内部の投稿をRepostすることはできません'); - } - } - - // Get 'poll' parameter - const [poll, pollErr] = $(params.poll).optional.strict.object() - .have('choices', $().array('string') - .unique() - .range(2, 10) - .each(c => c.length > 0 && c.length < 50)) - .$; - if (pollErr) return rej('invalid poll'); - - if (poll) { - (poll as any).choices = (poll as any).choices.map((choice, i) => ({ - id: i, // IDを付与 - text: choice.trim(), - votes: 0 - })); - } - - // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー - if (text === undefined && files === null && repost === null && poll === undefined) { - return rej('text, media_ids, repost_id or poll is required'); - } - - // 直近の投稿と重複してたらエラー - // TODO: 直近の投稿が一日前くらいなら重複とは見なさない - if (user.latest_post) { - if (deepEqual({ - text: user.latest_post.text, - reply: user.latest_post.reply_id ? user.latest_post.reply_id.toString() : null, - repost: user.latest_post.repost_id ? user.latest_post.repost_id.toString() : null, - media_ids: (user.latest_post.media_ids || []).map(id => id.toString()) - }, { - text: text, - reply: reply ? reply._id.toString() : null, - repost: repost ? repost._id.toString() : null, - media_ids: (files || []).map(file => file._id.toString()) - })) { - return rej('duplicate'); - } - } - - let tokens = null; - if (text) { - // Analyze - tokens = parse(text); - - // Extract hashtags - const hashtags = tokens - .filter(t => t.type == 'hashtag') - .map(t => t.hashtag); - - hashtags.forEach(tag => { - if (tags.indexOf(tag) == -1) { - tags.push(tag); - } - }); - } - - // 投稿を作成 - const post = await Post.insert({ - created_at: new Date(), - channel_id: channel ? channel._id : undefined, - index: channel ? channel.index + 1 : undefined, - media_ids: files ? files.map(file => file._id) : undefined, - reply_id: reply ? reply._id : undefined, - repost_id: repost ? repost._id : undefined, - poll: poll, - text: text, - tags: tags, - user_id: user._id, - app_id: app ? app._id : null, - via_mobile: viaMobile, - geo, - - // 以下非正規化データ - _reply: reply ? { user_id: reply.user_id } : undefined, - _repost: repost ? { user_id: repost.user_id } : undefined, - }); - - // Serialize - const postObj = await pack(post); - - // Reponse - res({ - created_post: postObj - }); - - //#region Post processes - - User.update({ _id: user._id }, { - $set: { - latest_post: post - } - }); - - const mentions = []; - - async function addMention(mentionee, reason) { - // Reject if already added - if (mentions.some(x => x.equals(mentionee))) return; - - // Add mention - mentions.push(mentionee); - - // Publish event - if (!user._id.equals(mentionee)) { - const mentioneeMutes = await Mute.find({ - muter_id: mentionee, - deleted_at: { $exists: false } - }); - const mentioneesMutedUserIds = mentioneeMutes.map(m => m.mutee_id.toString()); - if (mentioneesMutedUserIds.indexOf(user._id.toString()) == -1) { - event(mentionee, reason, postObj); - pushSw(mentionee, reason, postObj); - } - } - } - - // タイムラインへの投稿 - if (!channel) { - // Publish event to myself's stream - event(user._id, 'post', postObj); - - // Fetch all followers - const followers = await Following - .find({ - followee_id: user._id, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - follower_id: true, - _id: false - }); - - // Publish event to followers stream - followers.forEach(following => - event(following.follower_id, 'post', postObj)); - } - - // チャンネルへの投稿 - if (channel) { - // Increment channel index(posts count) - Channel.update({ _id: channel._id }, { - $inc: { - index: 1 - } - }); - - // Publish event to channel - publishChannelStream(channel._id, 'post', postObj); - - // Get channel watchers - const watches = await ChannelWatching.find({ - channel_id: channel._id, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }); - - // チャンネルの視聴者(のタイムライン)に配信 - watches.forEach(w => { - event(w.user_id, 'post', postObj); - }); - } - - // Increment my posts count - User.update({ _id: user._id }, { - $inc: { - posts_count: 1 - } - }); - - // If has in reply to post - if (reply) { - // Increment replies count - Post.update({ _id: reply._id }, { - $inc: { - replies_count: 1 - } - }); - - // 自分自身へのリプライでない限りは通知を作成 - notify(reply.user_id, user._id, 'reply', { - post_id: post._id - }); - - // Fetch watchers - Watching - .find({ - post_id: reply._id, - user_id: { $ne: user._id }, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - fields: { - user_id: true - } - }) - .then(watchers => { - watchers.forEach(watcher => { - notify(watcher.user_id, user._id, 'reply', { - post_id: post._id - }); - }); - }); - - // この投稿をWatchする - if ((user.account as ILocalAccount).settings.auto_watch !== false) { - watch(user._id, reply); - } - - // Add mention - addMention(reply.user_id, 'reply'); - } - - // If it is repost - if (repost) { - // Notify - const type = text ? 'quote' : 'repost'; - notify(repost.user_id, user._id, type, { - post_id: post._id - }); - - // Fetch watchers - Watching - .find({ - post_id: repost._id, - user_id: { $ne: user._id }, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - fields: { - user_id: true - } - }) - .then(watchers => { - watchers.forEach(watcher => { - notify(watcher.user_id, user._id, type, { - post_id: post._id - }); - }); - }); - - // この投稿をWatchする - // TODO: ユーザーが「Repostしたときに自動でWatchする」設定を - // オフにしていた場合はしない - watch(user._id, repost); - - // If it is quote repost - if (text) { - // Add mention - addMention(repost.user_id, 'quote'); - } else { - // Publish event - if (!user._id.equals(repost.user_id)) { - event(repost.user_id, 'repost', postObj); - } - } - - // 今までで同じ投稿をRepostしているか - const existRepost = await Post.findOne({ - user_id: user._id, - repost_id: repost._id, - _id: { - $ne: post._id - } - }); - - if (!existRepost) { - // Update repostee status - Post.update({ _id: repost._id }, { - $inc: { - repost_count: 1 - } - }); - } - } - - // If has text content - if (text) { - /* - // Extract a hashtags - const hashtags = tokens - .filter(t => t.type == 'hashtag') - .map(t => t.hashtag) - // Drop dupulicates - .filter((v, i, s) => s.indexOf(v) == i); - - // ハッシュタグをデータベースに登録 - registerHashtags(user, hashtags); - */ - // Extract an '@' mentions - const atMentions = tokens - .filter(t => t.type == 'mention') - .map(getAcct) - // Drop dupulicates - .filter((v, i, s) => s.indexOf(v) == i); - - // Resolve all mentions - await Promise.all(atMentions.map(async (mention) => { - // Fetch mentioned user - // SELECT _id - const mentionee = await User - .findOne(parseAcct(mention), { _id: true }); - - // When mentioned user not found - if (mentionee == null) return; - - // 既に言及されたユーザーに対する返信や引用repostの場合も無視 - if (reply && reply.user_id.equals(mentionee._id)) return; - if (repost && repost.user_id.equals(mentionee._id)) return; - - // Add mention - addMention(mentionee._id, 'mention'); - - // Create notification - notify(mentionee._id, user._id, 'mention', { - post_id: post._id - }); - - return; - })); - } - - // Register to search database - if (text && config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'post', - id: post._id.toString(), - body: { - text: post.text - } - }); - } - - // Append mentions data - if (mentions.length > 0) { - Post.update({ _id: post._id }, { - $set: { - mentions: mentions - } - }); - } - - //#endregion -}); diff --git a/src/api/endpoints/posts/favorites/create.ts b/src/api/endpoints/posts/favorites/create.ts deleted file mode 100644 index f9dee271b5..0000000000 --- a/src/api/endpoints/posts/favorites/create.ts +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Favorite from '../../../models/favorite'; -import Post from '../../../models/post'; - -/** - * Favorite a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get favoritee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // if already favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist !== null) { - return rej('already favorited'); - } - - // Create favorite - await Favorite.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/posts/favorites/delete.ts b/src/api/endpoints/posts/favorites/delete.ts deleted file mode 100644 index c4fe7d3234..0000000000 --- a/src/api/endpoints/posts/favorites/delete.ts +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Favorite from '../../../models/favorite'; -import Post from '../../../models/post'; - -/** - * Unfavorite a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get favoritee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // if already favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist === null) { - return rej('already not favorited'); - } - - // Delete favorite - await Favorite.deleteOne({ - _id: exist._id - }); - - // Send response - res(); -}); diff --git a/src/api/endpoints/posts/mentions.ts b/src/api/endpoints/posts/mentions.ts deleted file mode 100644 index 7127db0ad1..0000000000 --- a/src/api/endpoints/posts/mentions.ts +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../models/post'; -import getFriends from '../../common/get-friends'; -import { pack } from '../../models/post'; - -/** - * Get mentions of myself - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'following' parameter - const [following = false, followingError] = - $(params.following).optional.boolean().$; - if (followingError) return rej('invalid following param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Construct query - const query = { - mentions: user._id - } as any; - - const sort = { - _id: -1 - }; - - if (following) { - const followingIds = await getFriends(user._id); - - query.user_id = { - $in: followingIds - }; - } - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const mentions = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(mentions.map(async mention => - await pack(mention, user) - ))); -}); diff --git a/src/api/endpoints/posts/polls/recommendation.ts b/src/api/endpoints/posts/polls/recommendation.ts deleted file mode 100644 index 4a3fa3f55e..0000000000 --- a/src/api/endpoints/posts/polls/recommendation.ts +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Vote from '../../../models/poll-vote'; -import Post, { pack } from '../../../models/post'; - -/** - * Get recommended polls - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get votes - const votes = await Vote.find({ - user_id: user._id - }, { - fields: { - _id: false, - post_id: true - } - }); - - const nin = votes && votes.length != 0 ? votes.map(v => v.post_id) : []; - - const posts = await Post - .find({ - _id: { - $nin: nin - }, - user_id: { - $ne: user._id - }, - poll: { - $exists: true, - $ne: null - } - }, { - limit: limit, - skip: offset, - sort: { - _id: -1 - } - }); - - // Serialize - res(await Promise.all(posts.map(async post => - await pack(post, user, { detail: true })))); -}); diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts deleted file mode 100644 index 16ce76a6fa..0000000000 --- a/src/api/endpoints/posts/polls/vote.ts +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Vote from '../../../models/poll-vote'; -import Post from '../../../models/post'; -import Watching from '../../../models/post-watching'; -import notify from '../../../common/notify'; -import watch from '../../../common/watch-post'; -import { publishPostStream } from '../../../event'; - -/** - * Vote poll of a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get votee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - if (post.poll == null) { - return rej('poll not found'); - } - - // Get 'choice' parameter - const [choice, choiceError] = - $(params.choice).number() - .pipe(c => post.poll.choices.some(x => x.id == c)) - .$; - if (choiceError) return rej('invalid choice param'); - - // if already voted - const exist = await Vote.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist !== null) { - return rej('already voted'); - } - - // Create vote - await Vote.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id, - choice: choice - }); - - // Send response - res(); - - const inc = {}; - inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1; - - // Increment votes count - await Post.update({ _id: post._id }, { - $inc: inc - }); - - publishPostStream(post._id, 'poll_voted'); - - // Notify - notify(post.user_id, user._id, 'poll_vote', { - post_id: post._id, - choice: choice - }); - - // Fetch watchers - Watching - .find({ - post_id: post._id, - user_id: { $ne: user._id }, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - fields: { - user_id: true - } - }) - .then(watchers => { - watchers.forEach(watcher => { - notify(watcher.user_id, user._id, 'poll_vote', { - post_id: post._id, - choice: choice - }); - }); - }); - - // この投稿をWatchする - if (user.account.settings.auto_watch !== false) { - watch(user._id, post); - } -}); - -function findWithAttr(array, attr, value) { - for (let i = 0; i < array.length; i += 1) { - if (array[i][attr] === value) { - return i; - } - } - return -1; -} diff --git a/src/api/endpoints/posts/reactions.ts b/src/api/endpoints/posts/reactions.ts deleted file mode 100644 index feb140ab41..0000000000 --- a/src/api/endpoints/posts/reactions.ts +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../models/post'; -import Reaction, { pack } from '../../models/post-reaction'; - -/** - * Show reactions of a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'sort' parameter - const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$; - if (sortError) return rej('invalid sort param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // Issue query - const reactions = await Reaction - .find({ - post_id: post._id, - deleted_at: { $exists: false } - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(reactions.map(async reaction => - await pack(reaction, user)))); -}); diff --git a/src/api/endpoints/posts/reactions/create.ts b/src/api/endpoints/posts/reactions/create.ts deleted file mode 100644 index f77afed40c..0000000000 --- a/src/api/endpoints/posts/reactions/create.ts +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Reaction from '../../../models/post-reaction'; -import Post, { pack as packPost } from '../../../models/post'; -import { pack as packUser } from '../../../models/user'; -import Watching from '../../../models/post-watching'; -import notify from '../../../common/notify'; -import watch from '../../../common/watch-post'; -import { publishPostStream, pushSw } from '../../../event'; - -/** - * React to a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get 'reaction' parameter - const [reaction, reactionErr] = $(params.reaction).string().or([ - 'like', - 'love', - 'laugh', - 'hmm', - 'surprise', - 'congrats', - 'angry', - 'confused', - 'pudding' - ]).$; - if (reactionErr) return rej('invalid reaction param'); - - // Fetch reactee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // Myself - if (post.user_id.equals(user._id)) { - return rej('cannot react to my post'); - } - - // if already reacted - const exist = await Reaction.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already reacted'); - } - - // Create reaction - await Reaction.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id, - reaction: reaction - }); - - // Send response - res(); - - const inc = {}; - inc[`reaction_counts.${reaction}`] = 1; - - // Increment reactions count - await Post.update({ _id: post._id }, { - $inc: inc - }); - - publishPostStream(post._id, 'reacted'); - - // Notify - notify(post.user_id, user._id, 'reaction', { - post_id: post._id, - reaction: reaction - }); - - pushSw(post.user_id, 'reaction', { - user: await packUser(user, post.user_id), - post: await packPost(post, post.user_id), - reaction: reaction - }); - - // Fetch watchers - Watching - .find({ - post_id: post._id, - user_id: { $ne: user._id }, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - fields: { - user_id: true - } - }) - .then(watchers => { - watchers.forEach(watcher => { - notify(watcher.user_id, user._id, 'reaction', { - post_id: post._id, - reaction: reaction - }); - }); - }); - - // この投稿をWatchする - if (user.account.settings.auto_watch !== false) { - watch(user._id, post); - } -}); diff --git a/src/api/endpoints/posts/reactions/delete.ts b/src/api/endpoints/posts/reactions/delete.ts deleted file mode 100644 index 922c57ab18..0000000000 --- a/src/api/endpoints/posts/reactions/delete.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Reaction from '../../../models/post-reaction'; -import Post from '../../../models/post'; -// import event from '../../../event'; - -/** - * Unreact to a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Fetch unreactee - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // if already unreacted - const exist = await Reaction.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('never reacted'); - } - - // Delete reaction - await Reaction.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - const dec = {}; - dec[`reaction_counts.${exist.reaction}`] = -1; - - // Decrement reactions count - Post.update({ _id: post._id }, { - $inc: dec - }); -}); diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts deleted file mode 100644 index 613c4fa24c..0000000000 --- a/src/api/endpoints/posts/replies.ts +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post, { pack } from '../../models/post'; - -/** - * Show a replies of a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'sort' parameter - const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$; - if (sortError) return rej('invalid sort param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // Issue query - const replies = await Post - .find({ reply_id: post._id }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(replies.map(async post => - await pack(post, user)))); -}); diff --git a/src/api/endpoints/posts/reposts.ts b/src/api/endpoints/posts/reposts.ts deleted file mode 100644 index 89ab0e3d55..0000000000 --- a/src/api/endpoints/posts/reposts.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post, { pack } from '../../models/post'; - -/** - * Show a reposts of a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - repost_id: post._id - } as any; - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - - // Issue query - const reposts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(reposts.map(async post => - await pack(post, user)))); -}); diff --git a/src/api/endpoints/posts/search.ts b/src/api/endpoints/posts/search.ts deleted file mode 100644 index a36d1178a5..0000000000 --- a/src/api/endpoints/posts/search.ts +++ /dev/null @@ -1,364 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -const escapeRegexp = require('escape-regexp'); -import Post from '../../models/post'; -import User from '../../models/user'; -import Mute from '../../models/mute'; -import getFriends from '../../common/get-friends'; -import { pack } from '../../models/post'; - -/** - * Search a post - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'text' parameter - const [text, textError] = $(params.text).optional.string().$; - if (textError) return rej('invalid text param'); - - // Get 'include_user_ids' parameter - const [includeUserIds = [], includeUserIdsErr] = $(params.include_user_ids).optional.array('id').$; - if (includeUserIdsErr) return rej('invalid include_user_ids param'); - - // Get 'exclude_user_ids' parameter - const [excludeUserIds = [], excludeUserIdsErr] = $(params.exclude_user_ids).optional.array('id').$; - if (excludeUserIdsErr) return rej('invalid exclude_user_ids param'); - - // Get 'include_user_usernames' parameter - const [includeUserUsernames = [], includeUserUsernamesErr] = $(params.include_user_usernames).optional.array('string').$; - if (includeUserUsernamesErr) return rej('invalid include_user_usernames param'); - - // Get 'exclude_user_usernames' parameter - const [excludeUserUsernames = [], excludeUserUsernamesErr] = $(params.exclude_user_usernames).optional.array('string').$; - if (excludeUserUsernamesErr) return rej('invalid exclude_user_usernames param'); - - // Get 'following' parameter - const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$; - if (followingErr) return rej('invalid following param'); - - // Get 'mute' parameter - const [mute = 'mute_all', muteErr] = $(params.mute).optional.string().$; - if (muteErr) return rej('invalid mute param'); - - // Get 'reply' parameter - const [reply = null, replyErr] = $(params.reply).optional.nullable.boolean().$; - if (replyErr) return rej('invalid reply param'); - - // Get 'repost' parameter - const [repost = null, repostErr] = $(params.repost).optional.nullable.boolean().$; - if (repostErr) return rej('invalid repost param'); - - // Get 'media' parameter - const [media = null, mediaErr] = $(params.media).optional.nullable.boolean().$; - if (mediaErr) return rej('invalid media param'); - - // Get 'poll' parameter - const [poll = null, pollErr] = $(params.poll).optional.nullable.boolean().$; - if (pollErr) return rej('invalid poll param'); - - // Get 'since_date' parameter - const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$; - if (sinceDateErr) throw 'invalid since_date param'; - - // Get 'until_date' parameter - const [untilDate, untilDateErr] = $(params.until_date).optional.number().$; - if (untilDateErr) throw 'invalid until_date param'; - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 30).$; - 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({ - username_lower: 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({ - username_lower: username.toLowerCase() - }); - return _user ? _user._id : null; - }))).filter(id => id != null); - excludeUsers = excludeUsers.concat(ids); - } - - search(res, rej, me, text, includeUsers, excludeUsers, following, - mute, reply, repost, media, poll, sinceDate, untilDate, offset, limit); -}); - -async function search( - res, rej, me, text, includeUserIds, excludeUserIds, following, - mute, reply, repost, media, poll, sinceDate, untilDate, offset, max) { - - let q: any = { - $and: [] - }; - - const push = x => q.$and.push(x); - - if (text) { - // 完全一致検索 - if (/"""(.+?)"""/.test(text)) { - const x = text.match(/"""(.+?)"""/)[1]; - push({ - text: x - }); - } else { - const tags = text.split(' ').filter(x => x[0] == '#'); - if (tags) { - push({ - $and: tags.map(x => ({ - tags: x - })) - }); - } - - push({ - $and: text.split(' ').map(x => ({ - // キーワードが-で始まる場合そのキーワードを除外する - text: x[0] == '-' ? { - $not: new RegExp(escapeRegexp(x.substr(1))) - } : new RegExp(escapeRegexp(x)) - })) - }); - } - } - - if (includeUserIds && includeUserIds.length != 0) { - push({ - user_id: { - $in: includeUserIds - } - }); - } else if (excludeUserIds && excludeUserIds.length != 0) { - push({ - user_id: { - $nin: excludeUserIds - } - }); - } - - if (following != null && me != null) { - const ids = await getFriends(me._id, false); - push({ - user_id: following ? { - $in: ids - } : { - $nin: ids.concat(me._id) - } - }); - } - - if (me != null) { - const mutes = await Mute.find({ - muter_id: me._id, - deleted_at: { $exists: false } - }); - const mutedUserIds = mutes.map(m => m.mutee_id); - - switch (mute) { - case 'mute_all': - push({ - user_id: { - $nin: mutedUserIds - }, - '_reply.user_id': { - $nin: mutedUserIds - }, - '_repost.user_id': { - $nin: mutedUserIds - } - }); - break; - case 'mute_related': - push({ - '_reply.user_id': { - $nin: mutedUserIds - }, - '_repost.user_id': { - $nin: mutedUserIds - } - }); - break; - case 'mute_direct': - push({ - user_id: { - $nin: mutedUserIds - } - }); - break; - case 'direct_only': - push({ - user_id: { - $in: mutedUserIds - } - }); - break; - case 'related_only': - push({ - $or: [{ - '_reply.user_id': { - $in: mutedUserIds - } - }, { - '_repost.user_id': { - $in: mutedUserIds - } - }] - }); - break; - case 'all_only': - push({ - $or: [{ - user_id: { - $in: mutedUserIds - } - }, { - '_reply.user_id': { - $in: mutedUserIds - } - }, { - '_repost.user_id': { - $in: mutedUserIds - } - }] - }); - break; - } - } - - if (reply != null) { - if (reply) { - push({ - reply_id: { - $exists: true, - $ne: null - } - }); - } else { - push({ - $or: [{ - reply_id: { - $exists: false - } - }, { - reply_id: null - }] - }); - } - } - - if (repost != null) { - if (repost) { - push({ - repost_id: { - $exists: true, - $ne: null - } - }); - } else { - push({ - $or: [{ - repost_id: { - $exists: false - } - }, { - repost_id: null - }] - }); - } - } - - if (media != null) { - if (media) { - push({ - media_ids: { - $exists: true, - $ne: null - } - }); - } else { - push({ - $or: [{ - media_ids: { - $exists: false - } - }, { - media_ids: null - }] - }); - } - } - - if (poll != null) { - if (poll) { - push({ - poll: { - $exists: true, - $ne: null - } - }); - } else { - push({ - $or: [{ - poll: { - $exists: false - } - }, { - poll: null - }] - }); - } - } - - if (sinceDate) { - push({ - created_at: { - $gt: new Date(sinceDate) - } - }); - } - - if (untilDate) { - push({ - created_at: { - $lt: new Date(untilDate) - } - }); - } - - if (q.$and.length == 0) { - q = {}; - } - - // Search posts - const posts = await Post - .find(q, { - sort: { - _id: -1 - }, - limit: max, - skip: offset - }); - - // Serialize - res(await Promise.all(posts.map(async post => - await pack(post, me)))); -} diff --git a/src/api/endpoints/posts/show.ts b/src/api/endpoints/posts/show.ts deleted file mode 100644 index 3839490597..0000000000 --- a/src/api/endpoints/posts/show.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post, { pack } from '../../models/post'; - -/** - * Show a post - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'post_id' parameter - const [postId, postIdErr] = $(params.post_id).id().$; - if (postIdErr) return rej('invalid post_id param'); - - // Get post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - // Serialize - res(await pack(post, user, { - detail: true - })); -}); diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts deleted file mode 100644 index c41cfdb8bd..0000000000 --- a/src/api/endpoints/posts/timeline.ts +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import rap from '@prezzemolo/rap'; -import Post from '../../models/post'; -import Mute from '../../models/mute'; -import ChannelWatching from '../../models/channel-watching'; -import getFriends from '../../common/get-friends'; -import { pack } from '../../models/post'; - -/** - * Get timeline of myself - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise<any>} - */ -module.exports = async (params, user, app) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) throw 'invalid limit param'; - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) throw 'invalid since_id param'; - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) throw 'invalid until_id param'; - - // Get 'since_date' parameter - const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$; - if (sinceDateErr) throw 'invalid since_date param'; - - // Get 'until_date' parameter - const [untilDate, untilDateErr] = $(params.until_date).optional.number().$; - if (untilDateErr) throw 'invalid until_date param'; - - // Check if only one of since_id, until_id, since_date, until_date specified - if ([sinceId, untilId, sinceDate, untilDate].filter(x => x != null).length > 1) { - throw 'only one of since_id, until_id, since_date, until_date can be specified'; - } - - const { followingIds, watchingChannelIds, mutedUserIds } = await rap({ - // ID list of the user itself and other users who the user follows - followingIds: getFriends(user._id), - - // Watchしているチャンネルを取得 - watchingChannelIds: ChannelWatching.find({ - user_id: user._id, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }).then(watches => watches.map(w => w.channel_id)), - - // ミュートしているユーザーを取得 - mutedUserIds: Mute.find({ - muter_id: user._id, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }).then(ms => ms.map(m => m.mutee_id)) - }); - - //#region Construct query - const sort = { - _id: -1 - }; - - const query = { - $or: [{ - // フォローしている人のタイムラインへの投稿 - user_id: { - $in: followingIds - }, - // 「タイムラインへの」投稿に限定するためにチャンネルが指定されていないもののみに限る - $or: [{ - channel_id: { - $exists: false - } - }, { - channel_id: null - }] - }, { - // Watchしているチャンネルへの投稿 - channel_id: { - $in: watchingChannelIds - } - }], - // mute - user_id: { - $nin: mutedUserIds - }, - '_reply.user_id': { - $nin: mutedUserIds - }, - '_repost.user_id': { - $nin: mutedUserIds - }, - } as any; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } else if (sinceDate) { - sort._id = 1; - query.created_at = { - $gt: new Date(sinceDate) - }; - } else if (untilDate) { - query.created_at = { - $lt: new Date(untilDate) - }; - } - //#endregion - - // Issue query - const timeline = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - return await Promise.all(timeline.map(post => pack(post, user))); -}; diff --git a/src/api/endpoints/posts/trend.ts b/src/api/endpoints/posts/trend.ts deleted file mode 100644 index caded92bf5..0000000000 --- a/src/api/endpoints/posts/trend.ts +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Module dependencies - */ -const ms = require('ms'); -import $ from 'cafy'; -import Post, { pack } from '../../models/post'; - -/** - * Get trend posts - * - * @param {any} params - * @param {any} user - * @return {Promise<any>} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'reply' parameter - const [reply, replyErr] = $(params.reply).optional.boolean().$; - if (replyErr) return rej('invalid reply param'); - - // Get 'repost' parameter - const [repost, repostErr] = $(params.repost).optional.boolean().$; - if (repostErr) return rej('invalid repost param'); - - // Get 'media' parameter - const [media, mediaErr] = $(params.media).optional.boolean().$; - if (mediaErr) return rej('invalid media param'); - - // Get 'poll' parameter - const [poll, pollErr] = $(params.poll).optional.boolean().$; - if (pollErr) return rej('invalid poll param'); - - const query = { - created_at: { - $gte: new Date(Date.now() - ms('1days')) - }, - repost_count: { - $gt: 0 - } - } as any; - - if (reply != undefined) { - query.reply_id = reply ? { $exists: true, $ne: null } : null; - } - - if (repost != undefined) { - query.repost_id = repost ? { $exists: true, $ne: null } : null; - } - - if (media != undefined) { - query.media_ids = media ? { $exists: true, $ne: null } : null; - } - - if (poll != undefined) { - query.poll = poll ? { $exists: true, $ne: null } : null; - } - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - skip: offset, - sort: { - repost_count: -1, - _id: -1 - } - }); - - // Serialize - res(await Promise.all(posts.map(async post => - await pack(post, user, { detail: true })))); -}); diff --git a/src/api/endpoints/stats.ts b/src/api/endpoints/stats.ts deleted file mode 100644 index a6084cd17a..0000000000 --- a/src/api/endpoints/stats.ts +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Module dependencies - */ -import Post from '../models/post'; -import User from '../models/user'; - -/** - * @swagger - * /stats: - * post: - * summary: Show the misskey's statistics - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * posts_count: - * description: count of all posts of misskey - * type: number - * users_count: - * description: count of all users of misskey - * type: number - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show the misskey's statistics - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = params => new Promise(async (res, rej) => { - const postsCount = await Post - .count(); - - const usersCount = await User - .count(); - - res({ - posts_count: postsCount, - users_count: usersCount - }); -}); diff --git a/src/api/endpoints/sw/register.ts b/src/api/endpoints/sw/register.ts deleted file mode 100644 index 99406138db..0000000000 --- a/src/api/endpoints/sw/register.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Subscription from '../../models/sw-subscription'; - -/** - * subscribe service worker - * - * @param {any} params - * @param {any} user - * @param {any} _ - * @param {boolean} isSecure - * @return {Promise<any>} - */ -module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { - // Get 'endpoint' parameter - const [endpoint, endpointErr] = $(params.endpoint).string().$; - if (endpointErr) return rej('invalid endpoint param'); - - // Get 'auth' parameter - const [auth, authErr] = $(params.auth).string().$; - if (authErr) return rej('invalid auth param'); - - // Get 'publickey' parameter - const [publickey, publickeyErr] = $(params.publickey).string().$; - if (publickeyErr) return rej('invalid publickey param'); - - // if already subscribed - const exist = await Subscription.findOne({ - user_id: user._id, - endpoint: endpoint, - auth: auth, - publickey: publickey, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return res(); - } - - await Subscription.insert({ - user_id: user._id, - endpoint: endpoint, - auth: auth, - publickey: publickey - }); - - res(); -}); diff --git a/src/api/endpoints/username/available.ts b/src/api/endpoints/username/available.ts deleted file mode 100644 index aac7fadf5a..0000000000 --- a/src/api/endpoints/username/available.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import { validateUsername } from '../../models/user'; - -/** - * Check available username - * - * @param {any} params - * @return {Promise<any>} - */ -module.exports = async (params) => new Promise(async (res, rej) => { - // Get 'username' parameter - const [username, usernameError] = $(params.username).string().pipe(validateUsername).$; - if (usernameError) return rej('invalid username param'); - - // Get exist - const exist = await User - .count({ - host: null, - username_lower: username.toLowerCase() - }, { - limit: 1 - }); - - // Reply - res({ - available: exist === 0 - }); -}); diff --git a/src/api/endpoints/users.ts b/src/api/endpoints/users.ts deleted file mode 100644 index 4acc13c281..0000000000 --- a/src/api/endpoints/users.ts +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { pack } from '../models/user'; - -/** - * Lists all users - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'sort' parameter - const [sort, sortError] = $(params.sort).optional.string().or('+follower|-follower').$; - if (sortError) return rej('invalid sort param'); - - // Construct query - let _sort; - if (sort) { - if (sort == '+follower') { - _sort = { - followers_count: -1 - }; - } else if (sort == '-follower') { - _sort = { - followers_count: 1 - }; - } - } else { - _sort = { - _id: -1 - }; - } - - // Issue query - const users = await User - .find({}, { - limit: limit, - sort: _sort, - skip: offset - }); - - // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me)))); -}); diff --git a/src/api/endpoints/users/followers.ts b/src/api/endpoints/users/followers.ts deleted file mode 100644 index b0fb83c683..0000000000 --- a/src/api/endpoints/users/followers.ts +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import Following from '../../models/following'; -import { pack } from '../../models/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get followers of a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Get 'iknow' parameter - const [iknow = false, iknowErr] = $(params.iknow).optional.boolean().$; - if (iknowErr) return rej('invalid iknow param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'cursor' parameter - const [cursor = null, cursorErr] = $(params.cursor).optional.id().$; - if (cursorErr) return rej('invalid cursor param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Construct query - const query = { - followee_id: user._id, - deleted_at: { $exists: false } - } as any; - - // ログインしていてかつ iknow フラグがあるとき - if (me && iknow) { - // Get my friends - const myFriends = await getFriends(me._id); - - query.follower_id = { - $in: myFriends - }; - } - - // カーソルが指定されている場合 - if (cursor) { - query._id = { - $lt: cursor - }; - } - - // Get followers - const following = await Following - .find(query, { - limit: limit + 1, - sort: { _id: -1 } - }); - - // 「次のページ」があるかどうか - const inStock = following.length === limit + 1; - if (inStock) { - following.pop(); - } - - // Serialize - const users = await Promise.all(following.map(async f => - await pack(f.follower_id, me, { detail: true }))); - - // Response - res({ - users: users, - next: inStock ? following[following.length - 1]._id : null, - }); -}); diff --git a/src/api/endpoints/users/following.ts b/src/api/endpoints/users/following.ts deleted file mode 100644 index 8e88431e92..0000000000 --- a/src/api/endpoints/users/following.ts +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User from '../../models/user'; -import Following from '../../models/following'; -import { pack } from '../../models/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get following users of a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Get 'iknow' parameter - const [iknow = false, iknowErr] = $(params.iknow).optional.boolean().$; - if (iknowErr) return rej('invalid iknow param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'cursor' parameter - const [cursor = null, cursorErr] = $(params.cursor).optional.id().$; - if (cursorErr) return rej('invalid cursor param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Construct query - const query = { - follower_id: user._id, - deleted_at: { $exists: false } - } as any; - - // ログインしていてかつ iknow フラグがあるとき - if (me && iknow) { - // Get my friends - const myFriends = await getFriends(me._id); - - query.followee_id = { - $in: myFriends - }; - } - - // カーソルが指定されている場合 - if (cursor) { - query._id = { - $lt: cursor - }; - } - - // Get followers - const following = await Following - .find(query, { - limit: limit + 1, - sort: { _id: -1 } - }); - - // 「次のページ」があるかどうか - const inStock = following.length === limit + 1; - if (inStock) { - following.pop(); - } - - // Serialize - const users = await Promise.all(following.map(async f => - await pack(f.followee_id, me, { detail: true }))); - - // Response - res({ - users: users, - next: inStock ? following[following.length - 1]._id : null, - }); -}); diff --git a/src/api/endpoints/users/get_frequently_replied_users.ts b/src/api/endpoints/users/get_frequently_replied_users.ts deleted file mode 100644 index 87f4f77a5b..0000000000 --- a/src/api/endpoints/users/get_frequently_replied_users.ts +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../models/post'; -import User, { pack } from '../../models/user'; - -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Lookup user - const user = await User.findOne({ - _id: userId - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Fetch recent posts - const recentPosts = await Post.find({ - user_id: user._id, - reply_id: { - $exists: true, - $ne: null - } - }, { - sort: { - _id: -1 - }, - limit: 1000, - fields: { - _id: false, - reply_id: true - } - }); - - // 投稿が少なかったら中断 - if (recentPosts.length === 0) { - return res([]); - } - - const replyTargetPosts = await Post.find({ - _id: { - $in: recentPosts.map(p => p.reply_id) - }, - user_id: { - $ne: user._id - } - }, { - fields: { - _id: false, - user_id: true - } - }); - - const repliedUsers = {}; - - // Extract replies from recent posts - replyTargetPosts.forEach(post => { - const userId = post.user_id.toString(); - if (repliedUsers[userId]) { - repliedUsers[userId]++; - } else { - repliedUsers[userId] = 1; - } - }); - - // Calc peak - let peak = 0; - Object.keys(repliedUsers).forEach(user => { - if (repliedUsers[user] > peak) peak = repliedUsers[user]; - }); - - // Sort replies by frequency - const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]); - - // Extract top replied users - const topRepliedUsers = repliedUsersSorted.slice(0, limit); - - // Make replies object (includes weights) - const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({ - user: await pack(user, me, { detail: true }), - weight: repliedUsers[user] / peak - }))); - - // Response - res(repliesObj); -}); diff --git a/src/api/endpoints/users/posts.ts b/src/api/endpoints/users/posts.ts deleted file mode 100644 index 3c84bf0d80..0000000000 --- a/src/api/endpoints/users/posts.ts +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import getHostLower from '../../common/get-host-lower'; -import Post, { pack } from '../../models/post'; -import User from '../../models/user'; - -/** - * Get posts of a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).optional.id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Get 'username' parameter - const [username, usernameErr] = $(params.username).optional.string().$; - if (usernameErr) return rej('invalid username param'); - - if (userId === undefined && username === undefined) { - return rej('user_id or pair of username and host is required'); - } - - // Get 'host' parameter - const [host, hostErr] = $(params.host).optional.string().$; - if (hostErr) return rej('invalid host param'); - - if (userId === undefined && host === undefined) { - return rej('user_id or pair of username and host is required'); - } - - // Get 'include_replies' parameter - const [includeReplies = true, includeRepliesErr] = $(params.include_replies).optional.boolean().$; - if (includeRepliesErr) return rej('invalid include_replies param'); - - // Get 'with_media' parameter - const [withMedia = false, withMediaErr] = $(params.with_media).optional.boolean().$; - if (withMediaErr) return rej('invalid with_media param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Get 'since_date' parameter - const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$; - if (sinceDateErr) throw 'invalid since_date param'; - - // Get 'until_date' parameter - const [untilDate, untilDateErr] = $(params.until_date).optional.number().$; - if (untilDateErr) throw 'invalid until_date param'; - - // Check if only one of since_id, until_id, since_date, until_date specified - if ([sinceId, untilId, sinceDate, untilDate].filter(x => x != null).length > 1) { - throw 'only one of since_id, until_id, since_date, until_date can be specified'; - } - - const q = userId !== undefined - ? { _id: userId } - : { username_lower: username.toLowerCase(), host_lower: getHostLower(host) } ; - - // Lookup user - const user = await User.findOne(q, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - //#region Construct query - const sort = { - _id: -1 - }; - - const query = { - user_id: user._id - } as any; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } else if (sinceDate) { - sort._id = 1; - query.created_at = { - $gt: new Date(sinceDate) - }; - } else if (untilDate) { - query.created_at = { - $lt: new Date(untilDate) - }; - } - - if (!includeReplies) { - query.reply_id = null; - } - - if (withMedia) { - query.media_ids = { - $exists: true, - $ne: null - }; - } - //#endregion - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async (post) => - await pack(post, me) - ))); -}); diff --git a/src/api/endpoints/users/recommendation.ts b/src/api/endpoints/users/recommendation.ts deleted file mode 100644 index 45d90f422b..0000000000 --- a/src/api/endpoints/users/recommendation.ts +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Module dependencies - */ -const ms = require('ms'); -import $ from 'cafy'; -import User, { pack } from '../../models/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get recommended users - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // ID list of the user itself and other users who the user follows - const followingIds = await getFriends(me._id); - - const users = await User - .find({ - _id: { - $nin: followingIds - }, - $or: [ - { - 'account.last_used_at': { - $gte: new Date(Date.now() - ms('7days')) - } - }, { - host: { $not: null } - } - ] - }, { - limit: limit, - skip: offset, - sort: { - followers_count: -1 - } - }); - - // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me, { detail: true })))); -}); diff --git a/src/api/endpoints/users/search.ts b/src/api/endpoints/users/search.ts deleted file mode 100644 index 39e2ff9890..0000000000 --- a/src/api/endpoints/users/search.ts +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import $ from 'cafy'; -import User, { pack } from '../../models/user'; -import config from '../../../conf'; -const escapeRegexp = require('escape-regexp'); - -/** - * Search a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'query' parameter - const [query, queryError] = $(params.query).string().pipe(x => x != '').$; - if (queryError) return rej('invalid query param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'max' parameter - const [max = 10, maxErr] = $(params.max).optional.number().range(1, 30).$; - if (maxErr) return rej('invalid max param'); - - // If Elasticsearch is available, search by $ - // If not, search by MongoDB - (config.elasticsearch.enable ? byElasticsearch : byNative) - (res, rej, me, query, offset, max); -}); - -// Search by MongoDB -async function byNative(res, rej, me, query, offset, max) { - const escapedQuery = escapeRegexp(query); - - // Search users - const users = await User - .find({ - $or: [{ - username_lower: new RegExp(escapedQuery.replace('@', '').toLowerCase()) - }, { - name: new RegExp(escapedQuery) - }] - }, { - limit: max - }); - - // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me, { detail: true })))); -} - -// Search by Elasticsearch -async function byElasticsearch(res, rej, me, query, offset, max) { - const es = require('../../db/elasticsearch'); - - es.search({ - index: 'misskey', - type: 'user', - body: { - size: max, - from: offset, - query: { - simple_query_string: { - fields: ['username', 'name', 'bio'], - query: query, - default_operator: 'and' - } - } - } - }, async (error, response) => { - if (error) { - console.error(error); - return res(500); - } - - if (response.hits.total === 0) { - return res([]); - } - - const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id)); - - const users = await User - .find({ - _id: { - $in: hits - } - }); - - // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me, { detail: true })))); - }); -} diff --git a/src/api/endpoints/users/search_by_username.ts b/src/api/endpoints/users/search_by_username.ts deleted file mode 100644 index 9c5e1905aa..0000000000 --- a/src/api/endpoints/users/search_by_username.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import User, { pack } from '../../models/user'; - -/** - * Search a user by username - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - // Get 'query' parameter - const [query, queryError] = $(params.query).string().$; - if (queryError) return rej('invalid query param'); - - // Get 'offset' parameter - const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$; - if (offsetErr) return rej('invalid offset param'); - - // Get 'limit' parameter - const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$; - if (limitErr) return rej('invalid limit param'); - - const users = await User - .find({ - username_lower: new RegExp(query.toLowerCase()) - }, { - limit: limit, - skip: offset - }); - - // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me, { detail: true })))); -}); diff --git a/src/api/endpoints/users/show.ts b/src/api/endpoints/users/show.ts deleted file mode 100644 index 78df23f339..0000000000 --- a/src/api/endpoints/users/show.ts +++ /dev/null @@ -1,209 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import { JSDOM } from 'jsdom'; -import { toUnicode, toASCII } from 'punycode'; -import uploadFromUrl from '../../common/drive/upload_from_url'; -import User, { pack, validateUsername, isValidName, isValidDescription } from '../../models/user'; -const request = require('request-promise-native'); -const WebFinger = require('webfinger.js'); - -const webFinger = new WebFinger({}); - -async function getCollectionCount(url) { - if (!url) { - return null; - } - - try { - const collection = await request({ url, json: true }); - return collection ? collection.totalItems : null; - } catch (exception) { - return null; - } -} - -function findUser(q) { - return User.findOne(q, { - fields: { - data: false - } - }); -} - -function webFingerAndVerify(query, verifier) { - return new Promise((res, rej) => webFinger.lookup(query, (error, result) => { - if (error) { - return rej(error); - } - - if (result.object.subject.toLowerCase().replace(/^acct:/, '') !== verifier) { - return rej('WebFinger verfification failed'); - } - - res(result.object); - })); -} - -/** - * Show a user - * - * @param {any} params - * @param {any} me - * @return {Promise<any>} - */ -module.exports = (params, me) => new Promise(async (res, rej) => { - let user; - - // Get 'user_id' parameter - const [userId, userIdErr] = $(params.user_id).optional.id().$; - if (userIdErr) return rej('invalid user_id param'); - - // Get 'username' parameter - const [username, usernameErr] = $(params.username).optional.string().$; - if (usernameErr) return rej('invalid username param'); - - // Get 'host' parameter - const [host, hostErr] = $(params.host).optional.string().$; - if (hostErr) return rej('invalid username param'); - - if (userId === undefined && typeof username !== 'string') { - return rej('user_id or pair of username and host is required'); - } - - // Lookup user - if (typeof host === 'string') { - const username_lower = username.toLowerCase(); - const host_lower_ascii = toASCII(host).toLowerCase(); - const host_lower = toUnicode(host_lower_ascii); - - user = await findUser({ username_lower, host_lower }); - - if (user === null) { - const acct_lower = `${username_lower}@${host_lower_ascii}`; - let activityStreams; - let finger; - let followers_count; - let following_count; - let likes_count; - let posts_count; - - if (!validateUsername(username)) { - return rej('username validation failed'); - } - - try { - finger = await webFingerAndVerify(acct_lower, acct_lower); - } catch (exception) { - return rej('WebFinger lookup failed'); - } - - const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self'); - if (!self) { - return rej('WebFinger has no reference to self representation'); - } - - try { - activityStreams = await request({ - url: self.href, - headers: { - Accept: 'application/activity+json, application/ld+json' - }, - json: true - }); - } catch (exception) { - return rej('failed to retrieve ActivityStreams representation'); - } - - if (!(activityStreams && - (Array.isArray(activityStreams['@context']) ? - activityStreams['@context'].includes('https://www.w3.org/ns/activitystreams') : - activityStreams['@context'] === 'https://www.w3.org/ns/activitystreams') && - activityStreams.type === 'Person' && - typeof activityStreams.preferredUsername === 'string' && - activityStreams.preferredUsername.toLowerCase() === username_lower && - isValidName(activityStreams.name) && - isValidDescription(activityStreams.summary) - )) { - return rej('failed ActivityStreams validation'); - } - - try { - [followers_count, following_count, likes_count, posts_count] = await Promise.all([ - getCollectionCount(activityStreams.followers), - getCollectionCount(activityStreams.following), - getCollectionCount(activityStreams.liked), - getCollectionCount(activityStreams.outbox), - webFingerAndVerify(activityStreams.id, acct_lower), - ]); - } catch (exception) { - return rej('failed to fetch assets'); - } - - const summaryDOM = JSDOM.fragment(activityStreams.summary); - - // Create user - user = await User.insert({ - avatar_id: null, - banner_id: null, - created_at: new Date(), - description: summaryDOM.textContent, - followers_count, - following_count, - name: activityStreams.name, - posts_count, - likes_count, - liked_count: 0, - drive_capacity: 1073741824, // 1GB - username: username, - username_lower, - host: toUnicode(finger.subject.replace(/^.*?@/, '')), - host_lower, - account: { - uri: activityStreams.id, - }, - }); - - const [icon, image] = await Promise.all([ - activityStreams.icon, - activityStreams.image, - ].map(async image => { - if (!image || image.type !== 'Image') { - return { _id: null }; - } - - try { - return await uploadFromUrl(image.url, user); - } catch (exception) { - return { _id: null }; - } - })); - - User.update({ _id: user._id }, { - $set: { - avatar_id: icon._id, - banner_id: image._id, - }, - }); - - user.avatar_id = icon._id; - user.banner_id = icon._id; - } - } else { - const q = userId !== undefined - ? { _id: userId } - : { username_lower: username.toLowerCase(), host: null }; - - user = await findUser(q); - - if (user === null) { - return rej('user not found'); - } - } - - // Send response - res(await pack(user, me, { - detail: true - })); -}); |