Commiting

This commit is contained in:
mgabdev
2020-11-15 12:48:32 -06:00
parent 62515bbaee
commit fb612f60c8
1011 changed files with 3507 additions and 49604 deletions

View File

@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Api::BaseController < ApplicationController
DEFAULT_STATUSES_LIMIT = 20
DEFAULT_STATUSES_LIMIT = 18
DEFAULT_ACCOUNTS_LIMIT = 40
include RateLimitHeaders
@@ -77,8 +77,6 @@ class Api::BaseController < ApplicationController
# : todo : when figure out email/catpcha, put this back
# elsif !current_user.confirmed?
# render json: { error: 'Your login is missing a confirmed e-mail address' }, status: 403
elsif !current_user.approved?
render json: { error: 'Your login is currently pending approval' }, status: 403
else
set_user_activity
end

View File

@@ -1,7 +1,6 @@
# frozen_string_literal: true
class Api::OEmbedController < Api::BaseController
respond_to :json
def show
@status = status_finder.status

View File

@@ -3,7 +3,6 @@
class Api::ProofsController < Api::BaseController
before_action :set_account
before_action :set_provider
before_action :check_account_approval
before_action :check_account_suspension
def index
@@ -20,10 +19,6 @@ class Api::ProofsController < Api::BaseController
@account = Account.find_local!(params[:username])
end
def check_account_approval
not_found if @account.user_pending?
end
def check_account_suspension
gone if @account.suspended?
end

View File

@@ -1,73 +0,0 @@
# frozen_string_literal: true
class Api::PushController < Api::BaseController
include SignatureVerification
def update
response, status = process_push_request
render plain: response, status: status
end
private
def process_push_request
case hub_mode
when 'subscribe'
Pubsubhubbub::SubscribeService.new.call(account_from_topic, hub_callback, hub_secret, hub_lease_seconds, verified_domain)
when 'unsubscribe'
Pubsubhubbub::UnsubscribeService.new.call(account_from_topic, hub_callback)
else
["Unknown mode: #{hub_mode}", 422]
end
end
def hub_mode
params['hub.mode']
end
def hub_topic
params['hub.topic']
end
def hub_callback
params['hub.callback']
end
def hub_lease_seconds
params['hub.lease_seconds']
end
def hub_secret
params['hub.secret']
end
def account_from_topic
if hub_topic.present? && local_domain? && account_feed_path?
Account.find_local(hub_topic_params[:username])
end
end
def hub_topic_params
@_hub_topic_params ||= Rails.application.routes.recognize_path(hub_topic_uri.path)
end
def hub_topic_uri
@_hub_topic_uri ||= Addressable::URI.parse(hub_topic).normalize
end
def local_domain?
TagManager.instance.web_domain?(hub_topic_domain)
end
def verified_domain
return signed_request_account.domain if signed_request_account
end
def hub_topic_domain
hub_topic_uri.host + (hub_topic_uri.port ? ":#{hub_topic_uri.port}" : '')
end
def account_feed_path?
hub_topic_params[:controller] == 'accounts' && hub_topic_params[:action] == 'show' && hub_topic_params[:format] == 'atom'
end
end

View File

@@ -1,37 +0,0 @@
# frozen_string_literal: true
class Api::SalmonController < Api::BaseController
include SignatureVerification
before_action :set_account
respond_to :txt
def update
if verify_payload?
process_salmon
head 202
elsif payload.present?
render plain: signature_verification_failure_reason, status: 401
else
head 400
end
end
private
def set_account
@account = Account.find(params[:id])
end
def payload
@_payload ||= request.body.read
end
def verify_payload?
payload.present? && VerifySalmonService.new.call(payload)
end
def process_salmon
SalmonWorker.perform_async(@account.id, payload.force_encoding('UTF-8'))
end
end

View File

@@ -1,51 +0,0 @@
# frozen_string_literal: true
class Api::SubscriptionsController < Api::BaseController
before_action :set_account
respond_to :txt
def show
if subscription.valid?(params['hub.topic'])
@account.update(subscription_expires_at: future_expires)
render plain: encoded_challenge, status: 200
else
head 404
end
end
def update
if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
ProcessingWorker.perform_async(@account.id, body.force_encoding('UTF-8'))
end
head 200
end
private
def subscription
@_subscription ||= @account.subscription(
api_subscription_url(@account.id)
)
end
def body
@_body ||= request.body.read
end
def encoded_challenge
HTMLEntities.new.encode(params['hub.challenge'])
end
def future_expires
Time.now.utc + lease_seconds_or_default
end
def lease_seconds_or_default
(params['hub.lease_seconds'] || 1.day).to_i.seconds
end
def set_account
@account = Account.find(params[:id])
end
end

View File

@@ -5,8 +5,6 @@ class Api::V1::AccountByUsernameController < EmptyController
before_action :check_account_suspension
before_action :check_account_local
respond_to :json
def show
render json: @account, serializer: REST::AccountSerializer
end

View File

@@ -14,7 +14,6 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController
@account = current_account
UpdateAccountService.new.call(@account, account_params, raise_error: true)
UserSettingsDecorator.new(current_user).update(user_settings_params) if user_settings_params
ActivityPub::UpdateDistributionWorker.perform_async(@account.id)
render json: @account, serializer: REST::CredentialAccountSerializer
end

View File

@@ -5,8 +5,6 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
before_action :set_account
after_action :insert_pagination_headers
respond_to :json
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer

View File

@@ -5,8 +5,6 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
before_action :set_account
after_action :insert_pagination_headers
respond_to :json
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
@@ -25,6 +23,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
end
def hide_results?
# : todo : where tf is this?
(@account.user_hides_network? && current_account.id != @account.id) || (current_account && @account.blocking?(current_account))
end

View File

@@ -1,18 +0,0 @@
# frozen_string_literal: true
class Api::V1::Accounts::IdentityProofsController < Api::BaseController
before_action :set_account
respond_to :json
def index
@proofs = @account.identity_proofs.active
render json: @proofs, each_serializer: REST::IdentityProofSerializer
end
private
def set_account
@account = Account.find(params[:account_id])
end
end

View File

@@ -5,8 +5,6 @@ class Api::V1::Accounts::ListsController < Api::BaseController
before_action :require_user!
before_action :set_account
respond_to :json
def index
@lists = @account.lists.where(account: current_account)
render json: @lists, each_serializer: REST::ListSerializer

View File

@@ -1,32 +0,0 @@
# frozen_string_literal: true
class Api::V1::Accounts::PinsController < Api::BaseController
include Authorization
before_action -> { doorkeeper_authorize! :write, :'write:accounts' }
before_action :require_user!
before_action :set_account
respond_to :json
def create
AccountPin.create!(account: current_account, target_account: @account)
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter
end
def destroy
pin = AccountPin.find_by(account: current_account, target_account: @account)
pin&.destroy!
render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter
end
private
def set_account
@account = Account.find(params[:account_id])
end
def relationships_presenter
AccountRelationshipsPresenter.new([@account.id], current_user.account_id)
end
end

View File

@@ -4,8 +4,6 @@ class Api::V1::Accounts::RelationshipsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:follows' }
before_action :require_user!
respond_to :json
def index
accounts = Account.where(id: account_ids).select('id')
# .where doesn't guarantee that our results are in the same order

View File

@@ -1,26 +0,0 @@
# frozen_string_literal: true
class Api::V1::Accounts::SearchController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:accounts' }
before_action :require_user!
respond_to :json
def show
@accounts = account_search
render json: @accounts, each_serializer: REST::AccountSerializer
end
private
def account_search
AccountSearchService.new.call(
params[:q],
current_account,
limit: limit_param(DEFAULT_ACCOUNTS_LIMIT),
resolve: truthy_param?(:resolve),
following: truthy_param?(:following),
offset: params[:offset]
)
end
end

View File

@@ -5,11 +5,12 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
before_action :set_account
after_action :insert_pagination_headers
respond_to :json
def index
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
render json: @statuses,
each_serializer: REST::StatusSerializer,
account_id: params[:account_id],
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private

View File

@@ -10,9 +10,6 @@ class Api::V1::AccountsController < Api::BaseController
before_action :require_user!, except: [:show, :create]
before_action :set_account, except: [:create]
before_action :check_account_suspension, only: [:show]
before_action :check_enabled_registrations, only: [:create]
respond_to :json
def show
render json: @account, serializer: REST::AccountSerializer
@@ -78,12 +75,4 @@ class Api::V1::AccountsController < Api::BaseController
def account_params
params.permit(:username, :email, :password, :agreement, :locale)
end
def check_enabled_registrations
forbidden if single_user_mode? || !allowed_registrations?
end
def allowed_registrations?
Setting.registrations_mode != 'none'
end
end

View File

@@ -3,8 +3,6 @@
class Api::V1::Apps::CredentialsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read }
respond_to :json
def show
render json: doorkeeper_token.application, serializer: REST::ApplicationSerializer, fields: %i(name website vapid_key)
end

View File

@@ -5,8 +5,6 @@ class Api::V1::BlocksController < Api::BaseController
before_action :require_user!
after_action :insert_pagination_headers
respond_to :json
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer

View File

@@ -5,8 +5,6 @@ class Api::V1::BookmarksController < Api::BaseController
before_action :require_user!
after_action :insert_pagination_headers
respond_to :json
def index
@statuses = []
if current_account.is_pro

View File

@@ -1,71 +0,0 @@
# frozen_string_literal: true
class Api::V1::ConversationsController < Api::BaseController
LIMIT = 20
before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: :index
before_action -> { doorkeeper_authorize! :write, :'write:conversations' }, except: :index
before_action :require_user!
before_action :set_conversation, except: :index
after_action :insert_pagination_headers, only: :index
respond_to :json
def index
@conversations = paginated_conversations
render json: @conversations, each_serializer: REST::ConversationSerializer
end
def read
@conversation.update!(unread: false)
render json: @conversation, serializer: REST::ConversationSerializer
end
def destroy
@conversation.destroy!
render_empty
end
private
def set_conversation
@conversation = AccountConversation.where(account: current_account).find(params[:id])
end
def paginated_conversations
AccountConversation.where(account: current_account)
.paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
end
def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
def next_path
if records_continue?
api_v1_conversations_url pagination_params(max_id: pagination_max_id)
end
end
def prev_path
unless @conversations.empty?
api_v1_conversations_url pagination_params(min_id: pagination_since_id)
end
end
def pagination_max_id
@conversations.last.last_status_id
end
def pagination_since_id
@conversations.first.last_status_id
end
def records_continue?
@conversations.size == limit_param(LIMIT)
end
def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end
end

View File

@@ -1,8 +1,10 @@
# frozen_string_literal: true
class Api::V1::CustomEmojisController < EmptyController
def index
data = ActiveModelSerializers::SerializableResource.new(CustomEmoji.local.where(disabled: false), each_serializer: REST::CustomEmojiSerializer)
data = ActiveModelSerializers::SerializableResource.new(CustomEmoji.local, each_serializer: REST::CustomEmojiSerializer)
render json: data.to_json, content_type: 'application/json'
end
end

View File

@@ -1,78 +0,0 @@
# frozen_string_literal: true
class Api::V1::DomainBlocksController < Api::BaseController
BLOCK_LIMIT = 100
before_action -> { doorkeeper_authorize! :follow, :'read:blocks' }, only: :show
before_action -> { doorkeeper_authorize! :follow, :'write:blocks' }, except: :show
before_action :require_user!
after_action :insert_pagination_headers, only: :show
respond_to :json
def show
@blocks = load_domain_blocks
render json: @blocks.map(&:domain)
end
def create
current_account.block_domain!(domain_block_params[:domain])
AfterAccountDomainBlockWorker.perform_async(current_account.id, domain_block_params[:domain])
render_empty
end
def destroy
current_account.unblock_domain!(domain_block_params[:domain])
render_empty
end
private
def load_domain_blocks
account_domain_blocks.paginate_by_max_id(
limit_param(BLOCK_LIMIT),
params[:max_id],
params[:since_id]
)
end
def account_domain_blocks
current_account.domain_blocks
end
def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
def next_path
if records_continue?
api_v1_domain_blocks_url pagination_params(max_id: pagination_max_id)
end
end
def prev_path
unless @blocks.empty?
api_v1_domain_blocks_url pagination_params(since_id: pagination_since_id)
end
end
def pagination_max_id
@blocks.last.id
end
def pagination_since_id
@blocks.first.id
end
def records_continue?
@blocks.size == limit_param(BLOCK_LIMIT)
end
def pagination_params(core_params)
params.slice(:limit).permit(:limit).merge(core_params)
end
def domain_block_params
params.permit(:domain)
end
end

View File

@@ -1,72 +0,0 @@
# frozen_string_literal: true
class Api::V1::EndorsementsController < 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
if unlimited?
endorsed_accounts.all
else
endorsed_accounts.paginate_by_max_id(
limit_param(DEFAULT_ACCOUNTS_LIMIT),
params[:max_id],
params[:since_id]
)
end
end
def endorsed_accounts
current_account.endorsed_accounts.includes(:account_stat)
end
def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
def next_path
return if unlimited?
if records_continue?
api_v1_endorsements_url pagination_params(max_id: pagination_max_id)
end
end
def prev_path
return if unlimited?
unless @accounts.empty?
api_v1_endorsements_url pagination_params(since_id: pagination_since_id)
end
end
def pagination_max_id
@accounts.last.id
end
def pagination_since_id
@accounts.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
def unlimited?
params[:limit] == '0'
end
end

View File

@@ -5,8 +5,6 @@ class Api::V1::FavouritesController < Api::BaseController
before_action :require_user!
after_action :insert_pagination_headers
respond_to :json
def index
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)

View File

@@ -7,8 +7,6 @@ class Api::V1::FiltersController < Api::BaseController
before_action :set_filters, only: :index
before_action :set_filter, only: [:show, :update, :destroy]
respond_to :json
def index
render json: @filters, each_serializer: REST::FilterSerializer
end

View File

@@ -4,18 +4,11 @@ class Api::V1::FollowsController < Api::BaseController
before_action -> { doorkeeper_authorize! :follow, :'write:follows' }
before_action :require_user!
respond_to :json
def create
raise ActiveRecord::RecordNotFound if follow_params[:uri].blank?
@account = FollowService.new.call(current_user.account, target_uri).try(:target_account)
if @account.nil?
username, domain = target_uri.split('@')
@account = Account.find_remote!(username, domain)
end
render json: @account, serializer: REST::AccountSerializer
end

View File

@@ -1,91 +0,0 @@
# frozen_string_literal: true
class Api::V1::GabTrendsController < EmptyController
def index
if Rails.env != 'development'
render json: nil
end
type = params[:type]
if type == 'feed'
body = Redis.current.get("gabtrends:feed")
if body.nil? || body.empty?
Request.new(:get, "https://trends.gab.com/trend-feed/json").perform do |res|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
if res.code == 200
body = res.body_with_limit
Redis.current.set("gabtrends:feed", body)
Redis.current.expire("gabtrends:feed", 1.hour.seconds)
render json: body
else
render json: nil
end
end
else
render json: body
end
elsif type == 'partner'
body = Redis.current.get("gabtrends:partner")
if body.nil? || body.empty?
Request.new(:get, "https://trends.gab.com/partner").perform do |res|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
if res.code == 200
body = res.body_with_limit
Redis.current.set("gabtrends:partner", body)
Redis.current.expire("gabtrends:partner", 1.minute.seconds)
render json: body
else
render json: nil
end
end
else
render json: body
end
elsif type == 'news'
body = Redis.current.get("gabtrends:news")
if body.nil? || body.empty?
Request.new(:get, "https://news.gab.com/feed/json").perform do |res|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
if res.code == 200
body = res.body_with_limit
Redis.current.set("gabtrends:news", body)
Redis.current.expire("gabtrends:news", 1.minute.seconds)
render json: body
else
render json: nil
end
end
else
render json: body
end
elsif type == 'rss'
body = Redis.current.get("gabtrends:feeds")
if body.nil? || body.empty?
Request.new(:get, "https://trends.gab.com/feed/#{params[:feedId]}?fmt=json&p=#{params[:page]}").perform do |res|
Rails.logger.debug "GabTrendsController: #{type} endpoint res code: #{res.code.to_s}"
if res.code == 200
body = res.body_with_limit
Redis.current.set("gabtrends:news", body)
Redis.current.expire("gabtrends:news", 1.minute.seconds)
render json: body
else
render json: nil
end
end
else
render json: body
end
else
raise GabSocial::NotPermittedError
end
rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, HTTP::Error
Rails.logger.debug "Error fetching gabtrends feed: #{type}"
render json: nil
end
end

View File

@@ -7,8 +7,6 @@ class Api::V1::Groups::PasswordController < Api::BaseController
before_action :require_user!
before_action :set_group
respond_to :json
def create
authorize @group, :join?

View File

@@ -8,8 +8,6 @@ class Api::V1::Groups::PinsController < Api::BaseController
before_action :set_group
before_action :set_status
respond_to :json
def create
authorize @group, :update?
@@ -17,6 +15,10 @@ class Api::V1::Groups::PinsController < Api::BaseController
render json: @status, serializer: REST::StatusSerializer
end
def show
# is status pinned by user of group?
end
def destroy
authorize @group, :update?

View File

@@ -4,8 +4,6 @@ class Api::V1::Groups::RelationshipsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:groups' }
before_action :require_user!
respond_to :json
def index
groups = Group.where(id: group_ids, is_archived: false).select('id')
# .where doesn't guarantee that our results are in the same order

View File

@@ -7,8 +7,6 @@ class Api::V1::MediaController < Api::BaseController
include ObfuscateFilename
obfuscate_filename :file
respond_to :json
def create
@media = current_account.media_attachments.create!(account: current_account, file: media_params[:file], description: media_params[:description], focus: media_params[:focus])
render json: @media, serializer: REST::MediaAttachmentSerializer

View File

@@ -5,8 +5,6 @@ class Api::V1::MutesController < Api::BaseController
before_action :require_user!
after_action :insert_pagination_headers
respond_to :json
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer

View File

@@ -1,14 +1,12 @@
# frozen_string_literal: true
class Api::V1::NotificationsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, except: [:clear, :dismiss, :mark_read]
before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, only: [:clear, :dismiss, :mark_read]
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, except: [:clear, :mark_read]
before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, only: [:clear, :mark_read]
before_action :require_user!
before_action :set_filter_params
after_action :insert_pagination_headers, only: :index
respond_to :json
DEFAULT_NOTIFICATIONS_LIMIT = 20
def index
@@ -26,11 +24,6 @@ class Api::V1::NotificationsController < Api::BaseController
render_empty
end
def dismiss
current_account.notifications.find_by!(id: params[:id]).destroy!
render_empty
end
def mark_read
current_account.notifications.find(params[:id]).mark_read!
render_empty

View File

@@ -7,8 +7,6 @@ class Api::V1::Polls::VotesController < Api::BaseController
before_action :require_user!
before_action :set_poll
respond_to :json
def create
VoteService.new.call(current_account, @poll, vote_params[:choices])
render json: @poll, serializer: REST::PollSerializer

View File

@@ -3,11 +3,8 @@
class Api::V1::PollsController < Api::BaseController
before_action -> { authorize_if_got_token! :read, :'read:statuses' }, only: :show
respond_to :json
def show
@poll = Poll.attached.find(params[:id])
ActivityPub::FetchRemotePollService.new.call(@poll, current_account) if user_signed_in? && @poll.possibly_stale?
render json: @poll, serializer: REST::PollSerializer, include_results: true
end
end

View File

@@ -4,8 +4,6 @@ class Api::V1::PreferencesController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:accounts' }
before_action :require_user!
respond_to :json
def index
render json: current_account, serializer: REST::PreferencesSerializer
end

View File

@@ -1,56 +0,0 @@
# frozen_string_literal: true
class Api::V1::Push::SubscriptionsController < Api::BaseController
before_action -> { doorkeeper_authorize! :push }
before_action :require_user!
before_action :set_web_push_subscription
def create
@web_subscription&.destroy!
@web_subscription = ::Web::PushSubscription.create!(
endpoint: subscription_params[:endpoint],
key_p256dh: subscription_params[:keys][:p256dh],
key_auth: subscription_params[:keys][:auth],
data: data_params,
user_id: current_user.id,
access_token_id: doorkeeper_token.id
)
render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
end
def show
raise ActiveRecord::RecordNotFound if @web_subscription.nil?
render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
end
def update
raise ActiveRecord::RecordNotFound if @web_subscription.nil?
@web_subscription.update!(data: data_params)
render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer
end
def destroy
@web_subscription&.destroy!
render_empty
end
private
def set_web_push_subscription
@web_subscription = ::Web::PushSubscription.find_by(access_token_id: doorkeeper_token.id)
end
def subscription_params
params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh])
end
def data_params
return {} if params[:data].blank?
params.require(:data).permit(alerts: [:follow, :favourite, :reblog, :mention, :poll])
end
end

View File

@@ -4,8 +4,6 @@ class Api::V1::ReportsController < Api::BaseController
before_action -> { doorkeeper_authorize! :write, :'write:reports' }, only: [:create]
before_action :require_user!
respond_to :json
def create
@report = ReportService.new.call(
current_account,

View File

@@ -1,9 +1,7 @@
# frozen_string_literal: true
class Api::V1::SearchController < Api::BaseController
RESULTS_LIMIT = 100
respond_to :json
class Api::V1::SearchController < EmptyController
RESULTS_LIMIT = 25
def index
@search = Search.new(search_results)

View File

@@ -1,36 +0,0 @@
# frozen_string_literal: true
class Api::V1::ShopController < EmptyController
def index
if Rails.env != 'development'
render json: nil
end
type = params[:type]
if type == 'featured_products'
body = Redis.current.get("gabstore:featuredproducts")
if body.nil? || body.empty?
Request.new(:get, "https://shop.dissenter.com/product/group/json").perform do |res|
Rails.logger.debug "ShopController dissenter products endpoint res code: #{res.code.to_s}"
if res.code == 200
body = res.body_with_limit
Redis.current.set("gabstore:featuredproducts", body)
Redis.current.expire("gabstore:featuredproducts", 15.minutes.seconds)
render json: body
else
render json: nil
end
end
else
render json: body
end
else
raise GabSocial::NotPermittedError
end
rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, HTTP::Error
Rails.logger.debug "Error fetching dissenter shop: #{type}"
render json: nil
end
end

View File

@@ -6,8 +6,6 @@ class Api::V1::Statuses::BookmarksController < Api::BaseController
before_action -> { doorkeeper_authorize! :write, :'write:bookmarks' }
before_action :require_user!
respond_to :json
def create
if current_user.account.is_pro
@status = bookmarked_status
@@ -17,6 +15,10 @@ class Api::V1::Statuses::BookmarksController < Api::BaseController
end
end
def show
# is status bookmarked by user?
end
def destroy
if current_user.account.is_pro
@status = requested_status

View File

@@ -6,11 +6,10 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController
before_action -> { doorkeeper_authorize! :write, :'write:favourites' }
before_action :require_user!
respond_to :json
def create
@status = favourited_status
render json: @status, serializer: REST::StatusSerializer
puts "tilly -- status: " + @status.inspect
render json: @status, serializer: REST::StatusStatSerializer
end
def destroy
@@ -19,7 +18,7 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController
UnfavouriteWorker.perform_async(current_user.account_id, @status.id)
render json: @status, serializer: REST::StatusSerializer, unfavourite: true, relationships: StatusRelationshipsPresenter.new([@status], current_user&.account_id, favourites_map: @favourites_map)
render json: @status, serializer: REST::StatusStatSerializer, unfavourite: true, relationships: StatusRelationshipsPresenter.new([@status], current_user&.account_id, favourites_map: @favourites_map)
end
private

View File

@@ -1,41 +0,0 @@
# frozen_string_literal: true
class Api::V1::Statuses::MutesController < Api::BaseController
include Authorization
before_action -> { doorkeeper_authorize! :write, :'write:mutes' }
before_action :require_user!
before_action :set_status
before_action :set_conversation
respond_to :json
def create
current_account.mute_conversation!(@conversation)
@mutes_map = { @conversation.id => true }
render json: @status, serializer: REST::StatusSerializer
end
def destroy
current_account.unmute_conversation!(@conversation)
@mutes_map = { @conversation.id => false }
render json: @status, serializer: REST::StatusSerializer
end
private
def set_status
@status = Status.find(params[:status_id])
authorize @status, :show?
rescue GabSocial::NotPermittedError
# Reraise in order to get a 404 instead of a 403 error code
raise ActiveRecord::RecordNotFound
end
def set_conversation
@conversation = @status.conversation
raise GabSocial::ValidationError if @conversation.nil?
end
end

View File

@@ -7,13 +7,15 @@ class Api::V1::Statuses::PinsController < Api::BaseController
before_action :require_user!
before_action :set_status
respond_to :json
def create
StatusPin.create!(account: current_account, status: @status)
render json: @status, serializer: REST::StatusSerializer
end
def show
# is status pinned by user?
end
def destroy
pin = StatusPin.find_by(account: current_account, status: @status)

View File

@@ -1,14 +1,12 @@
# frozen_string_literal: true
class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController
class Api::V1::Statuses::RepostedByAccountsController < Api::BaseController
include Authorization
before_action -> { authorize_if_got_token! :read, :'read:accounts' }
before_action :set_status
after_action :insert_pagination_headers
respond_to :json
def index
@accounts = load_accounts
render json: @accounts, each_serializer: REST::AccountSerializer
@@ -38,13 +36,13 @@ class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController
def next_path
if records_continue?
api_v1_status_reblogged_by_index_url pagination_params(max_id: pagination_max_id)
api_v1_status_reposted_by_index_url pagination_params(max_id: pagination_max_id)
end
end
def prev_path
unless @accounts.empty?
api_v1_status_reblogged_by_index_url pagination_params(since_id: pagination_since_id)
api_v1_status_reposted_by_index_url pagination_params(since_id: pagination_since_id)
end
end

View File

@@ -6,8 +6,6 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController
before_action -> { doorkeeper_authorize! :write, :'write:statuses' }
before_action :require_user!
respond_to :json
def create
if !current_user.account.local? || !status_for_reblog.local
return render json: { error: 'Invalid action' }, status: 422

View File

@@ -3,13 +3,12 @@
class Api::V1::StatusesController < Api::BaseController
include Authorization
# : todo : disable all oauth everything
before_action -> { authorize_if_got_token! :read, :'read:statuses' }, except: [:create, :update, :destroy]
before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:create, :update, :destroy]
before_action :require_user!, except: [:show, :comments, :context, :card]
before_action :set_status, only: [:show, :comments, :context, :card, :update, :revisions]
respond_to :json
# This API was originally unlimited, pagination cannot be introduced without
# breaking backwards-compatibility. Arbitrarily high number to cover most
# conversations as quasi-unlimited, it would be too much work to render more
@@ -126,7 +125,6 @@ class Api::V1::StatusesController < Api::BaseController
media_ids: [],
poll: [
:multiple,
:hide_totals,
:expires_in,
options: [],
],

View File

@@ -1,8 +1,6 @@
# frozen_string_literal: true
class Api::V1::StreamingController < Api::BaseController
respond_to :json
def index
if Rails.configuration.x.streaming_api_base_url != request.host
uri = URI.parse(request.url)

View File

@@ -6,13 +6,11 @@ class Api::V1::SuggestionsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read }
before_action :require_user!
respond_to :json
def index
type = params[:type]
if type == 'related'
count = truthy_param?(:unlimited) ? 80 : 10
count = truthy_param?(:unlimited) ? PotentialFriendshipTracker::MAX_ITEMS : 10
@accounts = PotentialFriendshipTracker.get(current_account.id, limit: count)
render json: @accounts, each_serializer: REST::AccountSerializer
elsif type == 'verified'

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true
class Api::V1::Timelines::ExploreController < Api::BaseController
class Api::V1::Timelines::ExploreController < EmptyController
before_action :set_sort_type
before_action :set_statuses
@@ -45,110 +45,7 @@ class Api::V1::Timelines::ExploreController < Api::BaseController
end
def explore_statuses
statuses = nil
date_limit = 30.days.ago
top_order = 'status_stats.favourites_count DESC, status_stats.reblogs_count DESC, status_stats.replies_count DESC'
if @sort_type == 'hot'
# : todo :
# unique groups
# unique users
date_limit = 8.hours.ago
elsif @sort_type == 'top_today'
date_limit = 24.hours.ago
elsif @sort_type == 'top_weekly'
date_limit = 7.days.ago
elsif @sort_type == 'top_monthly'
date_limit = 30.days.ago
elsif @sort_type == 'top_yearly'
date_limit = 1.year.ago
end
if current_account
if @sort_type == 'newest'
statuses = Status.with_public_visibility.where(
reply: false
).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif @sort_type == 'recent'
statuses = Status.with_public_visibility.where(
reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.with_public_visibility.where(
reply: false
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif @sort_type == 'hot'
statuses = Status.unscoped.with_public_visibility.where(
reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
else
statuses = Status.unscoped.with_public_visibility.where(
reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
end
end
else
if @sort_type == 'newest'
statuses = Status.with_public_visibility.where(
reply: false
).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
)
elsif @sort_type == 'recent'
statuses = Status.with_public_visibility.where(
reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
)
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.with_public_visibility.where(
reply: false
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
)
else
statuses = Status.unscoped.with_public_visibility.where(
reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id)
)
end
end
end
statuses
SortingQueryBuilder.new.call(@sort_type, params[:max_id])
end
def insert_pagination_headers

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true
class Api::V1::Timelines::GroupCollectionController < Api::BaseController
class Api::V1::Timelines::GroupCollectionController < EmptyController
before_action :set_collection_type
before_action :set_sort_type
before_action :set_statuses
@@ -61,8 +61,6 @@ class Api::V1::Timelines::GroupCollectionController < Api::BaseController
end
def group_collection_statuses
statuses = nil
@groupIds = []
if @collection_type == 'featured'
@groupIds = FetchGroupsService.new.call("featured")
@@ -72,98 +70,7 @@ class Api::V1::Timelines::GroupCollectionController < Api::BaseController
return []
end
date_limit = 30.days.ago
top_order = 'status_stats.favourites_count DESC, status_stats.reblogs_count DESC, status_stats.replies_count DESC'
if @sort_type == 'hot'
# : todo :
# unique groups
# unique users
date_limit = 8.hours.ago
elsif @sort_type == 'top_today'
date_limit = 24.hours.ago
elsif @sort_type == 'top_weekly'
date_limit = 7.days.ago
elsif @sort_type == 'top_monthly'
date_limit = 30.days.ago
elsif @sort_type == 'top_yearly'
date_limit = 1.year.ago
end
if current_account
if @sort_type == 'newest'
statuses = Status.where(
group: @groupIds, reply: false
).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif @sort_type == 'recent'
statuses = Status.where(
group: @groupIds, reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.where(
group: @groupIds, reply: false
).joins(:status_stat).order(top_order)
.paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
else
statuses = Status.unscoped.where(
group: @groupIds, reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
end
end
else
if @sort_type == 'newest'
statuses = Status.where(
group: @groupIds, reply: false
).paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
elsif @sort_type == 'recent'
statuses = Status.where(
group: @groupIds, reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.where(
group: @groupIds, reply: false
).joins(:status_stat).order(top_order)
.paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
else
statuses = Status.unscoped.where(
group: @groupIds, reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
end
end
end
statuses
SortingQueryBuilder.new.call(@sort_type, params[:max_id], @groupIds)
end
def insert_pagination_headers

View File

@@ -13,6 +13,7 @@ class Api::V1::Timelines::GroupController < Api::BaseController
if current_user
render json: @statuses,
each_serializer: REST::StatusSerializer,
group_id: params[:id],
relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id, group_id: @group.id)
else
render json: @statuses, each_serializer: REST::StatusSerializer
@@ -50,103 +51,7 @@ class Api::V1::Timelines::GroupController < Api::BaseController
end
def group_statuses
statuses = nil
date_limit = 30.days.ago
top_order = 'status_stats.favourites_count DESC, status_stats.reblogs_count DESC, status_stats.replies_count DESC'
if @sort_type == 'hot'
# : todo :
# unique groups
# unique users
date_limit = 8.hours.ago
elsif @sort_type == 'top_today'
date_limit = 24.hours.ago
elsif @sort_type == 'top_weekly'
date_limit = 7.days.ago
elsif @sort_type == 'top_monthly'
date_limit = 30.days.ago
elsif @sort_type == 'top_yearly'
date_limit = 1.year.ago
end
if current_account
if @sort_type == 'newest'
statuses = Status.where(
group: @group, reply: false
).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif @sort_type == 'recent'
statuses = Status.where(
group: @group, reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.where(
group: @group, reply: false
).joins(:status_stat).order(top_order)
.paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
else
statuses = Status.unscoped.where(
group: @group, reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) }
end
end
else
if @sort_type == 'newest'
statuses = Status.where(
group: @group, reply: false
).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
elsif @sort_type == 'recent'
statuses = Status.where(
group: @group, reply: false
).joins(:status_stat).where(
'status_stats.replies_count > 0 OR status_stats.reblogs_count > 0 OR status_stats.favourites_count > 0'
).order('status_stats.updated_at DESC').paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
elsif ['top_today', 'top_weekly', 'top_monthly', 'top_yearly', 'top_all_time', 'hot'].include? @sort_type
if @sort_type == 'top_all_time'
statuses = Status.unscoped.where(
group: @group, reply: false
).joins(:status_stat).order(top_order)
.paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
else
statuses = Status.unscoped.where(
group: @group, reply: false
).where(
'statuses.created_at > ?', date_limit
).joins(:status_stat).order(top_order).paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
end
end
end
statuses
SortingQueryBuilder.new.call(@sort_type, params[:max_id], @group)
end
def insert_pagination_headers

View File

@@ -5,8 +5,6 @@ class Api::V1::Timelines::HomeController < Api::BaseController
before_action :require_user!, only: [:show]
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
respond_to :json
def show
@statuses = load_statuses
render json: @statuses,

View File

@@ -10,6 +10,7 @@ class Api::V1::Timelines::PreviewCardController < Api::BaseController
def show
render json: @statuses,
each_serializer: REST::StatusSerializer,
preview_card_id: params[:id],
relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id)
end

View File

@@ -4,8 +4,6 @@ class Api::V1::Timelines::ProController < Api::BaseController
before_action :require_user!, only: [:show]
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
respond_to :json
def show
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)

View File

@@ -1,67 +0,0 @@
# frozen_string_literal: true
class Api::V1::Timelines::PublicController < Api::BaseController
before_action :require_user!, only: [:show]
before_action :require_admin!
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
respond_to :json
def show
@statuses = load_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private
def load_statuses
cached_public_statuses
end
def cached_public_statuses
cache_collection public_statuses, Status
end
def public_statuses
statuses = public_timeline_statuses.paginate_by_id(
limit_param(DEFAULT_STATUSES_LIMIT),
params_slice(:max_id, :since_id, :min_id)
)
if truthy_param?(:only_media)
# `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id)
statuses.where(id: status_ids)
else
statuses
end
end
def public_timeline_statuses
Status.as_public_timeline(current_account)
end
def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
def pagination_params(core_params)
params.slice(:limit, :only_media).permit(:limit, :only_media).merge(core_params)
end
def next_path
api_v1_timelines_public_url pagination_params(max_id: pagination_max_id)
end
def prev_path
api_v1_timelines_public_url pagination_params(min_id: pagination_since_id)
end
def pagination_max_id
@statuses.last.id
end
def pagination_since_id
@statuses.first.id
end
end

View File

@@ -5,11 +5,11 @@ class Api::V1::Timelines::TagController < Api::BaseController
before_action :require_user!, only: [:show]
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
respond_to :json
def show
@statuses = tagged_statuses
render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
render json: @statuses,
each_serializer: REST::StatusSerializer,
relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end
private

View File

@@ -1,8 +1,6 @@
# frozen_string_literal: true
class Api::Web::EmbedsController < Api::Web::BaseController
respond_to :json
def create
status = StatusFinder.new(params[:url]).status
render json: status, serializer: OEmbedSerializer, width: 400

View File

@@ -1,8 +1,6 @@
# frozen_string_literal: true
class Api::Web::PushSubscriptionsController < Api::Web::BaseController
respond_to :json
before_action :require_user!
def create

View File

@@ -1,8 +1,6 @@
# frozen_string_literal: true
class Api::Web::SettingsController < Api::Web::BaseController
respond_to :json
before_action :require_user!
def update