Adding hashtag model

This commit is contained in:
Eugen Rochko
2016-11-04 19:12:59 +01:00
parent 6471a548fe
commit 62292797ec
11 changed files with 60 additions and 9 deletions

View File

@ -14,12 +14,12 @@ class Api::V1::AccountsController < ApiController
end
def following
@accounts = @account.following.limit(40)
@accounts = @account.following.with_counters.limit(40)
render action: :index
end
def followers
@accounts = @account.followers.limit(40)
@accounts = @account.followers.with_counters.limit(40)
render action: :index
end

View File

@ -15,12 +15,12 @@ class Api::V1::StatusesController < ApiController
end
def reblogged_by
@accounts = @status.reblogs.includes(:account).limit(40).map(&:account)
@accounts = @status.reblogged_by(40)
render action: :accounts
end
def favourited_by
@accounts = @status.favourites.includes(:account).limit(40).map(&:account)
@accounts = @status.favourited_by(40)
render action: :accounts
end

View File

@ -14,6 +14,7 @@ class Formatter
html = simple_format(html, sanitize: false)
html = link_urls(html)
html = link_mentions(html, status.mentions)
html = link_hashtags(html)
html.html_safe
end
@ -43,6 +44,17 @@ class Formatter
end
end
def link_hashtags(html)
html.gsub(Tag::HASHTAG_RE) do |match|
hashtag_html(match)
end
end
def hashtag_html(match)
prefix, affix = match.split('#')
"#{prefix}<a href=\"#\" class=\"mention hashtag\">#<span>#{affix}</span></a>"
end
def mention_html(match, account)
"#{match.split('@').first}<a href=\"#{TagManager.instance.url_for(account)}\" class=\"mention\">@<span>#{account.username}</span></a>"
end

View File

@ -33,12 +33,12 @@ class Account < ApplicationRecord
has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
has_many :following, -> { order('follows.created_at desc') }, through: :active_relationships, source: :target_account
has_many :followers, -> { order('follows.created_at desc') }, through: :passive_relationships, source: :account
has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account
# Block relationships
has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
has_many :blocking, -> { order('blocks.created_at desc') }, through: :block_relationships, source: :target_account
has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
has_many :media_attachments, dependent: :destroy

View File

@ -77,6 +77,14 @@ class Status < ApplicationRecord
ids.map { |id| statuses[id].first }
end
def reblogged_by(limit)
Account.where(id: reblogs.limit(limit).pluck(:account_id)).with_counters
end
def favourited_by(limit)
Account.where(id: favourites.limit(limit).pluck(:account_id)).with_counters
end
def self.as_home_timeline(account)
where(account: [account] + account.following).with_includes.with_counters
end

5
app/models/tag.rb Normal file
View File

@ -0,0 +1,5 @@
class Tag < ApplicationRecord
HASHTAG_RE = /[?:^|\s|\.|>]#([[:word:]_]+)/i
validates :name, presence: true, uniqueness: true
end

View File

@ -7,7 +7,7 @@ class User < ApplicationRecord
validates :account, presence: true
scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') }
scope :recent, -> { order('created_at desc') }
scope :recent, -> { order('id desc') }
scope :admins, -> { where(admin: true) }
has_settings do |s|