From ffb00fcc5c27266739381133e53b015172869054 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 28 Apr 2018 14:19:39 -0700 Subject: [PATCH] approve/reject follow requests, unblock, unmute (#230) * approve/reject follow requests, unblock, unmute * make tests less flaky --- CONTRIBUTING.md | 3 +- package.json | 1 + routes/_actions/block.js | 2 + routes/_actions/mute.js | 2 + routes/_actions/requests.js | 29 +++++++ routes/_api/requests.js | 12 +++ routes/_components/AccountsListPage.html | 25 ++++-- .../_components/community/PageListItem.html | 2 +- .../search/AccountSearchResult.html | 46 ++++++++++- routes/_pages/blocked.html | 12 ++- routes/_pages/muted.html | 12 ++- routes/_pages/requests.html | 20 ++++- tests/roles.js | 7 +- tests/serverActions.js | 9 +++ tests/spec/116-follow-requests.js | 76 +++++++++++++++++++ tests/users.js | 6 ++ tests/utils.js | 4 + 17 files changed, 250 insertions(+), 18 deletions(-) create mode 100644 routes/_actions/requests.js create mode 100644 routes/_api/requests.js create mode 100644 tests/spec/116-follow-requests.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 28c57a0..108eaba 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,8 +31,7 @@ Lint: Automatically fix most linting issues: - npx standard --fix - npx standard --fix --plugin html 'routes/**/*.html' + npm run lint-fix ## Testing diff --git a/package.json b/package.json index f104533..18ac3c1 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.2.3", "scripts": { "lint": "standard && standard --plugin html 'routes/**/*.html'", + "lint-fix": "standard --fix && standard --fix --plugin html 'routes/**/*.html'", "dev": "run-s build-svg build-inline-script serve-dev", "serve-dev": "run-p --race build-sass-watch serve", "serve": "node server.js", diff --git a/routes/_actions/block.js b/routes/_actions/block.js index 850cce0..b6fb30f 100644 --- a/routes/_actions/block.js +++ b/routes/_actions/block.js @@ -2,6 +2,7 @@ import { store } from '../_store/store' import { blockAccount, unblockAccount } from '../_api/block' import { toast } from '../_utils/toast' import { updateProfileAndRelationship } from './accounts' +import { emit } from '../_utils/eventBus' export async function setAccountBlocked (accountId, block, toastOnSuccess) { let { currentInstance, accessToken } = store.get() @@ -19,6 +20,7 @@ export async function setAccountBlocked (accountId, block, toastOnSuccess) { toast.say('Unblocked account') } } + emit('refreshAccountsList') } catch (e) { console.error(e) toast.say(`Unable to ${block ? 'block' : 'unblock'} account: ` + (e.message || '')) diff --git a/routes/_actions/mute.js b/routes/_actions/mute.js index ff6571f..7b28541 100644 --- a/routes/_actions/mute.js +++ b/routes/_actions/mute.js @@ -2,6 +2,7 @@ import { store } from '../_store/store' import { muteAccount, unmuteAccount } from '../_api/mute' import { toast } from '../_utils/toast' import { updateProfileAndRelationship } from './accounts' +import { emit } from '../_utils/eventBus' export async function setAccountMuted (accountId, mute, toastOnSuccess) { let { currentInstance, accessToken } = store.get() @@ -19,6 +20,7 @@ export async function setAccountMuted (accountId, mute, toastOnSuccess) { toast.say('Unmuted account') } } + emit('refreshAccountsList') } catch (e) { console.error(e) toast.say(`Unable to ${mute ? 'mute' : 'unmute'} account: ` + (e.message || '')) diff --git a/routes/_actions/requests.js b/routes/_actions/requests.js new file mode 100644 index 0000000..6a9d1f3 --- /dev/null +++ b/routes/_actions/requests.js @@ -0,0 +1,29 @@ +import { store } from '../_store/store' +import { approveFollowRequest, rejectFollowRequest } from '../_api/requests' +import { emit } from '../_utils/eventBus' +import { toast } from '../_utils/toast' + +export async function setFollowRequestApprovedOrRejected (accountId, approved, toastOnSuccess) { + let { + currentInstance, + accessToken + } = store.get() + try { + if (approved) { + await approveFollowRequest(currentInstance, accessToken, accountId) + } else { + await rejectFollowRequest(currentInstance, accessToken, accountId) + } + if (toastOnSuccess) { + if (approved) { + toast.say('Approved follow request') + } else { + toast.say('Rejected follow request') + } + } + emit('refreshAccountsList') + } catch (e) { + console.error(e) + toast.say(`Unable to ${approved ? 'approve' : 'reject'} account: ` + (e.message || '')) + } +} diff --git a/routes/_api/requests.js b/routes/_api/requests.js new file mode 100644 index 0000000..394729e --- /dev/null +++ b/routes/_api/requests.js @@ -0,0 +1,12 @@ +import { postWithTimeout } from '../_utils/ajax' +import { auth, basename } from './utils' + +export async function approveFollowRequest (instanceName, accessToken, accountId) { + let url = `${basename(instanceName)}/api/v1/follow_requests/${accountId}/authorize` + return postWithTimeout(url, null, auth(accessToken)) +} + +export async function rejectFollowRequest (instanceName, accessToken, accountId) { + let url = `${basename(instanceName)}/api/v1/follow_requests/${accountId}/reject` + return postWithTimeout(url, null, auth(accessToken)) +} diff --git a/routes/_components/AccountsListPage.html b/routes/_components/AccountsListPage.html index 3d9a784..c969e85 100644 --- a/routes/_components/AccountsListPage.html +++ b/routes/_components/AccountsListPage.html @@ -4,7 +4,11 @@ {{elseif accounts && accounts.length}} {{/if}} @@ -31,19 +35,19 @@ import LoadingPage from '../_components/LoadingPage.html' import AccountSearchResult from '../_components/search/AccountSearchResult.html' import { toast } from '../_utils/toast' + import { on } from '../_utils/eventBus' + // TODO: paginate export default { async oncreate () { - let { accountsFetcher } = this.get() try { - // TODO: paginate - let accounts = await accountsFetcher() - this.set({ accounts: accounts }) + await this.refreshAccounts() } catch (e) { toast.say('Error: ' + (e.name || '') + ' ' + (e.message || '')) } finally { this.set({loading: false}) } + on('refreshAccountsList', this, () => this.refreshAccounts()) }, data: () => ({ loading: true, @@ -53,6 +57,17 @@ components: { LoadingPage, AccountSearchResult + }, + methods: { + onClickAction (event) { + let { action, accountId } = event + action.onclick(accountId) + }, + async refreshAccounts () { + let { accountsFetcher } = this.get() + let accounts = await accountsFetcher() + this.set({ accounts: accounts }) + } } } \ No newline at end of file diff --git a/routes/_components/community/PageListItem.html b/routes/_components/community/PageListItem.html index 7107c88..20e450e 100644 --- a/routes/_components/community/PageListItem.html +++ b/routes/_components/community/PageListItem.html @@ -9,7 +9,7 @@ {{#if pinnable}} {{/if}} diff --git a/routes/_components/search/AccountSearchResult.html b/routes/_components/search/AccountSearchResult.html index e8ec88d..2ece5c2 100644 --- a/routes/_components/search/AccountSearchResult.html +++ b/routes/_components/search/AccountSearchResult.html @@ -7,16 +7,28 @@ + {{#if actions && actions.length}} + + {{/if}} \ No newline at end of file diff --git a/routes/_pages/blocked.html b/routes/_pages/blocked.html index 1b577d9..07eec95 100644 --- a/routes/_pages/blocked.html +++ b/routes/_pages/blocked.html @@ -1,12 +1,22 @@ - +