2018-03-11 00:21:10 +00:00
|
|
|
import { getIdsThatRebloggedThisStatus, getNotificationIdsForStatuses } from './statuses'
|
2018-02-17 03:38:21 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2018-04-06 01:57:36 +01:00
|
|
|
import forEach from 'lodash-es/forEach'
|
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-04-17 04:56:21 +01:00
|
|
|
import {
|
|
|
|
deleteStatusesAndNotifications as deleteStatusesAndNotificationsFromDatabase
|
|
|
|
} from '../_database/timelines/deletion'
|
2018-03-11 05:05:00 +00:00
|
|
|
|
|
|
|
function filterItemIdsFromTimelines (instanceName, timelineFilter, idFilter) {
|
|
|
|
let keys = ['timelineItemIds', 'itemIdsToAdd']
|
|
|
|
|
|
|
|
keys.forEach(key => {
|
|
|
|
let timelineData = store.getAllTimelineData(instanceName, key)
|
|
|
|
forEach(timelineData, (ids, timelineName) => {
|
|
|
|
if (!timelineFilter(timelineName)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let filteredIds = ids.filter(idFilter)
|
|
|
|
if (!isEqual(ids, filteredIds)) {
|
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
[key]: filteredIds
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-02-17 03:38:21 +00:00
|
|
|
|
|
|
|
function deleteStatusIdsFromStore (instanceName, idsToDelete) {
|
|
|
|
let idsToDeleteSet = new Set(idsToDelete)
|
2018-03-11 00:21:10 +00:00
|
|
|
let idWasNotDeleted = id => !idsToDeleteSet.has(id)
|
2018-03-11 05:05:00 +00:00
|
|
|
let notNotificationTimeline = timelineName => timelineName !== 'notifications'
|
2018-03-11 00:21:10 +00:00
|
|
|
|
2018-03-11 05:05:00 +00:00
|
|
|
filterItemIdsFromTimelines(instanceName, notNotificationTimeline, idWasNotDeleted)
|
|
|
|
}
|
2018-03-11 00:21:10 +00:00
|
|
|
|
2018-03-11 05:05:00 +00:00
|
|
|
function deleteNotificationIdsFromStore (instanceName, idsToDelete) {
|
|
|
|
let idsToDeleteSet = new Set(idsToDelete)
|
|
|
|
let idWasNotDeleted = id => !idsToDeleteSet.has(id)
|
|
|
|
let isNotificationTimeline = timelineName => timelineName === 'notifications'
|
2018-03-11 00:21:10 +00:00
|
|
|
|
2018-03-11 05:05:00 +00:00
|
|
|
filterItemIdsFromTimelines(instanceName, isNotificationTimeline, idWasNotDeleted)
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteStatusesAndNotifications (instanceName, statusIdsToDelete, notificationIdsToDelete) {
|
|
|
|
deleteStatusIdsFromStore(instanceName, statusIdsToDelete)
|
2018-03-11 05:05:00 +00:00
|
|
|
deleteNotificationIdsFromStore(instanceName, notificationIdsToDelete)
|
2018-04-17 04:56:21 +01:00
|
|
|
await deleteStatusesAndNotificationsFromDatabase(instanceName, statusIdsToDelete, notificationIdsToDelete)
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function doDeleteStatus (instanceName, statusId) {
|
2018-03-11 00:21:10 +00:00
|
|
|
console.log('deleting statusId', statusId)
|
|
|
|
let rebloggedIds = await getIdsThatRebloggedThisStatus(instanceName, statusId)
|
|
|
|
let statusIdsToDelete = Array.from(new Set([statusId].concat(rebloggedIds).filter(Boolean)))
|
2018-03-11 05:05:00 +00:00
|
|
|
let notificationIdsToDelete = Array.from(new Set(await getNotificationIdsForStatuses(instanceName, statusIdsToDelete)))
|
2018-03-11 04:24:07 +00:00
|
|
|
await deleteStatusesAndNotifications(instanceName, statusIdsToDelete, notificationIdsToDelete)
|
2018-02-17 03:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteStatus (instanceName, statusId) {
|
|
|
|
scheduleIdleTask(() => {
|
|
|
|
/* no await */ doDeleteStatus(instanceName, statusId)
|
|
|
|
})
|
|
|
|
}
|