import generator , { Entity } from 'megalodon' ; import { Module , MutationTree , ActionTree } from 'vuex' ; import { RootState } from '@/store' ; import { LoadPositionWithAccount } from '@/types/loadPosition' ; export type PostsState = { timeline : Array < Entity . Status > ; pinnedToots : Array < Entity . Status > ; lazyLoading : boolean ; } ; const state = ( ) : PostsState => ( { timeline : [ ] , pinnedToots : [ ] , lazyLoading : false } ) ; export const MUTATION_TYPES = { UPDATE_TIMELINE : 'updateTimeline' , INSERT_TIMELINE : 'insertTimeline' , UPDATE_PINNED_TOOTS : 'updatePinnedToots' , CHANGE_LAZY_LOADING : 'changeLazyLoading' , UPDATE_PINNED_TOOT : 'updatePinnedToot' , UPDATE_TOOT : 'updateToot' , DELETE_TOOT : 'deleteToot' } ; const mutations = { [ MUTATION_TYPES . UPDATE_TIMELINE ] : ( state , timeline ) => { state . timeline = timeline ; } , [ MUTATION_TYPES . INSERT_TIMELINE ] : ( state , messages ) => { state . timeline = state . timeline . concat ( messages ) ; } , [ MUTATION_TYPES . UPDATE_PINNED_TOOTS ] : ( state , messages ) => { state . pinnedToots = messages ; } , [ MUTATION_TYPES . CHANGE_LAZY_LOADING ] : ( state , value ) => { state . lazyLoading = value ; } , [ MUTATION_TYPES . UPDATE_PINNED_TOOT ] : ( state , message ) => { state . pinnedToots = state . pinnedToots . map ( toot => { if ( toot . id === message . id ) { return message ; } else if ( toot . reblog !== null && toot . reblog . id === message . id ) { const reblog = { reblog : message } ; return Object . assign ( toot , reblog ) ; } else { return toot ; } } ) ; } , [ MUTATION_TYPES . UPDATE_TOOT ] : ( 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 ) { const reblog = { reblog : message } ; return Object . assign ( toot , reblog ) ; } else { return toot ; } } ) ; } , [ MUTATION_TYPES . DELETE_TOOT ] : ( 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 ; } } ) ; } } ; const actions = { fetchTimeline : async ( { commit , rootState } , account ) => { commit ( 'TimelineSpace/Contents/SideBar/AccountProfile/changeLoading' , true , { root : true } ) ; const client = generator ( rootState . TimelineSpace . sns , rootState . TimelineSpace . account . baseURL , rootState . TimelineSpace . account . accessToken , rootState . App . userAgent ) ; const pinned = await client . getAccountStatuses ( account . id , { pinned : true , limit : 10 } ) ; commit ( MUTATION_TYPES . UPDATE_PINNED_TOOTS , pinned . data ) ; const res = await client . getAccountStatuses ( account . id , { limit : 40 , pinned : false , exclude_replies : true } ) ; commit ( 'TimelineSpace/Contents/SideBar/AccountProfile/changeLoading' , false , { root : true } ) ; commit ( MUTATION_TYPES . UPDATE_TIMELINE , res . data ) ; return res . data ; } , lazyFetchTimeline : async ( { state , commit , rootState } , loadPosition ) : Promise < null > => { if ( state . lazyLoading ) { return Promise . resolve ( null ) ; } commit ( MUTATION_TYPES . CHANGE_LAZY_LOADING , true ) ; const client = generator ( rootState . TimelineSpace . sns , rootState . TimelineSpace . account . baseURL , rootState . TimelineSpace . account . accessToken , rootState . App . userAgent ) ; try { const res = await client . getAccountStatuses ( loadPosition . account . id , { max_id : loadPosition . status . id , limit : 40 , pinned : false , exclude_replies : true } ) ; commit ( MUTATION_TYPES . INSERT_TIMELINE , res . data ) ; } finally { commit ( MUTATION_TYPES . CHANGE_LAZY_LOADING , false ) ; } return null ; } , clearTimeline : ( { commit } ) => { commit ( MUTATION_TYPES . UPDATE_TIMELINE , [ ] ) ; } } ; const Posts = { namespaced : true , state : state , mutations : mutations , actions : actions } ; export default Posts ;