2018-02-17 04:38:21 +01:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
|
|
|
import { store } from '../_store/store'
|
2018-04-06 02:57:36 +02:00
|
|
|
import uniqBy from 'lodash-es/uniqBy'
|
2018-04-14 20:52:47 +02:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-08-30 04:03:12 +02:00
|
|
|
import { database } from '../_database/database'
|
2019-03-31 18:21:57 +02:00
|
|
|
import { concat } from '../_utils/arrays'
|
2018-12-13 08:45:52 +01:00
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2019-03-03 22:24:55 +01:00
|
|
|
import { timelineItemToSummary } from '../_utils/timelineItemToSummary'
|
2018-04-03 03:02:09 +02:00
|
|
|
|
2018-03-18 17:31:36 +01:00
|
|
|
function getExistingItemIdsSet (instanceName, timelineName) {
|
2019-03-03 22:24:55 +01:00
|
|
|
let timelineItemSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries') || []
|
|
|
|
return new Set(timelineItemSummaries.map(_ => _.id))
|
2018-02-17 04:38:21 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 17:31:36 +01:00
|
|
|
function removeDuplicates (instanceName, timelineName, updates) {
|
2018-02-17 04:38:21 +01:00
|
|
|
// remove duplicates, including duplicates due to reblogs
|
2018-03-18 17:31:36 +01:00
|
|
|
let existingItemIds = getExistingItemIdsSet(instanceName, timelineName)
|
2018-02-17 04:38:21 +01:00
|
|
|
return updates.filter(update => !existingItemIds.has(update.id))
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:31:26 +01:00
|
|
|
async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) {
|
2018-03-18 17:31:36 +01:00
|
|
|
updates = removeDuplicates(instanceName, timelineName, updates)
|
|
|
|
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-10 07:31:26 +01:00
|
|
|
|
2018-08-30 04:03:12 +02:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, updates)
|
2018-03-10 07:31:26 +01:00
|
|
|
|
2019-03-03 22:24:55 +01:00
|
|
|
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
|
|
|
|
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))',
|
|
|
|
JSON.parse(JSON.stringify(concat(itemSummariesToAdd, updates.map(timelineItemToSummary)))))
|
|
|
|
let newItemSummariesToAdd = uniqBy(
|
|
|
|
concat(itemSummariesToAdd, updates.map(timelineItemToSummary)),
|
|
|
|
_ => _.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 20:52:47 +02:00
|
|
|
}
|
2018-03-10 07:31:26 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:24:55 +01:00
|
|
|
function isValidStatusForThread (thread, timelineName, itemSummariesToAdd) {
|
|
|
|
let itemSummariesToAddIdSet = new Set(itemSummariesToAdd.map(_ => _.id))
|
|
|
|
let threadIdSet = new Set(thread.map(_ => _.id))
|
2018-12-12 07:06:50 +01:00
|
|
|
let focusedStatusId = timelineName.split('/')[1] // e.g. "status/123456"
|
2019-03-31 18:21:57 +02:00
|
|
|
let focusedStatusIdx = thread.findIndex(_ => _.id === focusedStatusId)
|
2018-12-12 07:06:50 +01:00
|
|
|
return status => {
|
2019-03-31 18:21:57 +02:00
|
|
|
let repliedToStatusIdx = thread.findIndex(_ => _.id === status.in_reply_to_id)
|
2018-12-12 07:06:50 +01: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 22:24:55 +01:00
|
|
|
!threadIdSet.has(status.id) &&
|
2018-12-12 07:06:50 +01:00
|
|
|
// Not already about to be added
|
2019-03-03 22:24:55 +01:00
|
|
|
!itemSummariesToAddIdSet.has(status.id)
|
2018-12-12 07:06:50 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:31:26 +01:00
|
|
|
async function insertUpdatesIntoThreads (instanceName, updates) {
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-11 01:21:10 +01:00
|
|
|
let threads = store.getThreads(instanceName)
|
2018-12-12 07:06:50 +01:00
|
|
|
let timelineNames = Object.keys(threads)
|
|
|
|
for (let timelineName of timelineNames) {
|
2018-03-10 07:31:26 +01:00
|
|
|
let thread = threads[timelineName]
|
2018-12-12 07:06:50 +01:00
|
|
|
|
2019-03-03 22:24:55 +01:00
|
|
|
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
|
|
|
|
let validUpdates = updates.filter(isValidStatusForThread(thread, timelineName, itemSummariesToAdd))
|
2018-12-12 07:06:50 +01:00
|
|
|
if (!validUpdates.length) {
|
2018-04-14 20:52:47 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-03-03 22:24:55 +01:00
|
|
|
let newItemSummariesToAdd = uniqBy(
|
|
|
|
concat(itemSummariesToAdd, validUpdates.map(timelineItemToSummary)),
|
|
|
|
_ => _.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-10 07:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 04:38:21 +01:00
|
|
|
async function processFreshUpdates (instanceName, timelineName) {
|
|
|
|
mark('processFreshUpdates')
|
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates')
|
|
|
|
if (freshUpdates && freshUpdates.length) {
|
|
|
|
let updates = freshUpdates.slice()
|
2018-08-30 06:42:57 +02:00
|
|
|
store.setForTimeline(instanceName, timelineName, { freshUpdates: [] })
|
2018-02-17 04:38:21 +01:00
|
|
|
|
2018-04-14 20:52:47 +02:00
|
|
|
await Promise.all([
|
|
|
|
insertUpdatesIntoTimeline(instanceName, timelineName, updates),
|
|
|
|
insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id))
|
|
|
|
])
|
2018-02-17 04:38:21 +01:00
|
|
|
}
|
2018-03-10 07:31:26 +01:00
|
|
|
stop('processFreshUpdates')
|
2018-02-17 04:38:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 08:45:52 +01:00
|
|
|
function lazilyProcessFreshUpdates (instanceName, timelineName) {
|
|
|
|
scheduleIdleTask(() => {
|
2018-02-17 04:38:21 +01:00
|
|
|
/* no await */ processFreshUpdates(instanceName, timelineName)
|
|
|
|
})
|
2018-12-13 08:45:52 +01:00
|
|
|
}
|
2018-02-17 04:38:21 +01:00
|
|
|
|
|
|
|
export function addStatusOrNotification (instanceName, timelineName, newStatusOrNotification) {
|
2018-03-19 18:09:05 +01:00
|
|
|
addStatusesOrNotifications(instanceName, timelineName, [newStatusOrNotification])
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) {
|
2018-06-23 19:11:14 +02:00
|
|
|
console.log('addStatusesOrNotifications', Date.now())
|
2018-02-17 04:38:21 +01:00
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
|
2018-08-31 18:12:39 +02:00
|
|
|
freshUpdates = concat(freshUpdates, newStatusesOrNotifications)
|
2018-03-15 07:13:27 +01:00
|
|
|
freshUpdates = uniqBy(freshUpdates, _ => _.id)
|
2018-08-30 06:42:57 +02:00
|
|
|
store.setForTimeline(instanceName, timelineName, { freshUpdates: freshUpdates })
|
2018-02-17 04:38:21 +01:00
|
|
|
lazilyProcessFreshUpdates(instanceName, timelineName)
|
|
|
|
}
|