2018-02-17 04:38:21 +01:00
|
|
|
import throttle from 'lodash/throttle'
|
|
|
|
import { getIdsThatTheseStatusesReblogged } from './statuses'
|
|
|
|
import { database } from '../_database/database'
|
|
|
|
import { mark, stop } from '../_utils/marks'
|
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2018-03-14 17:46:08 +01:00
|
|
|
import uniqBy from 'lodash/uniqBy'
|
|
|
|
import uniq from 'lodash/uniq'
|
2018-02-17 04:38:21 +01:00
|
|
|
|
|
|
|
async function getExistingItemIdsSet (instanceName, timelineName) {
|
|
|
|
let timelineItemIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds') || []
|
|
|
|
if (timelineName === 'notifications') {
|
|
|
|
return new Set(timelineItemIds)
|
|
|
|
}
|
|
|
|
let reblogIds = await getIdsThatTheseStatusesReblogged(instanceName, timelineItemIds)
|
|
|
|
return new Set([].concat(timelineItemIds).concat(reblogIds))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeDuplicates (instanceName, timelineName, updates) {
|
|
|
|
// remove duplicates, including duplicates due to reblogs
|
|
|
|
let existingItemIds = await getExistingItemIdsSet(instanceName, timelineName)
|
|
|
|
return updates.filter(update => !existingItemIds.has(update.id))
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:31:26 +01:00
|
|
|
async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) {
|
|
|
|
updates = await removeDuplicates(instanceName, timelineName, updates)
|
|
|
|
|
|
|
|
await database.insertTimelineItems(instanceName, timelineName, updates)
|
|
|
|
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
|
|
|
if (updates && updates.length) {
|
|
|
|
itemIdsToAdd = itemIdsToAdd.concat(updates.map(_ => _.id))
|
2018-03-14 17:46:08 +01:00
|
|
|
itemIdsToAdd = uniq(itemIdsToAdd)
|
2018-03-10 07:31:26 +01:00
|
|
|
console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd')
|
|
|
|
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function insertUpdatesIntoThreads (instanceName, updates) {
|
|
|
|
if (!updates.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-11 01:21:10 +01:00
|
|
|
let threads = store.getThreads(instanceName)
|
2018-03-10 07:31:26 +01:00
|
|
|
|
|
|
|
for (let timelineName of Object.keys(threads)) {
|
|
|
|
let thread = threads[timelineName]
|
|
|
|
let updatesForThisThread = updates.filter(status => {
|
|
|
|
return thread.includes(status.in_reply_to_id) && !thread.includes(status.id)
|
|
|
|
})
|
|
|
|
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
|
|
|
|
for (let update of updatesForThisThread) {
|
|
|
|
if (!itemIdsToAdd.includes(update.id)) {
|
|
|
|
itemIdsToAdd.push(update.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd for thread', timelineName)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
store.setForTimeline(instanceName, timelineName, {freshUpdates: []})
|
|
|
|
|
2018-03-10 07:31:26 +01:00
|
|
|
await insertUpdatesIntoTimeline(instanceName, timelineName, updates)
|
|
|
|
await 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
|
|
|
}
|
|
|
|
|
|
|
|
const lazilyProcessFreshUpdates = throttle((instanceName, timelineName) => {
|
|
|
|
scheduleIdleTask(() => {
|
|
|
|
/* no await */ processFreshUpdates(instanceName, timelineName)
|
|
|
|
})
|
|
|
|
}, 5000)
|
|
|
|
|
|
|
|
export function addStatusOrNotification (instanceName, timelineName, newStatusOrNotification) {
|
|
|
|
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
|
|
|
|
freshUpdates.push(newStatusOrNotification)
|
2018-03-14 17:46:08 +01:00
|
|
|
freshUpdates = uniqBy(freshUpdates, 'id')
|
2018-02-17 04:38:21 +01:00
|
|
|
store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates})
|
|
|
|
lazilyProcessFreshUpdates(instanceName, timelineName)
|
|
|
|
}
|