2019-04-14 15:51:28 +02:00
|
|
|
import Mastodon, { Status, Response } from 'megalodon'
|
|
|
|
import parse from 'parse-link-header'
|
|
|
|
import { Module, MutationTree, ActionTree } from 'vuex'
|
|
|
|
import { RootState } from '@/store'
|
2019-06-06 16:44:50 +02:00
|
|
|
import { LocalAccount } from '~/src/types/localAccount'
|
2019-04-14 15:51:28 +02:00
|
|
|
|
2019-06-06 16:44:50 +02:00
|
|
|
export type FavouritesState = {
|
2019-05-27 15:56:54 +02:00
|
|
|
favourites: Array<Status>
|
|
|
|
lazyLoading: boolean
|
|
|
|
filter: string
|
|
|
|
maxId: string | null
|
2019-04-14 15:51:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const state = (): FavouritesState => ({
|
|
|
|
favourites: [],
|
|
|
|
lazyLoading: false,
|
|
|
|
filter: '',
|
|
|
|
maxId: null
|
|
|
|
})
|
|
|
|
|
|
|
|
export const MUTATION_TYPES = {
|
|
|
|
UPDATE_FAVOURITES: 'updateFavourites',
|
|
|
|
INSERT_FAVOURITES: 'insertFavourites',
|
|
|
|
UPDATE_TOOT: 'updateToot',
|
|
|
|
DELETE_TOOT: 'deleteToot',
|
|
|
|
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
|
|
|
CHANGE_FILTER: 'changeFilter',
|
|
|
|
CHANGE_MAX_ID: 'changeMaxId'
|
|
|
|
}
|
|
|
|
|
|
|
|
const mutations: MutationTree<FavouritesState> = {
|
|
|
|
[MUTATION_TYPES.UPDATE_FAVOURITES]: (state, favourites: Array<Status>) => {
|
|
|
|
state.favourites = favourites
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.INSERT_FAVOURITES]: (state, favourites: Array<Status>) => {
|
|
|
|
state.favourites = state.favourites.concat(favourites)
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
|
2019-05-27 15:56:54 +02:00
|
|
|
state.favourites = state.favourites.map(toot => {
|
2019-04-14 15:51:28 +02:00
|
|
|
if (toot.id === message.id) {
|
|
|
|
return message
|
|
|
|
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
|
|
|
|
// When user reblog/favourite a reblogged toot, target message is a original toot.
|
|
|
|
// So, a message which is received now is original toot.
|
|
|
|
const reblog = {
|
|
|
|
reblog: message
|
|
|
|
}
|
|
|
|
return Object.assign(toot, reblog)
|
|
|
|
} else {
|
|
|
|
return toot
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
|
2019-05-27 15:56:54 +02:00
|
|
|
state.favourites = state.favourites.filter(toot => {
|
2019-04-14 15:51:28 +02:00
|
|
|
if (toot.reblog !== null && toot.reblog.id === message.id) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return toot.id !== message.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
|
|
|
|
state.lazyLoading = value
|
|
|
|
},
|
|
|
|
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
|
|
|
|
state.filter = filter
|
|
|
|
},
|
2019-05-27 15:56:54 +02:00
|
|
|
[MUTATION_TYPES.CHANGE_MAX_ID]: (state, id: string | null) => {
|
2019-04-14 15:51:28 +02:00
|
|
|
state.maxId = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions: ActionTree<FavouritesState, RootState> = {
|
2019-10-27 06:41:03 +01:00
|
|
|
fetchFavourites: async ({ commit, rootState }, account: LocalAccount): Promise<Array<Status>> => {
|
|
|
|
const client = new Mastodon(
|
|
|
|
account.accessToken!,
|
|
|
|
account.baseURL + '/api/v1',
|
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2019-04-14 15:51:28 +02:00
|
|
|
const res: Response<Array<Status>> = await client.get<Array<Status>>('/favourites', { limit: 40 })
|
|
|
|
commit(MUTATION_TYPES.UPDATE_FAVOURITES, res.data)
|
|
|
|
// Parse link header
|
|
|
|
try {
|
|
|
|
const link = parse(res.headers.link)
|
|
|
|
if (link !== null) {
|
2019-05-27 15:56:54 +02:00
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, link.next.max_id)
|
2019-04-14 15:51:28 +02:00
|
|
|
} else {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
return res.data
|
|
|
|
},
|
|
|
|
lazyFetchFavourites: async ({ state, commit, rootState }): Promise<Array<Status> | null> => {
|
|
|
|
if (state.lazyLoading) {
|
|
|
|
return Promise.resolve(null)
|
|
|
|
}
|
|
|
|
if (!state.maxId) {
|
|
|
|
return Promise.resolve(null)
|
|
|
|
}
|
|
|
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
|
2019-10-27 06:41:03 +01:00
|
|
|
const client = new Mastodon(
|
|
|
|
rootState.TimelineSpace.account.accessToken!,
|
|
|
|
rootState.TimelineSpace.account.baseURL + '/api/v1',
|
|
|
|
rootState.App.userAgent,
|
|
|
|
rootState.App.proxyConfiguration
|
|
|
|
)
|
2019-05-27 15:56:54 +02:00
|
|
|
const res: Response<Array<Status>> = await client.get<Array<Status>>('/favourites', { max_id: state.maxId, limit: 40 }).finally(() => {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
|
|
|
|
})
|
2019-04-14 15:51:28 +02:00
|
|
|
commit(MUTATION_TYPES.INSERT_FAVOURITES, res.data)
|
|
|
|
// Parse link header
|
|
|
|
try {
|
|
|
|
const link = parse(res.headers.link)
|
|
|
|
if (link !== null) {
|
2019-05-27 15:56:54 +02:00
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, link.next.max_id)
|
2019-04-14 15:51:28 +02:00
|
|
|
} else {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
commit(MUTATION_TYPES.CHANGE_MAX_ID, null)
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
return res.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Favourites: Module<FavouritesState, RootState> = {
|
|
|
|
namespaced: true,
|
|
|
|
state: state,
|
|
|
|
mutations: mutations,
|
|
|
|
actions: actions
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Favourites
|