import generator , { Entity , NotificationType } from 'megalodon' ; import { Module , MutationTree , ActionTree , GetterTree } from 'vuex' ; import { RootState } from '@/store' ; export type MentionsState = { lazyLoading : boolean ; heading : boolean ; mentions : Array < Entity . Notification > ; scrolling : boolean ; } ; const state = ( ) : MentionsState => ( { lazyLoading : false , heading : true , mentions : [ ] , scrolling : false } ) ; export const MUTATION_TYPES = { CHANGE_LAZY_LOADING : 'changeLazyLoading' , CHANGE_HEADING : 'changeHeading' , APPEND_MENTIONS : 'appendMentions' , UPDATE_MENTIONS : 'updateMentions' , INSERT_MENTIONS : 'insertMentions' , ARCHIVE_MENTIONS : 'archiveMentions' , CLEAR_MENTIONS : 'clearMentions' , UPDATE_TOOT : 'updateToot' , DELETE_TOOT : 'deleteToot' , CHANGE_SCROLLING : 'changeScrolling' } ; const mutations = { [ MUTATION_TYPES . CHANGE_LAZY_LOADING ] : ( state , value ) => { state . lazyLoading = value ; } , [ MUTATION_TYPES . CHANGE_HEADING ] : ( state , value ) => { state . heading = value ; } , [ MUTATION_TYPES . APPEND_MENTIONS ] : ( state , update ) => { if ( ! state . mentions . find ( item => item . id === update . id ) ) { state . mentions = [ update ] . concat ( state . mentions ) ; } } , [ MUTATION_TYPES . UPDATE_MENTIONS ] : ( state , messages ) => { state . mentions = messages ; } , [ MUTATION_TYPES . INSERT_MENTIONS ] : ( state , messages ) => { state . mentions = state . mentions . concat ( messages ) ; } , [ MUTATION_TYPES . ARCHIVE_MENTIONS ] : state => { state . mentions = state . mentions . slice ( 0 , 40 ) ; } , [ MUTATION_TYPES . CLEAR_MENTIONS ] : state => { state . mentions = [ ] ; } , [ MUTATION_TYPES . UPDATE_TOOT ] : ( state , message ) => { state . mentions = state . mentions . map ( mention => { if ( mention . status !== undefined && mention . status . id === message . id ) { const status = { status : message } ; return Object . assign ( mention , status ) ; } else { return mention ; } } ) ; } , [ MUTATION_TYPES . DELETE_TOOT ] : ( state , id ) => { state . mentions = state . mentions . filter ( mention => { if ( mention . status ) { if ( mention . status . reblog && mention . status . reblog . id === id ) { return false ; } else { return mention . status . id !== id ; } } else { return true ; } } ) ; } , [ MUTATION_TYPES . CHANGE_SCROLLING ] : ( state , value ) => { state . scrolling = value ; } } ; const actions = { fetchMentions : async ( { commit , rootState } ) : Promise < Array < Entity . Notification > > => { const client = generator ( rootState . TimelineSpace . sns , rootState . TimelineSpace . account . baseURL , rootState . TimelineSpace . account . accessToken , rootState . App . userAgent ) ; const res = await client . getNotifications ( { limit : 30 , exclude_types : [ NotificationType . Follow , NotificationType . Favourite , NotificationType . Reblog , NotificationType . PollVote , NotificationType . PollExpired ] } ) ; commit ( MUTATION_TYPES . UPDATE_MENTIONS , res . data ) ; return res . data ; } , lazyFetchMentions : async ( { state , commit , rootState } , lastMention ) : Promise < Array < Entity . Notification > | 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 ) ; return client . getNotifications ( { max_id : lastMention . id , limit : 30 , exclude_types : [ NotificationType . Follow , NotificationType . Favourite , NotificationType . Reblog , NotificationType . PollVote , NotificationType . PollExpired ] } ) . then ( res => { commit ( MUTATION_TYPES . INSERT_MENTIONS , res . data ) ; return res . data ; } ) . finally ( ( ) => { commit ( MUTATION_TYPES . CHANGE_LAZY_LOADING , false ) ; } ) ; } } ; const getters = { mentions : state => { return state . mentions . filter ( mention => mention . type === 'mention' ) ; } } ; const Mentions = { namespaced : true , state : state , mutations : mutations , actions : actions , getters : getters } ; export default Mentions ;