Skip to content

Commit

Permalink
Add account subscribe support to WebUI
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed Feb 27, 2020
1 parent 12988e1 commit 7646adf
Show file tree
Hide file tree
Showing 50 changed files with 662 additions and 100 deletions.
1 change: 1 addition & 0 deletions app/chewy/accounts_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AccountsIndex < Chewy::Index

field :following_count, type: 'long', value: ->(account) { account.following.local.count }
field :followers_count, type: 'long', value: ->(account) { account.followers.local.count }
field :subscribing_count, type: 'long', value: ->(account) { account.subscribing.local.count }
field :last_status_at, type: 'date', value: ->(account) { account.last_status_at || account.created_at }
end
end
Expand Down
43 changes: 0 additions & 43 deletions app/controllers/api/v1/account_subscribes_controller.rb

This file was deleted.

64 changes: 64 additions & 0 deletions app/controllers/api/v1/accounts/subscribing_accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

class Api::V1::Accounts::SubscribingAccountsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:accounts' }
before_action :require_user!
after_action :insert_pagination_headers

respond_to :json

def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
end

private

def load_accounts
default_accounts.merge(paginated_subscribings).to_a
end

def default_accounts
Account.includes(:passive_subscribes, :account_stat).references(:passive_subscribes)
end

def paginated_subscribings
AccountSubscribe.where(account_id: current_user.account_id).paginate_by_max_id(
limit_param(DEFAULT_ACCOUNTS_LIMIT),
params[:max_id],
params[:since_id]
)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def next_path
if records_continue?
api_v1_accounts_subscribing_index_url pagination_params(max_id: pagination_max_id)
end
end

def prev_path
unless @accounts.empty?
api_v1_accounts_subscribing_index_url pagination_params(since_id: pagination_since_id)
end
end

def pagination_max_id
@accounts.last.passive_subscribes.first.id
end

def pagination_since_id
@accounts.first.passive_subscribes.first.id
end

def records_continue?
@accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
end

def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end
end
14 changes: 12 additions & 2 deletions app/controllers/api/v1/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class Api::V1::AccountsController < Api::BaseController
before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :block, :unblock, :mute, :unmute]
before_action -> { doorkeeper_authorize! :follow, :'write:follows' }, only: [:follow, :unfollow]
before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :subscribe, :unsubscribe, :block, :unblock, :mute, :unmute]
before_action -> { doorkeeper_authorize! :follow, :'write:follows' }, only: [:follow, :unfollow, :subscribe, :unsubscribe]
before_action -> { doorkeeper_authorize! :follow, :'write:mutes' }, only: [:mute, :unmute]
before_action -> { doorkeeper_authorize! :follow, :'write:blocks' }, only: [:block, :unblock]
before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:create]
Expand Down Expand Up @@ -38,6 +38,11 @@ def follow
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(options)
end

def subscribe
AccountSubscribeService.new.call(current_user.account, @account)
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end

def block
BlockService.new.call(current_user.account, @account)
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
Expand All @@ -53,6 +58,11 @@ def unfollow
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end

def unsubscribe
UnsubscribeAccountService.new.call(current_user.account, @account)
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
end

def unblock
UnblockService.new.call(current_user.account, @account)
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
Expand Down
1 change: 1 addition & 0 deletions app/controllers/settings/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def user_settings_params
:setting_default_sensitive,
:setting_default_language,
:setting_unfollow_modal,
:setting_unsubscribe_modal,
:setting_boost_modal,
:setting_delete_modal,
:setting_auto_play_gif,
Expand Down
181 changes: 181 additions & 0 deletions app/javascript/mastodon/actions/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export const ACCOUNT_UNFOLLOW_REQUEST = 'ACCOUNT_UNFOLLOW_REQUEST';
export const ACCOUNT_UNFOLLOW_SUCCESS = 'ACCOUNT_UNFOLLOW_SUCCESS';
export const ACCOUNT_UNFOLLOW_FAIL = 'ACCOUNT_UNFOLLOW_FAIL';

export const ACCOUNT_SUBSCRIBE_REQUEST = 'ACCOUNT_SUBSCRIBE_REQUEST';
export const ACCOUNT_SUBSCRIBE_SUCCESS = 'ACCOUNT_SUBSCRIBE_SUCCESS';
export const ACCOUNT_SUBSCRIBE_FAIL = 'ACCOUNT_SUBSCRIBE_FAIL';

export const ACCOUNT_UNSUBSCRIBE_REQUEST = 'ACCOUNT_UNSUBSCRIBE_REQUEST';
export const ACCOUNT_UNSUBSCRIBE_SUCCESS = 'ACCOUNT_UNSUBSCRIBE_SUCCESS';
export const ACCOUNT_UNSUBSCRIBE_FAIL = 'ACCOUNT_UNSUBSCRIBE_FAIL';

export const ACCOUNT_BLOCK_REQUEST = 'ACCOUNT_BLOCK_REQUEST';
export const ACCOUNT_BLOCK_SUCCESS = 'ACCOUNT_BLOCK_SUCCESS';
export const ACCOUNT_BLOCK_FAIL = 'ACCOUNT_BLOCK_FAIL';
Expand Down Expand Up @@ -54,6 +62,14 @@ export const FOLLOWING_EXPAND_REQUEST = 'FOLLOWING_EXPAND_REQUEST';
export const FOLLOWING_EXPAND_SUCCESS = 'FOLLOWING_EXPAND_SUCCESS';
export const FOLLOWING_EXPAND_FAIL = 'FOLLOWING_EXPAND_FAIL';

export const SUBSCRIBING_FETCH_REQUEST = 'SUBSCRIBING_FETCH_REQUEST';
export const SUBSCRIBING_FETCH_SUCCESS = 'SUBSCRIBING_FETCH_SUCCESS';
export const SUBSCRIBING_FETCH_FAIL = 'SUBSCRIBING_FETCH_FAIL';

export const SUBSCRIBING_EXPAND_REQUEST = 'SUBSCRIBING_EXPAND_REQUEST';
export const SUBSCRIBING_EXPAND_SUCCESS = 'SUBSCRIBING_EXPAND_SUCCESS';
export const SUBSCRIBING_EXPAND_FAIL = 'SUBSCRIBING_EXPAND_FAIL';

export const RELATIONSHIPS_FETCH_REQUEST = 'RELATIONSHIPS_FETCH_REQUEST';
export const RELATIONSHIPS_FETCH_SUCCESS = 'RELATIONSHIPS_FETCH_SUCCESS';
export const RELATIONSHIPS_FETCH_FAIL = 'RELATIONSHIPS_FETCH_FAIL';
Expand Down Expand Up @@ -221,6 +237,85 @@ export function unfollowAccountFail(error) {
};
};

export function subscribeAccount(id, reblogs = true) {
return (dispatch, getState) => {
const alreadySubscribe = getState().getIn(['relationships', id, 'subscribing']);
const locked = getState().getIn(['accounts', id, 'locked'], false);

dispatch(subscribeAccountRequest(id, locked));

api(getState).post(`/api/v1/accounts/${id}/subscribe`).then(response => {
dispatch(subscribeAccountSuccess(response.data, alreadySubscribe));
}).catch(error => {
dispatch(subscribeAccountFail(error, locked));
});
};
};

export function unsubscribeAccount(id) {
return (dispatch, getState) => {
dispatch(unsubscribeAccountRequest(id));

api(getState).post(`/api/v1/accounts/${id}/unsubscribe`).then(response => {
dispatch(unsubscribeAccountSuccess(response.data, getState().get('statuses')));
}).catch(error => {
dispatch(unsubscribeAccountFail(error));
});
};
};

export function subscribeAccountRequest(id, locked) {
return {
type: ACCOUNT_SUBSCRIBE_REQUEST,
id,
locked,
skipLoading: true,
};
};

export function subscribeAccountSuccess(relationship, alreadySubscribe) {
return {
type: ACCOUNT_SUBSCRIBE_SUCCESS,
relationship,
alreadySubscribe,
skipLoading: true,
};
};

export function subscribeAccountFail(error, locked) {
return {
type: ACCOUNT_SUBSCRIBE_FAIL,
error,
locked,
skipLoading: true,
};
};

export function unsubscribeAccountRequest(id) {
return {
type: ACCOUNT_UNSUBSCRIBE_REQUEST,
id,
skipLoading: true,
};
};

export function unsubscribeAccountSuccess(relationship, statuses) {
return {
type: ACCOUNT_UNSUBSCRIBE_SUCCESS,
relationship,
statuses,
skipLoading: true,
};
};

export function unsubscribeAccountFail(error) {
return {
type: ACCOUNT_UNSUBSCRIBE_FAIL,
error,
skipLoading: true,
};
};

export function blockAccount(id) {
return (dispatch, getState) => {
dispatch(blockAccountRequest(id));
Expand Down Expand Up @@ -531,6 +626,92 @@ export function expandFollowingFail(id, error) {
};
};

export function fetchSubscribing(id) {
return (dispatch, getState) => {
dispatch(fetchSubscribeRequest(id));

api(getState).get(`/api/v1/accounts/subscribing`).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');

dispatch(importFetchedAccounts(response.data));
dispatch(fetchSubscribeSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map(item => item.id)));
}).catch(error => {
dispatch(fetchSubscribeFail(id, error));
});
};
};

export function fetchSubscribeRequest(id) {
return {
type: SUBSCRIBING_FETCH_REQUEST,
id,
};
};

export function fetchSubscribeSuccess(id, accounts, next) {
return {
type: SUBSCRIBING_FETCH_SUCCESS,
id,
accounts,
next,
};
};

export function fetchSubscribeFail(id, error) {
return {
type: SUBSCRIBING_FETCH_FAIL,
id,
error,
};
};

export function expandSubscribing(id) {
return (dispatch, getState) => {
const url = getState().getIn(['user_lists', 'subscribing', id, 'next']);

if (url === null) {
return;
}

dispatch(expandSubscribeRequest(id));

api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');

dispatch(importFetchedAccounts(response.data));
dispatch(expandSubscribeSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map(item => item.id)));
}).catch(error => {
dispatch(expandSubscribeFail(id, error));
});
};
};

export function expandSubscribeRequest(id) {
return {
type: SUBSCRIBING_EXPAND_REQUEST,
id,
};
};

export function expandSubscribeSuccess(id, accounts, next) {
return {
type: SUBSCRIBING_EXPAND_SUCCESS,
id,
accounts,
next,
};
};

export function expandSubscribeFail(id, error) {
return {
type: SUBSCRIBING_EXPAND_FAIL,
id,
error,
};
};

export function fetchRelationships(accountIds) {
return (dispatch, getState) => {
const loadedRelationships = getState().get('relationships');
Expand Down
Loading

0 comments on commit 7646adf

Please sign in to comment.