From 48d47160d7cf0f1eb8994972b73334bb6d7c54be Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Tue, 23 Jan 2018 09:21:21 -0800 Subject: [PATCH] use database for account caching --- routes/_components/Timeline.html | 16 --------- routes/_utils/database/constants.js | 3 +- routes/_utils/database/databaseCore.js | 22 +++++++++--- routes/_utils/database/databaseLifecycle.js | 4 ++- routes/accounts/[accountId].html | 37 +++++++++++++-------- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/routes/_components/Timeline.html b/routes/_components/Timeline.html index 27447160..2b43f17f 100644 --- a/routes/_components/Timeline.html +++ b/routes/_components/Timeline.html @@ -38,22 +38,6 @@ this.fire('initialized') })) }) - this.observe('statuses', statuses => { - let cachedAccountNames = this.store.get('cachedAccountNames') || {} - for (let status of statuses) { - cachedAccountNames[status.account.id] = { - username: status.account.username, - acct: status.account.acct - } - if (status.reblog) { - cachedAccountNames[status.reblog.account.id] = { - username: status.reblog.account.username, - acct: status.reblog.account.acct - } - } - } - this.store.set({'cachedAccountNames': cachedAccountNames}) - }) }, data: () => ({ StatusListItem: StatusListItem, diff --git a/routes/_utils/database/constants.js b/routes/_utils/database/constants.js index 987e1c5b..90d9a32d 100644 --- a/routes/_utils/database/constants.js +++ b/routes/_utils/database/constants.js @@ -1,3 +1,4 @@ export const STATUSES_STORE = 'statuses' export const TIMELINE_STORE = 'timelines' -export const META_STORE = 'meta' \ No newline at end of file +export const META_STORE = 'meta' +export const ACCOUNTS_STORE= 'accounts' \ No newline at end of file diff --git a/routes/_utils/database/databaseCore.js b/routes/_utils/database/databaseCore.js index c61a25a3..0f531aa5 100644 --- a/routes/_utils/database/databaseCore.js +++ b/routes/_utils/database/databaseCore.js @@ -10,7 +10,7 @@ import { import { META_STORE, TIMELINE_STORE, - STATUSES_STORE + STATUSES_STORE, ACCOUNTS_STORE } from './constants' export async function getTimeline(instanceName, timeline, maxId = null, limit = 20) { @@ -38,16 +38,19 @@ export async function getTimeline(instanceName, timeline, maxId = null, limit = export async function insertStatuses(instanceName, timeline, statuses) { const db = await getDatabase(instanceName, timeline) - await dbPromise(db, [TIMELINE_STORE, STATUSES_STORE], 'readwrite', (stores) => { - let [ timelineStore, statusesStore ] = stores + await dbPromise(db, [TIMELINE_STORE, STATUSES_STORE, ACCOUNTS_STORE], 'readwrite', (stores) => { + let [ timelineStore, statusesStore, accountsStore ] = stores for (let status of statuses) { statusesStore.put(status) // reverse chronological order, prefixed by timeline - let id = timeline + '\u0000' + toReversePaddedBigInt(status.id) timelineStore.put({ - id: id, + id: (timeline + '\u0000' + toReversePaddedBigInt(status.id)), statusId: status.id }) + accountsStore.put(status.account) + if (status.reblog) { + accountsStore.put(status.reblog.account) + } } }) } @@ -71,6 +74,15 @@ export async function setInstanceVerifyCredentials(instanceName, verifyCredentia }) } +export async function getAccount(instanceName, accountId) { + const db = await getDatabase(instanceName) + return await dbPromise(db, ACCOUNTS_STORE, 'readonly', (store, callback) => { + store.get(accountId).onsuccess = (e) => { + callback(e.target.result && e.target.result) + } + }) +} + export async function clearDatabaseForInstance(instanceName) { await deleteDatabase(instanceName) } \ No newline at end of file diff --git a/routes/_utils/database/databaseLifecycle.js b/routes/_utils/database/databaseLifecycle.js index 538857fd..9af324a2 100644 --- a/routes/_utils/database/databaseLifecycle.js +++ b/routes/_utils/database/databaseLifecycle.js @@ -4,7 +4,8 @@ const databaseCache = {} import { META_STORE, TIMELINE_STORE, - STATUSES_STORE + STATUSES_STORE, + ACCOUNTS_STORE } from './constants' export function getDatabase(instanceName) { @@ -23,6 +24,7 @@ export function getDatabase(instanceName) { let db = req.result; db.createObjectStore(META_STORE, {keyPath: 'key'}) db.createObjectStore(STATUSES_STORE, {keyPath: 'id'}) + db.createObjectStore(ACCOUNTS_STORE, {keyPath: 'id'}) let timelineStore = db.createObjectStore(TIMELINE_STORE, {keyPath: 'id'}) timelineStore.createIndex('statusId', 'statusId') } diff --git a/routes/accounts/[accountId].html b/routes/accounts/[accountId].html index dc6816f6..2451fbb2 100644 --- a/routes/accounts/[accountId].html +++ b/routes/accounts/[accountId].html @@ -1,14 +1,14 @@ <:Head> - {{'Pinafore – ' + (cachedProfileName || profileName || '')}} + {{'Pinafore – ' + profileName}} {{#if $isUserLoggedIn}} - + {{else}} @@ -28,28 +28,39 @@ import HiddenFromSSR from '../_components/HiddenFromSSR' import DynamicPageBanner from '../_components/DynamicPageBanner.html' import { getAccount } from '../_utils/mastodon/user' + import { database } from '../_utils/database/database' export default { - async oncreate() { + oncreate() { let currentInstance = this.store.get('currentInstance') let accessToken = this.store.get('accessToken') let accountId = this.get('params').accountId - let account = await getAccount(currentInstance, accessToken, accountId) - this.set({account: account}) + database.getAccount(currentInstance, accountId).then(account => { + this.set({cachedAccount: account}) + }) + getAccount(currentInstance, accessToken, accountId).then(account => { + this.set({account: account}) + }) }, store: () => store, computed: { - profileName: (account) => { + remoteProfileName: (account) => { return account && ('@' + account.acct) }, - shortProfileName: (account) => { + remoteShortProfileName: (account) => { return account && ('@' + account.username) }, - cachedProfileName: ($cachedAccountNames, params) => { - return $cachedAccountNames && $cachedAccountNames[params.accountId] && ('@' + $cachedAccountNames[params.accountId].acct) + cachedProfileName: (cachedAccount) => { + return cachedAccount && ('@' + cachedAccount.acct) }, - cachedShortProfileName: ($cachedAccountNames, params) => { - return $cachedAccountNames && $cachedAccountNames[params.accountId] && ('@' + $cachedAccountNames[params.accountId].username) + cachedShortProfileName: (cachedAccount) => { + return cachedAccount && ('@' + cachedAccount.username) + }, + profileName: (remoteProfileName, cachedProfileName) => { + return remoteProfileName || cachedProfileName || '' + }, + shortProfileName: (remoteShortProfileName, cachedShortProfileName) => { + return remoteShortProfileName || cachedShortProfileName || '' } }, components: {