| @@ -2,8 +2,8 @@ class UsersController < ApplicationController | |||||
| include ApplicationHelper | include ApplicationHelper | ||||
| include Ci::DbConnectable | 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 :require_login, only: %i[me list sync_user_info] | ||||
| before_action :connect_to_ci_db, only: [:get_user_info] | before_action :connect_to_ci_db, only: [:get_user_info] | ||||
| skip_before_action :check_sign, only: [:attachment_show] | skip_before_action :check_sign, only: [:attachment_show] | ||||
| @@ -56,6 +56,9 @@ class UsersController < ApplicationController | |||||
| @watchers = paginate(watchers) | @watchers = paginate(watchers) | ||||
| end | end | ||||
| def hovercard | |||||
| end | |||||
| def update | def update | ||||
| @user = User.find params[:id] | @user = User.find params[:id] | ||||
| @user.update!(user_params) | @user.update!(user_params) | ||||
| @@ -35,12 +35,6 @@ module ApplicationHelper | |||||
| course.course_modules.find_by(module_type: "graduation").try(:id) | course.course_modules.find_by(module_type: "graduation").try(:id) | ||||
| end | 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用户 | ||||
| # git用户命名规则:login+"@educoder.net" | # git用户命名规则:login+"@educoder.net" | ||||
| def git_username(email) | def git_username(email) | ||||
| @@ -6,6 +6,7 @@ module Watchable | |||||
| has_many :watcher_users, through: :watchers, source: :user, validate: false | has_many :watcher_users, through: :watchers, source: :user, validate: false | ||||
| scope :watched_by, -> (user_id) { includes(:watchers).where(watchers: { user_id: user_id }) } | scope :watched_by, -> (user_id) { includes(:watchers).where(watchers: { user_id: user_id }) } | ||||
| scope :following, -> (user_id) { watched_by } | |||||
| end | end | ||||
| def watched?(watchable) | def watched?(watchable) | ||||
| @@ -21,6 +22,24 @@ module Watchable | |||||
| obj.destroy! if obj.present? | obj.destroy! if obj.present? | ||||
| end | 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 | module ClassMethods | ||||
| end | end | ||||
| end | end | ||||
| @@ -137,10 +137,6 @@ class User < Owner | |||||
| has_many :attachments,foreign_key: :author_id, :dependent => :destroy | 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 | has_one :ci_cloud_account, class_name: 'Ci::CloudAccount', dependent: :destroy | ||||
| # 认证 | # 认证 | ||||
| @@ -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 | |||||
| @@ -198,6 +198,7 @@ Rails.application.routes.draw do | |||||
| get :projects | get :projects | ||||
| get :watch_users | get :watch_users | ||||
| get :fan_users | get :fan_users | ||||
| get :hovercard | |||||
| end | end | ||||
| collection do | collection do | ||||
| post :following | post :following | ||||