2018-01-23 18:03:31 +01:00
|
|
|
import {
|
|
|
|
toReversePaddedBigInt
|
|
|
|
} from './utils'
|
|
|
|
import {
|
|
|
|
getDatabase,
|
|
|
|
dbPromise,
|
|
|
|
deleteDatabase,
|
|
|
|
} from './databaseLifecycle'
|
|
|
|
|
|
|
|
import {
|
|
|
|
META_STORE,
|
2018-02-04 03:06:02 +01:00
|
|
|
STATUS_TIMELINES_STORE,
|
2018-01-28 21:51:48 +01:00
|
|
|
STATUSES_STORE,
|
|
|
|
ACCOUNTS_STORE,
|
2018-02-04 03:06:02 +01:00
|
|
|
RELATIONSHIPS_STORE,
|
2018-02-04 03:06:57 +01:00
|
|
|
NOTIFICATIONS_STORE,
|
|
|
|
NOTIFICATION_TIMELINES_STORE
|
2018-01-23 18:03:31 +01:00
|
|
|
} from './constants'
|
2018-01-22 03:36:40 +01:00
|
|
|
|
2018-01-28 21:51:48 +01:00
|
|
|
import {
|
|
|
|
statusesCache,
|
|
|
|
relationshipsCache,
|
|
|
|
accountsCache,
|
|
|
|
metaCache,
|
2018-02-04 03:06:02 +01:00
|
|
|
notificationsCache,
|
2018-01-28 21:51:48 +01:00
|
|
|
clearCache,
|
|
|
|
getInCache,
|
|
|
|
hasInCache,
|
|
|
|
setInCache
|
|
|
|
} from './cache'
|
2018-01-24 03:15:14 +01:00
|
|
|
|
2018-01-28 21:51:48 +01:00
|
|
|
//
|
|
|
|
// helpers
|
|
|
|
//
|
2018-01-27 23:55:46 +01:00
|
|
|
|
2018-01-28 21:51:48 +01:00
|
|
|
async function getGenericEntityWithId(store, cache, instanceName, id) {
|
|
|
|
if (hasInCache(cache, instanceName, id)) {
|
|
|
|
return getInCache(cache, instanceName, id)
|
2018-01-27 23:55:46 +01:00
|
|
|
}
|
2018-01-28 21:51:48 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
let result = await dbPromise(db, store, 'readonly', (store, callback) => {
|
|
|
|
store.get(id).onsuccess = (e) => callback(e.target.result)
|
|
|
|
})
|
|
|
|
setInCache(cache, instanceName, id, result)
|
|
|
|
return result
|
2018-01-27 23:45:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 21:51:48 +01:00
|
|
|
async function setGenericEntityWithId(store, cache, instanceName, entity) {
|
|
|
|
setInCache(cache, instanceName, entity.id, entity)
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return await dbPromise(db, store, 'readwrite', (store) => {
|
|
|
|
store.put(entity)
|
|
|
|
})
|
2018-01-27 23:45:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
//
|
2018-02-04 03:06:02 +01:00
|
|
|
// timelines/statuses/notifications
|
2018-01-25 17:44:10 +01:00
|
|
|
//
|
|
|
|
|
2018-02-04 03:06:02 +01:00
|
|
|
function getTimelineVariables(timeline) {
|
|
|
|
if (timeline === 'notifications') {
|
|
|
|
return {
|
|
|
|
stores: [NOTIFICATION_TIMELINES_STORE, NOTIFICATIONS_STORE, ACCOUNTS_STORE],
|
|
|
|
remoteId: 'notificationId',
|
|
|
|
itemsCache: notificationsCache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
stores: [STATUS_TIMELINES_STORE, STATUSES_STORE, ACCOUNTS_STORE],
|
|
|
|
remoteId: 'statusId',
|
|
|
|
itemsCache: statusesCache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 03:36:40 +01:00
|
|
|
export async function getTimeline(instanceName, timeline, maxId = null, limit = 20) {
|
2018-02-04 03:06:02 +01:00
|
|
|
let { stores, remoteId } = getTimelineVariables(timeline)
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return await dbPromise(db, stores, 'readonly', (stores, callback) => {
|
|
|
|
let [ timelineStore, itemsStore ] = stores
|
2018-01-23 18:03:31 +01:00
|
|
|
|
|
|
|
let negBigInt = maxId && toReversePaddedBigInt(maxId)
|
|
|
|
let start = negBigInt ? (timeline + '\u0000' + negBigInt) : (timeline + '\u0000')
|
|
|
|
let end = timeline + '\u0000\uffff'
|
|
|
|
let query = IDBKeyRange.bound(start, end, false, false)
|
|
|
|
|
|
|
|
timelineStore.getAll(query, limit).onsuccess = e => {
|
|
|
|
let timelineResults = e.target.result
|
|
|
|
let res = new Array(timelineResults.length)
|
|
|
|
timelineResults.forEach((timelineResult, i) => {
|
2018-02-04 03:06:02 +01:00
|
|
|
itemsStore.get(timelineResult[remoteId]).onsuccess = e => {
|
2018-01-23 18:03:31 +01:00
|
|
|
res[i] = e.target.result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(res)
|
2018-01-22 03:36:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-04 03:06:02 +01:00
|
|
|
export async function insertTimelineItems(instanceName, timeline, timelineItems) {
|
|
|
|
let { stores, remoteId, itemsCache } = getTimelineVariables(timeline)
|
|
|
|
for (let timelineItem of timelineItems) {
|
|
|
|
setInCache(itemsCache, instanceName, timelineItem.id, timelineItem)
|
|
|
|
setInCache(accountsCache, instanceName, timelineItem.account.id, timelineItem.account)
|
|
|
|
if (timelineItem.reblog) {
|
|
|
|
setInCache(accountsCache, instanceName, timelineItem.reblog.account.id, timelineItem.reblog.account)
|
2018-01-25 09:01:56 +01:00
|
|
|
}
|
2018-01-24 03:15:14 +01:00
|
|
|
}
|
2018-02-04 03:06:02 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
await dbPromise(db, stores, 'readwrite', (stores) => {
|
|
|
|
let [ timelineStore, itemsStore, accountsStore ] = stores
|
|
|
|
for (let item of timelineItems) {
|
|
|
|
itemsStore.put(item)
|
2018-01-23 18:03:31 +01:00
|
|
|
// reverse chronological order, prefixed by timeline
|
|
|
|
timelineStore.put({
|
2018-02-04 03:06:02 +01:00
|
|
|
id: (timeline + '\u0000' + toReversePaddedBigInt(item.id)),
|
|
|
|
[remoteId]: item.id
|
2018-01-23 18:03:31 +01:00
|
|
|
})
|
2018-02-04 03:06:02 +01:00
|
|
|
accountsStore.put(item.account)
|
|
|
|
if (item.reblog) {
|
|
|
|
accountsStore.put(item.reblog.account)
|
2018-01-23 18:21:21 +01:00
|
|
|
}
|
2018-01-22 03:36:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
export async function getStatus(instanceName, statusId) {
|
2018-01-28 21:51:48 +01:00
|
|
|
return await getGenericEntityWithId(STATUSES_STORE, statusesCache, instanceName, statusId)
|
2018-01-25 17:44:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 03:06:02 +01:00
|
|
|
export async function getNotification(instanceName, notificationId) {
|
|
|
|
return await getGenericEntityWithId(NOTIFICATIONS_STORE, notificationsCache, instanceName, notificationId)
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
//
|
|
|
|
// meta
|
|
|
|
//
|
|
|
|
|
|
|
|
async function getMetaProperty(instanceName, key) {
|
2018-01-27 23:45:51 +01:00
|
|
|
if (hasInCache(metaCache, instanceName, key)) {
|
|
|
|
return getInCache(metaCache, instanceName, key)
|
2018-01-25 17:44:10 +01:00
|
|
|
}
|
2018-01-23 18:03:31 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
2018-01-25 17:44:10 +01:00
|
|
|
let result = await dbPromise(db, META_STORE, 'readonly', (store, callback) => {
|
|
|
|
store.get(key).onsuccess = (e) => {
|
2018-01-22 03:36:40 +01:00
|
|
|
callback(e.target.result && e.target.result.value)
|
|
|
|
}
|
|
|
|
})
|
2018-01-27 23:45:51 +01:00
|
|
|
setInCache(metaCache, instanceName, key, result)
|
2018-01-25 17:44:10 +01:00
|
|
|
return result
|
2018-01-22 03:36:40 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
async function setMetaProperty(instanceName, key, value) {
|
2018-01-27 23:45:51 +01:00
|
|
|
setInCache(metaCache, instanceName, key, value)
|
2018-01-23 18:03:31 +01:00
|
|
|
const db = await getDatabase(instanceName)
|
2018-01-22 03:36:40 +01:00
|
|
|
return await dbPromise(db, META_STORE, 'readwrite', (store) => {
|
|
|
|
store.put({
|
2018-01-25 17:44:10 +01:00
|
|
|
key: key,
|
|
|
|
value: value
|
2018-01-22 03:36:40 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
export async function getInstanceVerifyCredentials(instanceName) {
|
|
|
|
return await getMetaProperty(instanceName, 'verifyCredentials')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setInstanceVerifyCredentials(instanceName, value) {
|
|
|
|
return await setMetaProperty(instanceName, 'verifyCredentials', value)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getInstanceInfo(instanceName) {
|
|
|
|
return await getMetaProperty(instanceName, 'instance')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setInstanceInfo(instanceName, value) {
|
|
|
|
return await setMetaProperty(instanceName, 'instance', value)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-01-28 21:51:48 +01:00
|
|
|
// accounts/relationships
|
2018-01-25 17:44:10 +01:00
|
|
|
//
|
|
|
|
|
2018-01-23 18:21:21 +01:00
|
|
|
export async function getAccount(instanceName, accountId) {
|
2018-01-28 21:51:48 +01:00
|
|
|
return await getGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, accountId)
|
2018-01-23 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 05:23:52 +01:00
|
|
|
export async function setAccount(instanceName, account) {
|
2018-01-28 21:51:48 +01:00
|
|
|
return await setGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRelationship(instanceName, accountId) {
|
|
|
|
return await getGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, accountId)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setRelationship(instanceName, relationship) {
|
|
|
|
return await setGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, relationship)
|
2018-01-28 05:23:52 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:10 +01:00
|
|
|
//
|
|
|
|
// lifecycle
|
|
|
|
//
|
|
|
|
|
2018-01-23 18:03:31 +01:00
|
|
|
export async function clearDatabaseForInstance(instanceName) {
|
2018-01-27 23:55:46 +01:00
|
|
|
clearCache(statusesCache, instanceName)
|
|
|
|
clearCache(accountsCache, instanceName)
|
|
|
|
clearCache(metaCache, instanceName)
|
2018-01-23 18:03:31 +01:00
|
|
|
await deleteDatabase(instanceName)
|
2018-01-22 03:36:40 +01:00
|
|
|
}
|