@ -1,35 +1,19 @@
import api , { getLinks } from 'flavours/glitch/util/api' ;
import { Map as ImmutableMap , List as ImmutableList } from 'immutable' ;
import { Map as ImmutableMap } from 'immutable' ;
export const TIMELINE _UPDATE = 'TIMELINE_UPDATE' ;
export const TIMELINE _DELETE = 'TIMELINE_DELETE' ;
export const TIMELINE _REFRESH _REQUEST = 'TIMELINE_REFRESH_REQUEST' ;
export const TIMELINE _REFRESH _SUCCESS = 'TIMELINE_REFRESH_SUCCESS' ;
export const TIMELINE _REFRESH _FAIL = 'TIMELINE_REFRESH_FAIL' ;
export const TIMELINE _EXPAND _REQUEST = 'TIMELINE_EXPAND_REQUEST' ;
export const TIMELINE _EXPAND _SUCCESS = 'TIMELINE_EXPAND_SUCCESS' ;
export const TIMELINE _EXPAND _FAIL = 'TIMELINE_EXPAND_FAIL' ;
export const TIMELINE _SCROLL _TOP = 'TIMELINE_SCROLL_TOP' ;
export const TIMELINE _CONNECT = 'TIMELINE_CONNECT' ;
export const TIMELINE _DISCONNECT = 'TIMELINE_DISCONNECT' ;
export const TIMELINE _CONTEXT _UPDATE = 'CONTEXT_UPDATE' ;
export function refreshTimelineSuccess ( timeline , statuses , skipLoading , next , partial ) {
return {
type : TIMELINE _REFRESH _SUCCESS ,
timeline ,
statuses ,
skipLoading ,
next ,
partial ,
} ;
} ;
export function updateTimeline ( timeline , status ) {
return ( dispatch , getState ) => {
const references = status . reblog ? getState ( ) . get ( 'statuses' ) . filter ( ( item , itemId ) => ( itemId === status . reblog . id || item . get ( 'reblog' ) === status . reblog . id ) ) . map ( ( _ , itemId ) => itemId ) : [ ] ;
@ -77,97 +61,34 @@ export function deleteFromTimelines(id) {
} ;
} ;
export function refreshTimelineRequest ( timeline , skipLoading ) {
return {
type : TIMELINE _REFRESH _REQUEST ,
timeline ,
skipLoading ,
} ;
} ;
export function refreshTimeline ( timelineId , path , params = { } ) {
return function ( dispatch , getState ) {
const timeline = getState ( ) . getIn ( [ 'timelines' , timelineId ] , ImmutableMap ( ) ) ;
if ( timeline . get ( 'isLoading' ) || ( timeline . get ( 'online' ) && ! timeline . get ( 'isPartial' ) ) ) {
return ;
}
const ids = timeline . get ( 'items' , ImmutableList ( ) ) ;
const newestId = ids . size > 0 ? ids . first ( ) : null ;
let skipLoading = timeline . get ( 'loaded' ) ;
if ( newestId !== null ) {
params . since _id = newestId ;
}
dispatch ( refreshTimelineRequest ( timelineId , skipLoading ) ) ;
api ( getState ) . get ( path , { params } ) . then ( response => {
if ( response . status === 206 ) {
dispatch ( refreshTimelineSuccess ( timelineId , [ ] , skipLoading , null , true ) ) ;
} else {
const next = getLinks ( response ) . refs . find ( link => link . rel === 'next' ) ;
dispatch ( refreshTimelineSuccess ( timelineId , response . data , skipLoading , next ? next . uri : null , false ) ) ;
}
} ) . catch ( error => {
dispatch ( refreshTimelineFail ( timelineId , error , skipLoading ) ) ;
} ) ;
} ;
} ;
export const refreshHomeTimeline = ( ) => refreshTimeline ( 'home' , '/api/v1/timelines/home' ) ;
export const refreshPublicTimeline = ( ) => refreshTimeline ( 'public' , '/api/v1/timelines/public' ) ;
export const refreshCommunityTimeline = ( ) => refreshTimeline ( 'community' , '/api/v1/timelines/public' , { local : true } ) ;
export const refreshDirectTimeline = ( ) => refreshTimeline ( 'direct' , '/api/v1/timelines/direct' ) ;
export const refreshAccountTimeline = ( accountId , withReplies ) => refreshTimeline ( ` account: ${ accountId } ${ withReplies ? ':with_replies' : '' } ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { exclude _replies : ! withReplies } ) ;
export const refreshAccountFeaturedTimeline = accountId => refreshTimeline ( ` account: ${ accountId } :pinned ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { pinned : true } ) ;
export const refreshAccountMediaTimeline = accountId => refreshTimeline ( ` account: ${ accountId } :media ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { only _media : true } ) ;
export const refreshHashtagTimeline = hashtag => refreshTimeline ( ` hashtag: ${ hashtag } ` , ` /api/v1/timelines/tag/ ${ hashtag } ` ) ;
export const refreshListTimeline = id => refreshTimeline ( ` list: ${ id } ` , ` /api/v1/timelines/list/ ${ id } ` ) ;
export function refreshTimelineFail ( timeline , error , skipLoading ) {
return {
type : TIMELINE _REFRESH _FAIL ,
timeline ,
error ,
skipLoading ,
skipAlert : error . response && error . response . status === 404 ,
} ;
} ;
export function expandTimeline ( timelineId , path , params = { } ) {
return ( dispatch , getState ) => {
const timeline = getState ( ) . getIn ( [ 'timelines' , timelineId ] , ImmutableMap ( ) ) ;
const ids = timeline . get ( 'items' , ImmutableList ( ) ) ;
if ( timeline . get ( 'isLoading' ) || ids . size === 0 ) {
if ( timeline . get ( 'isLoading' ) ) {
return ;
}
params . max _id = ids . last ( ) ;
params . limit = 10 ;
dispatch ( expandTimelineRequest ( timelineId ) ) ;
api ( getState ) . get ( path , { params } ) . then ( response => {
const next = getLinks ( response ) . refs . find ( link => link . rel === 'next' ) ;
dispatch ( expandTimelineSuccess ( timelineId , response . data , next ? next . uri : null )) ;
dispatch ( expandTimelineSuccess ( timelineId , response . data , next ? next . uri : null , response . code === 206 ) ) ;
} ) . catch ( error => {
dispatch ( expandTimelineFail ( timelineId , error ) ) ;
} ) ;
} ;
} ;
export const expandHomeTimeline = ( ) => expandTimeline ( 'home' , '/api/v1/timelines/home' ) ;
export const expandPublicTimeline = ( ) => expandTimeline ( 'public' , '/api/v1/timelines/public' ) ;
export const expandCommunityTimeline = ( ) => expandTimeline ( 'community' , '/api/v1/timelines/public' , { local : true } ) ;
export const expandDirectTimeline = ( ) => expandTimeline ( 'direct' , '/api/v1/timelines/direct' ) ;
export const expandAccountTimeline = ( accountId , withReplies ) => expandTimeline ( ` account: ${ accountId } ${ withReplies ? ':with_replies' : '' } ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { exclude _replies : ! withReplies } )
export const expandAccountMediaTimeline = accountId => expandTimeline ( ` account: ${ accountId } :media ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { only _media : true } ) ;
export const expandHashtagTimeline = hashtag => expandTimeline ( ` hashtag: ${ hashtag } ` , ` /api/v1/timelines/tag/ ${ hashtag } ` ) ;
export const expandListTimeline = id => expandTimeline ( ` list: ${ id } ` , ` /api/v1/timelines/list/ ${ id } ` ) ;
export const expandHomeTimeline = ( { maxId } = { } ) => expandTimeline ( 'home' , '/api/v1/timelines/home' , { max _id : maxId } ) ;
export const expandPublicTimeline = ( { maxId } = { } ) => expandTimeline ( 'public' , '/api/v1/timelines/public' , { max _id : maxId } ) ;
export const expandCommunityTimeline = ( { maxId } = { } ) => expandTimeline ( 'community' , '/api/v1/timelines/public' , { local : true , max _id : maxId } ) ;
export const expandDirectTimeline = ( { maxId } = { } ) => expandTimeline ( 'direct' , '/api/v1/timelines/direct' , { max _id : maxId } ) ;
export const expandAccountTimeline = ( accountId , { maxId , withReplies } = { } ) => expandTimeline ( ` account: ${ accountId } ${ withReplies ? ':with_replies' : '' } ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { exclude _replies : ! withReplies , max _id : maxId } ) ;
export const expandAccountFeaturedTimeline = accountId => expandTimeline ( ` account: ${ accountId } :pinned ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { pinned : true } ) ;
export const expandAccountMediaTimeline = ( accountId , { maxId } = { } ) => expandTimeline ( ` account: ${ accountId } :media ` , ` /api/v1/accounts/ ${ accountId } /statuses ` , { max _id : maxId , only _media : true } ) ;
export const expandHashtagTimeline = ( hashtag , { maxId } = { } ) => expandTimeline ( ` hashtag: ${ hashtag } ` , ` /api/v1/timelines/tag/ ${ hashtag } ` , { max _id : maxId } ) ;
export const expandListTimeline = ( id , { maxId } = { } ) => expandTimeline ( ` list: ${ id } ` , ` /api/v1/timelines/list/ ${ id } ` , { max _id : maxId } ) ;
export function expandTimelineRequest ( timeline ) {
return {
@ -176,12 +97,13 @@ export function expandTimelineRequest(timeline) {
} ;
} ;
export function expandTimelineSuccess ( timeline , statuses , next ) {
export function expandTimelineSuccess ( timeline , statuses , next , partial ) {
return {
type : TIMELINE _EXPAND _SUCCESS ,
timeline ,
statuses ,
next ,
partial ,
} ;
} ;
@ -201,13 +123,6 @@ export function scrollTopTimeline(timeline, top) {
} ;
} ;
export function connectTimeline ( timeline ) {
return {
type : TIMELINE _CONNECT ,
timeline ,
} ;
} ;
export function disconnectTimeline ( timeline ) {
return {
type : TIMELINE _DISCONNECT ,