Pinafore-Web-Client-Frontend/src/routes/_actions/addStatusOrNotification.js

125 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-02-17 04:38:21 +01:00
import { mark, stop } from '../_utils/marks'
import { store } from '../_store/store'
import uniqBy from 'lodash-es/uniqBy'
import isEqual from 'lodash-es/isEqual'
import { database } from '../_database/database'
import { concat } from '../_utils/arrays'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import { timelineItemToSummary } from '../_utils/timelineItemToSummary'
2018-04-03 03:02:09 +02:00
function getExistingItemIdsSet (instanceName, timelineName) {
let timelineItemSummaries = store.getForTimeline(instanceName, timelineName, 'timelineItemSummaries') || []
return new Set(timelineItemSummaries.map(_ => _.id))
2018-02-17 04:38:21 +01:00
}
function removeDuplicates (instanceName, timelineName, updates) {
2018-02-17 04:38:21 +01:00
// remove duplicates, including duplicates due to reblogs
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) {
updates = removeDuplicates(instanceName, timelineName, updates)
if (!updates.length) {
return
}
2018-03-10 07:31:26 +01:00
await database.insertTimelineItems(instanceName, timelineName, updates)
2018-03-10 07:31:26 +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-03-10 07:31:26 +01:00
}
function isValidStatusForThread (thread, timelineName, itemSummariesToAdd) {
let itemSummariesToAddIdSet = new Set(itemSummariesToAdd.map(_ => _.id))
let threadIdSet = new Set(thread.map(_ => _.id))
let focusedStatusId = timelineName.split('/')[1] // e.g. "status/123456"
let focusedStatusIdx = thread.findIndex(_ => _.id === focusedStatusId)
return status => {
let repliedToStatusIdx = thread.findIndex(_ => _.id === status.in_reply_to_id)
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
!threadIdSet.has(status.id) &&
// Not already about to be added
!itemSummariesToAddIdSet.has(status.id)
)
}
}
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)
let timelineNames = Object.keys(threads)
for (let timelineName of timelineNames) {
2018-03-10 07:31:26 +01:00
let thread = threads[timelineName]
let itemSummariesToAdd = store.getForTimeline(instanceName, timelineName, 'timelineItemSummariesToAdd') || []
let validUpdates = updates.filter(isValidStatusForThread(thread, timelineName, itemSummariesToAdd))
if (!validUpdates.length) {
continue
}
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()
store.setForTimeline(instanceName, timelineName, { freshUpdates: [] })
2018-02-17 04:38:21 +01: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
}
function lazilyProcessFreshUpdates (instanceName, timelineName) {
scheduleIdleTask(() => {
2018-02-17 04:38:21 +01:00
/* no await */ processFreshUpdates(instanceName, timelineName)
})
}
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) {
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)
freshUpdates = uniqBy(freshUpdates, _ => _.id)
store.setForTimeline(instanceName, timelineName, { freshUpdates: freshUpdates })
2018-02-17 04:38:21 +01:00
lazilyProcessFreshUpdates(instanceName, timelineName)
}