Merge pull request #899 from h3poteto/search

Use accounts/search API instead of v2/search
This commit is contained in:
AkiraFukushima 2019-04-29 17:11:54 +09:00 committed by GitHub
commit ae92522412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 51 deletions

View File

@ -1,4 +1,4 @@
import Mastodon, { Account, Results } from 'megalodon' import Mastodon, { Account } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex' import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
@ -23,14 +23,12 @@ const mutations: MutationTree<AccountState> = {
const actions: ActionTree<AccountState, RootState> = { const actions: ActionTree<AccountState, RootState> = {
search: async ({ commit, rootState }, query: string): Promise<Array<Account>> => { search: async ({ commit, rootState }, query: string): Promise<Array<Account>> => {
commit('TimelineSpace/Contents/Search/changeLoading', true, { root: true }) commit('TimelineSpace/Contents/Search/changeLoading', true, { root: true })
const client = new Mastodon( const client = new Mastodon(rootState.TimelineSpace.account.accessToken!, rootState.TimelineSpace.account.baseURL + '/api/v1')
rootState.TimelineSpace.account.accessToken!, return client
rootState.TimelineSpace.account.baseURL + '/api/v1' .get<Array<Account>>('/accounts/search', { q: query, resolve: true })
)
return client.get<Results>('/search', { q: query, resolve: true })
.then(res => { .then(res => {
commit(MUTATION_TYPES.UPDATE_RESULTS, res.data.accounts) commit(MUTATION_TYPES.UPDATE_RESULTS, res.data)
return res.data.accounts return res.data
}) })
.finally(() => { .finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true }) commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })

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