2018-04-06 01:57:36 +01:00
|
|
|
import throttle from 'lodash-es/throttle'
|
2018-02-17 03:38:21 +00:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
|
|
|
import { store } from '../_store/store'
|
2018-04-06 01:57:36 +01:00
|
|
|
import uniqBy from 'lodash-es/uniqBy'
|
|
|
|
import uniq from 'lodash-es/uniq'
|
2018-04-14 19:52:47 +01:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-08-30 03:03:12 +01:00
|
|
|
import { database } from '../_database/database'
|
2018-06-23 18:11:14 +01:00
|
|
|
import { runMediumPriorityTask } from '../_utils/runMediumPriorityTask'
|
2018-02-17 03:38:21 +00:00
|
|
|
|
2018-04-03 02:02:09 +01:00
|
|
|
const STREAMING_THROTTLE_DELAY = 3000
|
|
|
|
|
2018-03-18 16:31:36 +00:00
|
|
|
function getExistingItemIdsSet (instanceName, timelineName) {
|
2018-02-17 03:38:21 +00:00
|
|
|
let timelineItemIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds') || []
|
2018-03-18 16:31:36 +00:00
|
|
|
return new Set(timelineItemIds)
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:31:36 +00:00
|
|
|
function removeDuplicates (instanceName, timelineName, updates) {
|
2018-02-17 03:38:21 +00:00
|
|
|
// remove duplicates, including duplicates due to reblogs
|
2018-03-18 16:31:36 +00:00
|
|
|
let existingItemIds = getExistingItemIdsSet(instanceName, timelineName)
|
2018-02-17 03:38:21 +00:00
|
|
|
return updates.filter(update => !existingItemIds.has(update.id))
|
|
|
|
}
|
|
|
|
|
2018-03-10 06:31:26 +00:00
|
|
|
async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) {
|
2018-03-18 16:31:36 +00:00
|
|
|
updates = removeDuplicates(instanceName, timelineName, updates)
|
|
|
|
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
|
2018-08-30 03:03:12 +01:00
|
|
|
await database.insertTimelineItems(instanceName, timelineName, updates)
|
2018-03-10 06:31:26 +00:00
|
|
|
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
2018-04-14 19:52:47 +01:00
|
|
|
let newItemIdsToAdd = uniq([].concat(itemIdsToAdd).concat(updates.map(_ => _.id)))
|
|
|
|
if (!isEqual(itemIdsToAdd, newItemIdsToAdd)) {
|
|
|
|
console.log('adding ', (newItemIdsToAdd.length - itemIdsToAdd.length),
|
|
|
|
'items to itemIdsToAdd for timeline', timelineName)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: newItemIdsToAdd})
|
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function insertUpdatesIntoThreads (instanceName, updates) {
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-11 00:21:10 +00:00
|
|
|
let threads = store.getThreads(instanceName)
|
2018-03-10 06:31:26 +00:00
|
|
|
|
|
|
|
for (let timelineName of Object.keys(threads)) {
|
|
|
|
let thread = threads[timelineName]
|
2018-04-14 19:52:47 +01:00
|
|
|
let updatesForThisThread = updates.filter(
|
|
|
|
status => thread.includes(status.in_reply_to_id) && !thread.includes(status.id)
|
|
|
|
)
|
|
|
|
if (!updatesForThisThread.length) {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
2018-04-14 19:52:47 +01:00
|
|
|
let newItemIdsToAdd = uniq([].concat(itemIdsToAdd).concat(updatesForThisThread.map(_ => _.id)))
|
|
|
|
if (!isEqual(itemIdsToAdd, newItemIdsToAdd)) {
|
|
|
|
console.log('adding ', (newItemIdsToAdd.length - itemIdsToAdd.length),
|
|
|
|
'items to itemIdsToAdd for thread', timelineName)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: newItemIdsToAdd})
|
2018-03-10 06:31:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 03:38:21 +00:00
|
|
|
async function processFreshUpdates (instanceName, timelineName) {
|
|
|
|
mark('processFreshUpdates')
|
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates')
|
|
|
|
if (freshUpdates && freshUpdates.length) {
|
|
|
|
let updates = freshUpdates.slice()
|
|
|
|
store.setForTimeline(instanceName, timelineName, {freshUpdates: []})
|
|
|
|
|
2018-04-14 19:52:47 +01:00
|
|
|
await Promise.all([
|
|
|
|
insertUpdatesIntoTimeline(instanceName, timelineName, updates),
|
|
|
|
insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id))
|
|
|
|
])
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
2018-03-10 06:31:26 +00:00
|
|
|
stop('processFreshUpdates')
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const lazilyProcessFreshUpdates = throttle((instanceName, timelineName) => {
|
2018-06-23 18:11:14 +01:00
|
|
|
runMediumPriorityTask(() => {
|
2018-02-17 03:38:21 +00:00
|
|
|
/* no await */ processFreshUpdates(instanceName, timelineName)
|
|
|
|
})
|
2018-04-03 02:02:09 +01:00
|
|
|
}, STREAMING_THROTTLE_DELAY)
|
2018-02-17 03:38:21 +00:00
|
|
|
|
|
|
|
export function addStatusOrNotification (instanceName, timelineName, newStatusOrNotification) {
|
2018-03-19 17:09:05 +00:00
|
|
|
addStatusesOrNotifications(instanceName, timelineName, [newStatusOrNotification])
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) {
|
2018-06-23 18:11:14 +01:00
|
|
|
console.log('addStatusesOrNotifications', Date.now())
|
2018-02-17 03:38:21 +00:00
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
|
2018-04-14 19:52:47 +01:00
|
|
|
freshUpdates = [].concat(freshUpdates).concat(newStatusesOrNotifications)
|
2018-03-15 06:13:27 +00:00
|
|
|
freshUpdates = uniqBy(freshUpdates, _ => _.id)
|
2018-02-17 03:38:21 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates})
|
|
|
|
lazilyProcessFreshUpdates(instanceName, timelineName)
|
|
|
|
}
|