Add compatibility to follow hashtags
This commit is contained in:
parent
ee27368a79
commit
eee07915f3
10 changed files with 198 additions and 4 deletions
|
@ -6,7 +6,7 @@ class Api::V1::FeaturedTags::SuggestionsController < Api::BaseController
|
||||||
before_action :set_recently_used_tags, only: :index
|
before_action :set_recently_used_tags, only: :index
|
||||||
|
|
||||||
def index
|
def index
|
||||||
render json: @recently_used_tags, each_serializer: REST::TagSerializer
|
render json: @recently_used_tags, each_serializer: REST::TagSerializer, relationships: TagRelationshipsPresenter.new(@recently_used_tags, current_user&.account_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
52
app/controllers/api/v1/followed_tags_controller.rb
Normal file
52
app/controllers/api/v1/followed_tags_controller.rb
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Api::V1::FollowedTagsController < Api::BaseController
|
||||||
|
TAGS_LIMIT = 100
|
||||||
|
|
||||||
|
before_action -> { doorkeeper_authorize! :follow, :read, :'read:follows' }, except: :show
|
||||||
|
before_action :require_user!
|
||||||
|
before_action :set_results
|
||||||
|
|
||||||
|
after_action :insert_pagination_headers, only: :show
|
||||||
|
|
||||||
|
def index
|
||||||
|
render json: @results.map(&:tag), each_serializer: REST::TagSerializer, relationships: TagRelationshipsPresenter.new(@results.map(&:tag), current_user&.account_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_results
|
||||||
|
@results = FollowTag.where(account: current_account).joins(:tag).eager_load(:tag).to_a_paginated_by_id(
|
||||||
|
limit_param(TAGS_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
|
||||||
|
api_v1_followed_tags_url pagination_params(max_id: pagination_max_id) if records_continue?
|
||||||
|
end
|
||||||
|
|
||||||
|
def prev_path
|
||||||
|
api_v1_followed_tags_url pagination_params(since_id: pagination_since_id) unless @results.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination_max_id
|
||||||
|
@results.last.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination_since_id
|
||||||
|
@results.first.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def records_continue?
|
||||||
|
@results.size == limit_param(TAG_LIMIT)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination_params(core_params)
|
||||||
|
params.slice(:limit).permit(:limit).merge(core_params)
|
||||||
|
end
|
||||||
|
end
|
30
app/controllers/api/v1/tags_controller.rb
Normal file
30
app/controllers/api/v1/tags_controller.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Api::V1::TagsController < Api::BaseController
|
||||||
|
before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, except: :show
|
||||||
|
before_action :require_user!, except: :show
|
||||||
|
before_action :set_or_create_tag
|
||||||
|
|
||||||
|
override_rate_limit_headers :follow, family: :follows
|
||||||
|
|
||||||
|
def show
|
||||||
|
render json: @tag, serializer: REST::TagSerializer
|
||||||
|
end
|
||||||
|
|
||||||
|
def follow
|
||||||
|
FollowTag.create!(tag: @tag, account: current_account, rate_limit: true)
|
||||||
|
render json: @tag, serializer: REST::TagSerializer
|
||||||
|
end
|
||||||
|
|
||||||
|
def unfollow
|
||||||
|
FollowTag.find_by(account: current_account, tag: @tag)&.destroy!
|
||||||
|
render json: @tag, serializer: REST::TagSerializer
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_or_create_tag
|
||||||
|
return not_found unless /\A(#{Tag::HASHTAG_NAME_RE})\z/.match?(params[:id])
|
||||||
|
@tag = Tag.find_normalized(params[:id]) || Tag.new(name: Tag.normalize(params[:id]), display_name: params[:id])
|
||||||
|
end
|
||||||
|
end
|
57
app/controllers/api/v1/trends/tags_controller.rb
Normal file
57
app/controllers/api/v1/trends/tags_controller.rb
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Api::V1::Trends::TagsController < Api::BaseController
|
||||||
|
before_action :set_tags
|
||||||
|
|
||||||
|
after_action :insert_pagination_headers
|
||||||
|
|
||||||
|
DEFAULT_TAGS_LIMIT = 10
|
||||||
|
|
||||||
|
def index
|
||||||
|
render json: @tags, each_serializer: REST::TagSerializer, relationships: TagRelationshipsPresenter.new(@tags, current_user&.account_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def enabled?
|
||||||
|
Setting.trends
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_tags
|
||||||
|
@tags = begin
|
||||||
|
if enabled?
|
||||||
|
tags_from_trends.offset(offset_param).limit(limit_param(DEFAULT_TAGS_LIMIT))
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tags_from_trends
|
||||||
|
Trends.tags.query.allowed
|
||||||
|
end
|
||||||
|
|
||||||
|
def insert_pagination_headers
|
||||||
|
set_pagination_headers(next_path, prev_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination_params(core_params)
|
||||||
|
params.slice(:limit).permit(:limit).merge(core_params)
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_path
|
||||||
|
api_v1_trends_tags_url pagination_params(offset: offset_param + limit_param(DEFAULT_TAGS_LIMIT)) if records_continue?
|
||||||
|
end
|
||||||
|
|
||||||
|
def prev_path
|
||||||
|
api_v1_trends_tags_url pagination_params(offset: offset_param - limit_param(DEFAULT_TAGS_LIMIT)) if offset_param > limit_param(DEFAULT_TAGS_LIMIT)
|
||||||
|
end
|
||||||
|
|
||||||
|
def offset_param
|
||||||
|
params[:offset].to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def records_continue?
|
||||||
|
@tags.size == limit_param(DEFAULT_TAGS_LIMIT)
|
||||||
|
end
|
||||||
|
end
|
|
@ -12,6 +12,9 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class FollowTag < ApplicationRecord
|
class FollowTag < ApplicationRecord
|
||||||
|
include RateLimitable
|
||||||
|
include Paginable
|
||||||
|
|
||||||
belongs_to :account, inverse_of: :follow_tags, required: true
|
belongs_to :account, inverse_of: :follow_tags, required: true
|
||||||
belongs_to :tag, inverse_of: :follow_tags, required: true
|
belongs_to :tag, inverse_of: :follow_tags, required: true
|
||||||
belongs_to :list, optional: true
|
belongs_to :list, optional: true
|
||||||
|
@ -26,6 +29,10 @@ class FollowTag < ApplicationRecord
|
||||||
scope :list, -> { where.not(list_id: nil) }
|
scope :list, -> { where.not(list_id: nil) }
|
||||||
scope :with_media, ->(status) { where(media_only: false) unless status.with_media? }
|
scope :with_media, ->(status) { where(media_only: false) unless status.with_media? }
|
||||||
|
|
||||||
|
accepts_nested_attributes_for :tag
|
||||||
|
|
||||||
|
rate_limit by: :account, family: :follows
|
||||||
|
|
||||||
def name=(str)
|
def name=(str)
|
||||||
self.tag = Tag.find_or_create_by_names(str.strip)&.first
|
self.tag = Tag.find_or_create_by_names(str.strip)&.first
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,13 +24,16 @@ class Tag < ApplicationRecord
|
||||||
has_many :favourite_tags, dependent: :destroy, inverse_of: :tag
|
has_many :favourite_tags, dependent: :destroy, inverse_of: :tag
|
||||||
has_many :featured_tags, dependent: :destroy, inverse_of: :tag
|
has_many :featured_tags, dependent: :destroy, inverse_of: :tag
|
||||||
has_many :follow_tags, dependent: :destroy, inverse_of: :tag
|
has_many :follow_tags, dependent: :destroy, inverse_of: :tag
|
||||||
|
has_many :passive_relationships, class_name: 'FollowTag', inverse_of: :tag, dependent: :destroy
|
||||||
|
has_many :followers, through: :passive_relationships, source: :account
|
||||||
|
|
||||||
HASHTAG_SEPARATORS = "_\u00B7\u200c"
|
HASHTAG_SEPARATORS = "_\u00B7\u200c"
|
||||||
HASHTAG_NAME_RE = "([[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}][[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)"
|
HASHTAG_NAME_RE = "([[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}][[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)"
|
||||||
HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i
|
HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i
|
||||||
|
|
||||||
validates :name, presence: true, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i }
|
validates :name, presence: true, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i }
|
||||||
validate :validate_name_change, if: -> { !new_record? && name_changed? }
|
# validates :display_name, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i }
|
||||||
|
# validate :validate_name_change, if: -> { !new_record? && name_changed? }
|
||||||
|
|
||||||
scope :reviewed, -> { where.not(reviewed_at: nil) }
|
scope :reviewed, -> { where.not(reviewed_at: nil) }
|
||||||
scope :unreviewed, -> { where(reviewed_at: nil) }
|
scope :unreviewed, -> { where(reviewed_at: nil) }
|
||||||
|
@ -109,7 +112,10 @@ class Tag < ApplicationRecord
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def find_or_create_by_names(name_or_names)
|
def find_or_create_by_names(name_or_names)
|
||||||
Array(name_or_names).map(&method(:normalize)).uniq { |str| str.mb_chars.downcase.to_s }.map do |normalized_name|
|
names = Array(name_or_names).map { |str| [normalize(str), str] }.uniq(&:first)
|
||||||
|
|
||||||
|
names.map do |(normalized_name, display_name)|
|
||||||
|
# tag = matching_name(normalized_name).first || create(name: normalized_name, display_name: display_name.gsub(/[^[:alnum:]#{HASHTAG_SEPARATORS}]/, ''))
|
||||||
tag = matching_name(normalized_name).first || create(name: normalized_name)
|
tag = matching_name(normalized_name).first || create(name: normalized_name)
|
||||||
|
|
||||||
yield tag if block_given?
|
yield tag if block_given?
|
||||||
|
|
15
app/presenters/tag_relationships_presenter.rb
Normal file
15
app/presenters/tag_relationships_presenter.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class TagRelationshipsPresenter
|
||||||
|
attr_reader :following_map
|
||||||
|
|
||||||
|
def initialize(tags, current_account_id = nil, **options)
|
||||||
|
@following_map = begin
|
||||||
|
if current_account_id.nil?
|
||||||
|
{}
|
||||||
|
else
|
||||||
|
FollowTag.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -5,7 +5,25 @@ class REST::TagSerializer < ActiveModel::Serializer
|
||||||
|
|
||||||
attributes :name, :url, :history
|
attributes :name, :url, :history
|
||||||
|
|
||||||
|
attribute :following, if: :current_user?
|
||||||
|
|
||||||
def url
|
def url
|
||||||
tag_url(object)
|
tag_url(object)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def name
|
||||||
|
# object.display_name
|
||||||
|
# end
|
||||||
|
|
||||||
|
def following
|
||||||
|
if instance_options && instance_options[:relationships]
|
||||||
|
instance_options[:relationships].following_map[object.id] || false
|
||||||
|
else
|
||||||
|
FollowTag.where(tag_id: object.id, account_id: current_user.account_id).exists?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_user?
|
||||||
|
!current_user.nil?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,4 +13,4 @@
|
||||||
%h4.emojify= t('footer.trending_now')
|
%h4.emojify= t('footer.trending_now')
|
||||||
|
|
||||||
- trends.each do |tag|
|
- trends.each do |tag|
|
||||||
= react_component :hashtag, hashtag: ActiveModelSerializers::SerializableResource.new(tag, serializer: REST::TagSerializer).as_json
|
= react_component :hashtag, hashtag: ActiveModelSerializers::SerializableResource.new(tag, serializer: REST::TagSerializer, scope: current_user, scope_name: :current_user).as_json
|
||||||
|
|
|
@ -496,6 +496,15 @@ Rails.application.routes.draw do
|
||||||
resource :note, only: :create, controller: 'accounts/notes'
|
resource :note, only: :create, controller: 'accounts/notes'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :tags, only: [:show] do
|
||||||
|
member do
|
||||||
|
post :follow
|
||||||
|
post :unfollow
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
resources :followed_tags, only: [:index]
|
||||||
|
|
||||||
resources :lists, only: [:index, :create, :show, :update, :destroy] do
|
resources :lists, only: [:index, :create, :show, :update, :destroy] do
|
||||||
resource :accounts, only: [:show, :create, :destroy], controller: 'lists/accounts'
|
resource :accounts, only: [:show, :create, :destroy], controller: 'lists/accounts'
|
||||||
resource :subscribes, only: [:show, :create, :destroy], controller: 'lists/subscribes'
|
resource :subscribes, only: [:show, :create, :destroy], controller: 'lists/subscribes'
|
||||||
|
|
Loading…
Reference in a new issue