forked from Gitlink/forgeplus
修改个人主页
This commit is contained in:
parent
e8e3a45227
commit
c977b9350c
|
@ -1,7 +1,7 @@
|
|||
class UsersController < ApplicationController
|
||||
|
||||
before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects]
|
||||
before_action :check_user_exist, only: [:show, :homepage_info]
|
||||
before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users]
|
||||
before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users]
|
||||
before_action :require_login, only: %i[me list projects]
|
||||
skip_before_action :check_sign, only: [:attachment_show]
|
||||
|
||||
|
@ -11,7 +11,34 @@ class UsersController < ApplicationController
|
|||
@users = paginate(scope)
|
||||
end
|
||||
|
||||
def show;end
|
||||
def show
|
||||
#待办事项,现在未做
|
||||
@undo_events = 0
|
||||
#用户的组织数量
|
||||
# @user_composes_count = @user.composes.size
|
||||
@user_composes_count = 0
|
||||
user_projects = User.current.logged? && (User.current.admin? || User.current.login == @user.login) ? @user.projects : @user.projects.visible
|
||||
@projects_common_count = user_projects.common.size
|
||||
@projects_mirrior_count = user_projects.mirror.size
|
||||
end
|
||||
|
||||
def watch_users
|
||||
watchers = Watcher.watching_users(@user.id).includes(:user).order("watchers.created_at asc")
|
||||
if params[:search].present?
|
||||
search_user_ids = User.where(id: watchers.pluck(:watchable_id)).like(params[:search]).pluck(:id)
|
||||
watchers = watchers.where(watchable_id: search_user_ids)
|
||||
end
|
||||
@watchers_count = watchers.size
|
||||
@watchers = paginate(watchers)
|
||||
end
|
||||
|
||||
def fan_users
|
||||
watchers = @user.watchers.includes(:user).order("watchers.created_at asc")
|
||||
watchers = watchers.joins(:user).where("LOWER(concat(users.lastname, users.firstname, users.login)) LIKE ?", "%#{params[:search].split(" ").join('|')}%") if params[:search].present?
|
||||
|
||||
@watchers_count = watchers.size
|
||||
@watchers = paginate(watchers)
|
||||
end
|
||||
|
||||
def update
|
||||
@user = User.find params[:id]
|
||||
|
@ -85,7 +112,7 @@ class UsersController < ApplicationController
|
|||
@projects = projects.select(:id, :name)
|
||||
end
|
||||
|
||||
# 个人主页信息
|
||||
#TODO 个人主页信息,forge上弃用-hs, 0602
|
||||
def homepage_info
|
||||
#待办事项,现在未做
|
||||
@undo_events = 10
|
||||
|
@ -119,7 +146,8 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def projects
|
||||
scope = Projects::ListMyQuery.call(params.merge(category: params[:category],is_public: params[:status]), @user)
|
||||
is_current_admin_user = User.current.logged? && (current_user&.admin? || current_user.id == @user.id)
|
||||
scope = Projects::ListMyQuery.call(params, @user,is_current_admin_user)
|
||||
@total_count = scope.size
|
||||
@projects = paginate(scope)
|
||||
end
|
||||
|
|
|
@ -70,6 +70,8 @@ class User < ApplicationRecord
|
|||
# 关注
|
||||
has_many :be_watchers, foreign_key: :user_id, dependent: :destroy # 我的关注
|
||||
has_many :be_watcher_users, through: :be_watchers, dependent: :destroy # 我关注的用户
|
||||
|
||||
has_many :watchers, as: :watchable, dependent: :destroy
|
||||
|
||||
# 认证
|
||||
has_many :apply_user_authentication
|
||||
|
@ -143,13 +145,13 @@ class User < ApplicationRecord
|
|||
|
||||
# 关注数
|
||||
def follow_count
|
||||
Watcher.where(user_id: id, watchable_type: %w(Principal User)).count
|
||||
Watcher.where(user_id: 1, watchable_type: %w(User)).count
|
||||
# User.watched_by(id).count
|
||||
end
|
||||
|
||||
# 粉丝数
|
||||
def fan_count
|
||||
Watcher.where(watchable_type: %w(Principal User), watchable_id: id).count
|
||||
Watcher.where(watchable_type: %w(User), watchable_id: id).count
|
||||
# watchers.count
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
class Watcher < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
belongs_to :watchable, polymorphic: true, counter_cache: :watchers_count
|
||||
|
||||
scope :watching_users, ->(watchable_id){ where("watchable_type = ? and user_id = ?",'User',watchable_id)}
|
||||
end
|
||||
|
|
|
@ -1,32 +1,48 @@
|
|||
class Projects::ListMyQuery < ApplicationQuery
|
||||
|
||||
attr_reader :params, :user
|
||||
attr_reader :params, :user, :is_current_admin_user
|
||||
|
||||
# sort_columns :updated_on, :created_on, :forked_count, :praises_count, default_by: :updated_on, default_direction: :desc
|
||||
|
||||
def initialize(params,user)
|
||||
def initialize(params,user,is_current_admin_user)
|
||||
@params = params
|
||||
@user = user
|
||||
@is_current_admin_user = is_current_admin_user
|
||||
end
|
||||
|
||||
def call
|
||||
if params[:category].blank?
|
||||
projects = Project.joins(:members).where(members: { user_id: user.id })
|
||||
elsif params[:category].to_s == "manage"
|
||||
projects = Project.where(user_id: user.id)
|
||||
elsif params[:category].to_s == "watched" #我关注的
|
||||
projects = Project.joins(:watchers).where("watchable_type = ? and user_id = ?", "Project", user.id)
|
||||
elsif params[:category].to_s == "forked" #我fork的
|
||||
fork_ids = user.fork_users.select(:id, :fork_project_id).pluck(:fork_project_id)
|
||||
projects = Project.where(id: fork_ids)
|
||||
else
|
||||
projects = Project.where.not(user_id: user.id).joins(:members).where(members: { user_id: user.id })
|
||||
end
|
||||
unless params[:is_public].blank?
|
||||
projects = projects.where(is_public: (params[:is_public].to_s == "publicly"))
|
||||
if is_current_admin_user
|
||||
projects = Project
|
||||
else
|
||||
projects = Project.visible
|
||||
end
|
||||
|
||||
scope = projects.includes(:project_category,:project_score, :project_language,:owner)
|
||||
if params[:category].blank?
|
||||
projects = projects.joins(:members).where(members: { user_id: user.id })
|
||||
elsif params[:category].to_s == "join"
|
||||
projects = projects.where.not(user_id: user.id).joins(:members).where(members: { user_id: user.id })
|
||||
elsif params[:category].to_s == "manage"
|
||||
projects = projects.where(user_id: user.id)
|
||||
elsif params[:category].to_s == "watched" #我关注的
|
||||
projects = projects.where.not(user_id: user.id).joins(:watchers).where(watchers: {watchable_type: "Project", user_id: user.id})
|
||||
elsif params[:category].to_s == "forked" #我fork的
|
||||
fork_ids = user.fork_users.select(:id, :fork_project_id).pluck(:fork_project_id)
|
||||
projects = projects.where(id: fork_ids)
|
||||
elsif params[:category].to_s == "public"
|
||||
projects = projects.visible.joins(:members).where(members: { user_id: user.id })
|
||||
elsif params[:category].to_s == "private"
|
||||
projects = projects.is_private.joins(:members).where(members: { user_id: user.id })
|
||||
end
|
||||
|
||||
if params[:project_type].to_s === "common"
|
||||
projects = projects.common
|
||||
elsif params[:project_type].to_s === "mirror"
|
||||
projects = projects.mirror
|
||||
end
|
||||
|
||||
q = projects.ransack(name_or_identifier_cont: params[:search])
|
||||
|
||||
scope = q.result.includes(:project_category, :project_language,:owner, :repository)
|
||||
|
||||
sort = params[:sort_by] || "updated_on"
|
||||
sort_direction = params[:sort_direction] || "desc"
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
user = project.owner
|
||||
if user.blank?
|
||||
nil
|
||||
else
|
||||
json.id project.id
|
||||
json.repo_id project&.repository&.id
|
||||
json.identifier project.identifier
|
||||
json.name project.name
|
||||
json.description Nokogiri::HTML(project.description).text
|
||||
json.visits project.visits
|
||||
json.praises_count project.praises_count.to_i
|
||||
json.forked_count project.forked_count.to_i
|
||||
json.is_public project.is_public
|
||||
json.mirror_url project.repository&.mirror_url
|
||||
json.last_update_time render_unix_time(project.updated_on)
|
||||
json.time_ago time_from_now(project.updated_on)
|
||||
json.forked_from_project_id project.forked_from_project_id
|
||||
json.author do
|
||||
json.name user.try(:show_real_name)
|
||||
json.login user.login
|
||||
json.image_url url_to_avatar(project.owner)
|
||||
end
|
||||
json.category do
|
||||
if project.project_category.blank?
|
||||
json.nil!
|
||||
else
|
||||
json.id project.project_category.id
|
||||
json.name project.project_category.name
|
||||
end
|
||||
end
|
||||
json.language do
|
||||
if project.project_language.blank?
|
||||
json.nil!
|
||||
else
|
||||
json.id project.project_language.id
|
||||
json.name project.project_language.name
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,40 +1,4 @@
|
|||
json.total_count @total_count
|
||||
json.projects @projects do |project|
|
||||
user = project.owner
|
||||
next if user.blank?
|
||||
|
||||
json.id project.id
|
||||
json.repo_id project&.repository.id
|
||||
json.identifier project.identifier
|
||||
json.name project.name
|
||||
json.description Nokogiri::HTML(project.description).text
|
||||
json.visits project.visits
|
||||
json.praises_count project.praises_count.to_i
|
||||
json.forked_count project.forked_count.to_i
|
||||
json.is_public project.is_public
|
||||
json.mirror_url project.repository&.mirror_url
|
||||
json.last_update_time render_unix_time(project.updated_on)
|
||||
json.time_ago time_from_now(project.updated_on)
|
||||
json.forked_from_project_id project.forked_from_project_id
|
||||
json.author do
|
||||
json.name user.try(:show_real_name)
|
||||
json.login user.login
|
||||
json.image_url url_to_avatar(project.owner)
|
||||
end
|
||||
json.category do
|
||||
if project.project_category.blank?
|
||||
json.nil!
|
||||
else
|
||||
json.id project.project_category.id
|
||||
json.name project.project_category.name
|
||||
end
|
||||
end
|
||||
json.language do
|
||||
if project.project_language.blank?
|
||||
json.nil!
|
||||
else
|
||||
json.id project.project_language.id
|
||||
json.name project.project_language.name
|
||||
end
|
||||
end
|
||||
json.partial! "/projects/project_detail", project: project
|
||||
end
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
user = target.watchable
|
||||
|
||||
json.format_time target.created_at.strftime("%Y-%m-%d")
|
||||
json.name user.try(:show_real_name)
|
||||
json.login user.try(:login)
|
||||
json.image_url url_to_avatar(user)
|
||||
json.is_current_user current_user.try(:id) == target.user_id
|
||||
json.is_watch current_user&.watched?(user)
|
|
@ -0,0 +1,4 @@
|
|||
json.count @watchers_count
|
||||
json.users do
|
||||
json.partial! "/users/watch_user_detail", collection: @watchers, as: :target
|
||||
end
|
|
@ -12,6 +12,5 @@ json.user_phone_binded @user.phone.present?
|
|||
# json.email @user.mail
|
||||
json.profile_completed @user.profile_completed?
|
||||
json.professional_certification @user.professional_certification
|
||||
json.is_watch current_user&.watched?(@user)
|
||||
|
||||
|
||||
|
|
|
@ -1,24 +1,6 @@
|
|||
json.count @total_count
|
||||
json.projects do
|
||||
json.array! @projects do |project|
|
||||
user = project&.owner
|
||||
json.members_count project.members_count
|
||||
json.issues_count project.issues_count
|
||||
json.changesets_count project&.project_score&.changeset_num.to_i
|
||||
json.can_visited project.can_visited?
|
||||
json.id project.id
|
||||
json.identifier project.identifier
|
||||
json.name project.name
|
||||
json.is_public project.is_public
|
||||
json.owner do
|
||||
json.real_name user.present? ? user.try(:real_name) : "未知用户"
|
||||
json.avatar_url user.present? ? url_to_avatar(user) : "avatars/User/b"
|
||||
end
|
||||
json.category do
|
||||
json.name project&.project_category&.name
|
||||
end
|
||||
json.language do
|
||||
json.name project&.project_language&.name
|
||||
end
|
||||
json.partial! "/projects/project_detail", project: project
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1 +1,16 @@
|
|||
json.partial! 'users/user', locals: { user: @user }
|
||||
# json.partial! 'users/user', locals: { user: @user }
|
||||
|
||||
json.username @user.full_name
|
||||
json.real_name @user.real_name
|
||||
json.login @user.login
|
||||
json.user_id @user.id
|
||||
json.image_url url_to_avatar(@user)
|
||||
json.admin @user.admin?
|
||||
json.user_identity @user.identity
|
||||
json.is_watch current_user&.watched?(@user)
|
||||
json.watched_count @user.fan_count #粉丝
|
||||
json.watching_count @user.follow_count #关注数
|
||||
json.undo_events @undo_events
|
||||
json.user_composes_count @user_composes_count
|
||||
json.common_projects_count @projects_common_count
|
||||
json.mirror_projects_count @projects_mirrior_count
|
|
@ -0,0 +1,4 @@
|
|||
json.count @watchers_count
|
||||
json.users do
|
||||
json.partial! "/users/watch_user_detail", collection: @watchers, as: :target
|
||||
end
|
|
@ -140,6 +140,8 @@ Rails.application.routes.draw do
|
|||
member do
|
||||
get :homepage_info
|
||||
get :projects
|
||||
get :watch_users
|
||||
get :fan_users
|
||||
end
|
||||
collection do
|
||||
post :following
|
||||
|
|
Loading…
Reference in New Issue