2018-03-11 05:24:07 +01:00
|
|
|
import difference from 'lodash/difference'
|
|
|
|
import times from 'lodash/times'
|
2018-02-14 04:34:37 +01:00
|
|
|
import { cloneForStorage } from './helpers'
|
2018-02-09 07:04:10 +01:00
|
|
|
import { dbPromise, getDatabase } from './databaseLifecycle'
|
2018-02-17 04:38:21 +01:00
|
|
|
import {
|
|
|
|
accountsCache, deleteFromCache, getInCache, hasInCache, notificationsCache, setInCache,
|
|
|
|
statusesCache
|
|
|
|
} from './cache'
|
2018-02-14 04:34:37 +01:00
|
|
|
import { scheduleCleanup } from './cleanup'
|
2018-02-09 07:04:10 +01:00
|
|
|
import {
|
2018-02-10 04:48:52 +01:00
|
|
|
ACCOUNTS_STORE,
|
|
|
|
NOTIFICATION_TIMELINES_STORE,
|
2018-02-11 19:35:25 +01:00
|
|
|
NOTIFICATIONS_STORE, PINNED_STATUSES_STORE,
|
2018-02-10 04:48:52 +01:00
|
|
|
STATUS_TIMELINES_STORE,
|
2018-02-14 04:34:37 +01:00
|
|
|
STATUSES_STORE,
|
|
|
|
ACCOUNT_ID,
|
|
|
|
REBLOG_ID,
|
2018-03-09 03:31:59 +01:00
|
|
|
STATUS_ID, THREADS_STORE
|
2018-02-09 07:04:10 +01:00
|
|
|
} from './constants'
|
2018-03-11 05:24:07 +01:00
|
|
|
import {
|
|
|
|
createThreadKeyRange,
|
|
|
|
createTimelineKeyRange,
|
|
|
|
createTimelineId,
|
|
|
|
createThreadId,
|
|
|
|
createPinnedStatusKeyRange,
|
|
|
|
createPinnedStatusId
|
|
|
|
} from './keys'
|
|
|
|
import { deleteAll } from './utils'
|
2018-02-10 04:48:52 +01:00
|
|
|
|
2018-02-11 23:11:03 +01:00
|
|
|
function cacheStatus (status, instanceName) {
|
2018-02-11 19:35:25 +01:00
|
|
|
setInCache(statusesCache, instanceName, status.id, status)
|
|
|
|
setInCache(accountsCache, instanceName, status.account.id, status.account)
|
|
|
|
if (status.reblog) {
|
|
|
|
setInCache(accountsCache, instanceName, status.reblog.account.id, status.reblog.account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 04:48:52 +01:00
|
|
|
//
|
|
|
|
// pagination
|
|
|
|
//
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function getNotificationTimeline (instanceName, timeline, maxId, limit) {
|
2018-02-10 04:48:52 +01:00
|
|
|
let storeNames = [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
2018-02-09 07:04:10 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
2018-02-09 07:29:29 +01:00
|
|
|
return dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
2018-02-10 04:48:52 +01:00
|
|
|
let [ timelineStore, notificationsStore, statusesStore, accountsStore ] = stores
|
2018-02-11 18:36:01 +01:00
|
|
|
let keyRange = createTimelineKeyRange(timeline, maxId)
|
2018-02-09 07:04:10 +01:00
|
|
|
|
|
|
|
timelineStore.getAll(keyRange, limit).onsuccess = e => {
|
|
|
|
let timelineResults = e.target.result
|
|
|
|
let res = new Array(timelineResults.length)
|
2018-03-09 08:18:18 +01:00
|
|
|
timelineResults.forEach((notificationId, i) => {
|
|
|
|
fetchNotification(notificationsStore, statusesStore, accountsStore, notificationId, notification => {
|
2018-02-10 04:48:52 +01:00
|
|
|
res[i] = notification
|
|
|
|
})
|
2018-02-09 07:04:10 +01:00
|
|
|
})
|
|
|
|
callback(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function getStatusTimeline (instanceName, timeline, maxId, limit) {
|
2018-02-10 04:48:52 +01:00
|
|
|
let storeNames = [STATUS_TIMELINES_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
2018-02-09 07:04:10 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
2018-02-09 07:29:29 +01:00
|
|
|
return dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
2018-02-10 04:48:52 +01:00
|
|
|
let [ timelineStore, statusesStore, accountsStore ] = stores
|
2018-03-09 03:31:59 +01:00
|
|
|
let getReq = timelineStore.getAll(createTimelineKeyRange(timeline, maxId), limit)
|
2018-02-11 18:36:01 +01:00
|
|
|
getReq.onsuccess = e => {
|
2018-02-09 07:04:10 +01:00
|
|
|
let timelineResults = e.target.result
|
|
|
|
let res = new Array(timelineResults.length)
|
2018-03-09 08:18:18 +01:00
|
|
|
timelineResults.forEach((statusId, i) => {
|
|
|
|
fetchStatus(statusesStore, accountsStore, statusId, status => {
|
2018-02-10 04:48:52 +01:00
|
|
|
res[i] = status
|
|
|
|
})
|
2018-02-09 07:04:10 +01:00
|
|
|
})
|
|
|
|
callback(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-09 03:31:59 +01:00
|
|
|
async function getStatusThread (instanceName, statusId) {
|
|
|
|
let storeNames = [THREADS_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
|
|
|
let [ threadsStore, statusesStore, accountsStore ] = stores
|
2018-03-11 05:24:07 +01:00
|
|
|
let keyRange = createThreadKeyRange(statusId)
|
|
|
|
threadsStore.getAll(keyRange).onsuccess = e => {
|
2018-03-09 08:18:18 +01:00
|
|
|
let thread = e.target.result
|
2018-03-09 03:31:59 +01:00
|
|
|
let res = new Array(thread.length)
|
|
|
|
thread.forEach((otherStatusId, i) => {
|
|
|
|
fetchStatus(statusesStore, accountsStore, otherStatusId, status => {
|
|
|
|
res[i] = status
|
|
|
|
})
|
|
|
|
})
|
|
|
|
callback(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function getTimeline (instanceName, timeline, maxId = null, limit = 20) {
|
2018-03-09 03:31:59 +01:00
|
|
|
if (timeline === 'notifications') {
|
|
|
|
return getNotificationTimeline(instanceName, timeline, maxId, limit)
|
|
|
|
} else if (timeline.startsWith('status/')) {
|
|
|
|
let statusId = timeline.split('/').slice(-1)[0]
|
|
|
|
return getStatusThread(instanceName, statusId)
|
|
|
|
} else {
|
|
|
|
return getStatusTimeline(instanceName, timeline, maxId, limit)
|
|
|
|
}
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:48:52 +01:00
|
|
|
//
|
|
|
|
// insertion
|
|
|
|
//
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function putStatus (statusesStore, status) {
|
2018-02-10 04:48:52 +01:00
|
|
|
statusesStore.put(cloneForStorage(status))
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function putAccount (accountsStore, account) {
|
2018-02-10 04:48:52 +01:00
|
|
|
accountsStore.put(cloneForStorage(account))
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function putNotification (notificationsStore, notification) {
|
2018-02-10 04:48:52 +01:00
|
|
|
notificationsStore.put(cloneForStorage(notification))
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function storeAccount (accountsStore, account) {
|
2018-02-10 04:48:52 +01:00
|
|
|
putAccount(accountsStore, account)
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function storeStatus (statusesStore, accountsStore, status) {
|
2018-02-10 04:48:52 +01:00
|
|
|
putStatus(statusesStore, status)
|
|
|
|
putAccount(accountsStore, status.account)
|
|
|
|
if (status.reblog) {
|
|
|
|
putStatus(statusesStore, status.reblog)
|
|
|
|
putAccount(accountsStore, status.reblog.account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function storeNotification (notificationsStore, statusesStore, accountsStore, notification) {
|
2018-02-10 04:48:52 +01:00
|
|
|
if (notification.status) {
|
|
|
|
storeStatus(statusesStore, accountsStore, notification.status)
|
|
|
|
}
|
|
|
|
storeAccount(accountsStore, notification.account)
|
|
|
|
putNotification(notificationsStore, notification)
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function fetchAccount (accountsStore, id, callback) {
|
2018-02-10 04:48:52 +01:00
|
|
|
accountsStore.get(id).onsuccess = e => {
|
|
|
|
callback(e.target.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function fetchStatus (statusesStore, accountsStore, id, callback) {
|
2018-02-10 04:48:52 +01:00
|
|
|
statusesStore.get(id).onsuccess = e => {
|
|
|
|
let status = e.target.result
|
|
|
|
callback(status)
|
|
|
|
fetchAccount(accountsStore, status[ACCOUNT_ID], account => {
|
|
|
|
status.account = account
|
|
|
|
})
|
|
|
|
if (status[REBLOG_ID]) {
|
|
|
|
fetchStatus(statusesStore, accountsStore, status[REBLOG_ID], reblog => {
|
|
|
|
status.reblog = reblog
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 18:37:13 +01:00
|
|
|
function fetchNotification (notificationsStore, statusesStore, accountsStore, id, callback) {
|
2018-02-10 04:48:52 +01:00
|
|
|
notificationsStore.get(id).onsuccess = e => {
|
|
|
|
let notification = e.target.result
|
|
|
|
callback(notification)
|
|
|
|
fetchAccount(accountsStore, notification[ACCOUNT_ID], account => {
|
|
|
|
notification.account = account
|
|
|
|
})
|
|
|
|
if (notification[STATUS_ID]) {
|
|
|
|
fetchStatus(statusesStore, accountsStore, notification[STATUS_ID], status => {
|
|
|
|
notification.status = status
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function insertTimelineNotifications (instanceName, timeline, notifications) {
|
2018-02-09 07:04:10 +01:00
|
|
|
for (let notification of notifications) {
|
|
|
|
setInCache(notificationsCache, instanceName, notification.id, notification)
|
|
|
|
setInCache(accountsCache, instanceName, notification.account.id, notification.account)
|
2018-02-15 18:02:46 +01:00
|
|
|
if (notification.status) {
|
|
|
|
setInCache(statusesCache, instanceName, notification.status.id, notification.status)
|
|
|
|
}
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
2018-02-10 04:48:52 +01:00
|
|
|
let storeNames = [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE, ACCOUNTS_STORE, STATUSES_STORE]
|
2018-02-09 07:04:10 +01:00
|
|
|
await dbPromise(db, storeNames, 'readwrite', (stores) => {
|
2018-02-10 04:48:52 +01:00
|
|
|
let [ timelineStore, notificationsStore, accountsStore, statusesStore ] = stores
|
2018-02-09 07:04:10 +01:00
|
|
|
for (let notification of notifications) {
|
2018-02-10 04:48:52 +01:00
|
|
|
storeNotification(notificationsStore, statusesStore, accountsStore, notification)
|
2018-03-09 08:18:18 +01:00
|
|
|
timelineStore.put(notification.id, createTimelineId(timeline, notification.id))
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
async function insertTimelineStatuses (instanceName, timeline, statuses) {
|
2018-02-09 07:04:10 +01:00
|
|
|
for (let status of statuses) {
|
2018-02-11 19:35:25 +01:00
|
|
|
cacheStatus(status, instanceName)
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
2018-02-10 04:48:52 +01:00
|
|
|
let storeNames = [STATUS_TIMELINES_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
2018-02-09 07:04:10 +01:00
|
|
|
await dbPromise(db, storeNames, 'readwrite', (stores) => {
|
|
|
|
let [ timelineStore, statusesStore, accountsStore ] = stores
|
|
|
|
for (let status of statuses) {
|
2018-02-10 04:48:52 +01:00
|
|
|
storeStatus(statusesStore, accountsStore, status)
|
2018-03-09 08:18:18 +01:00
|
|
|
timelineStore.put(status.id, createTimelineId(timeline, status.id))
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-09 03:31:59 +01:00
|
|
|
async function insertStatusThread (instanceName, statusId, statuses) {
|
|
|
|
for (let status of statuses) {
|
|
|
|
cacheStatus(status, instanceName)
|
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let storeNames = [THREADS_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
await dbPromise(db, storeNames, 'readwrite', (stores) => {
|
|
|
|
let [ threadsStore, statusesStore, accountsStore ] = stores
|
2018-03-11 05:24:07 +01:00
|
|
|
threadsStore.getAllKeys(createThreadKeyRange(statusId)).onsuccess = e => {
|
|
|
|
let existingKeys = e.target.result
|
|
|
|
let newKeys = times(statuses.length, i => createThreadId(statusId, i))
|
|
|
|
let keysToDelete = difference(existingKeys, newKeys)
|
|
|
|
for (let key of keysToDelete) {
|
|
|
|
threadsStore.delete(key)
|
|
|
|
}
|
2018-03-09 03:31:59 +01:00
|
|
|
}
|
2018-03-11 05:24:07 +01:00
|
|
|
statuses.forEach((otherStatus, i) => {
|
|
|
|
storeStatus(statusesStore, accountsStore, otherStatus)
|
|
|
|
threadsStore.put(otherStatus.id, createThreadId(statusId, i))
|
|
|
|
})
|
2018-03-09 03:31:59 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function insertTimelineItems (instanceName, timeline, timelineItems) {
|
2018-03-09 03:31:59 +01:00
|
|
|
/* no await */ scheduleCleanup()
|
|
|
|
if (timeline === 'notifications') {
|
|
|
|
return insertTimelineNotifications(instanceName, timeline, timelineItems)
|
|
|
|
} else if (timeline.startsWith('status/')) {
|
|
|
|
let statusId = timeline.split('/').slice(-1)[0]
|
|
|
|
return insertStatusThread(instanceName, statusId, timelineItems)
|
|
|
|
} else {
|
|
|
|
return insertTimelineStatuses(instanceName, timeline, timelineItems)
|
|
|
|
}
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:48:52 +01:00
|
|
|
//
|
|
|
|
// get
|
|
|
|
//
|
|
|
|
|
|
|
|
export async function getStatus (instanceName, id) {
|
|
|
|
if (hasInCache(statusesCache, instanceName, id)) {
|
|
|
|
return getInCache(statusesCache, instanceName, id)
|
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let storeNames = [STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
let result = await dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
|
|
|
let [ statusesStore, accountsStore ] = stores
|
|
|
|
fetchStatus(statusesStore, accountsStore, id, callback)
|
|
|
|
})
|
|
|
|
setInCache(statusesCache, instanceName, id, result)
|
|
|
|
return result
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:48:52 +01:00
|
|
|
export async function getNotification (instanceName, id) {
|
|
|
|
if (hasInCache(notificationsCache, instanceName, id)) {
|
|
|
|
return getInCache(notificationsCache, instanceName, id)
|
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let storeNames = [NOTIFICATIONS_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
let result = await dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
|
|
|
let [ notificationsStore, statusesStore, accountsStore ] = stores
|
|
|
|
fetchNotification(notificationsStore, statusesStore, accountsStore, id, callback)
|
|
|
|
})
|
|
|
|
setInCache(notificationsCache, instanceName, id, result)
|
|
|
|
return result
|
2018-02-09 07:29:29 +01:00
|
|
|
}
|
2018-02-11 19:35:25 +01:00
|
|
|
|
2018-02-17 04:38:21 +01:00
|
|
|
//
|
|
|
|
// lookup by reblogs
|
|
|
|
//
|
|
|
|
|
|
|
|
export async function getReblogsForStatus (instanceName, id) {
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
await dbPromise(db, STATUSES_STORE, 'readonly', (statusesStore, callback) => {
|
|
|
|
statusesStore.index(REBLOG_ID).getAll(IDBKeyRange.only(id)).onsuccess = e => {
|
|
|
|
callback(e.target.result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-11 01:21:10 +01:00
|
|
|
//
|
|
|
|
// lookups by statusId
|
|
|
|
//
|
|
|
|
|
|
|
|
export async function getNotificationIdsForStatuses (instanceName, statusIds) {
|
|
|
|
const db = await getDatabase(instanceName)
|
2018-03-11 06:05:00 +01:00
|
|
|
return dbPromise(db, NOTIFICATIONS_STORE, 'readonly', (notificationsStore, callback) => {
|
2018-03-11 01:21:10 +01:00
|
|
|
let res = []
|
|
|
|
callback(res)
|
|
|
|
statusIds.forEach(statusId => {
|
|
|
|
let req = notificationsStore.index(STATUS_ID).getAllKeys(IDBKeyRange.only(statusId))
|
|
|
|
req.onsuccess = e => {
|
2018-03-11 06:05:00 +01:00
|
|
|
for (let id of e.target.result) {
|
|
|
|
res.push(id)
|
|
|
|
}
|
2018-03-11 01:21:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-17 04:38:21 +01:00
|
|
|
//
|
|
|
|
// deletes
|
|
|
|
//
|
|
|
|
|
|
|
|
export async function deleteStatusesAndNotifications (instanceName, statusIds, notificationIds) {
|
|
|
|
for (let statusId of statusIds) {
|
|
|
|
deleteFromCache(statusesCache, instanceName, statusId)
|
|
|
|
}
|
|
|
|
for (let notificationId of notificationIds) {
|
|
|
|
deleteFromCache(notificationsCache, instanceName, notificationId)
|
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let storeNames = [
|
|
|
|
STATUSES_STORE,
|
|
|
|
STATUS_TIMELINES_STORE,
|
|
|
|
NOTIFICATIONS_STORE,
|
|
|
|
NOTIFICATION_TIMELINES_STORE,
|
2018-03-11 05:24:07 +01:00
|
|
|
PINNED_STATUSES_STORE,
|
|
|
|
THREADS_STORE
|
2018-02-17 04:38:21 +01:00
|
|
|
]
|
|
|
|
await dbPromise(db, storeNames, 'readwrite', (stores) => {
|
|
|
|
let [
|
|
|
|
statusesStore,
|
|
|
|
statusTimelinesStore,
|
|
|
|
notificationsStore,
|
|
|
|
notificationTimelinesStore,
|
2018-03-11 05:24:07 +01:00
|
|
|
pinnedStatusesStore,
|
|
|
|
threadsStore
|
2018-02-17 04:38:21 +01:00
|
|
|
] = stores
|
|
|
|
|
|
|
|
function deleteStatus (statusId) {
|
|
|
|
statusesStore.delete(statusId)
|
2018-03-11 05:24:07 +01:00
|
|
|
deleteAll(
|
|
|
|
pinnedStatusesStore,
|
|
|
|
pinnedStatusesStore.index('statusId'),
|
|
|
|
IDBKeyRange.only(statusId)
|
|
|
|
)
|
|
|
|
deleteAll(
|
|
|
|
statusTimelinesStore,
|
|
|
|
statusTimelinesStore.index('statusId'),
|
|
|
|
IDBKeyRange.only(statusId)
|
|
|
|
)
|
|
|
|
deleteAll(
|
|
|
|
threadsStore,
|
|
|
|
threadsStore.index('statusId'),
|
|
|
|
IDBKeyRange.only(statusId)
|
|
|
|
)
|
|
|
|
deleteAll(
|
|
|
|
threadsStore,
|
|
|
|
threadsStore,
|
|
|
|
createThreadKeyRange(statusId)
|
|
|
|
)
|
2018-02-17 04:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteNotification (notificationId) {
|
|
|
|
notificationsStore.delete(notificationId)
|
2018-03-11 05:24:07 +01:00
|
|
|
deleteAll(
|
|
|
|
notificationTimelinesStore,
|
2018-03-11 06:05:00 +01:00
|
|
|
notificationTimelinesStore.index('notificationId'),
|
2018-03-11 05:24:07 +01:00
|
|
|
IDBKeyRange.only(notificationId)
|
|
|
|
)
|
2018-02-17 04:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let statusId of statusIds) {
|
|
|
|
deleteStatus(statusId)
|
|
|
|
}
|
|
|
|
for (let notificationId of notificationIds) {
|
|
|
|
deleteNotification(notificationId)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-11 19:35:25 +01:00
|
|
|
//
|
|
|
|
// pinned statuses
|
|
|
|
//
|
|
|
|
|
|
|
|
export async function insertPinnedStatuses (instanceName, accountId, statuses) {
|
|
|
|
for (let status of statuses) {
|
|
|
|
cacheStatus(status, instanceName)
|
|
|
|
}
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let storeNames = [PINNED_STATUSES_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
await dbPromise(db, storeNames, 'readwrite', (stores) => {
|
|
|
|
let [ pinnedStatusesStore, statusesStore, accountsStore ] = stores
|
|
|
|
statuses.forEach((status, i) => {
|
|
|
|
storeStatus(statusesStore, accountsStore, status)
|
2018-03-11 05:24:07 +01:00
|
|
|
pinnedStatusesStore.put(status.id, createPinnedStatusId(accountId, i))
|
2018-02-11 19:35:25 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPinnedStatuses (instanceName, accountId) {
|
|
|
|
let storeNames = [PINNED_STATUSES_STORE, STATUSES_STORE, ACCOUNTS_STORE]
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return dbPromise(db, storeNames, 'readonly', (stores, callback) => {
|
|
|
|
let [ pinnedStatusesStore, statusesStore, accountsStore ] = stores
|
2018-03-11 05:24:07 +01:00
|
|
|
let keyRange = createPinnedStatusKeyRange(accountId)
|
2018-02-11 19:35:25 +01:00
|
|
|
pinnedStatusesStore.getAll(keyRange).onsuccess = e => {
|
|
|
|
let pinnedResults = e.target.result
|
|
|
|
let res = new Array(pinnedResults.length)
|
2018-03-09 08:18:18 +01:00
|
|
|
pinnedResults.forEach((statusId, i) => {
|
|
|
|
fetchStatus(statusesStore, accountsStore, statusId, status => {
|
2018-02-11 19:35:25 +01:00
|
|
|
res[i] = status
|
|
|
|
})
|
|
|
|
})
|
|
|
|
callback(res)
|
|
|
|
}
|
|
|
|
})
|
2018-02-11 23:11:03 +01:00
|
|
|
}
|
2018-02-17 04:38:21 +01:00
|
|
|
|
2018-02-24 03:23:36 +01:00
|
|
|
//
|
2018-02-24 23:49:28 +01:00
|
|
|
// update statuses
|
2018-02-24 03:23:36 +01:00
|
|
|
//
|
|
|
|
|
2018-02-24 23:49:28 +01:00
|
|
|
async function updateStatus (instanceName, statusId, updateFunc) {
|
2018-02-24 03:23:36 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
2018-02-24 23:49:28 +01:00
|
|
|
if (hasInCache(statusesCache, instanceName, statusId)) {
|
|
|
|
let status = getInCache(statusesCache, instanceName, statusId)
|
|
|
|
updateFunc(status)
|
|
|
|
cacheStatus(status, instanceName)
|
|
|
|
}
|
2018-02-24 03:23:36 +01:00
|
|
|
return dbPromise(db, STATUSES_STORE, 'readwrite', (statusesStore) => {
|
2018-02-24 23:49:28 +01:00
|
|
|
statusesStore.get(statusId).onsuccess = e => {
|
|
|
|
let status = e.target.result
|
|
|
|
updateFunc(status)
|
|
|
|
putStatus(statusesStore, status)
|
|
|
|
}
|
2018-02-24 03:23:36 +01:00
|
|
|
})
|
2018-02-24 23:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function setStatusFavorited (instanceName, statusId, favorited) {
|
|
|
|
return updateStatus(instanceName, statusId, status => {
|
|
|
|
let delta = (favorited ? 1 : 0) - (status.favourited ? 1 : 0)
|
|
|
|
status.favourited = favorited
|
|
|
|
status.favourites_count = (status.favourites_count || 0) + delta
|
|
|
|
})
|
|
|
|
}
|
2018-02-25 03:20:33 +01:00
|
|
|
|
|
|
|
export async function setStatusReblogged (instanceName, statusId, reblogged) {
|
|
|
|
return updateStatus(instanceName, statusId, status => {
|
|
|
|
let delta = (reblogged ? 1 : 0) - (status.reblogged ? 1 : 0)
|
|
|
|
status.reblogged = reblogged
|
|
|
|
status.reblogs_count = (status.reblogs_count || 0) + delta
|
|
|
|
})
|
|
|
|
}
|