summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints
diff options
context:
space:
mode:
authorAkihiko Odaki <nekomanma@pixiv.co.jp>2018-04-02 21:57:36 +0900
committerAkihiko Odaki <nekomanma@pixiv.co.jp>2018-04-02 22:00:40 +0900
commit319e905bf9e5398cb62eaeee7da80ff6e942c094 (patch)
tree5d0edf01e29d7904bf4b82d57ff60754741ce100 /src/server/api/endpoints
parentImplement Follow activity (diff)
downloadsharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.tar.gz
sharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.tar.bz2
sharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.zip
Introduce followed log and following log
Diffstat (limited to 'src/server/api/endpoints')
-rw-r--r--src/server/api/endpoints/aggregation/users/followers.ts64
-rw-r--r--src/server/api/endpoints/aggregation/users/following.ts63
-rw-r--r--src/server/api/endpoints/following/create.ts3
-rw-r--r--src/server/api/endpoints/following/delete.ts9
-rw-r--r--src/server/api/endpoints/users/followers.ts3
-rw-r--r--src/server/api/endpoints/users/following.ts3
6 files changed, 59 insertions, 86 deletions
diff --git a/src/server/api/endpoints/aggregation/users/followers.ts b/src/server/api/endpoints/aggregation/users/followers.ts
index dda34ed7be..580d31a3f2 100644
--- a/src/server/api/endpoints/aggregation/users/followers.ts
+++ b/src/server/api/endpoints/aggregation/users/followers.ts
@@ -2,8 +2,9 @@
* Module dependencies
*/
import $ from 'cafy';
+import { ObjectID } from 'mongodb';
import User from '../../../../../models/user';
-import Following from '../../../../../models/following';
+import FollowedLog from '../../../../../models/followed-log';
/**
* Aggregate followers of a user
@@ -29,47 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => {
return rej('user not found');
}
- const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
+ const today = new Date();
+ const graph = [];
- const following = await Following
- .find({
- followeeId: user._id,
- $or: [
- { deletedAt: { $exists: false } },
- { deletedAt: { $gt: startTime } }
- ]
- }, {
- sort: { createdAt: -1 },
- fields: {
- _id: false,
- followerId: false,
- followeeId: false
- }
- });
+ today.setMinutes(0);
+ today.setSeconds(0);
+ today.setMilliseconds(0);
- const graph = [];
+ let cursorDate = new Date(today.getTime());
+ let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1);
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.createdAt < day && (f.deletedAt == null || f.deletedAt > day)
- ).length;
+ graph.push(FollowedLog.findOne({
+ _id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
+ userId: user._id
+ }, {
+ sort: { _id: -1 },
+ }).then(log => {
+ cursorDate = new Date(today.getTime());
+ cursorTime = cursorDate.setDate(today.getDate() - i);
- graph.push({
- date: {
- year: day.getFullYear(),
- month: day.getMonth() + 1, // In JavaScript, month is zero-based.
- day: day.getDate()
- },
- count: count
- });
+ return {
+ date: {
+ year: cursorDate.getFullYear(),
+ month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: cursorDate.getDate()
+ },
+ count: log ? log.count : 0
+ };
+ }));
}
- res(graph);
+ 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
index cd08d89e49..3ac0e3a539 100644
--- a/src/server/api/endpoints/aggregation/users/following.ts
+++ b/src/server/api/endpoints/aggregation/users/following.ts
@@ -2,8 +2,9 @@
* Module dependencies
*/
import $ from 'cafy';
+import { ObjectID } from 'mongodb';
import User from '../../../../../models/user';
-import Following from '../../../../../models/following';
+import FollowingLog from '../../../../../models/following-log';
/**
* Aggregate following of a user
@@ -29,46 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => {
return rej('user not found');
}
- const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
+ const today = new Date();
+ const graph = [];
- const following = await Following
- .find({
- followerId: user._id,
- $or: [
- { deletedAt: { $exists: false } },
- { deletedAt: { $gt: startTime } }
- ]
- }, {
- sort: { createdAt: -1 },
- fields: {
- _id: false,
- followerId: false,
- followeeId: false
- }
- });
+ today.setMinutes(0);
+ today.setSeconds(0);
+ today.setMilliseconds(0);
- const graph = [];
+ let cursorDate = new Date(today.getTime());
+ let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1);
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.createdAt < day && (f.deletedAt == null || f.deletedAt > day)
- ).length;
+ graph.push(FollowingLog.findOne({
+ _id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
+ userId: user._id
+ }, {
+ sort: { _id: -1 },
+ }).then(log => {
+ cursorDate = new Date(today.getTime());
+ cursorTime = cursorDate.setDate(today.getDate() - i);
- graph.push({
- date: {
- year: day.getFullYear(),
- month: day.getMonth() + 1, // In JavaScript, month is zero-based.
- day: day.getDate()
- },
- count: count
- });
+ return {
+ date: {
+ year: cursorDate.getFullYear(),
+ month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: cursorDate.getDate()
+ },
+ count: log ? log.count : 0
+ };
+ }));
}
- res(graph);
+ res(await Promise.all(graph));
});
diff --git a/src/server/api/endpoints/following/create.ts b/src/server/api/endpoints/following/create.ts
index 03c13ab7fc..e568595215 100644
--- a/src/server/api/endpoints/following/create.ts
+++ b/src/server/api/endpoints/following/create.ts
@@ -42,8 +42,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Check if already following
const exist = await Following.findOne({
followerId: follower._id,
- followeeId: followee._id,
- deletedAt: { $exists: false }
+ followeeId: followee._id
});
if (exist !== null) {
diff --git a/src/server/api/endpoints/following/delete.ts b/src/server/api/endpoints/following/delete.ts
index 3facfdcdd4..5deddc9196 100644
--- a/src/server/api/endpoints/following/delete.ts
+++ b/src/server/api/endpoints/following/delete.ts
@@ -42,8 +42,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Check not following
const exist = await Following.findOne({
followerId: follower._id,
- followeeId: followee._id,
- deletedAt: { $exists: false }
+ followeeId: followee._id
});
if (exist === null) {
@@ -51,12 +50,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
}
// Delete following
- await Following.update({
+ await Following.findOneAndDelete({
_id: exist._id
- }, {
- $set: {
- deletedAt: new Date()
- }
});
// Send response
diff --git a/src/server/api/endpoints/users/followers.ts b/src/server/api/endpoints/users/followers.ts
index 39b69a6aa9..0222313e81 100644
--- a/src/server/api/endpoints/users/followers.ts
+++ b/src/server/api/endpoints/users/followers.ts
@@ -46,8 +46,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
// Construct query
const query = {
- followeeId: user._id,
- deletedAt: { $exists: false }
+ followeeId: user._id
} as any;
// ログインしていてかつ iknow フラグがあるとき
diff --git a/src/server/api/endpoints/users/following.ts b/src/server/api/endpoints/users/following.ts
index aa6628dde2..2372f57fbe 100644
--- a/src/server/api/endpoints/users/following.ts
+++ b/src/server/api/endpoints/users/following.ts
@@ -46,8 +46,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
// Construct query
const query = {
- followerId: user._id,
- deletedAt: { $exists: false }
+ followerId: user._id
} as any;
// ログインしていてかつ iknow フラグがあるとき