Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add profile links from other communities #1409

Merged
merged 10 commits into from
Sep 20, 2024
7 changes: 6 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UsersController < ApplicationController
:annotate, :annotations, :mod_privileges, :mod_privilege_action]
before_action :set_user, only: [:show, :mod, :destroy, :soft_delete, :posts, :role_toggle, :full_log, :activity,
:annotate, :annotations, :mod_privileges, :mod_privilege_action,
:vote_summary, :avatar]
:vote_summary, :network, :avatar]
before_action :check_deleted, only: [:show, :posts, :activity]

def index
Expand Down Expand Up @@ -201,6 +201,11 @@ def posts
end
end

def network
@communities = Community.all
render layout: 'without_sidebar'
end

def activity
@posts = Post.undeleted.where(user: @user).count
@comments = Comment.joins(:comment_thread, :post).undeleted.where(user: @user, comment_threads: { deleted: false },
Expand Down
10 changes: 7 additions & 3 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ def rtl_safe_username(user)

def user_link(user, url_opts = nil, **link_opts)
url_opts ||= {}
if deleted_user?(user)
link_to 'deleted user', '#', { dir: 'ltr' }.merge(link_opts)
anchortext = link_opts[:anchortext]
link_opts_reduced = { dir: 'ltr' }.merge(link_opts).except(:anchortext)
if !anchortext.nil?
link_to anchortext, user_url(user, **url_opts), { dir: 'ltr' }.merge(link_opts)
elsif deleted_user?(user)
link_to 'deleted user', '#', link_opts_reduced
else
link_to user.rtl_safe_username, user_url(user, **url_opts), { dir: 'ltr' }.merge(link_opts)
link_to user.rtl_safe_username, user_url(user, **url_opts), link_opts_reduced
end
end

Expand Down
5 changes: 5 additions & 0 deletions app/models/community_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def suspended?
false
end

# All undeleted posts on this community by this user.
def post_count
Post.unscoped.where(community_id: community_id).where(user: user).undeleted.count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be expensive when run for multiple users and/or communities on one page. We're probably alright at current scale, but we should ultimately cache this on the CommunityUser model for each community.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that the community-specific post count shows up other places, including on the profile, where we call @user.posts.undeleted.count. But I'm not quite sure how to do that for a non-local user -- any advice? We're calling this from a user on site A and asking about the user on site B, C, D... .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #1411 after discussion with @ArtOfCode- .

end

# Calculation functions for privilege scores
# These are quite expensive, so we'll cache them for a while
def post_score
Expand Down
21 changes: 21 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ def is_admin
is_global_admin || community_user&.is_admin || false
end

# Used by network profile: does this user have a profile on that other comm?
def has_profile_on(community_id)
cu = community_users.where(community_id: community_id).first
!cu&.user_id.nil? || false
end

def reputation_on(community_id)
cu = community_users.where(community_id: community_id).first
cu&.reputation || 1
end

def post_count_on(community_id)
cu = community_users.where(community_id: community_id).first
cu&.post_count || 0
end

def is_moderator_on(community_id)
cu = community_users.where(community_id: community_id).first
cu&.is_moderator || cu&.privilege?('mod')
end

def has_ability_on(community_id, ability_internal_id)
cu = community_users.where(community_id: community_id).first
if cu&.is_moderator || cu&.is_admin || is_global_moderator || is_global_admin || cu&.privilege?('mod')
Expand Down
22 changes: 22 additions & 0 deletions app/views/users/_network.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="table is-with-hover is-full-width">
<tr>
<th>Profile on Community</th>
<th>Posts</th>
<th>Reputation</th>
</tr>

<% @communities.each do |c| %>
<% if @user.has_profile_on(c) %>
<tr>
<td><%= user_link @user, { host: c.host}, anchortext: c.name %>
<% if @user.is_moderator_on(c) %>
(moderator)
<% end %>
</td>
<td><%= @user.post_count_on(c) %> </td>
<td><%= @user.reputation_on(c) %> </td>
</tr>
<% end %>
<% end %>

</table>
5 changes: 4 additions & 1 deletion app/views/users/_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
Notifications
<% end %>
<% end %>
</div>
<%= link_to network_path(user), class: "tabs--item #{current_page?(network_path(user)) ? 'is-active' : ''}" do %>
All Communities
<% end %>
</div>
10 changes: 10 additions & 0 deletions app/views/users/network.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= render 'tabs', user: @user %>

<h1>Profiles for <%= user_link @user %></h1>

<p>
Links to profiles on other communities on this network.
</p>

<%= render 'network' %>

2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
get '/edit/profile', to: 'users#edit_profile', as: :edit_user_profile
patch '/edit/profile', to: 'users#update_profile', as: :update_user_profile
get '/me/vote-summary', to: 'users#my_vote_summary', as: :my_vote_summary
get '/me/network', to: 'users#my_network', as: :my_network
get '/avatar/:letter/:color/:size', to: 'users#specific_avatar', as: :specific_auto_avatar
get '/disconnect-sso', to: 'users#disconnect_sso', as: :user_disconnect_sso
post '/disconnect-sso', to: 'users#confirm_disconnect_sso', as: :user_confirm_disconnect_sso
Expand All @@ -202,6 +203,7 @@
get '/:id/mod', to: 'users#mod', as: :mod_user
get '/:id/posts', to: 'users#posts', as: :user_posts
get '/:id/vote-summary', to: 'users#vote_summary', as: :vote_summary
get '/:id/network', to: 'users#network', as: :network
get '/:id/mod/privileges', to: 'users#mod_privileges', as: :user_privileges
post '/:id/mod/privileges', to: 'users#mod_privilege_action', as: :user_privilege_action
post '/:id/mod/toggle-role', to: 'users#role_toggle', as: :toggle_user_role
Expand Down
Loading