Add backend support for local_only on status create

Based off ThibG implementation on glitch #502
This commit is contained in:
Renato "Lond" Cerqueira 2018-08-25 13:27:58 +02:00
parent 4969ea4f2c
commit f37ca30ea1
4 changed files with 21 additions and 4 deletions

View File

@ -52,7 +52,8 @@ class Api::V1::StatusesController < Api::BaseController
spoiler_text: status_params[:spoiler_text],
visibility: status_params[:visibility],
application: doorkeeper_token.application,
idempotency: request.headers['Idempotency-Key'])
idempotency: request.headers['Idempotency-Key'],
local_only: status_params[:local_only])
render json: @status, serializer: REST::StatusSerializer
end
@ -77,7 +78,7 @@ class Api::V1::StatusesController < Api::BaseController
end
def status_params
params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, media_ids: [])
params.permit(:status, :in_reply_to_id, :sensitive, :spoiler_text, :visibility, :local_only, media_ids: [])
end
def pagination_params(core_params)

View File

@ -130,6 +130,10 @@ class Status < ApplicationRecord
attributes['local'] || uri.nil?
end
def local_only?
local_only
end
def reblog?
!reblog_of_id.nil?
end
@ -222,6 +226,8 @@ class Status < ApplicationRecord
around_create Mastodon::Snowflake::Callbacks
before_create :set_locality
before_validation :prepare_contents, if: :local?
before_validation :set_reblog
before_validation :set_visibility
@ -445,6 +451,10 @@ class Status < ApplicationRecord
self.local = account.local?
end
def set_locality
self.local_only = reblog.local_only if reblog?
end
def update_statistics
return unless public_visibility? || unlisted_visibility?
ActivityTracker.increment('activity:statuses:local')

View File

@ -4,7 +4,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
:sensitive, :spoiler_text, :visibility, :language,
:uri, :content, :url, :replies_count, :reblogs_count,
:favourites_count
:favourites_count, :local_only
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?

View File

@ -31,7 +31,8 @@ class PostStatusService < BaseService
spoiler_text: options[:spoiler_text] || '',
visibility: options[:visibility] || account.user&.setting_default_privacy,
language: language_from_option(options[:language]) || account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(text, account),
application: options[:application])
application: options[:application],
local_only: local_only_option(options[:local_only], in_reply_to))
end
process_hashtags_service.call(status)
@ -57,6 +58,11 @@ class PostStatusService < BaseService
private
def local_only_option(local_only, in_reply_to)
return in_reply_to&.local_only? if local_only.nil? # XXX temporary, just until clients implement to avoid leaking local_only posts
local_only
end
def validate_media!(media_ids)
return if media_ids.blank? || !media_ids.is_a?(Enumerable)