From 1d5a54ff6f74569fa89c4083301d9b01eb80ad29 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 17 Feb 2019 23:41:47 +0900 Subject: ハッシュタグでユーザー検索できるように (#4298) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ハッシュタグでユーザー検索できるように * :art: * Increase limit * リモートユーザーも表示 * Fix bug * Fix bug * Improve performance --- src/server/api/endpoints/hashtags/list.ts | 55 ++++++++++++++++++++ src/server/api/endpoints/hashtags/users.ts | 83 ++++++++++++++++++++++++++++++ src/server/api/endpoints/i/update.ts | 5 ++ 3 files changed, 143 insertions(+) create mode 100644 src/server/api/endpoints/hashtags/list.ts create mode 100644 src/server/api/endpoints/hashtags/users.ts (limited to 'src/server/api/endpoints') diff --git a/src/server/api/endpoints/hashtags/list.ts b/src/server/api/endpoints/hashtags/list.ts new file mode 100644 index 0000000000..5c37dbd6b5 --- /dev/null +++ b/src/server/api/endpoints/hashtags/list.ts @@ -0,0 +1,55 @@ +import $ from 'cafy'; +import define from '../../define'; +import Hashtag from '../../../../models/hashtag'; + +export const meta = { + requireCredential: false, + + params: { + limit: { + validator: $.optional.num.range(1, 100), + default: 10 + }, + + sort: { + validator: $.str.or([ + '+mentionedUsers', + '-mentionedUsers', + '+mentionedLocalUsers', + '-mentionedLocalUsers', + '+attachedUsers', + '-attachedUsers', + '+attachedLocalUsers', + '-attachedLocalUsers', + ]), + }, + } +}; + +const sort: any = { + '+mentionedUsers': { mentionedUsersCount: -1 }, + '-mentionedUsers': { mentionedUsersCount: 1 }, + '+mentionedLocalUsers': { mentionedLocalUsersCount: -1 }, + '-mentionedLocalUsers': { mentionedLocalUsersCount: 1 }, + '+attachedUsers': { attachedUsersCount: -1 }, + '-attachedUsers': { attachedUsersCount: 1 }, + '+attachedLocalUsers': { attachedLocalUsersCount: -1 }, + '-attachedLocalUsers': { attachedLocalUsersCount: 1 }, +}; + +export default define(meta, (ps, me) => new Promise(async (res, rej) => { + const tags = await Hashtag + .find({}, { + limit: ps.limit, + sort: sort[ps.sort], + fields: { + tag: true, + mentionedUsersCount: true, + mentionedLocalUsersCount: true, + attachedUsersCount: true, + attachedLocalUsersCount: true + } + }); + + res(tags); +})); diff --git a/src/server/api/endpoints/hashtags/users.ts b/src/server/api/endpoints/hashtags/users.ts new file mode 100644 index 0000000000..be6b53b889 --- /dev/null +++ b/src/server/api/endpoints/hashtags/users.ts @@ -0,0 +1,83 @@ +import $ from 'cafy'; +import User, { pack } from '../../../../models/user'; +import define from '../../define'; + +export const meta = { + requireCredential: false, + + params: { + tag: { + validator: $.str, + }, + + limit: { + validator: $.optional.num.range(1, 100), + default: 10 + }, + + sort: { + validator: $.str.or([ + '+follower', + '-follower', + '+createdAt', + '-createdAt', + '+updatedAt', + '-updatedAt', + ]), + }, + + state: { + validator: $.optional.str.or([ + 'all', + 'alive' + ]), + default: 'all' + }, + + origin: { + validator: $.optional.str.or([ + 'combined', + 'local', + 'remote', + ]), + default: 'local' + } + } +}; + +const sort: any = { + '+follower': { followersCount: -1 }, + '-follower': { followersCount: 1 }, + '+createdAt': { createdAt: -1 }, + '-createdAt': { createdAt: 1 }, + '+updatedAt': { updatedAt: -1 }, + '-updatedAt': { updatedAt: 1 }, +}; + +export default define(meta, (ps, me) => new Promise(async (res, rej) => { + const q = { + tags: ps.tag, + $and: [] + } as any; + + // state + q.$and.push( + ps.state == 'alive' ? { updatedAt: { $gt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 5)) } } : + {} + ); + + // origin + q.$and.push( + ps.origin == 'local' ? { host: null } : + ps.origin == 'remote' ? { host: { $ne: null } } : + {} + ); + + const users = await User + .find(q, { + limit: ps.limit, + sort: sort[ps.sort], + }); + + res(await Promise.all(users.map(user => pack(user, me, { detail: true })))); +})); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 6ae63c52db..b3ec53223f 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -11,6 +11,7 @@ import { parse, parsePlain } from '../../../../mfm/parse'; import extractEmojis from '../../../../misc/extract-emojis'; import extractHashtags from '../../../../misc/extract-hashtags'; import * as langmap from 'langmap'; +import { updateHashtag } from '../../../../services/update-hashtag'; export const meta = { desc: { @@ -221,6 +222,10 @@ export default define(meta, (ps, user, app) => new Promise(async (res, rej) => { updates.emojis = emojis; updates.tags = tags; + + // ハッシュタグ更新 + for (const tag of tags) updateHashtag(user, tag, true, true); + for (const tag of (user.tags || []).filter(x => !tags.includes(x))) updateHashtag(user, tag, true, false); } //#endregion -- cgit v1.2.3-freya