2018-01-28 21:09:39 +00:00
|
|
|
import { store } from '../_store/store'
|
2018-02-08 16:22:14 +00:00
|
|
|
import { getTimeline } from '../_api/timelines'
|
2018-12-22 23:37:51 +00:00
|
|
|
import { toast } from '../_components/toast/toast'
|
2018-01-28 01:34:08 +00:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-08-24 18:50:40 +00:00
|
|
|
import { concat, mergeArrays } from '../_utils/arrays'
|
2019-03-03 21:24:55 +00:00
|
|
|
import { compareTimelineItemSummaries } from '../_utils/sorting'
|
2018-04-06 00:57:36 +00:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-08-30 02:03:12 +00:00
|
|
|
import { database } from '../_database/database'
|
2018-08-24 18:50:40 +00:00
|
|
|
import { getStatus, getStatusContext } from '../_api/statuses'
|
2018-08-28 13:45:15 +00:00
|
|
|
import { emit } from '../_utils/eventBus'
|
2018-08-30 05:49:14 +00:00
|
|
|
import { TIMELINE_BATCH_SIZE } from '../_static/timelines'
|
2019-03-03 21:24:55 +00:00
|
|
|
import { timelineItemToSummary } from '../_utils/timelineItemToSummary'
|
2018-01-28 01:34:08 +00:00
|
|
|
|
2018-08-28 13:45:15 +00:00
|
|
|
async function storeFreshTimelineItemsInDatabase (instanceName, timelineName, items) {
|
2018-08-30 02:03:12 +00:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, items)
|
2018-08-28 13:45:15 +00: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 18:50:40 +00: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 05:49:14 +00:00
|
|
|
return getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, null, TIMELINE_BATCH_SIZE)
|
2018-08-24 18:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-30 05:49:14 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-03-11 19:11:06 +00:00
|
|
|
stale = true
|
2018-01-28 01:34:08 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2018-08-31 16:12:48 +00:00
|
|
|
console.log('fetchTimelineItemsFromNetwork')
|
2018-08-24 18:50:40 +00:00
|
|
|
items = await fetchTimelineItemsFromNetwork(instanceName, accessToken, timelineName, lastTimelineItemId)
|
2018-08-28 13:45:15 +00:00
|
|
|
/* no await */ storeFreshTimelineItemsInDatabase(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-08-30 05:49:14 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
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)
|
2019-03-03 21:24:55 +00:00
|
|
|
mark('addTimelineItemSummaries')
|
|
|
|
let newSummaries = items.map(timelineItemToSummary)
|
|
|
|
addTimelineItemSummaries(instanceName, timelineName, newSummaries, stale)
|
|
|
|
stop('addTimelineItemSummaries')
|
2018-02-11 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 21:24:55 +00:00
|
|
|
export async function addTimelineItemSummaries (instanceName, timelineName, newSummaries, newStale) {
|
|
|
|
let oldSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries') || []
|
|
|
|
let oldStale = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesAreStale')
|
2018-03-11 19:11:06 +00:00
|
|
|
|
2019-03-03 21:24:55 +00:00
|
|
|
let mergedSummaries = mergeArrays(oldSummaries, newSummaries, compareTimelineItemSummaries)
|
2018-03-11 19:11:06 +00:00
|
|
|
|
2019-03-03 21:24:55 +00:00
|
|
|
if (!isEqual(oldSummaries, mergedSummaries)) {
|
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummaries: mergedSummaries })
|
2018-03-11 19:11:06 +00:00
|
|
|
}
|
|
|
|
if (oldStale !== newStale) {
|
2019-03-03 21:24:55 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummariesAreStale: newStale })
|
2018-03-11 19:11:06 +00:00
|
|
|
}
|
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-04-19 16:37:05 +00:00
|
|
|
let {
|
|
|
|
currentTimeline,
|
|
|
|
currentInstance,
|
|
|
|
accessToken,
|
|
|
|
lastTimelineItemId,
|
|
|
|
online
|
|
|
|
} = store.get()
|
2018-01-28 01:34:08 +00:00
|
|
|
|
2018-04-19 16:37:05 +00:00
|
|
|
let { items, stale } = await fetchTimelineItems(currentInstance, accessToken, currentTimeline, lastTimelineItemId, online)
|
|
|
|
addTimelineItems(currentInstance, currentTimeline, 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 async function setupTimeline () {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('setupTimeline')
|
2019-03-03 21:24:55 +00:00
|
|
|
// If we don't have any item summaries, or if the current item summaries are stale
|
2018-03-11 19:11:06 +00:00
|
|
|
// (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 16:37:05 +00:00
|
|
|
let {
|
2019-03-03 21:24:55 +00:00
|
|
|
timelineItemSummaries,
|
|
|
|
timelineItemSummariesAreStale,
|
2018-04-19 16:37:05 +00:00
|
|
|
currentTimeline
|
|
|
|
} = store.get()
|
2019-03-03 21:24:55 +00:00
|
|
|
if (!timelineItemSummaries ||
|
|
|
|
timelineItemSummariesAreStale ||
|
2018-03-11 19:11:06 +00:00
|
|
|
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-08-31 16:12:48 +00:00
|
|
|
console.log('setting runningUpdate: true')
|
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-09-01 21:11:39 +00:00
|
|
|
console.log('setting runningUpdate: false')
|
|
|
|
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')
|
2019-03-03 21:24:55 +00:00
|
|
|
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd')
|
|
|
|
itemSummariesToAdd = itemSummariesToAdd.sort(compareTimelineItemSummaries).reverse()
|
|
|
|
addTimelineItemSummaries(instanceName, timelineName, itemSummariesToAdd, false)
|
2018-02-12 03:15:21 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
2019-03-03 21:24:55 +00:00
|
|
|
timelineItemSummariesToAdd: [],
|
2018-02-12 03:15:21 +00:00
|
|
|
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 () {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentInstance, currentTimeline } = store.get()
|
2018-03-10 18:54:16 +00:00
|
|
|
return showMoreItemsForTimeline(
|
2018-04-19 16:37:05 +00:00
|
|
|
currentInstance,
|
|
|
|
currentTimeline
|
2018-03-10 18:54:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showMoreItemsForThread (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForThread')
|
2019-03-03 21:24:55 +00:00
|
|
|
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd')
|
|
|
|
let timelineItemSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries')
|
|
|
|
let timelineItemIds = new Set(timelineItemSummaries.map(_ => _.id))
|
2018-03-10 18:54:16 +00:00
|
|
|
// TODO: update database and do the thread merge correctly
|
2019-03-03 21:24:55 +00:00
|
|
|
for (let itemSummaryToAdd of itemSummariesToAdd) {
|
|
|
|
if (!timelineItemIds.has(itemSummaryToAdd.id)) {
|
|
|
|
timelineItemSummaries.push(itemSummaryToAdd)
|
2019-02-06 19:36:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
2019-03-03 21:24:55 +00:00
|
|
|
timelineItemSummariesToAdd: [],
|
|
|
|
timelineItemSummaries: timelineItemSummaries
|
2018-03-10 06:31:26 +00:00
|
|
|
})
|
2018-03-10 18:54:16 +00:00
|
|
|
stop('showMoreItemsForThread')
|
2018-03-10 06:31:26 +00:00
|
|
|
}
|