Merge pull request #1374 from akihikodaki/log
Introduce followed log and following log
This commit is contained in:
commit
5b80b6e901
11
src/models/followed-log.ts
Normal file
11
src/models/followed-log.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { ObjectID } from 'mongodb';
|
||||||
|
import db from '../db/mongodb';
|
||||||
|
|
||||||
|
const FollowedLog = db.get<IFollowedLog>('followedLogs');
|
||||||
|
export default FollowedLog;
|
||||||
|
|
||||||
|
export type IFollowedLog = {
|
||||||
|
_id: ObjectID;
|
||||||
|
userId: ObjectID;
|
||||||
|
count: number;
|
||||||
|
};
|
11
src/models/following-log.ts
Normal file
11
src/models/following-log.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { ObjectID } from 'mongodb';
|
||||||
|
import db from '../db/mongodb';
|
||||||
|
|
||||||
|
const FollowingLog = db.get<IFollowingLog>('followingLogs');
|
||||||
|
export default FollowingLog;
|
||||||
|
|
||||||
|
export type IFollowingLog = {
|
||||||
|
_id: ObjectID;
|
||||||
|
userId: ObjectID;
|
||||||
|
count: number;
|
||||||
|
};
|
@ -8,7 +8,6 @@ export default Following;
|
|||||||
export type IFollowing = {
|
export type IFollowing = {
|
||||||
_id: mongo.ObjectID;
|
_id: mongo.ObjectID;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
deletedAt: Date;
|
|
||||||
followeeId: mongo.ObjectID;
|
followeeId: mongo.ObjectID;
|
||||||
followerId: mongo.ObjectID;
|
followerId: mongo.ObjectID;
|
||||||
};
|
};
|
||||||
|
@ -234,8 +234,7 @@ export const pack = (
|
|||||||
_user.isFollowing = (async () => {
|
_user.isFollowing = (async () => {
|
||||||
const follow = await Following.findOne({
|
const follow = await Following.findOne({
|
||||||
followerId: meId,
|
followerId: meId,
|
||||||
followeeId: _user.id,
|
followeeId: _user.id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
return follow !== null;
|
return follow !== null;
|
||||||
})();
|
})();
|
||||||
@ -244,8 +243,7 @@ export const pack = (
|
|||||||
_user.isFollowed = (async () => {
|
_user.isFollowed = (async () => {
|
||||||
const follow2 = await Following.findOne({
|
const follow2 = await Following.findOne({
|
||||||
followerId: _user.id,
|
followerId: _user.id,
|
||||||
followeeId: meId,
|
followeeId: meId
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
return follow2 !== null;
|
return follow2 !== null;
|
||||||
})();
|
})();
|
||||||
@ -275,15 +273,13 @@ export const pack = (
|
|||||||
// Get following you know count
|
// Get following you know count
|
||||||
_user.followingYouKnowCount = Following.count({
|
_user.followingYouKnowCount = Following.count({
|
||||||
followeeId: { $in: myFollowingIds },
|
followeeId: { $in: myFollowingIds },
|
||||||
followerId: _user.id,
|
followerId: _user.id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get followers you know count
|
// Get followers you know count
|
||||||
_user.followersYouKnowCount = Following.count({
|
_user.followersYouKnowCount = Following.count({
|
||||||
followeeId: _user.id,
|
followeeId: _user.id,
|
||||||
followerId: { $in: myFollowingIds },
|
followerId: { $in: myFollowingIds }
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,7 @@ export default async (user, mentions, post) => {
|
|||||||
// Fetch all followers
|
// Fetch all followers
|
||||||
const followers = await Following
|
const followers = await Following
|
||||||
.find({
|
.find({
|
||||||
followeeId: user._id,
|
followeeId: user._id
|
||||||
// 削除されたドキュメントは除く
|
|
||||||
deletedAt: { $exists: false }
|
|
||||||
}, {
|
}, {
|
||||||
followerId: true,
|
followerId: true,
|
||||||
_id: false
|
_id: false
|
||||||
|
@ -3,6 +3,8 @@ import { sign } from 'http-signature';
|
|||||||
import { URL } from 'url';
|
import { URL } from 'url';
|
||||||
import User, { isLocalUser, pack as packUser } from '../../models/user';
|
import User, { isLocalUser, pack as packUser } from '../../models/user';
|
||||||
import Following from '../../models/following';
|
import Following from '../../models/following';
|
||||||
|
import FollowingLog from '../../models/following-log';
|
||||||
|
import FollowedLog from '../../models/followed-log';
|
||||||
import event from '../../publishers/stream';
|
import event from '../../publishers/stream';
|
||||||
import notify from '../../publishers/notify';
|
import notify from '../../publishers/notify';
|
||||||
import context from '../../remote/activitypub/renderer/context';
|
import context from '../../remote/activitypub/renderer/context';
|
||||||
@ -21,6 +23,11 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
promisedFollower.then(({ followingCount }) => FollowingLog.insert({
|
||||||
|
userId: followerId,
|
||||||
|
count: followingCount + 1
|
||||||
|
})),
|
||||||
|
|
||||||
// Increment followers count
|
// Increment followers count
|
||||||
User.update({ _id: followeeId }, {
|
User.update({ _id: followeeId }, {
|
||||||
$inc: {
|
$inc: {
|
||||||
@ -28,6 +35,11 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
|
||||||
|
userId: followerId,
|
||||||
|
count: followersCount + 1
|
||||||
|
})),
|
||||||
|
|
||||||
// Notify
|
// Notify
|
||||||
promisedFollowee.then(followee => followee.host === null ?
|
promisedFollowee.then(followee => followee.host === null ?
|
||||||
notify(followeeId, followerId, 'follow') : null),
|
notify(followeeId, followerId, 'follow') : null),
|
||||||
|
@ -6,9 +6,7 @@ export default async (me: mongodb.ObjectID, includeMe: boolean = true) => {
|
|||||||
// SELECT followee
|
// SELECT followee
|
||||||
const myfollowing = await Following
|
const myfollowing = await Following
|
||||||
.find({
|
.find({
|
||||||
followerId: me,
|
followerId: me
|
||||||
// 削除されたドキュメントは除く
|
|
||||||
deletedAt: { $exists: false }
|
|
||||||
}, {
|
}, {
|
||||||
fields: {
|
fields: {
|
||||||
followeeId: true
|
followeeId: true
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
* Module dependencies
|
* Module dependencies
|
||||||
*/
|
*/
|
||||||
import $ from 'cafy';
|
import $ from 'cafy';
|
||||||
|
import { ObjectID } from 'mongodb';
|
||||||
import User from '../../../../../models/user';
|
import User from '../../../../../models/user';
|
||||||
import Following from '../../../../../models/following';
|
import FollowedLog from '../../../../../models/followed-log';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aggregate followers of a user
|
* Aggregate followers of a user
|
||||||
@ -29,47 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => {
|
|||||||
return rej('user not found');
|
return rej('user not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
|
const today = new Date();
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const graph = [];
|
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++) {
|
for (let i = 0; i < 30; i++) {
|
||||||
let day = new Date(new Date().setDate(new Date().getDate() - i));
|
graph.push(FollowedLog.findOne({
|
||||||
day = new Date(day.setMilliseconds(999));
|
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
|
||||||
day = new Date(day.setSeconds(59));
|
userId: user._id
|
||||||
day = new Date(day.setMinutes(59));
|
}, {
|
||||||
day = new Date(day.setHours(23));
|
sort: { _id: -1 },
|
||||||
// day = day.getTime();
|
}).then(log => {
|
||||||
|
cursorDate = new Date(today.getTime());
|
||||||
|
cursorTime = cursorDate.setDate(today.getDate() - i);
|
||||||
|
|
||||||
const count = following.filter(f =>
|
return {
|
||||||
f.createdAt < day && (f.deletedAt == null || f.deletedAt > day)
|
|
||||||
).length;
|
|
||||||
|
|
||||||
graph.push({
|
|
||||||
date: {
|
date: {
|
||||||
year: day.getFullYear(),
|
year: cursorDate.getFullYear(),
|
||||||
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
|
month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
|
||||||
day: day.getDate()
|
day: cursorDate.getDate()
|
||||||
},
|
},
|
||||||
count: count
|
count: log ? log.count : 0
|
||||||
});
|
};
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
res(graph);
|
res(await Promise.all(graph));
|
||||||
});
|
});
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
* Module dependencies
|
* Module dependencies
|
||||||
*/
|
*/
|
||||||
import $ from 'cafy';
|
import $ from 'cafy';
|
||||||
|
import { ObjectID } from 'mongodb';
|
||||||
import User from '../../../../../models/user';
|
import User from '../../../../../models/user';
|
||||||
import Following from '../../../../../models/following';
|
import FollowingLog from '../../../../../models/following-log';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aggregate following of a user
|
* Aggregate following of a user
|
||||||
@ -29,46 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => {
|
|||||||
return rej('user not found');
|
return rej('user not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
|
const today = new Date();
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const graph = [];
|
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++) {
|
for (let i = 0; i < 30; i++) {
|
||||||
let day = new Date(new Date().setDate(new Date().getDate() - i));
|
graph.push(FollowingLog.findOne({
|
||||||
day = new Date(day.setMilliseconds(999));
|
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
|
||||||
day = new Date(day.setSeconds(59));
|
userId: user._id
|
||||||
day = new Date(day.setMinutes(59));
|
}, {
|
||||||
day = new Date(day.setHours(23));
|
sort: { _id: -1 },
|
||||||
|
}).then(log => {
|
||||||
|
cursorDate = new Date(today.getTime());
|
||||||
|
cursorTime = cursorDate.setDate(today.getDate() - i);
|
||||||
|
|
||||||
const count = following.filter(f =>
|
return {
|
||||||
f.createdAt < day && (f.deletedAt == null || f.deletedAt > day)
|
|
||||||
).length;
|
|
||||||
|
|
||||||
graph.push({
|
|
||||||
date: {
|
date: {
|
||||||
year: day.getFullYear(),
|
year: cursorDate.getFullYear(),
|
||||||
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
|
month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
|
||||||
day: day.getDate()
|
day: cursorDate.getDate()
|
||||||
},
|
},
|
||||||
count: count
|
count: log ? log.count : 0
|
||||||
});
|
};
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
res(graph);
|
res(await Promise.all(graph));
|
||||||
});
|
});
|
||||||
|
@ -42,8 +42,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
|
|||||||
// Check if already following
|
// Check if already following
|
||||||
const exist = await Following.findOne({
|
const exist = await Following.findOne({
|
||||||
followerId: follower._id,
|
followerId: follower._id,
|
||||||
followeeId: followee._id,
|
followeeId: followee._id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (exist !== null) {
|
if (exist !== null) {
|
||||||
|
@ -42,8 +42,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
|
|||||||
// Check not following
|
// Check not following
|
||||||
const exist = await Following.findOne({
|
const exist = await Following.findOne({
|
||||||
followerId: follower._id,
|
followerId: follower._id,
|
||||||
followeeId: followee._id,
|
followeeId: followee._id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (exist === null) {
|
if (exist === null) {
|
||||||
@ -51,12 +50,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete following
|
// Delete following
|
||||||
await Following.update({
|
await Following.findOneAndDelete({
|
||||||
_id: exist._id
|
_id: exist._id
|
||||||
}, {
|
|
||||||
$set: {
|
|
||||||
deletedAt: new Date()
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send response
|
// Send response
|
||||||
|
@ -46,8 +46,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
|
|||||||
|
|
||||||
// Construct query
|
// Construct query
|
||||||
const query = {
|
const query = {
|
||||||
followeeId: user._id,
|
followeeId: user._id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
// ログインしていてかつ iknow フラグがあるとき
|
// ログインしていてかつ iknow フラグがあるとき
|
||||||
|
@ -46,8 +46,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
|
|||||||
|
|
||||||
// Construct query
|
// Construct query
|
||||||
const query = {
|
const query = {
|
||||||
followerId: user._id,
|
followerId: user._id
|
||||||
deletedAt: { $exists: false }
|
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
// ログインしていてかつ iknow フラグがあるとき
|
// ログインしていてかつ iknow フラグがあるとき
|
||||||
|
14
test/api.js
14
test/api.js
@ -609,20 +609,6 @@ describe('API', () => {
|
|||||||
res.should.have.status(204);
|
res.should.have.status(204);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('過去にフォロー歴があった状態でフォローできる', async(async () => {
|
|
||||||
const hima = await insertHimawari();
|
|
||||||
const me = await insertSakurako();
|
|
||||||
await db.get('following').insert({
|
|
||||||
followeeId: hima._id,
|
|
||||||
followerId: me._id,
|
|
||||||
deletedAt: new Date()
|
|
||||||
});
|
|
||||||
const res = await request('/following/create', {
|
|
||||||
userId: hima._id.toString()
|
|
||||||
}, me);
|
|
||||||
res.should.have.status(204);
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('既にフォローしている場合は怒る', async(async () => {
|
it('既にフォローしている場合は怒る', async(async () => {
|
||||||
const hima = await insertHimawari();
|
const hima = await insertHimawari();
|
||||||
const me = await insertSakurako();
|
const me = await insertSakurako();
|
||||||
|
Loading…
Reference in New Issue
Block a user