Use accounts/search API to search account in sidebar

This commit is contained in:
AkiraFukushima 2019-04-28 20:50:14 +09:00
parent 18567a54d7
commit f0490d4e62
1 changed files with 19 additions and 43 deletions

View File

@ -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 Timeline, { TimelineState } from './AccountProfile/Timeline'
import Follows, { FollowsState } from './AccountProfile/Follows' import Follows, { FollowsState } from './AccountProfile/Follows'
import Followers, { FollowersState } from './AccountProfile/Followers' import Followers, { FollowersState } from './AccountProfile/Followers'
@ -6,14 +6,14 @@ import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
export interface AccountProfileState { export interface AccountProfileState {
account: Account | null, account: Account | null
relationship: Relationship | null, relationship: Relationship | null
loading: boolean loading: boolean
} }
export interface AccountProfileModuleState extends AccountProfileState { export interface AccountProfileModuleState extends AccountProfileState {
Followers: FollowersState, Followers: FollowersState
Follows: FollowsState, Follows: FollowsState
Timeline: TimelineState Timeline: TimelineState
} }
@ -43,27 +43,21 @@ const mutations: MutationTree<AccountProfileState> = {
const actions: ActionTree<AccountProfileState, RootState> = { const actions: ActionTree<AccountProfileState, RootState> = {
fetchAccount: async ({ rootState }, accountID: number): Promise<Account> => { fetchAccount: async ({ rootState }, accountID: number): Promise<Account> => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Account> = await client.get<Account>(`/accounts/${accountID}`) const res: Response<Account> = await client.get<Account>(`/accounts/${accountID}`)
return res.data return res.data
}, },
searchAccount: async ({ rootState }, parsedAccount): Promise<Account> => { searchAccount: async ({ rootState }, parsedAccount): Promise<Account> => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!, const res: Response<Array<Account>> = await client.get<Array<Account>>('/accounts/search', { q: parsedAccount.url, resolve: true })
rootState.TimelineSpace.account.baseURL + '/api/v1' if (res.data.length <= 0) throw new AccountNotFound('empty result')
) const account = res.data.find(a => `@${a.acct}` === parsedAccount.acct)
const res: Response<Results> = await client.get<Results>('/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)
if (account) return account 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 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 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') if (!user) throw new AccountNotFound('not found')
return user return user
}, },
@ -73,28 +67,19 @@ const actions: ActionTree<AccountProfileState, RootState> = {
}, },
fetchRelationship: async ({ commit, rootState }, account: Account): Promise<Relationship> => { fetchRelationship: async ({ commit, rootState }, account: Account): Promise<Relationship> => {
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, null) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, null)
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.get<Relationship>('/accounts/relationships', { id: [account.id] }) const res: Response<Relationship> = await client.get<Relationship>('/accounts/relationships', { id: [account.id] })
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data[0]) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data[0])
return res.data return res.data
}, },
follow: async ({ commit, rootState }, account: Account) => { follow: async ({ commit, rootState }, account: Account) => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/follow`) const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/follow`)
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data)
return res.data return res.data
}, },
unfollow: async ({ commit, rootState }, account: Account) => { unfollow: async ({ commit, rootState }, account: Account) => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unfollow`) const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unfollow`)
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data)
return res.data return res.data
@ -103,28 +88,19 @@ const actions: ActionTree<AccountProfileState, RootState> = {
commit(MUTATION_TYPES.CHANGE_ACCOUNT, null) commit(MUTATION_TYPES.CHANGE_ACCOUNT, null)
}, },
unmute: async ({ rootState, commit }, account: Account) => { unmute: async ({ rootState, commit }, account: Account) => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unmute`) const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unmute`)
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data)
return res.data return res.data
}, },
block: async ({ rootState, commit }, account: Account) => { block: async ({ rootState, commit }, account: Account) => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/block`) const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/block`)
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data)
return res.data return res.data
}, },
unblock: async ({ rootState, commit }, account: Account) => { unblock: async ({ rootState, commit }, account: Account) => {
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unblock`) const res: Response<Relationship> = await client.post<Relationship>(`/accounts/${account.id}/unblock`)
commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data) commit(MUTATION_TYPES.CHANGE_RELATIONSHIP, res.data)
return res.data return res.data