From f0490d4e62331a02eeeef3776b50d128c443257a Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 28 Apr 2019 20:50:14 +0900 Subject: [PATCH] Use accounts/search API to search account in sidebar --- .../Contents/SideBar/AccountProfile.ts | 62 ++++++------------- 1 file changed, 19 insertions(+), 43 deletions(-) diff --git a/src/renderer/store/TimelineSpace/Contents/SideBar/AccountProfile.ts b/src/renderer/store/TimelineSpace/Contents/SideBar/AccountProfile.ts index 92330993..57341a81 100644 --- a/src/renderer/store/TimelineSpace/Contents/SideBar/AccountProfile.ts +++ b/src/renderer/store/TimelineSpace/Contents/SideBar/AccountProfile.ts @@ -1,4 +1,4 @@ -import Mastodon, { Account, Relationship, Response, Results } from 'megalodon' +import Mastodon, { Account, Relationship, Response } from 'megalodon' import Timeline, { TimelineState } from './AccountProfile/Timeline' import Follows, { FollowsState } from './AccountProfile/Follows' import Followers, { FollowersState } from './AccountProfile/Followers' @@ -6,14 +6,14 @@ import { Module, MutationTree, ActionTree } from 'vuex' import { RootState } from '@/store' export interface AccountProfileState { - account: Account | null, - relationship: Relationship | null, + account: Account | null + relationship: Relationship | null loading: boolean } export interface AccountProfileModuleState extends AccountProfileState { - Followers: FollowersState, - Follows: FollowsState, + Followers: FollowersState + Follows: FollowsState Timeline: TimelineState } @@ -43,27 +43,21 @@ const mutations: MutationTree = { const actions: ActionTree = { fetchAccount: async ({ rootState }, accountID: number): Promise => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.get(`/accounts/${accountID}`) return res.data }, searchAccount: async ({ rootState }, parsedAccount): Promise => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) - const res: Response = await client.get('/search', { q: parsedAccount.url, resolve: true }) - if (res.data.accounts.length <= 0) throw new AccountNotFound('empty result') - const account = res.data.accounts.find(a => `@${a.acct}` === parsedAccount.acct) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') + const res: Response> = await client.get>('/accounts/search', { q: parsedAccount.url, resolve: true }) + if (res.data.length <= 0) throw new AccountNotFound('empty result') + const account = res.data.find(a => `@${a.acct}` === parsedAccount.acct) if (account) return account - const pleromaUser = res.data.accounts.find(a => a.acct === parsedAccount.acct) + const pleromaUser = res.data.find(a => a.acct === parsedAccount.acct) if (pleromaUser) return pleromaUser - const localUser = res.data.accounts.find(a => `@${a.username}@${rootState.TimelineSpace.account.domain}` === parsedAccount.acct) + const localUser = res.data.find(a => `@${a.username}@${rootState.TimelineSpace.account.domain}` === parsedAccount.acct) if (localUser) return localUser - const user = res.data.accounts.find(a => a.url === parsedAccount.url) + const user = res.data.find(a => a.url === parsedAccount.url) if (!user) throw new AccountNotFound('not found') return user }, @@ -73,28 +67,19 @@ const actions: ActionTree = { }, fetchRelationship: async ({ commit, rootState }, account: Account): Promise => { commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, null) - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.get('/accounts/relationships', { id: [account.id] }) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data[0]) return res.data }, follow: async ({ commit, rootState }, account: Account) => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.post(`/accounts/${account.id}/follow`) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) return res.data }, unfollow: async ({ commit, rootState }, account: Account) => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.post(`/accounts/${account.id}/unfollow`) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) return res.data @@ -103,28 +88,19 @@ const actions: ActionTree = { commit(MUTATION_TYPES.CHANGE_ACCOUNT, null) }, unmute: async ({ rootState, commit }, account: Account) => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.post(`/accounts/${account.id}/unmute`) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) return res.data }, block: async ({ rootState, commit }, account: Account) => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.post(`/accounts/${account.id}/block`) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) return res.data }, unblock: async ({ rootState, commit }, account: Account) => { - const client = new Mastodon( - rootState.TimelineSpace.account.accessToken!, - rootState.TimelineSpace.account.baseURL + '/api/v1' - ) + const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1') const res: Response = await client.post(`/accounts/${account.id}/unblock`) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) return res.data