diff --git a/routes/_utils/database/databaseCore.js b/routes/_utils/database/databaseCore.js index 5728254c..44bd91aa 100644 --- a/routes/_utils/database/databaseCore.js +++ b/routes/_utils/database/databaseCore.js @@ -222,6 +222,14 @@ export async function getAccount(instanceName, accountId) { return result } +export async function setAccount(instanceName, account) { + setInCache(accountsCache, instanceName, account.id, account) + const db = await getDatabase(instanceName) + return await dbPromise(db, ACCOUNTS_STORE, 'readwrite', (store) => { + store.put(account) + }) +} + // // lifecycle // diff --git a/routes/accounts/[accountId].html b/routes/accounts/[accountId].html index beeea377..6c468072 100644 --- a/routes/accounts/[accountId].html +++ b/routes/accounts/[accountId].html @@ -29,40 +29,20 @@ import { store } from '../_utils/store.js' import HiddenFromSSR from '../_components/HiddenFromSSR' import DynamicPageBanner from '../_components/DynamicPageBanner.html' - import { getAccount } from '../_utils/mastodon/user' - import { database } from '../_utils/database/database' + import { showAccountProfile } from './_actions/[accountId]' export default { oncreate() { - let currentInstance = this.store.get('currentInstance') - let accessToken = this.store.get('accessToken') let accountId = this.get('params').accountId - database.getAccount(currentInstance, accountId).then(account => { - this.set({cachedAccount: account}) - }) - getAccount(currentInstance, accessToken, accountId).then(account => { - this.set({remoteAccount: account}) - }) + showAccountProfile(accountId) }, store: () => store, computed: { - remoteProfileName: (remoteAccount) => { - return remoteAccount && ('@' + remoteAccount.acct) + profileName: ($currentAccountProfile) => { + return $currentAccountProfile && ('@' + $currentAccountProfile.acct) }, - remoteShortProfileName: (remoteAccount) => { - return remoteAccount && ('@' + remoteAccount.username) - }, - cachedProfileName: (cachedAccount) => { - return cachedAccount && ('@' + cachedAccount.acct) - }, - cachedShortProfileName: (cachedAccount) => { - return cachedAccount && ('@' + cachedAccount.username) - }, - profileName: (remoteProfileName, cachedProfileName) => { - return remoteProfileName || cachedProfileName || '' - }, - shortProfileName: (remoteShortProfileName, cachedShortProfileName) => { - return remoteShortProfileName || cachedShortProfileName || '' + shortProfileName: ($currentAccountProfile) => { + return $currentAccountProfile && ('@' + $currentAccountProfile.username) } }, components: { diff --git a/routes/accounts/_actions/[accountId].js b/routes/accounts/_actions/[accountId].js new file mode 100644 index 00000000..ddeae1cc --- /dev/null +++ b/routes/accounts/_actions/[accountId].js @@ -0,0 +1,24 @@ +import { getAccount } from '../../_utils/mastodon/user' +import { database } from '../../_utils/database/database' +import { store } from '../../_utils/store' + +export async function showAccountProfile(accountId) { + store.set({currentAccountProfile: null}) + let instanceName = store.get('currentInstance') + let accessToken = store.get('accessToken') + + let localPromise = database.getAccount(instanceName, accountId) + let remotePromise = getAccount(instanceName, accessToken, accountId).then(account => { + database.setAccount(instanceName, account) + return account + }) + + let localAccount = await localPromise + store.set({currentAccountProfile: localAccount}) + try { + let remoteAccount = await remotePromise + store.set({currentAccountProfile: remoteAccount}) + } catch (e) { + console.error("couldn't fetch profile", e) + } +} \ No newline at end of file