2018-01-28 13:09:39 -08:00
|
|
|
import { store } from '../_store/store'
|
2018-02-08 08:22:14 -08:00
|
|
|
import { getTimeline } from '../_api/timelines'
|
2018-12-22 15:37:51 -08:00
|
|
|
import { toast } from '../_components/toast/toast'
|
2018-01-27 17:34:08 -08:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-08-24 11:50:40 -07:00
|
|
|
import { concat, mergeArrays } from '../_utils/arrays'
|
2019-03-03 17:21:22 -08:00
|
|
|
import { compareTimelineItemSummaries } from '../_utils/statusIdSorting'
|
2018-04-05 17:57:36 -07:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-08-29 19:03:12 -07:00
|
|
|
import { database } from '../_database/database'
|
2018-08-24 11:50:40 -07:00
|
|
|
import { getStatus, getStatusContext } from '../_api/statuses'
|
2018-08-28 06:45:15 -07:00
|
|
|
import { emit } from '../_utils/eventBus'
|
2018-08-29 22:49:14 -07:00
|
|
|
import { TIMELINE_BATCH_SIZE } from '../_static/timelines'
|
2019-03-03 13:24:55 -08:00
|
|
|
import { timelineItemToSummary } from '../_utils/timelineItemToSummary'
|
2018-01-27 17:34:08 -08:00
|
|
|
|
2018-08-28 06:45:15 -07:00
|
|
|
async function storeFreshTimelineItemsInDatabase (instanceName, timelineName, items) {
|
2018-08-29 19:03:12 -07:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, items)
|
2018-08-28 06:45:15 -07: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 11:50:40 -07:00
|
|
|
async function fetchTimelineItemsFromNetwork (instanceName, accessToken, timelineName, lastTimelineItemId) {
|
|
|
|
if (timelineName.startsWith('status/')) { // special case - this is a list of descendents and ancestors
|
2019-08-03 13:49:37 -07:00
|
|
|
const statusId = timelineName.split('/').slice(-1)[0]
|
|
|
|
const statusRequest = getStatus(instanceName, accessToken, statusId)
|
|
|
|
const contextRequest = getStatusContext(instanceName, accessToken, statusId)
|
|
|
|
const [status, context] = await Promise.all([statusRequest, contextRequest])
|
2018-08-24 11:50:40 -07:00
|
|
|
return concat(context.ancestors, status, context.descendants)
|
|
|
|
} else { // normal timeline
|
2018-08-29 22:49:14 -07:00
|
|
|
return getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, null, TIMELINE_BATCH_SIZE)
|
2018-08-24 11:50:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
2018-02-03 18:06:02 -08:00
|
|
|
mark('fetchTimelineItems')
|
|
|
|
let items
|
2018-03-11 12:11:06 -07:00
|
|
|
let stale = false
|
2018-01-27 17:34:08 -08:00
|
|
|
if (!online) {
|
2018-08-29 22:49:14 -07:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-03-11 12:11:06 -07:00
|
|
|
stale = true
|
2018-01-27 17:34:08 -08:00
|
|
|
} else {
|
|
|
|
try {
|
2018-08-31 09:12:48 -07:00
|
|
|
console.log('fetchTimelineItemsFromNetwork')
|
2018-08-24 11:50:40 -07:00
|
|
|
items = await fetchTimelineItemsFromNetwork(instanceName, accessToken, timelineName, lastTimelineItemId)
|
2018-08-28 06:45:15 -07:00
|
|
|
/* no await */ storeFreshTimelineItemsInDatabase(instanceName, timelineName, items)
|
2018-01-27 17:34:08 -08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Internet request failed. Showing offline content.')
|
2018-08-29 22:49:14 -07:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, TIMELINE_BATCH_SIZE)
|
2018-03-11 12:11:06 -07:00
|
|
|
stale = true
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-03 18:06:02 -08:00
|
|
|
stop('fetchTimelineItems')
|
2018-03-11 12:11:06 -07:00
|
|
|
return { items, stale }
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
|
|
|
|
2018-03-11 12:11:06 -07:00
|
|
|
async function addTimelineItems (instanceName, timelineName, items, stale) {
|
|
|
|
console.log('addTimelineItems, length:', items.length)
|
2019-03-03 13:24:55 -08:00
|
|
|
mark('addTimelineItemSummaries')
|
2019-08-03 13:49:37 -07:00
|
|
|
const newSummaries = items.map(timelineItemToSummary)
|
2019-03-03 13:24:55 -08:00
|
|
|
addTimelineItemSummaries(instanceName, timelineName, newSummaries, stale)
|
|
|
|
stop('addTimelineItemSummaries')
|
2018-02-11 13:46:57 -08:00
|
|
|
}
|
|
|
|
|
2019-03-03 13:24:55 -08:00
|
|
|
export async function addTimelineItemSummaries (instanceName, timelineName, newSummaries, newStale) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const oldSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries') || []
|
|
|
|
const oldStale = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesAreStale')
|
2018-03-11 12:11:06 -07:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const mergedSummaries = mergeArrays(oldSummaries, newSummaries, compareTimelineItemSummaries)
|
2018-03-11 12:11:06 -07:00
|
|
|
|
2019-03-03 13:24:55 -08:00
|
|
|
if (!isEqual(oldSummaries, mergedSummaries)) {
|
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummaries: mergedSummaries })
|
2018-03-11 12:11:06 -07:00
|
|
|
}
|
|
|
|
if (oldStale !== newStale) {
|
2019-03-03 13:24:55 -08:00
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummariesAreStale: newStale })
|
2018-03-11 12:11:06 -07:00
|
|
|
}
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
async function fetchTimelineItemsAndPossiblyFallBack () {
|
2018-02-03 18:06:02 -08:00
|
|
|
mark('fetchTimelineItemsAndPossiblyFallBack')
|
2019-08-03 13:49:37 -07:00
|
|
|
const {
|
2018-04-19 09:37:05 -07:00
|
|
|
currentTimeline,
|
|
|
|
currentInstance,
|
|
|
|
accessToken,
|
|
|
|
lastTimelineItemId,
|
|
|
|
online
|
|
|
|
} = store.get()
|
2018-01-27 17:34:08 -08:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const { items, stale } = await fetchTimelineItems(currentInstance, accessToken, currentTimeline, lastTimelineItemId, online)
|
2018-04-19 09:37:05 -07:00
|
|
|
addTimelineItems(currentInstance, currentTimeline, items, stale)
|
2018-02-03 18:06:02 -08:00
|
|
|
stop('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
export async function setupTimeline () {
|
2018-02-03 18:06:02 -08:00
|
|
|
mark('setupTimeline')
|
2019-03-03 13:24:55 -08:00
|
|
|
// If we don't have any item summaries, or if the current item summaries are stale
|
2018-03-11 12:11:06 -07: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.
|
2019-08-03 13:49:37 -07:00
|
|
|
const {
|
2019-03-03 13:24:55 -08:00
|
|
|
timelineItemSummaries,
|
|
|
|
timelineItemSummariesAreStale,
|
2018-04-19 09:37:05 -07:00
|
|
|
currentTimeline
|
|
|
|
} = store.get()
|
2019-03-03 13:24:55 -08:00
|
|
|
if (!timelineItemSummaries ||
|
|
|
|
timelineItemSummariesAreStale ||
|
2018-03-11 12:11:06 -07:00
|
|
|
currentTimeline.startsWith('status/')) {
|
2018-02-03 18:06:02 -08:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
2018-02-03 18:06:02 -08:00
|
|
|
stop('setupTimeline')
|
2018-01-27 17:34:08 -08:00
|
|
|
}
|
|
|
|
|
2019-05-28 22:46:01 -07:00
|
|
|
export async function fetchMoreItemsAtBottomOfTimeline (instanceName, timelineName) {
|
2018-08-31 09:12:48 -07:00
|
|
|
console.log('setting runningUpdate: true')
|
2018-01-27 17:34:08 -08:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: true })
|
2018-02-03 18:06:02 -08:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-09-01 14:11:39 -07:00
|
|
|
console.log('setting runningUpdate: false')
|
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: false })
|
2018-02-08 22:29:29 -08:00
|
|
|
}
|
2018-02-11 19:15:21 -08:00
|
|
|
|
2018-03-10 10:54:16 -08:00
|
|
|
export async function showMoreItemsForTimeline (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForTimeline')
|
2019-03-18 09:09:24 -07:00
|
|
|
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
|
2019-03-03 13:24:55 -08:00
|
|
|
itemSummariesToAdd = itemSummariesToAdd.sort(compareTimelineItemSummaries).reverse()
|
|
|
|
addTimelineItemSummaries(instanceName, timelineName, itemSummariesToAdd, false)
|
2018-02-11 19:15:21 -08:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
2019-03-03 13:24:55 -08:00
|
|
|
timelineItemSummariesToAdd: [],
|
2018-02-11 19:15:21 -08:00
|
|
|
shouldShowHeader: false,
|
|
|
|
showHeader: false
|
|
|
|
})
|
2018-03-10 10:54:16 -08:00
|
|
|
stop('showMoreItemsForTimeline')
|
2018-02-13 19:35:46 -08:00
|
|
|
}
|
2018-03-09 22:31:26 -08:00
|
|
|
|
2019-03-18 09:09:24 -07:00
|
|
|
export function showMoreItemsForCurrentTimeline () {
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentInstance, currentTimeline } = store.get()
|
2018-03-10 10:54:16 -08:00
|
|
|
return showMoreItemsForTimeline(
|
2018-04-19 09:37:05 -07:00
|
|
|
currentInstance,
|
|
|
|
currentTimeline
|
2018-03-10 10:54:16 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function showMoreItemsForThread (instanceName, timelineName) {
|
|
|
|
mark('showMoreItemsForThread')
|
2019-08-03 13:49:37 -07:00
|
|
|
const itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd')
|
|
|
|
const timelineItemSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries')
|
|
|
|
const timelineItemIds = new Set(timelineItemSummaries.map(_ => _.id))
|
2018-03-10 10:54:16 -08:00
|
|
|
// TODO: update database and do the thread merge correctly
|
2019-08-03 13:49:37 -07:00
|
|
|
for (const itemSummaryToAdd of itemSummariesToAdd) {
|
2019-03-03 13:24:55 -08:00
|
|
|
if (!timelineItemIds.has(itemSummaryToAdd.id)) {
|
|
|
|
timelineItemSummaries.push(itemSummaryToAdd)
|
2019-02-06 11:36:46 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 22:31:26 -08:00
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
2019-03-03 13:24:55 -08:00
|
|
|
timelineItemSummariesToAdd: [],
|
|
|
|
timelineItemSummaries: timelineItemSummaries
|
2018-03-09 22:31:26 -08:00
|
|
|
})
|
2018-03-10 10:54:16 -08:00
|
|
|
stop('showMoreItemsForThread')
|
2018-03-09 22:31:26 -08:00
|
|
|
}
|