2018-01-28 22:09:39 +01:00
|
|
|
import { store } from '../_store/store'
|
2018-02-08 17:22:14 +01:00
|
|
|
import { getTimeline } from '../_api/timelines'
|
2018-01-28 02:34:08 +01:00
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-08-24 20:50:40 +02:00
|
|
|
import { concat, mergeArrays } from '../_utils/arrays'
|
2018-03-10 19:54:16 +01:00
|
|
|
import { byItemIds } from '../_utils/sorting'
|
2018-04-06 02:57:36 +02:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-08-30 04:03:12 +02:00
|
|
|
import { database } from '../_database/database'
|
2018-08-24 20:50:40 +02:00
|
|
|
import { getStatus, getStatusContext } from '../_api/statuses'
|
2018-08-28 15:45:15 +02:00
|
|
|
import { emit } from '../_utils/eventBus'
|
2018-08-30 07:49:14 +02:00
|
|
|
import { TIMELINE_BATCH_SIZE } from '../_static/timelines'
|
2018-01-28 02:34:08 +01:00
|
|
|
|
2018-08-30 07:49:14 +02:00
|
|
|
const SCROLL_TO_BOTTOM_DELAY = 2000
|
2018-01-28 02:34:08 +01:00
|
|
|
|
2018-08-28 15:45:15 +02:00
|
|
|
async function storeFreshTimelineItemsInDatabase (instanceName, timelineName, items) {
|
2018-08-30 04:03:12 +02:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, items)
|
2018-08-28 15:45:15 +02:00
|
|
|
if (timelineName.startsWith('status/')) {
|
|
|
|
// For status threads, we want to be sure to update the favorite/reblog counts even if
|
|
|
|
// this is a stale "view" of the status. See 119-status-counts-update.js for
|
|
|
|
// an example of why we need this.
|
|
|
|
items.forEach(item => {
|
|
|
|
emit('statusUpdated', item)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 20:50:40 +02:00
|
|
|
async function fetchTimelineItemsFromNetwork (instanceName, accessToken, timelineName, lastTimelineItemId) {
|
|
|
|
if (timelineName.startsWith('status/')) { // special case - this is a list of descendents and ancestors
|
|
|
|
let statusId = timelineName.split('/').slice(-1)[0]
|
|
|
|
let statusRequest = getStatus(instanceName, accessToken, statusId)
|
|
|
|
let contextRequest = getStatusContext(instanceName, accessToken, statusId)
|
|
|
|
let [ status, context ] = await Promise.all([statusRequest, contextRequest])
|
|
|
|
return concat(context.ancestors, status, context.descendants)
|
|
|
|
} else { // normal timeline
|
2018-08-30 07:49:14 +02:00
|
|
|
return getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, null, TIMELINE_BATCH_SIZE)
|
2018-08-24 20:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
2018-02-04 03:06:02 +01:00
|
|
|
mark('fetchTimelineItems')
|
|
|
|
let items
|
2018-03-11 20:11:06 +01:00
|
|
|
let stale = false
|
2018-01-28 02:34:08 +01:00
|
|
|
if (!online) {
|
2018-08-30 07:49:14 +02:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-03-11 20:11:06 +01:00
|
|
|
stale = true
|
2018-01-28 02:34:08 +01:00
|
|
|
} else {
|
|
|
|
try {
|
2018-08-31 18:12:48 +02:00
|
|
|
console.log('fetchTimelineItemsFromNetwork')
|
2018-08-24 20:50:40 +02:00
|
|
|
items = await fetchTimelineItemsFromNetwork(instanceName, accessToken, timelineName, lastTimelineItemId)
|
2018-08-28 15:45:15 +02:00
|
|
|
/* no await */ storeFreshTimelineItemsInDatabase(instanceName, timelineName, items)
|
2018-01-28 02:34:08 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Internet request failed. Showing offline content.')
|
2018-08-30 07:49:14 +02:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-03-11 20:11:06 +01:00
|
|
|
stale = true
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 03:06:02 +01:00
|
|
|
stop('fetchTimelineItems')
|
2018-03-11 20:11:06 +01:00
|
|
|
return { items, stale }
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 20:11:06 +01:00
|
|
|
async function addTimelineItems (instanceName, timelineName, items, stale) {
|
|
|
|
console.log('addTimelineItems, length:', items.length)
|
2018-02-04 03:06:02 +01:00
|
|
|
mark('addTimelineItems')
|
2018-03-11 20:11:06 +01:00
|
|
|
let newIds = items.map(item => item.id)
|
|
|
|
addTimelineItemIds(instanceName, timelineName, newIds, stale)
|
2018-02-11 22:46:57 +01:00
|
|
|
stop('addTimelineItems')
|
|
|
|
}
|
|
|
|
|
2018-03-11 20:11:06 +01:00
|
|
|
export async function addTimelineItemIds (instanceName, timelineName, newIds, newStale) {
|
2018-03-15 06:14:06 +01:00
|
|
|
let oldIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds')
|
2018-03-11 20:11:06 +01:00
|
|
|
let oldStale = store.getForTimeline(instanceName, timelineName, 'timelineItemIdsAreStale')
|
|
|
|
|
2018-03-15 06:14:06 +01:00
|
|
|
let mergedIds = mergeArrays(oldIds || [], newIds)
|
2018-03-11 20:11:06 +01:00
|
|
|
|
|
|
|
if (!isEqual(oldIds, mergedIds)) {
|
2018-08-30 06:42:57 +02:00
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemIds: mergedIds })
|
2018-03-11 20:11:06 +01:00
|
|
|
}
|
|
|
|
if (oldStale !== newStale) {
|
2018-08-30 06:42:57 +02:00
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemIdsAreStale: newStale })
|
2018-03-11 20:11:06 +01:00
|
|
|
}
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function fetchTimelineItemsAndPossiblyFallBack () {
|
2018-02-04 03:06:02 +01:00
|
|
|
mark('fetchTimelineItemsAndPossiblyFallBack')
|
2018-04-19 18:37:05 +02:00
|
|
|
let {
|
|
|
|
currentTimeline,
|
|
|
|
currentInstance,
|
|
|
|
accessToken,
|
|
|
|
lastTimelineItemId,
|
|
|
|
online
|
|
|
|
} = store.get()
|
2018-01-28 02:34:08 +01:00
|
|
|
|
2018-04-19 18:37:05 +02:00
|
|
|
let { items, stale } = await fetchTimelineItems(currentInstance, accessToken, currentTimeline, lastTimelineItemId, online)
|
|
|
|
addTimelineItems(currentInstance, currentTimeline, items, stale)
|
2018-02-04 03:06:02 +01:00
|
|
|
stop('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function setupTimeline () {
|
2018-02-04 03:06:02 +01:00
|
|
|
mark('setupTimeline')
|
2018-03-11 20:11:06 +01: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.
|
2018-04-19 18:37:05 +02:00
|
|
|
let {
|
|
|
|
timelineItemIds,
|
|
|
|
timelineItemIdsAreStale,
|
|
|
|
currentTimeline
|
|
|
|
} = store.get()
|
2018-03-11 20:11:06 +01:00
|
|
|
if (!timelineItemIds ||
|
|
|
|
timelineItemIdsAreStale ||
|
|
|
|
currentTimeline.startsWith('status/')) {
|
2018-02-04 03:06:02 +01:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
2018-02-04 03:06:02 +01:00
|
|
|
stop('setupTimeline')
|
2018-01-28 02:34:08 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 19:54:16 +01:00
|
|
|
export async function fetchTimelineItemsOnScrollToBottom (instanceName, timelineName) {
|
2018-08-31 18:12:48 +02:00
|
|
|
console.log('setting runningUpdate: true')
|
2018-01-28 02:34:08 +01:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: true })
|
2018-02-04 03:06:02 +01:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-08-30 07:49:14 +02:00
|
|
|
setTimeout(() => {
|
2018-08-31 18:12:48 +02:00
|
|
|
console.log('setting runningUpdate: false')
|
2018-08-30 07:49:14 +02:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: false })
|
|
|
|
}, SCROLL_TO_BOTTOM_DELAY) // delay to avoid spamming network calls on scroll to bottom
|
2018-02-09 07:29:29 +01:00
|
|
|
}
|
2018-02-12 04:15:21 +01:00
|
|
|
|
2018-03-10 19:54:16 +01:00
|
|
|
export async function showMoreItemsForTimeline (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForTimeline')
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd')
|
|
|
|
itemIdsToAdd = itemIdsToAdd.sort(byItemIds).reverse()
|
2018-03-11 20:11:06 +01:00
|
|
|
addTimelineItemIds(instanceName, timelineName, itemIdsToAdd, false)
|
2018-02-12 04:15:21 +01:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
itemIdsToAdd: [],
|
|
|
|
shouldShowHeader: false,
|
|
|
|
showHeader: false
|
|
|
|
})
|
2018-03-10 19:54:16 +01:00
|
|
|
stop('showMoreItemsForTimeline')
|
2018-02-14 04:35:46 +01:00
|
|
|
}
|
2018-03-10 07:31:26 +01:00
|
|
|
|
2018-03-10 19:54:16 +01:00
|
|
|
export async function showMoreItemsForCurrentTimeline () {
|
2018-04-19 18:37:05 +02:00
|
|
|
let { currentInstance, currentTimeline } = store.get()
|
2018-03-10 19:54:16 +01:00
|
|
|
return showMoreItemsForTimeline(
|
2018-04-19 18:37:05 +02:00
|
|
|
currentInstance,
|
|
|
|
currentTimeline
|
2018-03-10 19:54:16 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showMoreItemsForThread (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForThread')
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd')
|
2018-03-10 07:31:26 +01:00
|
|
|
let timelineItemIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds')
|
2018-03-10 19:54:16 +01:00
|
|
|
// TODO: update database and do the thread merge correctly
|
2018-03-10 07:31:26 +01:00
|
|
|
timelineItemIds = timelineItemIds.concat(itemIdsToAdd)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
itemIdsToAdd: [],
|
|
|
|
timelineItemIds: timelineItemIds
|
|
|
|
})
|
2018-03-10 19:54:16 +01:00
|
|
|
stop('showMoreItemsForThread')
|
2018-03-10 07:31:26 +01:00
|
|
|
}
|