2018-01-28 21:09:39 +00:00
|
|
|
import { store } from '../_store/store'
|
2018-02-08 16:22:14 +00:00
|
|
|
import { database } from '../_database/database'
|
|
|
|
import { getTimeline } from '../_api/timelines'
|
2018-01-28 01:34:08 +00:00
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-01-28 23:02:02 +00:00
|
|
|
import { mergeArrays } from '../_utils/arrays'
|
2018-03-10 18:54:16 +00:00
|
|
|
import { byItemIds } from '../_utils/sorting'
|
2018-03-11 19:11:06 +00:00
|
|
|
import isEqual from 'lodash/isEqual'
|
2018-01-28 01:34:08 +00:00
|
|
|
|
|
|
|
const FETCH_LIMIT = 20
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('fetchTimelineItems')
|
|
|
|
let items
|
2018-03-11 19:11:06 +00:00
|
|
|
let stale = false
|
2018-01-28 01:34:08 +00:00
|
|
|
if (!online) {
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
2018-03-11 19:11:06 +00:00
|
|
|
stale = true
|
2018-01-28 01:34:08 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
|
|
|
/* no await */ database.insertTimelineItems(instanceName, timelineName, items)
|
2018-01-28 01:34:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Internet request failed. Showing offline content.')
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
2018-03-11 19:11:06 +00:00
|
|
|
stale = true
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 02:06:02 +00:00
|
|
|
stop('fetchTimelineItems')
|
2018-03-11 19:11:06 +00:00
|
|
|
return { items, stale }
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 19:11:06 +00:00
|
|
|
async function addTimelineItems (instanceName, timelineName, items, stale) {
|
|
|
|
console.log('addTimelineItems, length:', items.length)
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('addTimelineItems')
|
2018-03-11 19:11:06 +00:00
|
|
|
let newIds = items.map(item => item.id)
|
|
|
|
addTimelineItemIds(instanceName, timelineName, newIds, stale)
|
2018-02-11 21:46:57 +00:00
|
|
|
stop('addTimelineItems')
|
|
|
|
}
|
|
|
|
|
2018-03-11 19:11:06 +00:00
|
|
|
export async function addTimelineItemIds (instanceName, timelineName, newIds, newStale) {
|
2018-02-04 02:06:02 +00:00
|
|
|
let oldIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds') || []
|
2018-03-11 19:11:06 +00:00
|
|
|
let oldStale = store.getForTimeline(instanceName, timelineName, 'timelineItemIdsAreStale')
|
|
|
|
|
|
|
|
let mergedIds = mergeArrays(oldIds, newIds)
|
|
|
|
|
|
|
|
if (!isEqual(oldIds, mergedIds)) {
|
|
|
|
store.setForTimeline(instanceName, timelineName, {timelineItemIds: mergedIds})
|
|
|
|
}
|
|
|
|
if (oldStale !== newStale) {
|
|
|
|
store.setForTimeline(instanceName, timelineName, {timelineItemIdsAreStale: newStale})
|
|
|
|
}
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function fetchTimelineItemsAndPossiblyFallBack () {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-28 01:34:08 +00:00
|
|
|
let timelineName = store.get('currentTimeline')
|
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let accessToken = store.get('accessToken')
|
2018-02-04 02:06:02 +00:00
|
|
|
let lastTimelineItemId = store.get('lastTimelineItemId')
|
2018-01-28 01:34:08 +00:00
|
|
|
let online = store.get('online')
|
|
|
|
|
2018-03-11 19:11:06 +00:00
|
|
|
let { items, stale } = await fetchTimelineItems(instanceName, accessToken, timelineName, lastTimelineItemId, online)
|
|
|
|
addTimelineItems(instanceName, timelineName, items, stale)
|
2018-02-04 02:06:02 +00:00
|
|
|
stop('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function initializeTimeline () {
|
2018-01-28 01:34:08 +00:00
|
|
|
mark('initializeTimeline')
|
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let timeline = store.get('currentTimeline')
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
requestAnimationFrame(() => {
|
2018-02-12 00:10:39 +00:00
|
|
|
console.log('initialized')
|
2018-01-28 01:34:08 +00:00
|
|
|
store.setForTimeline(instanceName, timeline, {initialized: true})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
stop('initializeTimeline')
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function setupTimeline () {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('setupTimeline')
|
2018-03-11 19:11:06 +00:00
|
|
|
// If we don't have any item ids, or if the current item ids are stale
|
|
|
|
// (i.e. via offline mode), then we need to re-fetch
|
|
|
|
// Also do this if it's a thread, because threads change pretty frequently and
|
|
|
|
// we don't have a good way to update them.
|
|
|
|
|
|
|
|
let timelineItemIds = store.get('timelineItemIds')
|
|
|
|
let timelineItemIdsAreStale = store.get('timelineItemIdsAreStale')
|
|
|
|
let currentTimeline = store.get('currentTimeline')
|
|
|
|
if (!timelineItemIds ||
|
|
|
|
timelineItemIdsAreStale ||
|
|
|
|
currentTimeline.startsWith('status/')) {
|
2018-02-04 02:06:02 +00:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
2018-02-04 02:06:02 +00:00
|
|
|
stop('setupTimeline')
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:54:16 +00:00
|
|
|
export async function fetchTimelineItemsOnScrollToBottom (instanceName, timelineName) {
|
2018-01-28 01:34:08 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: true })
|
2018-02-04 02:06:02 +00:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-28 01:34:08 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: false })
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-02-12 03:15:21 +00:00
|
|
|
|
2018-03-10 18:54:16 +00:00
|
|
|
export async function showMoreItemsForTimeline (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForTimeline')
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd')
|
|
|
|
itemIdsToAdd = itemIdsToAdd.sort(byItemIds).reverse()
|
2018-03-11 19:11:06 +00:00
|
|
|
addTimelineItemIds(instanceName, timelineName, itemIdsToAdd, false)
|
2018-02-12 03:15:21 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
itemIdsToAdd: [],
|
|
|
|
shouldShowHeader: false,
|
|
|
|
showHeader: false
|
|
|
|
})
|
2018-03-10 18:54:16 +00:00
|
|
|
stop('showMoreItemsForTimeline')
|
2018-02-14 03:35:46 +00:00
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
|
2018-03-10 18:54:16 +00:00
|
|
|
export async function showMoreItemsForCurrentTimeline () {
|
|
|
|
return showMoreItemsForTimeline(
|
|
|
|
store.get('currentInstance'),
|
|
|
|
store.get('currentTimeline')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showMoreItemsForThread (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForThread')
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd')
|
2018-03-10 06:31:26 +00:00
|
|
|
let timelineItemIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds')
|
2018-03-10 18:54:16 +00:00
|
|
|
// TODO: update database and do the thread merge correctly
|
2018-03-10 06:31:26 +00:00
|
|
|
timelineItemIds = timelineItemIds.concat(itemIdsToAdd)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
itemIdsToAdd: [],
|
|
|
|
timelineItemIds: timelineItemIds
|
|
|
|
})
|
2018-03-10 18:54:16 +00:00
|
|
|
stop('showMoreItemsForThread')
|
2018-03-10 06:31:26 +00:00
|
|
|
}
|