2018-03-25 01:04:54 +00:00
|
|
|
import {
|
|
|
|
ACCOUNTS_STORE, RELATIONSHIPS_STORE, USERNAME_LOWERCASE
|
|
|
|
} from './constants'
|
2018-02-09 06:04:10 +00:00
|
|
|
import { accountsCache, relationshipsCache } from './cache'
|
2018-02-14 03:34:37 +00:00
|
|
|
import { cloneForStorage, getGenericEntityWithId, setGenericEntityWithId } from './helpers'
|
2018-03-25 01:04:54 +00:00
|
|
|
import { dbPromise, getDatabase } from './databaseLifecycle'
|
|
|
|
import { createAccountUsernamePrefixKeyRange } from './keys'
|
2018-02-09 06:04:10 +00:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function getAccount (instanceName, accountId) {
|
|
|
|
return getGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, accountId)
|
2018-02-09 06:04:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function setAccount (instanceName, account) {
|
2018-02-14 03:34:37 +00:00
|
|
|
return setGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, cloneForStorage(account))
|
2018-02-09 06:04:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function getRelationship (instanceName, accountId) {
|
|
|
|
return getGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, accountId)
|
2018-02-09 06:04:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function setRelationship (instanceName, relationship) {
|
2018-02-14 03:34:37 +00:00
|
|
|
return setGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, cloneForStorage(relationship))
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
|
|
|
|
export async function searchAccountsByUsername (instanceName, usernamePrefix, limit = 20) {
|
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return dbPromise(db, ACCOUNTS_STORE, 'readonly', (accountsStore, callback) => {
|
|
|
|
let keyRange = createAccountUsernamePrefixKeyRange(usernamePrefix.toLowerCase())
|
|
|
|
accountsStore.index(USERNAME_LOWERCASE).getAll(keyRange, limit).onsuccess = e => {
|
|
|
|
let results = e.target.result
|
|
|
|
results = results.sort((a, b) => {
|
|
|
|
// accounts you're following go first
|
|
|
|
if (a.following !== b.following) {
|
|
|
|
return a.following ? -1 : 1
|
|
|
|
}
|
|
|
|
// after that, just sort by username
|
|
|
|
if (a[USERNAME_LOWERCASE] !== b[USERNAME_LOWERCASE]) {
|
|
|
|
return a[USERNAME_LOWERCASE] < b[USERNAME_LOWERCASE] ? -1 : 1
|
|
|
|
}
|
|
|
|
return 0 // eslint-disable-line
|
|
|
|
})
|
|
|
|
callback(results)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|