From 95665f6d74a47d2a55d9ec39a627c8411865a832 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 26 Aug 2018 14:16:00 -0700 Subject: [PATCH] add domain blocking (#496) * add domain blocking fixes another thing from #6 * show "domain blocking" on profile page --- routes/_actions/accounts.js | 14 +++++++--- routes/_actions/setDomainBlocked.js | 26 ++++++++++++++++++ routes/_api/blockDomain.js | 12 +++++++++ .../AccountProfileOptionsDialog.html | 27 ++++++++++++++++--- .../profile/AccountProfileHeader.html | 10 ++++++- 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 routes/_actions/setDomainBlocked.js create mode 100644 routes/_api/blockDomain.js diff --git a/routes/_actions/accounts.js b/routes/_actions/accounts.js index 5fdcd7e..4bffe9e 100644 --- a/routes/_actions/accounts.js +++ b/routes/_actions/accounts.js @@ -9,7 +9,7 @@ import { } from '../_database/relationships' import { store } from '../_store/store' -async function updateAccount (accountId, instanceName, accessToken) { +async function _updateAccount (accountId, instanceName, accessToken) { let localPromise = getAccountFromDatabase(instanceName, accountId) let remotePromise = getAccount(instanceName, accessToken, accountId).then(account => { /* no await */ setAccountInDatabase(instanceName, account) @@ -28,7 +28,7 @@ async function updateAccount (accountId, instanceName, accessToken) { } } -async function updateRelationship (accountId, instanceName, accessToken) { +async function _updateRelationship (accountId, instanceName, accessToken) { let localPromise = getRelationshipFromDatabase(instanceName, accountId) let remotePromise = getRelationship(instanceName, accessToken, accountId).then(relationship => { /* no await */ setRelationshipInDatabase(instanceName, relationship) @@ -66,7 +66,13 @@ export async function updateProfileAndRelationship (accountId) { let { currentInstance, accessToken } = store.get() await Promise.all([ - updateAccount(accountId, currentInstance, accessToken), - updateRelationship(accountId, currentInstance, accessToken) + _updateAccount(accountId, currentInstance, accessToken), + _updateRelationship(accountId, currentInstance, accessToken) ]) } + +export async function updateRelationship (accountId) { + let { currentInstance, accessToken } = store.get() + + await _updateRelationship(accountId, currentInstance, accessToken) +} diff --git a/routes/_actions/setDomainBlocked.js b/routes/_actions/setDomainBlocked.js new file mode 100644 index 0000000..4306a27 --- /dev/null +++ b/routes/_actions/setDomainBlocked.js @@ -0,0 +1,26 @@ +import { store } from '../_store/store' +import { blockDomain, unblockDomain } from '../_api/blockDomain' +import { toast } from '../_utils/toast' +import { updateRelationship } from './accounts' + +export async function setDomainBlocked (accountId, domain, block, toastOnSuccess) { + let { currentInstance, accessToken } = store.get() + try { + if (block) { + await blockDomain(currentInstance, accessToken, domain) + } else { + await unblockDomain(currentInstance, accessToken, domain) + } + await updateRelationship(accountId) + if (toastOnSuccess) { + if (block) { + toast.say(`Hiding ${domain}`) + } else { + toast.say(`Unhiding ${domain}`) + } + } + } catch (e) { + console.error(e) + toast.say(`Unable to ${block ? 'hide' : 'unhide'} domain: ` + (e.message || '')) + } +} diff --git a/routes/_api/blockDomain.js b/routes/_api/blockDomain.js new file mode 100644 index 0000000..db7346e --- /dev/null +++ b/routes/_api/blockDomain.js @@ -0,0 +1,12 @@ +import { post, WRITE_TIMEOUT, paramsString, del } from '../_utils/ajax' +import { auth, basename } from './utils' + +export async function blockDomain (instanceName, accessToken, domain) { + let url = `${basename(instanceName)}/api/v1/domain_blocks?${paramsString({ domain })}` + return post(url, null, auth(accessToken), {timeout: WRITE_TIMEOUT}) +} + +export async function unblockDomain (instanceName, accessToken, domain) { + let url = `${basename(instanceName)}/api/v1/domain_blocks?${paramsString({ domain })}` + return del(url, auth(accessToken), {timeout: WRITE_TIMEOUT}) +} diff --git a/routes/_components/dialog/components/AccountProfileOptionsDialog.html b/routes/_components/dialog/components/AccountProfileOptionsDialog.html index 2e4f833..5f41597 100644 --- a/routes/_components/dialog/components/AccountProfileOptionsDialog.html +++ b/routes/_components/dialog/components/AccountProfileOptionsDialog.html @@ -19,6 +19,7 @@ import { setAccountBlocked } from '../../../_actions/block' import { setAccountMuted } from '../../../_actions/mute' import { setAccountFollowed } from '../../../_actions/follow' import { setShowReblogs } from '../../../_actions/setShowReblogs' +import { setDomainBlocked } from '../../../_actions/setDomainBlocked' export default { oncreate, @@ -66,10 +67,18 @@ export default { ? `Hide boosts from @${acct}` : `Show boosts from @${acct}` ), + domain: ({ acct }) => acct.split('@')[1], + blockingDomain: ({ relationship }) => !!relationship.domain_blocking, + blockDomainLabel: ({ blockingDomain, domain }) => ( + blockingDomain + ? `Unhide ${domain}` + : `Hide ${domain}` + ), items: ({ blockLabel, blocking, blockIcon, muteLabel, muteIcon, followLabel, followIcon, following, followRequested, - accountId, verifyCredentialsId, acct, isUser, showReblogsLabel + accountId, verifyCredentialsId, acct, isUser, showReblogsLabel, + domain, blockDomainLabel }) => ([ !isUser && { key: 'mention', @@ -96,6 +105,11 @@ export default { label: showReblogsLabel, icon: '#fa-retweet' }, + !isUser && domain && { + key: 'blockDomain', + label: blockDomainLabel, + icon: '#fa-ban' + }, { key: 'copy', label: 'Copy link to account', @@ -116,10 +130,12 @@ export default { return this.onBlockClicked() case 'mute': return this.onMuteClicked() - case 'copy': - return this.onCopyClicked() case 'showReblogs': return this.onShowReblogsClicked() + case 'blockDomain': + return this.onBlockDomainClicked() + case 'copy': + return this.onCopyClicked() } }, async onMentionClicked () { @@ -151,6 +167,11 @@ export default { this.close() await setShowReblogs(accountId, !showingReblogs, true) }, + async onBlockDomainClicked () { + let { accountId, domain, blockingDomain } = this.get() + this.close() + await setDomainBlocked(accountId, domain, !blockingDomain, true) + }, async onCopyClicked () { let { account } = this.get() let { url } = account diff --git a/routes/_components/profile/AccountProfileHeader.html b/routes/_components/profile/AccountProfileHeader.html index 73c536e..4249933 100644 --- a/routes/_components/profile/AccountProfileHeader.html +++ b/routes/_components/profile/AccountProfileHeader.html @@ -20,7 +20,14 @@
{#if relationship && relationship.blocking} - {:elseif relationship && relationship.followed_by} + {/if} + {#if relationship && relationship.domain_blocking} + + {/if} + {#if relationship && relationship.muting} + + {/if} + {#if relationship && relationship.followed_by} {/if}
@@ -31,6 +38,7 @@ text-transform: uppercase; color: var(--deemphasized-text-color); font-size: 0.8em; + white-space: nowrap; } .account-profile-followed-by-span { background: rgba(30, 30, 30, 0.2);