forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add featured hashtags to profiles (mastodon#9755)
* Add hashtag filter to profiles GET /@:username/tagged/:hashtag GET /api/v1/accounts/:id/statuses?tagged=:hashtag * Display featured hashtags on public profile * Use separate model for featured tags * Update featured hashtag counters on-write * Limit featured tags to 10
- Loading branch information
Showing
24 changed files
with
238 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
class Settings::FeaturedTagsController < Settings::BaseController | ||
layout 'admin' | ||
|
||
before_action :authenticate_user! | ||
before_action :set_featured_tags, only: :index | ||
before_action :set_featured_tag, except: [:index, :create] | ||
before_action :set_most_used_tags, only: :index | ||
|
||
def index | ||
@featured_tag = FeaturedTag.new | ||
end | ||
|
||
def create | ||
@featured_tag = current_account.featured_tags.new(featured_tag_params) | ||
@featured_tag.reset_data | ||
|
||
if @featured_tag.save | ||
redirect_to settings_featured_tags_path | ||
else | ||
set_featured_tags | ||
set_most_used_tags | ||
|
||
render :index | ||
end | ||
end | ||
|
||
def destroy | ||
@featured_tag.destroy! | ||
redirect_to settings_featured_tags_path | ||
end | ||
|
||
private | ||
|
||
def set_featured_tag | ||
@featured_tag = current_account.featured_tags.find(params[:id]) | ||
end | ||
|
||
def set_featured_tags | ||
@featured_tags = current_account.featured_tags.reject(&:new_record?) | ||
end | ||
|
||
def set_most_used_tags | ||
@most_used_tags = Tag.most_used(current_account).where.not(id: @featured_tags.map(&:id)).limit(10) | ||
end | ||
|
||
def featured_tag_params | ||
params.require(:featured_tag).permit(:name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,6 @@ def account_params | |
end | ||
|
||
def set_account | ||
@account = current_user.account | ||
@account = current_account | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,3 +288,7 @@ | |
border-bottom: 0; | ||
} | ||
} | ||
|
||
.directory__tag .trends__item__current { | ||
width: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
# == Schema Information | ||
# | ||
# Table name: featured_tags | ||
# | ||
# id :bigint(8) not null, primary key | ||
# account_id :bigint(8) | ||
# tag_id :bigint(8) | ||
# statuses_count :bigint(8) default(0), not null | ||
# last_status_at :datetime | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class FeaturedTag < ApplicationRecord | ||
belongs_to :account, inverse_of: :featured_tags, required: true | ||
belongs_to :tag, inverse_of: :featured_tags, required: true | ||
|
||
delegate :name, to: :tag, allow_nil: true | ||
|
||
validates :name, presence: true | ||
validate :validate_featured_tags_limit, on: :create | ||
|
||
def name=(str) | ||
self.tag = Tag.find_or_initialize_by(name: str.delete('#').mb_chars.downcase.to_s) | ||
end | ||
|
||
def increment(timestamp) | ||
update(statuses_count: statuses_count + 1, last_status_at: timestamp) | ||
end | ||
|
||
def decrement(deleted_status_id) | ||
update(statuses_count: [0, statuses_count - 1].max, last_status_at: account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).where.not(id: deleted_status_id).select(:created_at).first&.created_at) | ||
end | ||
|
||
def reset_data | ||
self.statuses_count = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).count | ||
self.last_status_at = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).select(:created_at).first&.created_at | ||
end | ||
|
||
private | ||
|
||
def validate_featured_tags_limit | ||
errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= 10 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- content_for :page_title do | ||
= t('settings.featured_tags') | ||
|
||
= simple_form_for @featured_tag, url: settings_featured_tags_path do |f| | ||
= render 'shared/error_messages', object: @featured_tag | ||
|
||
.fields-group | ||
= f.input :name, wrapper: :with_block_label, hint: safe_join([t('simple_form.hints.featured_tag.name'), safe_join(@most_used_tags.map { |tag| link_to("##{tag.name}", settings_featured_tags_path(featured_tag: { name: tag.name }), method: :post) }, ', ')], ' ') | ||
|
||
.actions | ||
= f.button :button, t('featured_tags.add_new'), type: :submit | ||
|
||
%hr.spacer/ | ||
|
||
- @featured_tags.each do |featured_tag| | ||
.directory__tag{ class: params[:tag] == featured_tag.name ? 'active' : nil } | ||
%div | ||
%h4 | ||
= fa_icon 'hashtag' | ||
= featured_tag.name | ||
%small | ||
- if featured_tag.last_status_at.nil? | ||
= t('accounts.nothing_here') | ||
- else | ||
%time{ datetime: featured_tag.last_status_at.iso8601, title: l(featured_tag.last_status_at) }= l featured_tag.last_status_at | ||
= table_link_to 'trash', t('filters.index.delete'), settings_featured_tag_path(featured_tag), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } | ||
.trends__item__current= number_to_human featured_tag.statuses_count, strip_insignificant_zeros: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.