From d6af3b69a7eb083bdf1840dc7c0a8958d152377b Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 25 Aug 2018 22:03:33 -0700 Subject: [PATCH] Add ability to show/hide boosts from accounts (#491) Fixes some stuff in #6 --- routes/_actions/accounts.js | 8 ++++--- routes/_actions/follow.js | 2 +- routes/_actions/setShowReblogs.js | 22 +++++++++++++++++++ routes/_api/relationships.js | 8 +++++++ routes/_api/showReblogs.js | 7 ++++++ routes/_api/user.js | 9 +------- .../AccountProfileOptionsDialog.html | 21 +++++++++++++++++- ...ccountsAndRelationships.js => accounts.js} | 14 ++---------- routes/_database/relationships.js | 11 ++++++++++ .../_store/observers/autosuggestObservers.js | 2 +- 10 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 routes/_actions/setShowReblogs.js create mode 100644 routes/_api/relationships.js create mode 100644 routes/_api/showReblogs.js rename routes/_database/{accountsAndRelationships.js => accounts.js} (73%) create mode 100644 routes/_database/relationships.js diff --git a/routes/_actions/accounts.js b/routes/_actions/accounts.js index c90df722..6d3410fb 100644 --- a/routes/_actions/accounts.js +++ b/routes/_actions/accounts.js @@ -1,10 +1,12 @@ -import { getAccount, getRelationship } from '../_api/user' +import { getAccount } from '../_api/user' +import { getRelationship } from '../_api/relationships' import { getAccount as getAccountFromDatabase, - setAccount as setAccountInDatabase, + setAccount as setAccountInDatabase} from '../_database/accounts' +import { getRelationship as getRelationshipFromDatabase, setRelationship as setRelationshipInDatabase -} from '../_database/accountsAndRelationships' +} from '../_database/relationships' import { store } from '../_store/store' async function updateAccount (accountId, instanceName, accessToken) { diff --git a/routes/_actions/follow.js b/routes/_actions/follow.js index ffad433b..8b7db44e 100644 --- a/routes/_actions/follow.js +++ b/routes/_actions/follow.js @@ -4,7 +4,7 @@ import { toast } from '../_utils/toast' import { updateProfileAndRelationship } from './accounts' import { getRelationship as getRelationshipFromDatabase -} from '../_database/accountsAndRelationships' +} from '../_database/relationships' export async function setAccountFollowed (accountId, follow, toastOnSuccess) { let { currentInstance, accessToken } = store.get() diff --git a/routes/_actions/setShowReblogs.js b/routes/_actions/setShowReblogs.js new file mode 100644 index 00000000..9ea12867 --- /dev/null +++ b/routes/_actions/setShowReblogs.js @@ -0,0 +1,22 @@ +import { store } from '../_store/store' +import { setShowReblogs as setShowReblogsApi } from '../_api/showReblogs' +import { toast } from '../_utils/toast' +import { updateProfileAndRelationship } from './accounts' + +export async function setShowReblogs (accountId, showReblogs, toastOnSuccess) { + let { currentInstance, accessToken } = store.get() + try { + await setShowReblogsApi(currentInstance, accessToken, accountId, showReblogs) + await updateProfileAndRelationship(accountId) + if (toastOnSuccess) { + if (showReblogs) { + toast.say('Showing boosts') + } else { + toast.say('Hiding boosts') + } + } + } catch (e) { + console.error(e) + toast.say(`Unable to ${showReblogs ? 'show' : 'hide'} boosts: ` + (e.message || '')) + } +} diff --git a/routes/_api/relationships.js b/routes/_api/relationships.js new file mode 100644 index 00000000..0337db4d --- /dev/null +++ b/routes/_api/relationships.js @@ -0,0 +1,8 @@ +import { basename, auth } from './utils' +import { get, paramsString, DEFAULT_TIMEOUT } from '../_utils/ajax' + +export async function getRelationship (instanceName, accessToken, accountId) { + let url = `${basename(instanceName)}/api/v1/accounts/relationships?${paramsString({id: accountId})}` + let res = await get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT}) + return res[0] +} diff --git a/routes/_api/showReblogs.js b/routes/_api/showReblogs.js new file mode 100644 index 00000000..8f3c57f0 --- /dev/null +++ b/routes/_api/showReblogs.js @@ -0,0 +1,7 @@ +import { auth, basename } from './utils' +import { post, WRITE_TIMEOUT } from '../_utils/ajax' + +export function setShowReblogs (instanceName, accessToken, accountId, showReblogs) { + let url = `${basename(instanceName)}/api/v1/accounts/${accountId}/follow` + return post(url, { reblogs: !!showReblogs }, auth(accessToken), {timeout: WRITE_TIMEOUT}) +} diff --git a/routes/_api/user.js b/routes/_api/user.js index ce7b2ee9..60d3dfbf 100644 --- a/routes/_api/user.js +++ b/routes/_api/user.js @@ -1,4 +1,4 @@ -import { get, paramsString, DEFAULT_TIMEOUT } from '../_utils/ajax' +import { get, DEFAULT_TIMEOUT } from '../_utils/ajax' import { auth, basename } from './utils' export function getVerifyCredentials (instanceName, accessToken) { @@ -10,10 +10,3 @@ export function getAccount (instanceName, accessToken, accountId) { let url = `${basename(instanceName)}/api/v1/accounts/${accountId}` return get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT}) } - -export async function getRelationship (instanceName, accessToken, accountId) { - let url = `${basename(instanceName)}/api/v1/accounts/relationships` - url += '?' + paramsString({id: accountId}) - let res = await get(url, auth(accessToken), {timeout: DEFAULT_TIMEOUT}) - return res[0] -} diff --git a/routes/_components/dialog/components/AccountProfileOptionsDialog.html b/routes/_components/dialog/components/AccountProfileOptionsDialog.html index 1e884e33..2e4f833a 100644 --- a/routes/_components/dialog/components/AccountProfileOptionsDialog.html +++ b/routes/_components/dialog/components/AccountProfileOptionsDialog.html @@ -18,6 +18,7 @@ import { oncreate } from '../helpers/onCreateDialog' import { setAccountBlocked } from '../../../_actions/block' import { setAccountMuted } from '../../../_actions/mute' import { setAccountFollowed } from '../../../_actions/follow' +import { setShowReblogs } from '../../../_actions/setShowReblogs' export default { oncreate, @@ -59,10 +60,16 @@ export default { // // end copypasta (StatusOptionsDialog.html / AccountProfileOptionsDialog.html) // + showingReblogs: ({ relationship }) => !!relationship.showing_reblogs, + showReblogsLabel: ({ showingReblogs, acct }) => ( + showingReblogs + ? `Hide boosts from @${acct}` + : `Show boosts from @${acct}` + ), items: ({ blockLabel, blocking, blockIcon, muteLabel, muteIcon, followLabel, followIcon, following, followRequested, - accountId, verifyCredentialsId, acct, isUser + accountId, verifyCredentialsId, acct, isUser, showReblogsLabel }) => ([ !isUser && { key: 'mention', @@ -84,6 +91,11 @@ export default { label: muteLabel, icon: muteIcon }, + !isUser && following && { + key: 'showReblogs', + label: showReblogsLabel, + icon: '#fa-retweet' + }, { key: 'copy', label: 'Copy link to account', @@ -106,6 +118,8 @@ export default { return this.onMuteClicked() case 'copy': return this.onCopyClicked() + case 'showReblogs': + return this.onShowReblogsClicked() } }, async onMentionClicked () { @@ -132,6 +146,11 @@ export default { this.close() await setAccountMuted(accountId, !muting, true) }, + async onShowReblogsClicked () { + let { accountId, showingReblogs } = this.get() + this.close() + await setShowReblogs(accountId, !showingReblogs, true) + }, async onCopyClicked () { let { account } = this.get() let { url } = account diff --git a/routes/_database/accountsAndRelationships.js b/routes/_database/accounts.js similarity index 73% rename from routes/_database/accountsAndRelationships.js rename to routes/_database/accounts.js index aad00768..5f36e84a 100644 --- a/routes/_database/accountsAndRelationships.js +++ b/routes/_database/accounts.js @@ -1,7 +1,5 @@ -import { - ACCOUNTS_STORE, RELATIONSHIPS_STORE, USERNAME_LOWERCASE -} from './constants' -import { accountsCache, relationshipsCache } from './cache' +import { ACCOUNTS_STORE, USERNAME_LOWERCASE } from './constants' +import { accountsCache } from './cache' import { cloneForStorage, getGenericEntityWithId, setGenericEntityWithId } from './helpers' import { dbPromise, getDatabase } from './databaseLifecycle' import { createAccountUsernamePrefixKeyRange } from './keys' @@ -14,14 +12,6 @@ export async function setAccount (instanceName, account) { return setGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, cloneForStorage(account)) } -export async function getRelationship (instanceName, accountId) { - return getGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, accountId) -} - -export async function setRelationship (instanceName, relationship) { - return setGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, cloneForStorage(relationship)) -} - export async function searchAccountsByUsername (instanceName, usernamePrefix, limit = 20) { const db = await getDatabase(instanceName) return dbPromise(db, ACCOUNTS_STORE, 'readonly', (accountsStore, callback) => { diff --git a/routes/_database/relationships.js b/routes/_database/relationships.js new file mode 100644 index 00000000..0ce79aa6 --- /dev/null +++ b/routes/_database/relationships.js @@ -0,0 +1,11 @@ +import { cloneForStorage, getGenericEntityWithId, setGenericEntityWithId } from './helpers' +import { RELATIONSHIPS_STORE } from './constants' +import { relationshipsCache } from './cache' + +export async function getRelationship (instanceName, accountId) { + return getGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, accountId) +} + +export async function setRelationship (instanceName, relationship) { + return setGenericEntityWithId(RELATIONSHIPS_STORE, relationshipsCache, instanceName, cloneForStorage(relationship)) +} diff --git a/routes/_store/observers/autosuggestObservers.js b/routes/_store/observers/autosuggestObservers.js index b2729274..a39aa0b6 100644 --- a/routes/_store/observers/autosuggestObservers.js +++ b/routes/_store/observers/autosuggestObservers.js @@ -1,6 +1,6 @@ import { searchAccountsByUsername as searchAccountsByUsernameInDatabase -} from '../../_database/accountsAndRelationships' +} from '../../_database/accounts' const SEARCH_RESULTS_LIMIT = 4 const DATABASE_SEARCH_RESULTS_LIMIT = 30