Commit da411a0b authored by Eugen Rochko's avatar Eugen Rochko

Add pagination to friends

parent 1f6fa969
......@@ -557,3 +557,15 @@ h4 {
.empty-message {
margin-bottom: 20px;
}
.pagination {
text-align: center;
margin-bottom: 20px;
a {
display: inline-block;
text-transform: uppercase;
font-weight: 500;
text-decoration: none;
}
}
......@@ -4,37 +4,57 @@ require 'twitter'
class FriendsController < ApplicationController
before_action :authenticate_user!
before_action :set_friends, only: :index
before_action :set_top_instances, only: :index
before_action :set_page
before_action :set_friends
before_action :set_top_instances
before_action :set_next_page
before_action :paginate_friends
before_action :set_relationships, if: -> { current_user&.mastodon }
rescue_from Twitter::Error do |e|
redirect_to root_path, alert: "Twitter error: #{e}"
end
PER_PAGE_FRIENDS = 20
MAX_INSTANCES = 20
MIN_INSTANCES = 4
def index; end
private
def set_page
@page = (params['page'] || 1).to_i
end
def set_next_page
@next_page = @friends.size > (@page * PER_PAGE_FRIENDS) ? @page + 1 : nil
end
def set_friends
@friends = User.where(id: Authorization.where(provider: :twitter, uid: twitter_friend_ids).map(&:user_id))
.includes(:authorizations)
.reject { |user| user.mastodon.nil? }
.map { |user| fetch_account_id(user) }
end
def set_top_instances
@top_instances = friends_domains.map { |k, _| fetch_instance_info(k) }.compact
end
def paginate_friends
@friends = @friends.slice([(@page - 1) * PER_PAGE_FRIENDS, @friends.size].min, PER_PAGE_FRIENDS)
.map { |user| fetch_account_id(user) }
end
def friends_domains
return default_domains.sample(4) if @friends.empty?
return default_domains.sample(MIN_INSTANCES) if @friends.empty?
@friends.collect { |user| user&.mastodon&.uid }
.compact
.map { |uid| uid.split('@').last }
.inject(Hash.new(0)) { |h, k| h[k] += 1; h }
.sort_by { |k, v| -v }
.take(MAX_INSTANCES)
end
def default_domains
......
......@@ -13,6 +13,17 @@
- else
= render 'friends_grid', friends: @friends
- if @next_page || @page > 1
.pagination
- if @page > 1
= link_to friends_path(page: @page - 1) do
= fa_icon('chevron-left fw')
Previous
- if @next_page
= link_to friends_path(page: @next_page) do
Next
= fa_icon('chevron-right fw')
.page-heading
%h3
Your friends' instances
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment