summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/aggregation/users
diff options
context:
space:
mode:
authortamaina <tamaina@hotmail.co.jp>2018-04-11 20:27:09 +0900
committerGitHub <noreply@github.com>2018-04-11 20:27:09 +0900
commitd43fe853c3605696e2e57e240845d0fc9c284f61 (patch)
tree838914e262c0fca5737588a7bba64e2b9f3d8e5f /src/server/api/endpoints/aggregation/users
parentUpdate README.md (diff)
parentwip #1443 (diff)
downloadmisskey-d43fe853c3605696e2e57e240845d0fc9c284f61.tar.gz
misskey-d43fe853c3605696e2e57e240845d0fc9c284f61.tar.bz2
misskey-d43fe853c3605696e2e57e240845d0fc9c284f61.zip
Merge pull request #1 from syuilo/master
追従
Diffstat (limited to 'src/server/api/endpoints/aggregation/users')
-rw-r--r--src/server/api/endpoints/aggregation/users/activity.ts116
-rw-r--r--src/server/api/endpoints/aggregation/users/followers.ts64
-rw-r--r--src/server/api/endpoints/aggregation/users/following.ts64
-rw-r--r--src/server/api/endpoints/aggregation/users/post.ts110
-rw-r--r--src/server/api/endpoints/aggregation/users/reaction.ts80
5 files changed, 434 insertions, 0 deletions
diff --git a/src/server/api/endpoints/aggregation/users/activity.ts b/src/server/api/endpoints/aggregation/users/activity.ts
new file mode 100644
index 0000000000..318cce77a5
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/activity.ts
@@ -0,0 +1,116 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import Note from '../../../../../models/note';
+
+// 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 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Lookup user
+ const user = await User.findOne({
+ _id: userId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (user === null) {
+ return rej('user not found');
+ }
+
+ const datas = await Note
+ .aggregate([
+ { $match: { userId: user._id } },
+ { $project: {
+ renoteId: '$renoteId',
+ replyId: '$replyId',
+ createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST
+ }},
+ { $project: {
+ date: {
+ year: { $year: '$createdAt' },
+ month: { $month: '$createdAt' },
+ day: { $dayOfMonth: '$createdAt' }
+ },
+ type: {
+ $cond: {
+ if: { $ne: ['$renoteId', null] },
+ then: 'renote',
+ else: {
+ $cond: {
+ if: { $ne: ['$replyId', null] },
+ then: 'reply',
+ else: 'note'
+ }
+ }
+ }
+ }}
+ },
+ { $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.notes = (data.data.filter(x => x.type == 'note')[0] || { count: 0 }).count;
+ data.renotes = (data.data.filter(x => x.type == 'renote')[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()
+ },
+ notes: 0,
+ renotes: 0,
+ replies: 0
+ });
+ }
+ }
+
+ res(graph);
+});
diff --git a/src/server/api/endpoints/aggregation/users/followers.ts b/src/server/api/endpoints/aggregation/users/followers.ts
new file mode 100644
index 0000000000..7ccb2a3066
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/followers.ts
@@ -0,0 +1,64 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import FollowedLog from '../../../../../models/followed-log';
+
+/**
+ * Aggregate followers of a user
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Lookup user
+ const user = await User.findOne({
+ _id: userId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (user === null) {
+ return rej('user not found');
+ }
+
+ const today = new Date();
+ const graph = [];
+
+ today.setMinutes(0);
+ today.setSeconds(0);
+ today.setMilliseconds(0);
+
+ let cursorDate = new Date(today.getTime());
+ let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1);
+
+ for (let i = 0; i < 30; i++) {
+ graph.push(FollowedLog.findOne({
+ createdAt: { $lt: new Date(cursorTime / 1000) },
+ userId: user._id
+ }, {
+ sort: { createdAt: -1 },
+ }).then(log => {
+ cursorDate = new Date(today.getTime());
+ cursorTime = cursorDate.setDate(today.getDate() - i);
+
+ return {
+ date: {
+ year: cursorDate.getFullYear(),
+ month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: cursorDate.getDate()
+ },
+ count: log ? log.count : 0
+ };
+ }));
+ }
+
+ res(await Promise.all(graph));
+});
diff --git a/src/server/api/endpoints/aggregation/users/following.ts b/src/server/api/endpoints/aggregation/users/following.ts
new file mode 100644
index 0000000000..45e246495b
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/following.ts
@@ -0,0 +1,64 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import FollowingLog from '../../../../../models/following-log';
+
+/**
+ * Aggregate following of a user
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Lookup user
+ const user = await User.findOne({
+ _id: userId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (user === null) {
+ return rej('user not found');
+ }
+
+ const today = new Date();
+ const graph = [];
+
+ today.setMinutes(0);
+ today.setSeconds(0);
+ today.setMilliseconds(0);
+
+ let cursorDate = new Date(today.getTime());
+ let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1);
+
+ for (let i = 0; i < 30; i++) {
+ graph.push(FollowingLog.findOne({
+ createdAt: { $lt: new Date(cursorTime / 1000) },
+ userId: user._id
+ }, {
+ sort: { createdAt: -1 },
+ }).then(log => {
+ cursorDate = new Date(today.getTime());
+ cursorTime = cursorDate.setDate(today.getDate() - i);
+
+ return {
+ date: {
+ year: cursorDate.getFullYear(),
+ month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: cursorDate.getDate()
+ },
+ count: log ? log.count : 0
+ };
+ }));
+ }
+
+ res(await Promise.all(graph));
+});
diff --git a/src/server/api/endpoints/aggregation/users/post.ts b/src/server/api/endpoints/aggregation/users/post.ts
new file mode 100644
index 0000000000..e6170d83e2
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/post.ts
@@ -0,0 +1,110 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import Note from '../../../../../models/note';
+
+/**
+ * Aggregate note of a user
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Lookup user
+ const user = await User.findOne({
+ _id: userId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (user === null) {
+ return rej('user not found');
+ }
+
+ const datas = await Note
+ .aggregate([
+ { $match: { userId: user._id } },
+ { $project: {
+ renoteId: '$renoteId',
+ replyId: '$replyId',
+ createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST
+ }},
+ { $project: {
+ date: {
+ year: { $year: '$createdAt' },
+ month: { $month: '$createdAt' },
+ day: { $dayOfMonth: '$createdAt' }
+ },
+ type: {
+ $cond: {
+ if: { $ne: ['$renoteId', null] },
+ then: 'renote',
+ else: {
+ $cond: {
+ if: { $ne: ['$replyId', null] },
+ then: 'reply',
+ else: 'note'
+ }
+ }
+ }
+ }}
+ },
+ { $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.notes = (data.data.filter(x => x.type == 'note')[0] || { count: 0 }).count;
+ data.renotes = (data.data.filter(x => x.type == 'renote')[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()
+ },
+ notes: 0,
+ renotes: 0,
+ replies: 0
+ });
+ }
+ }
+
+ res(graph);
+});
diff --git a/src/server/api/endpoints/aggregation/users/reaction.ts b/src/server/api/endpoints/aggregation/users/reaction.ts
new file mode 100644
index 0000000000..881c7ea693
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/reaction.ts
@@ -0,0 +1,80 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import Reaction from '../../../../../models/note-reaction';
+
+/**
+ * Aggregate reaction of a user
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId 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: { userId: user._id } },
+ { $project: {
+ createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST
+ }},
+ { $project: {
+ date: {
+ year: { $year: '$createdAt' },
+ month: { $month: '$createdAt' },
+ day: { $dayOfMonth: '$createdAt' }
+ }
+ }},
+ { $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);
+});