diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 14c296906..7f49ab614 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,8 +2,8 @@ class UsersController < ApplicationController include ApplicationHelper include Ci::DbConnectable - 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 :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users, :hovercard] + before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users, :hovercard] before_action :require_login, only: %i[me list sync_user_info] before_action :connect_to_ci_db, only: [:get_user_info] skip_before_action :check_sign, only: [:attachment_show] @@ -56,6 +56,9 @@ class UsersController < ApplicationController @watchers = paginate(watchers) end + def hovercard + end + def update @user = User.find params[:id] @user.update!(user_params) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b22954bb1..ccc45df0e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -35,12 +35,6 @@ module ApplicationHelper course.course_modules.find_by(module_type: "graduation").try(:id) end - # 是否关注 - # from_user_id为被关注的用户 - def follow?(from_user_id, user_id) - Watcher.where(watchable_type: 'Principal', watchable_id: from_user_id, user_id: user_id).exists? - end - # git用户 # git用户命名规则:login+"@educoder.net" def git_username(email) diff --git a/app/models/concerns/watchable.rb b/app/models/concerns/watchable.rb index 4c52cf99a..3ab43d95a 100644 --- a/app/models/concerns/watchable.rb +++ b/app/models/concerns/watchable.rb @@ -6,6 +6,7 @@ module Watchable has_many :watcher_users, through: :watchers, source: :user, validate: false scope :watched_by, -> (user_id) { includes(:watchers).where(watchers: { user_id: user_id }) } + scope :following, -> (user_id) { watched_by } end def watched?(watchable) @@ -21,6 +22,24 @@ module Watchable obj.destroy! if obj.present? end + # 我正在关注的、我追随的 + def following + User.following(self.id) + end + + def following_count + following.size + end + + # 关注我的、我的粉丝、我的追随者 + def followers + watcher_users + end + + def followers_count + followers.size + end + module ClassMethods end end diff --git a/app/models/user.rb b/app/models/user.rb index 2bc2dc2c9..e16e5ca02 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -137,10 +137,6 @@ class User < Owner has_many :attachments,foreign_key: :author_id, :dependent => :destroy - # 关注 - # has_many :be_watchers, foreign_key: :user_id, dependent: :destroy # 我的关注 - # has_many :be_watcher_users, through: :be_watchers, dependent: :destroy # 我关注的用户 - has_one :ci_cloud_account, class_name: 'Ci::CloudAccount', dependent: :destroy # 认证 diff --git a/app/views/users/hovercard.json.jbuilder b/app/views/users/hovercard.json.jbuilder new file mode 100644 index 000000000..d74a14314 --- /dev/null +++ b/app/views/users/hovercard.json.jbuilder @@ -0,0 +1,15 @@ +json.id @user.id +json.login @user.login +json.name @user.full_name +json.image_url url_to_avatar(@user) +json.url "#{request.base_url }/users/#{@user.login}" +json.followers_count @user.followers_count +json.following_count @user.following_count +json.projects_count @user.projects_count +json.is_watch current_user&.watched?(@user) +json.organizations @user.organizations do |organization| + json.login organization.login + json.name organization.full_name + json.image_url url_to_avatar(organization) + json.url "#{request.base_url }/organize/#{organization.login}" +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 66c9d97af..bbcc29231 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -198,6 +198,7 @@ Rails.application.routes.draw do get :projects get :watch_users get :fan_users + get :hovercard end collection do post :following