diff options
author | Anthony Bellew <anthonyreflected@gmail.com> | 2017-01-03 11:51:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-03 11:51:35 -0700 |
commit | de154dbd5dbb04d083f5a66dc288d9fef7006c01 (patch) | |
tree | 88a1715021c33add55501351dbf74950deeac481 /app/controllers | |
parent | c3e9ba6a66bfcb9e33edebc73adea0a17d7f02a6 (diff) | |
parent | e6657d7342ee1ce08973bf1e5ab89e423eb8b760 (diff) |
Merge pull request #1 from Gargron/master
Catchup merge
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/api/v1/blocks_controller.rb | 21 | ||||
-rw-r--r-- | app/controllers/api/v1/favourites_controller.rb | 21 | ||||
-rw-r--r-- | app/controllers/api/v1/follow_requests_controller.rb | 29 | ||||
-rw-r--r-- | app/controllers/api/v1/notifications_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/application_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/authorize_follow_controller.rb | 45 | ||||
-rw-r--r-- | app/controllers/follow_requests_controller.rb | 28 | ||||
-rw-r--r-- | app/controllers/remote_follow_controller.rb | 47 | ||||
-rw-r--r-- | app/controllers/settings/preferences_controller.rb | 11 |
9 files changed, 172 insertions, 36 deletions
diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb new file mode 100644 index 000000000..8629242ab --- /dev/null +++ b/app/controllers/api/v1/blocks_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class Api::V1::BlocksController < ApiController + before_action -> { doorkeeper_authorize! :follow } + before_action :require_user! + + respond_to :json + + def index + results = Block.where(account: current_account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) + accounts = Account.where(id: results.map(&:target_account_id)).map { |a| [a.id, a] }.to_h + @accounts = results.map { |f| accounts[f.target_account_id] } + + set_account_counters_maps(@accounts) + + next_path = api_v1_blocks_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT + prev_path = api_v1_blocks_url(since_id: results.first.id) unless results.empty? + + set_pagination_headers(next_path, prev_path) + end +end diff --git a/app/controllers/api/v1/favourites_controller.rb b/app/controllers/api/v1/favourites_controller.rb new file mode 100644 index 000000000..a71592acd --- /dev/null +++ b/app/controllers/api/v1/favourites_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class Api::V1::FavouritesController < ApiController + before_action -> { doorkeeper_authorize! :read } + before_action :require_user! + + respond_to :json + + def index + results = Favourite.where(account: current_account).paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]) + @statuses = cache_collection(Status.where(id: results.map(&:status_id)), Status) + + set_maps(@statuses) + set_counters_maps(@statuses) + + next_path = api_v1_favourites_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT + prev_path = api_v1_favourites_url(since_id: results.first.id) unless results.empty? + + set_pagination_headers(next_path, prev_path) + end +end diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb new file mode 100644 index 000000000..a30e97e71 --- /dev/null +++ b/app/controllers/api/v1/follow_requests_controller.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class Api::V1::FollowRequestsController < ApiController + before_action -> { doorkeeper_authorize! :follow } + before_action :require_user! + + def index + results = FollowRequest.where(target_account: current_account).paginate_by_max_id(DEFAULT_ACCOUNTS_LIMIT, params[:max_id], params[:since_id]) + accounts = Account.where(id: results.map(&:account_id)).map { |a| [a.id, a] }.to_h + @accounts = results.map { |f| accounts[f.account_id] } + + set_account_counters_maps(@accounts) + + next_path = api_v1_follow_requests_url(max_id: results.last.id) if results.size == DEFAULT_ACCOUNTS_LIMIT + prev_path = api_v1_follow_requests_url(since_id: results.first.id) unless results.empty? + + set_pagination_headers(next_path, prev_path) + end + + def authorize + FollowRequest.find_by!(account_id: params[:id], target_account: current_account).authorize! + render_empty + end + + def reject + FollowRequest.find_by!(account_id: params[:id], target_account: current_account).reject! + render_empty + end +end diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index a24e0beb7..c8f162cb0 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -7,7 +7,7 @@ class Api::V1::NotificationsController < ApiController respond_to :json def index - @notifications = Notification.where(account: current_account).paginate_by_max_id(20, params[:max_id], params[:since_id]) + @notifications = Notification.where(account: current_account).browserable.paginate_by_max_id(20, params[:max_id], params[:since_id]) @notifications = cache_collection(@notifications, Notification) statuses = @notifications.select { |n| !n.target_status.nil? }.map(&:target_status) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e2d879d58..0a6b50a29 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -49,13 +49,13 @@ class ApplicationController < ActionController::Base def not_found respond_to do |format| - format.any { head 404 } + format.any { head 404 } end end def gone respond_to do |format| - format.any { head 410 } + format.any { head 410 } end end diff --git a/app/controllers/authorize_follow_controller.rb b/app/controllers/authorize_follow_controller.rb new file mode 100644 index 000000000..e866b5599 --- /dev/null +++ b/app/controllers/authorize_follow_controller.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class AuthorizeFollowController < ApplicationController + layout 'public' + + before_action :authenticate_user! + + def new + uri = Addressable::URI.parse(acct_param) + + if uri.path && %w(http https).include?(uri.scheme) + set_account_from_url + else + set_account_from_acct + end + + render :error if @account.nil? + end + + def create + @account = FollowService.new.call(current_account, acct_param).try(:target_account) + + if @account.nil? + render :error + else + redirect_to web_url("accounts/#{@account.id}") + end + rescue ActiveRecord::RecordNotFound, Mastodon::NotPermitted + render :error + end + + private + + def set_account_from_url + @account = FetchRemoteAccountService.new.call(acct_param) + end + + def set_account_from_acct + @account = FollowRemoteAccountService.new.call(acct_param) + end + + def acct_param + params[:acct].gsub(/\Aacct:/, '') + end +end diff --git a/app/controllers/follow_requests_controller.rb b/app/controllers/follow_requests_controller.rb deleted file mode 100644 index d4368f773..000000000 --- a/app/controllers/follow_requests_controller.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -class FollowRequestsController < ApplicationController - layout 'auth' - - before_action :authenticate_user! - before_action :set_follow_request, except: :index - - def index - @follow_requests = FollowRequest.where(target_account: current_account) - end - - def authorize - @follow_request.authorize! - redirect_to follow_requests_path - end - - def reject - @follow_request.reject! - redirect_to follow_requests_path - end - - private - - def set_follow_request - @follow_request = FollowRequest.find(params[:id]) - end -end diff --git a/app/controllers/remote_follow_controller.rb b/app/controllers/remote_follow_controller.rb new file mode 100644 index 000000000..7d4bfe6ce --- /dev/null +++ b/app/controllers/remote_follow_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class RemoteFollowController < ApplicationController + layout 'public' + + before_action :set_account + before_action :check_account_suspension + + def new + @remote_follow = RemoteFollow.new + end + + def create + @remote_follow = RemoteFollow.new(resource_params) + + if @remote_follow.valid? + resource = Goldfinger.finger("acct:#{@remote_follow.acct}") + redirect_url_link = resource&.link('http://ostatus.org/schema/1.0/subscribe') + + if redirect_url_link.nil? || redirect_url_link.template.nil? + @remote_follow.errors.add(:acct, I18n.t('remote_follow.missing_resource')) + render(:new) && return + end + + redirect_to Addressable::Template.new(redirect_url_link.template).expand(uri: "#{@account.username}@#{Rails.configuration.x.local_domain}").to_s + else + render :new + end + rescue Goldfinger::Error + @remote_follow.errors.add(:acct, I18n.t('remote_follow.missing_resource')) + render :new + end + + private + + def resource_params + params.require(:remote_follow).permit(:acct) + end + + def set_account + @account = Account.find_local!(params[:account_username]) + end + + def check_account_suspension + head 410 if @account.suspended? + end +end diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 692cf95ac..3b6d109a6 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -8,10 +8,11 @@ class Settings::PreferencesController < ApplicationController def show; end def update - current_user.settings(:notification_emails).follow = user_params[:notification_emails][:follow] == '1' - current_user.settings(:notification_emails).reblog = user_params[:notification_emails][:reblog] == '1' - current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1' - current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1' + current_user.settings(:notification_emails).follow = user_params[:notification_emails][:follow] == '1' + current_user.settings(:notification_emails).follow_request = user_params[:notification_emails][:follow_request] == '1' + current_user.settings(:notification_emails).reblog = user_params[:notification_emails][:reblog] == '1' + current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1' + current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1' current_user.settings(:interactions).must_be_follower = user_params[:interactions][:must_be_follower] == '1' current_user.settings(:interactions).must_be_following = user_params[:interactions][:must_be_following] == '1' @@ -26,6 +27,6 @@ class Settings::PreferencesController < ApplicationController private def user_params - params.require(:user).permit(:locale, notification_emails: [:follow, :reblog, :favourite, :mention], interactions: [:must_be_follower, :must_be_following]) + params.require(:user).permit(:locale, notification_emails: [:follow, :follow_request, :reblog, :favourite, :mention], interactions: [:must_be_follower, :must_be_following]) end end |