Browse Source

ADD users/hovercard api

tags/v3.0.2^2
jasder 5 years ago
parent
commit
908cda56f3
6 changed files with 40 additions and 12 deletions
  1. +5
    -2
      app/controllers/users_controller.rb
  2. +0
    -6
      app/helpers/application_helper.rb
  3. +19
    -0
      app/models/concerns/watchable.rb
  4. +0
    -4
      app/models/user.rb
  5. +15
    -0
      app/views/users/hovercard.json.jbuilder
  6. +1
    -0
      config/routes.rb

+ 5
- 2
app/controllers/users_controller.rb View File

@@ -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)


+ 0
- 6
app/helpers/application_helper.rb View File

@@ -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)


+ 19
- 0
app/models/concerns/watchable.rb View File

@@ -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

+ 0
- 4
app/models/user.rb View File

@@ -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

# 认证


+ 15
- 0
app/views/users/hovercard.json.jbuilder View File

@@ -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

+ 1
- 0
config/routes.rb View File

@@ -198,6 +198,7 @@ Rails.application.routes.draw do
get :projects
get :watch_users
get :fan_users
get :hovercard
end
collection do
post :following


Loading…
Cancel
Save