refs #850 Replace Publis with typescript
This commit is contained in:
parent
6c3fe850f0
commit
0e0cbabe23
|
@ -3,7 +3,7 @@ import Home, { HomeState } from './Contents/Home'
|
||||||
import Notifications from './Contents/Notifications'
|
import Notifications from './Contents/Notifications'
|
||||||
import Favourites from './Contents/Favourites'
|
import Favourites from './Contents/Favourites'
|
||||||
import Local, { LocalState } from './Contents/Local'
|
import Local, { LocalState } from './Contents/Local'
|
||||||
import Public from './Contents/Public'
|
import Public, { PublicState } from './Contents/Public'
|
||||||
import Search from './Contents/Search'
|
import Search from './Contents/Search'
|
||||||
import Lists from './Contents/Lists'
|
import Lists from './Contents/Lists'
|
||||||
import Hashtag from './Contents/Hashtag'
|
import Hashtag from './Contents/Hashtag'
|
||||||
|
@ -17,7 +17,8 @@ export interface ContentsState {}
|
||||||
export interface ContentsModuleState extends ContentsState {
|
export interface ContentsModuleState extends ContentsState {
|
||||||
SideBar: SideBarModuleState,
|
SideBar: SideBarModuleState,
|
||||||
Home: HomeState,
|
Home: HomeState,
|
||||||
Local: LocalState
|
Local: LocalState,
|
||||||
|
Public: PublicState
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = (): ContentsState => ({})
|
const state = (): ContentsState => ({})
|
||||||
|
|
|
@ -22,7 +22,7 @@ export const MUTATION_TYPES = {
|
||||||
CHANGE_HEADING: 'changeHeading',
|
CHANGE_HEADING: 'changeHeading',
|
||||||
APPEND_TIMELINE: 'appendTimeline',
|
APPEND_TIMELINE: 'appendTimeline',
|
||||||
UPDATE_TIMELINE: 'updateTimeline',
|
UPDATE_TIMELINE: 'updateTimeline',
|
||||||
MERGE_TIMELINE:' mergeTimeline',
|
MERGE_TIMELINE: 'mergeTimeline',
|
||||||
INSERT_TIMELINE: 'insertTimeline',
|
INSERT_TIMELINE: 'insertTimeline',
|
||||||
ARCHIVE_TIMELINE: 'archiveTimeline',
|
ARCHIVE_TIMELINE: 'archiveTimeline',
|
||||||
CLEAR_TIMELINE: 'clearTimeline',
|
CLEAR_TIMELINE: 'clearTimeline',
|
||||||
|
|
|
@ -1,109 +0,0 @@
|
||||||
import Mastodon from 'megalodon'
|
|
||||||
|
|
||||||
const Public = {
|
|
||||||
namespaced: true,
|
|
||||||
state: {
|
|
||||||
timeline: [],
|
|
||||||
unreadTimeline: [],
|
|
||||||
lazyLoading: false,
|
|
||||||
heading: true,
|
|
||||||
filter: ''
|
|
||||||
},
|
|
||||||
mutations: {
|
|
||||||
changeHeading (state, value) {
|
|
||||||
state.heading = value
|
|
||||||
},
|
|
||||||
appendTimeline (state, update) {
|
|
||||||
if (state.heading) {
|
|
||||||
state.timeline = [update].concat(state.timeline)
|
|
||||||
} else {
|
|
||||||
state.unreadTimeline = [update].concat(state.unreadTimeline)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
updateTimeline (state, messages) {
|
|
||||||
state.timeline = messages
|
|
||||||
},
|
|
||||||
mergeTimeline (state) {
|
|
||||||
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
|
|
||||||
state.unreadTimeline = []
|
|
||||||
},
|
|
||||||
insertTimeline (state, messages) {
|
|
||||||
state.timeline = state.timeline.concat(messages)
|
|
||||||
},
|
|
||||||
archiveTimeline (state) {
|
|
||||||
state.timeline = state.timeline.slice(0, 40)
|
|
||||||
},
|
|
||||||
clearTimeline (state) {
|
|
||||||
state.timeline = []
|
|
||||||
state.unreadTimeline = []
|
|
||||||
},
|
|
||||||
updateToot (state, message) {
|
|
||||||
state.timeline = state.timeline.map((toot) => {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
deleteToot (state, message) {
|
|
||||||
state.timeline = state.timeline.filter((toot) => {
|
|
||||||
if (toot.reblog !== null && toot.reblog.id === message.id) {
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
return toot.id !== message.id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
changeLazyLoading (state, value) {
|
|
||||||
state.lazyLoading = value
|
|
||||||
},
|
|
||||||
changeFilter (state, filter) {
|
|
||||||
state.filter = filter
|
|
||||||
}
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
fetchPublicTimeline ({ commit, rootState }) {
|
|
||||||
const client = new Mastodon(
|
|
||||||
rootState.TimelineSpace.account.accessToken,
|
|
||||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
|
||||||
)
|
|
||||||
return client.get('/timelines/public', { limit: 40 })
|
|
||||||
.then(res => {
|
|
||||||
commit('updateTimeline', res.data)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
lazyFetchTimeline ({ state, commit, rootState }, last) {
|
|
||||||
if (last === undefined || last === null) {
|
|
||||||
return Promise.resolve(null)
|
|
||||||
}
|
|
||||||
if (state.lazyLoading) {
|
|
||||||
return Promise.resolve(null)
|
|
||||||
}
|
|
||||||
commit('changeLazyLoading', true)
|
|
||||||
const client = new Mastodon(
|
|
||||||
rootState.TimelineSpace.account.accessToken,
|
|
||||||
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
|
||||||
)
|
|
||||||
return client.get('/timelines/public', { max_id: last.id, limit: 40 })
|
|
||||||
.then(res => {
|
|
||||||
commit('insertTimeline', res.data)
|
|
||||||
commit('changeLazyLoading', false)
|
|
||||||
return res.data
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
commit('changeLazyLoading', false)
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Public
|
|
|
@ -0,0 +1,132 @@
|
||||||
|
import Mastodon, { Status, Response } from 'megalodon'
|
||||||
|
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||||
|
import { RootState } from '@/store'
|
||||||
|
|
||||||
|
export interface PublicState {
|
||||||
|
timeline: Array<Status>,
|
||||||
|
unreadTimeline: Array<Status>,
|
||||||
|
lazyLoading: boolean,
|
||||||
|
heading: boolean,
|
||||||
|
filter: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = (): PublicState => ({
|
||||||
|
timeline: [],
|
||||||
|
unreadTimeline: [],
|
||||||
|
lazyLoading: false,
|
||||||
|
heading: true,
|
||||||
|
filter: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
export const MUTATION_TYPES = {
|
||||||
|
CHANGE_HEADING: 'changeHeading',
|
||||||
|
APPEND_TIMELINE: 'appendTimeline',
|
||||||
|
UPDATE_TIMELINE: 'updateTimeline',
|
||||||
|
MERGE_TIMELINE: 'mergeTimeline',
|
||||||
|
INSERT_TIMELINE: 'insertTimeline',
|
||||||
|
ARCHIVE_TIMELINE: 'archiveTimeine',
|
||||||
|
CLEAR_TIMELINE: 'clearTimeine',
|
||||||
|
UPDATE_TOOT: 'updateToot',
|
||||||
|
DELETE_TOOT: 'deleteToot',
|
||||||
|
CHANGE_LAZY_LOADING: 'changeLazyLoading',
|
||||||
|
CHANGE_FILTER: 'changeFilter'
|
||||||
|
}
|
||||||
|
|
||||||
|
const mutations: MutationTree<PublicState> = {
|
||||||
|
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
|
||||||
|
state.heading = value
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
|
||||||
|
if (state.heading) {
|
||||||
|
state.timeline = [update].concat(state.timeline)
|
||||||
|
} else {
|
||||||
|
state.unreadTimeline = [update].concat(state.unreadTimeline)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
|
||||||
|
state.timeline = messages
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
|
||||||
|
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
|
||||||
|
state.unreadTimeline = []
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
|
||||||
|
state.timeline = state.timeline.concat(messages)
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
|
||||||
|
state.timeline = state.timeline.slice(0, 40)
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
|
||||||
|
state.timeline = []
|
||||||
|
state.unreadTimeline = []
|
||||||
|
},
|
||||||
|
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
|
||||||
|
state.timeline = state.timeline.map((toot) => {
|
||||||
|
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) => {
|
||||||
|
state.timeline = state.timeline.filter((toot) => {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const actions: ActionTree<PublicState, RootState> = {
|
||||||
|
fetchPublicTimeline: async ({ commit, rootState }) => {
|
||||||
|
const client = new Mastodon(
|
||||||
|
rootState.TimelineSpace.account.accessToken!,
|
||||||
|
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||||
|
)
|
||||||
|
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/public', { limit: 40 })
|
||||||
|
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
|
||||||
|
},
|
||||||
|
lazyFetchTimeline: ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
|
||||||
|
if (state.lazyLoading) {
|
||||||
|
return Promise.resolve(null)
|
||||||
|
}
|
||||||
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
|
||||||
|
const client = new Mastodon(
|
||||||
|
rootState.TimelineSpace.account.accessToken!,
|
||||||
|
rootState.TimelineSpace.account.baseURL + '/api/v1'
|
||||||
|
)
|
||||||
|
return client.get<Array<Status>>('/timelines/public', { max_id: lastStatus.id, limit: 40 })
|
||||||
|
.then(res => {
|
||||||
|
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
|
||||||
|
return res.data
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Public: Module<PublicState, RootState> = {
|
||||||
|
namespaced: true,
|
||||||
|
state: state,
|
||||||
|
mutations: mutations,
|
||||||
|
actions: actions
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Public
|
Loading…
Reference in New Issue