list of users following/followed by user

This commit is contained in:
Dylan Knutson
2025-02-28 02:37:39 +00:00
parent fbd146484f
commit 87993562eb
5 changed files with 93 additions and 24 deletions

View File

@@ -3,7 +3,7 @@ class Domain::UsersController < DomainController
extend T::Sig
extend T::Helpers
before_action :set_user!, only: %i[show]
before_action :set_user!, only: %i[show followed_by following]
before_action :set_post!, only: %i[users_faving_post]
skip_before_action :authenticate_user!,
only: %i[
@@ -20,6 +20,50 @@ class Domain::UsersController < DomainController
@users = policy_scope(Domain::User).order(created_at: :desc)
end
sig(:final) { void }
def followed_by
@user = T.must(@user)
authorize @user
@users =
@user
.followed_by_users
.includes(avatar: :log_entry)
.page(params[:page])
.per(50)
@index_type = :followed_by
render :index
end
sig(:final) { void }
def following
@user = T.must(@user)
authorize @user
@users =
@user
.followed_users
.includes(avatar: :log_entry)
.page(params[:page])
.per(50)
@index_type = :following
render :index
end
sig(:final) { void }
def users_faving_post
@post = T.must(@post)
authorize @post
@users =
T
.unsafe(@post)
.faving_users
.includes(avatar: :log_entry)
.page(params[:page])
.per(50)
@index_type = :users_faving_post
render :index
end
# GET /users/:id
sig(:final) { void }
def show
@@ -58,19 +102,6 @@ class Domain::UsersController < DomainController
.limit(10)
end
sig(:final) { void }
def users_faving_post
@post = T.must(@post)
authorize @post
@user_post_favs =
@post
.user_post_favs
.includes(user: :avatar)
.page(params[:page])
.per(50)
.without_count
end
sig { void }
def similar_users
url_name = params[:url_name]

View File

@@ -94,6 +94,16 @@ module Domain::UsersHelper
"#{domain_user_path(user)}/favorites"
end
sig { params(user: Domain::User).returns(String) }
def domain_user_followed_by_path(user)
"#{domain_user_path(user)}/followed_by"
end
sig { params(user: Domain::User).returns(String) }
def domain_user_following_path(user)
"#{domain_user_path(user)}/following"
end
class StatRow < T::ImmutableStruct
include T::Struct::ActsAsComparable
@@ -114,15 +124,19 @@ module Domain::UsersHelper
rows << StatRow.new(name: "Favorites", value: user.user_post_favs.count)
end
if user.has_followed_by_users?
can_view_link = policy(user).followed_by?
rows << StatRow.new(
name: "Followed by",
value: user.user_user_follows_to.count,
link_to: can_view_link ? domain_user_followed_by_path(user) : nil,
)
end
if user.has_followed_users?
can_view_link = policy(user).following?
rows << StatRow.new(
name: "Following",
value: user.user_user_follows_from.count,
link_to: can_view_link ? domain_user_following_path(user) : nil,
)
end

View File

@@ -36,4 +36,14 @@ class Domain::UserPolicy < ApplicationPolicy
def view_log_entries?
user&.admin? || false
end
sig { returns(T::Boolean) }
def followed_by?
user&.admin? || user&.moderator? || false
end
sig { returns(T::Boolean) }
def following?
user&.admin? || user&.moderator? || false
end
end

View File

@@ -3,23 +3,34 @@
<div class="flex items-center justify-between gap-4">
<div class="flex min-w-0 items-center gap-2">
<h1 class="text-lg font-medium">
Users who favorited
<%= link_to @post.title,
domain_post_path(@post),
class: "text-blue-600 hover:underline" %>
<% case @index_type %>
<% when :followed_by %>
<%= pluralize(@users.total_count, "user") %> following
<%= link_to @user.name,
domain_user_following_path(@user),
class: "text-blue-600 hover:underline" %>
<% when :following %>
<%= pluralize(@users.total_count, "user") %> followed by
<%= link_to @user.name,
domain_user_following_path(@user),
class: "text-blue-600 hover:underline" %>
<% when :users_faving_post %>
<%= pluralize(@users.total_count, "user") %> favorited
<%= link_to @post.title,
domain_post_path(@post),
class: "text-blue-600 hover:underline" %>
<% end %>
</h1>
</div>
</div>
</section>
<%= render "shared/pagination_controls", collection: @user_post_favs %>
<% favs = @user_post_favs.to_a %>
<% if favs.any? %>
<%= render "shared/pagination_controls", collection: @users %>
<% if @users.any? %>
<section
class="overflow-hidden rounded-md border border-slate-300 bg-slate-50"
>
<div class="divide-y divide-slate-200">
<% favs.each do |user_post_fav| %>
<% user = user_post_fav.user %>
<% @users.each do |user| %>
<%= link_to domain_user_path(user),
class: "flex items-center gap-4 p-4 hover:bg-slate-100" do %>
<% if user.avatar&.log_entry.present? %>
@@ -46,7 +57,7 @@
class="rounded-md border border-slate-300 bg-slate-50 p-8 text-center"
>
<i class="bi bi-heart mb-3 block text-4xl text-slate-400"></i>
<p class="text-slate-600">No users have favorited this post yet.</p>
<p class="text-slate-600">No users found.</p>
</section>
<% end %>
</div>

View File

@@ -29,6 +29,9 @@ Rails.application.routes.draw do
} do
get :search_by_name, on: :collection
get "followed_by", on: :member, action: :followed_by
get "following", on: :member, action: :following
resources :posts, only: [], controller: "domain/posts", path: "" do
get :favorites, on: :collection, action: :user_favorite_posts
get :posts, on: :collection, action: :user_created_posts