From e0deaec695650d22c92512cc2672ba3aade96eed Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 7 Sep 2018 19:23:39 +0900 Subject: Implement new endpoint --- src/server/api/endpoints/aggregation/hashtags.ts | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/server/api/endpoints/aggregation/hashtags.ts (limited to 'src/server/api/endpoints/aggregation') diff --git a/src/server/api/endpoints/aggregation/hashtags.ts b/src/server/api/endpoints/aggregation/hashtags.ts new file mode 100644 index 0000000000..0daf42427b --- /dev/null +++ b/src/server/api/endpoints/aggregation/hashtags.ts @@ -0,0 +1,64 @@ +import Note from '../../../../models/note'; +import Meta from '../../../../models/meta'; + +export default () => new Promise(async (res, rej) => { + const meta = await Meta.findOne({}); + const hidedTags = (meta.hidedTags || []).map(t => t.toLowerCase()); + + const span = 1000 * 60 * 60 * 24 * 7; // 1週間 + + //#region 1. 指定期間の内に投稿されたハッシュタグ(とユーザーのペア)を集計 + const data = await Note.aggregate([{ + $match: { + createdAt: { + $gt: new Date(Date.now() - span) + }, + tagsLower: { + $exists: true, + $ne: [] + } + } + }, { + $unwind: '$tagsLower' + }, { + $group: { + _id: { tag: '$tagsLower', userId: '$userId' } + } + }]) as Array<{ + _id: { + tag: string; + userId: any; + } + }>; + //#endregion + + if (data.length == 0) { + return res([]); + } + + let tags: Array<{ + name: string; + count: number; + }> = []; + + // カウント + data.map(x => x._id).forEach(x => { + // ブラックリストに登録されているタグなら弾く + if (hidedTags.includes(x.tag)) return; + + const i = tags.findIndex(tag => tag.name == x.tag); + if (i != -1) { + tags[i].count++; + } else { + tags.push({ + name: x.tag, + count: 1 + }); + } + }); + + // タグを人気順に並べ替え + tags = tags.sort((a, b) => b.count - a.count); + + res(tags); +}); -- cgit v1.2.3-freya From d9fe9cc5df7d3b7964a303544dd3dbbdf1cf5dd7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 7 Sep 2018 20:23:46 +0900 Subject: 返すタグの数を制限 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/endpoints/aggregation/hashtags.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/server/api/endpoints/aggregation') diff --git a/src/server/api/endpoints/aggregation/hashtags.ts b/src/server/api/endpoints/aggregation/hashtags.ts index 0daf42427b..c5aacd89cd 100644 --- a/src/server/api/endpoints/aggregation/hashtags.ts +++ b/src/server/api/endpoints/aggregation/hashtags.ts @@ -60,5 +60,7 @@ export default () => new Promise(async (res, rej) => { // タグを人気順に並べ替え tags = tags.sort((a, b) => b.count - a.count); + tags = tags.slice(0, 30); + res(tags); }); -- cgit v1.2.3-freya From fc31e44fd2d70ae8f4114009a26f00fc5db01fca Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 15 Sep 2018 05:42:14 +0900 Subject: Fix bug --- src/server/api/endpoints/aggregation/hashtags.ts | 2 +- src/server/api/endpoints/hashtags/trend.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/server/api/endpoints/aggregation') diff --git a/src/server/api/endpoints/aggregation/hashtags.ts b/src/server/api/endpoints/aggregation/hashtags.ts index c5aacd89cd..ffeafb2538 100644 --- a/src/server/api/endpoints/aggregation/hashtags.ts +++ b/src/server/api/endpoints/aggregation/hashtags.ts @@ -3,7 +3,7 @@ import Meta from '../../../../models/meta'; export default () => new Promise(async (res, rej) => { const meta = await Meta.findOne({}); - const hidedTags = (meta.hidedTags || []).map(t => t.toLowerCase()); + const hidedTags = meta ? (meta.hidedTags || []).map(t => t.toLowerCase()) : []; const span = 1000 * 60 * 60 * 24 * 7; // 1週間 diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts index bfa475619c..0ec6a4ffec 100644 --- a/src/server/api/endpoints/hashtags/trend.ts +++ b/src/server/api/endpoints/hashtags/trend.ts @@ -19,7 +19,7 @@ const max = 5; */ export default () => new Promise(async (res, rej) => { const meta = await Meta.findOne({}); - const hidedTags = (meta.hidedTags || []).map(t => t.toLowerCase()); + const hidedTags = meta ? (meta.hidedTags || []).map(t => t.toLowerCase()) : []; //#region 1. 直近Aの内に投稿されたハッシュタグ(とユーザーのペア)を集計 const data = await Note.aggregate([{ -- cgit v1.2.3-freya