2021-07-04 20:19:04 -07:00
|
|
|
import { mark, stop } from '../_utils/marks.js'
|
|
|
|
import { store } from '../_store/store.js'
|
|
|
|
import { uniqBy, isEqual } from '../_thirdparty/lodash/objects.js'
|
|
|
|
import { database } from '../_database/database.js'
|
|
|
|
import { concat } from '../_utils/arrays.js'
|
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask.js'
|
|
|
|
import { timelineItemToSummary } from '../_utils/timelineItemToSummary.js'
|
2018-04-02 18:02:09 -07:00
|
|
|
|
2018-03-18 09:31:36 -07:00
|
|
|
function getExistingItemIdsSet (instanceName, timelineName) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const timelineItemSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries') || []
|
2019-03-03 13:24:55 -08:00
|
|
|
return new Set(timelineItemSummaries.map(_ => _.id))
|
2018-02-16 19:38:21 -08:00
|
|
|
}
|
|
|
|
|
2018-03-18 09:31:36 -07:00
|
|
|
function removeDuplicates (instanceName, timelineName, updates) {
|
2018-02-16 19:38:21 -08:00
|
|
|
// remove duplicates, including duplicates due to reblogs
|
2019-08-03 13:49:37 -07:00
|
|
|
const existingItemIds = getExistingItemIdsSet(instanceName, timelineName)
|
2018-02-16 19:38:21 -08:00
|
|
|
return updates.filter(update => !existingItemIds.has(update.id))
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:31:26 -08:00
|
|
|
async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) {
|
2018-03-18 09:31:36 -07:00
|
|
|
updates = removeDuplicates(instanceName, timelineName, updates)
|
|
|
|
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-09 22:31:26 -08:00
|
|
|
|
2018-08-29 19:03:12 -07:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, updates)
|
2018-03-09 22:31:26 -08:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
|
2019-03-03 13:24:55 -08:00
|
|
|
console.log('itemSummariesToAdd', JSON.parse(JSON.stringify(itemSummariesToAdd)))
|
|
|
|
console.log('updates.map(timelineItemToSummary)', JSON.parse(JSON.stringify(updates.map(timelineItemToSummary))))
|
|
|
|
console.log('concat(itemSummariesToAdd, updates.map(timelineItemToSummary))',
|
2021-03-13 17:31:17 -08:00
|
|
|
JSON.parse(JSON.stringify(concat(itemSummariesToAdd, updates.map(item => timelineItemToSummary(item, instanceName))))))
|
2019-08-03 13:49:37 -07:00
|
|
|
const newItemSummariesToAdd = uniqBy(
|
2021-03-13 17:31:17 -08:00
|
|
|
concat(itemSummariesToAdd, updates.map(item => timelineItemToSummary(item, instanceName))),
|
2019-03-03 13:24:55 -08:00
|
|
|
_ => _.id
|
|
|
|
)
|
|
|
|
if (!isEqual(itemSummariesToAdd, newItemSummariesToAdd)) {
|
|
|
|
console.log('adding ', (newItemSummariesToAdd.length - itemSummariesToAdd.length),
|
|
|
|
'items to timelineItemSummariesToAdd for timeline', timelineName)
|
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummariesToAdd: newItemSummariesToAdd })
|
2018-04-14 11:52:47 -07:00
|
|
|
}
|
2018-03-09 22:31:26 -08:00
|
|
|
}
|
|
|
|
|
2019-03-03 13:24:55 -08:00
|
|
|
function isValidStatusForThread (thread, timelineName, itemSummariesToAdd) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const itemSummariesToAddIdSet = new Set(itemSummariesToAdd.map(_ => _.id))
|
|
|
|
const threadIdSet = new Set(thread.map(_ => _.id))
|
|
|
|
const focusedStatusId = timelineName.split('/')[1] // e.g. "status/123456"
|
|
|
|
const focusedStatusIdx = thread.findIndex(_ => _.id === focusedStatusId)
|
2018-12-11 22:06:50 -08:00
|
|
|
return status => {
|
2019-08-03 13:49:37 -07:00
|
|
|
const repliedToStatusIdx = thread.findIndex(_ => _.id === status.in_reply_to_id)
|
2018-12-11 22:06:50 -08:00
|
|
|
return (
|
|
|
|
// A reply to an ancestor status is not valid for this thread, but for the focused status
|
|
|
|
// itself or any of its descendents, it is valid.
|
|
|
|
repliedToStatusIdx >= focusedStatusIdx &&
|
|
|
|
// Not a duplicate
|
2019-03-03 13:24:55 -08:00
|
|
|
!threadIdSet.has(status.id) &&
|
2018-12-11 22:06:50 -08:00
|
|
|
// Not already about to be added
|
2019-03-03 13:24:55 -08:00
|
|
|
!itemSummariesToAddIdSet.has(status.id)
|
2018-12-11 22:06:50 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:31:26 -08:00
|
|
|
async function insertUpdatesIntoThreads (instanceName, updates) {
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const threads = store.getThreads(instanceName)
|
|
|
|
const timelineNames = Object.keys(threads)
|
|
|
|
for (const timelineName of timelineNames) {
|
|
|
|
const thread = threads[timelineName]
|
2018-12-11 22:06:50 -08:00
|
|
|
|
2019-08-03 13:49:37 -07:00
|
|
|
const itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
|
|
|
|
const validUpdates = updates.filter(isValidStatusForThread(thread, timelineName, itemSummariesToAdd))
|
2018-12-11 22:06:50 -08:00
|
|
|
if (!validUpdates.length) {
|
2018-04-14 11:52:47 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-08-03 13:49:37 -07:00
|
|
|
const newItemSummariesToAdd = uniqBy(
|
2021-03-13 17:31:17 -08:00
|
|
|
concat(itemSummariesToAdd, validUpdates.map(item => timelineItemToSummary(item, instanceName))),
|
2019-03-03 13:24:55 -08:00
|
|
|
_ => _.id
|
|
|
|
)
|
|
|
|
if (!isEqual(itemSummariesToAdd, newItemSummariesToAdd)) {
|
|
|
|
console.log('adding ', (newItemSummariesToAdd.length - itemSummariesToAdd.length),
|
|
|
|
'items to timelineItemSummariesToAdd for thread', timelineName)
|
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemSummariesToAdd: newItemSummariesToAdd })
|
2018-03-09 22:31:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:38:21 -08:00
|
|
|
async function processFreshUpdates (instanceName, timelineName) {
|
|
|
|
mark('processFreshUpdates')
|
2019-08-03 13:49:37 -07:00
|
|
|
const freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates')
|
2018-02-16 19:38:21 -08:00
|
|
|
if (freshUpdates && freshUpdates.length) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const updates = freshUpdates.slice()
|
2018-08-29 21:42:57 -07:00
|
|
|
store.setForTimeline(instanceName, timelineName, { freshUpdates: [] })
|
2018-02-16 19:38:21 -08:00
|
|
|
|
2018-04-14 11:52:47 -07:00
|
|
|
await Promise.all([
|
|
|
|
insertUpdatesIntoTimeline(instanceName, timelineName, updates),
|
|
|
|
insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id))
|
|
|
|
])
|
2018-02-16 19:38:21 -08:00
|
|
|
}
|
2018-03-09 22:31:26 -08:00
|
|
|
stop('processFreshUpdates')
|
2018-02-16 19:38:21 -08:00
|
|
|
}
|
|
|
|
|
2018-12-12 23:45:52 -08:00
|
|
|
function lazilyProcessFreshUpdates (instanceName, timelineName) {
|
|
|
|
scheduleIdleTask(() => {
|
2018-02-16 19:38:21 -08:00
|
|
|
/* no await */ processFreshUpdates(instanceName, timelineName)
|
|
|
|
})
|
2018-12-12 23:45:52 -08:00
|
|
|
}
|
2018-02-16 19:38:21 -08:00
|
|
|
|
|
|
|
export function addStatusOrNotification (instanceName, timelineName, newStatusOrNotification) {
|
2018-03-19 10:09:05 -07:00
|
|
|
addStatusesOrNotifications(instanceName, timelineName, [newStatusOrNotification])
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) {
|
2018-06-23 10:11:14 -07:00
|
|
|
console.log('addStatusesOrNotifications', Date.now())
|
2018-02-16 19:38:21 -08:00
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
|
2018-08-31 09:12:39 -07:00
|
|
|
freshUpdates = concat(freshUpdates, newStatusesOrNotifications)
|
2018-03-14 23:13:27 -07:00
|
|
|
freshUpdates = uniqBy(freshUpdates, _ => _.id)
|
2018-08-29 21:42:57 -07:00
|
|
|
store.setForTimeline(instanceName, timelineName, { freshUpdates: freshUpdates })
|
2018-02-16 19:38:21 -08:00
|
|
|
lazilyProcessFreshUpdates(instanceName, timelineName)
|
|
|
|
}
|