Merge branch 'dev_aliyun' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_aliyun
commit
257e53f188
@ -0,0 +1,2 @@
|
|||||||
|
// Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
// All this logic will automatically be available in application.js.
|
@ -0,0 +1,2 @@
|
|||||||
|
// Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
// All this logic will automatically be available in application.js.
|
@ -0,0 +1,3 @@
|
|||||||
|
// Place all the styles related to the forums controller here.
|
||||||
|
// They will automatically be included in application.css.
|
||||||
|
// You can use Sass (SCSS) here: http://sass-lang.com/
|
@ -1,7 +1,7 @@
|
|||||||
module PaginateHelper
|
module PaginateHelper
|
||||||
def paginate(objs, **opts)
|
def paginate(objs, **opts)
|
||||||
page = params[:page].to_i <= 0 ? 1 : params[:page].to_i
|
page = params[:page].to_i <= 0 ? 1 : params[:page].to_i
|
||||||
per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : opts[:per_page] || 20
|
per_page = params[:per_page].to_i > 0 && params[:per_page].to_i < 50 ? params[:per_page].to_i : opts[:per_page] || 20
|
||||||
|
|
||||||
Kaminari.paginate_array(objs).page(page).per(per_page)
|
Kaminari.paginate_array(objs).page(page).per(per_page)
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
class ForumsController < ApplicationController
|
||||||
|
end
|
@ -0,0 +1,23 @@
|
|||||||
|
class Users::PrivateMessageDetailsController < Users::BaseController
|
||||||
|
before_action :private_user_resources!
|
||||||
|
|
||||||
|
after_action :update_message_status, only: [:show]
|
||||||
|
|
||||||
|
def show
|
||||||
|
messages = observed_user.private_messages.without_deleted.where(target: target_user)
|
||||||
|
|
||||||
|
@count = messages.count
|
||||||
|
@messages = messages.order(send_time: :asc).includes(sender: :user_extension)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def target_user
|
||||||
|
@_target_user ||= User.find(params[:target_id])
|
||||||
|
end
|
||||||
|
|
||||||
|
# 置为已读
|
||||||
|
def update_message_status
|
||||||
|
observed_user.private_messages.only_unread.where(target: target_user).update_all(status: 1)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,39 @@
|
|||||||
|
class Users::PrivateMessagesController < Users::BaseController
|
||||||
|
before_action :private_user_resources!
|
||||||
|
after_action :update_onclick_time!, only: [:index]
|
||||||
|
|
||||||
|
def index
|
||||||
|
@count = observed_user.private_messages.without_deleted.group(:target_id).count.count
|
||||||
|
|
||||||
|
subquery = observed_user.private_messages.without_deleted.order(send_time: :desc).to_sql
|
||||||
|
query = "SELECT subquery.*, COUNT(*) message_count FROM (#{subquery}) subquery "\
|
||||||
|
"GROUP BY subquery.target_id ORDER BY subquery.send_time desc LIMIT #{limit_value} OFFSET #{offset_value}"
|
||||||
|
@messages = PrivateMessage.select('*').from("(#{query}) AS query").includes(target: :user_extension)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
receiver = User.find_by(id: params[:target_id])
|
||||||
|
return render_error('用户未找到') if receiver.blank?
|
||||||
|
|
||||||
|
@message = PrivateMessages::CreateService.call(observed_user, receiver, create_params)
|
||||||
|
rescue PrivateMessages::CreateService::Error => ex
|
||||||
|
render_error(ex.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
message = observed_user.private_messages.without_deleted.find(params[:id])
|
||||||
|
message.destroy!
|
||||||
|
|
||||||
|
render_ok
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def update_onclick_time!
|
||||||
|
current_user.onclick_time.touch(:onclick_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_params
|
||||||
|
params.permit(:content)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,8 @@
|
|||||||
|
class Users::RecentContactsController < Users::BaseController
|
||||||
|
before_action :private_user_resources!
|
||||||
|
|
||||||
|
def index
|
||||||
|
contacts = observed_user.recent_contacts.distinct
|
||||||
|
@contacts = contacts.order('private_messages.created_at DESC').limit(10).includes(:user_extension)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,12 @@
|
|||||||
|
class Users::UnreadMessageInfosController < Users::BaseController
|
||||||
|
before_action :private_user_resources!
|
||||||
|
|
||||||
|
def show
|
||||||
|
click_time = observed_user.click_time
|
||||||
|
|
||||||
|
unread_tiding_count = observed_user.tidings.where('created_at > ?', click_time).count
|
||||||
|
unread_message_count = observed_user.private_messages.only_unread.group(:target_id).count.count
|
||||||
|
|
||||||
|
render_ok(unread_tiding_count: unread_tiding_count, unread_message_count: unread_message_count)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,17 @@
|
|||||||
|
class UsersForPrivateMessagesController < ApplicationController
|
||||||
|
before_action :require_login, :check_auth
|
||||||
|
|
||||||
|
def index
|
||||||
|
users = User.active.where.not(id: current_user.id)
|
||||||
|
|
||||||
|
keyword = params[:keyword].to_s.strip
|
||||||
|
if keyword.blank?
|
||||||
|
@users = []
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
users = users.where('LOWER(concat(lastname, firstname, nickname)) LIKE ?', "%#{keyword}%")
|
||||||
|
|
||||||
|
@users = users.limit(10).includes(:user_extension)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,9 @@
|
|||||||
|
module PrivateMessageDecorator
|
||||||
|
extend ApplicationDecorator
|
||||||
|
|
||||||
|
display_time_method :send_time
|
||||||
|
|
||||||
|
def unread?
|
||||||
|
status.zero?
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,2 @@
|
|||||||
|
module ForumsHelper
|
||||||
|
end
|
@ -0,0 +1,2 @@
|
|||||||
|
class Forum < ApplicationRecord
|
||||||
|
end
|
@ -1,3 +1,9 @@
|
|||||||
class PrivateMessage < ApplicationRecord
|
class PrivateMessage < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
belongs_to :target, class_name: "User"
|
||||||
|
belongs_to :sender, class_name: "User"
|
||||||
|
belongs_to :receiver, class_name: "User"
|
||||||
|
|
||||||
|
scope :without_deleted, -> { where.not(status: 2) }
|
||||||
|
scope :only_unread, -> { where(status: 0) }
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
class PrivateMessages::CreateService < ApplicationService
|
||||||
|
Error = Class.new(StandardError)
|
||||||
|
|
||||||
|
attr_reader :sender, :receiver, :params
|
||||||
|
|
||||||
|
def initialize(sender, receiver, **params)
|
||||||
|
@sender = sender
|
||||||
|
@receiver = receiver
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
validate!
|
||||||
|
|
||||||
|
same_attr = { sender: sender, receiver: receiver, content: content, send_time: Time.now }
|
||||||
|
|
||||||
|
message = nil
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
message = sender.private_messages.create!(same_attr.merge(target: receiver, status: 1))
|
||||||
|
receiver.private_messages.create!(same_attr.merge(target: sender, status: 0))
|
||||||
|
end
|
||||||
|
message
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def content
|
||||||
|
@_content ||= params[:content].to_s.strip
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate!
|
||||||
|
raise Error, '内容不能为空' if content.blank?
|
||||||
|
raise Error, '内容太长' if content.size > 255
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,11 @@
|
|||||||
|
json.count @count
|
||||||
|
json.messages do
|
||||||
|
json.array! @messages.each do |message|
|
||||||
|
json.extract! message, :id, :user_id, :receiver_id, :sender_id, :content
|
||||||
|
|
||||||
|
json.send_time message.display_send_time
|
||||||
|
json.sender do
|
||||||
|
json.partial! 'users/user_simple', user: message.sender
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,10 @@
|
|||||||
|
json.status 0
|
||||||
|
json.message 'success'
|
||||||
|
json.private_message do
|
||||||
|
json.extract! @message, :id, :user_id, :receiver_id, :sender_id, :content
|
||||||
|
|
||||||
|
json.send_time @message.display_send_time
|
||||||
|
json.sender do
|
||||||
|
json.partial! 'users/user_simple', user: @message.sender
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,13 @@
|
|||||||
|
json.count @count
|
||||||
|
json.private_messages do
|
||||||
|
json.array! @messages.each do |message|
|
||||||
|
json.extract! message, :id, :content, :message_count
|
||||||
|
|
||||||
|
json.unread message.unread?
|
||||||
|
json.send_time message.display_send_time
|
||||||
|
|
||||||
|
json.target do
|
||||||
|
json.partial! 'users/user_simple', user: message.target
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,2 @@
|
|||||||
|
json.users @contacts, partial: 'users/user_simple', as: :user
|
||||||
|
json.count @contacts.size
|
@ -0,0 +1,2 @@
|
|||||||
|
json.users @users, partial: 'users/user_simple', as: :user
|
||||||
|
json.count @users.size
|
@ -0,0 +1,8 @@
|
|||||||
|
class CreateForums < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :forums do |t|
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe ForumsController, type: :controller do
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,15 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
# Specs in this file have access to a helper object that includes
|
||||||
|
# the ForumsHelper. For example:
|
||||||
|
#
|
||||||
|
# describe ForumsHelper do
|
||||||
|
# describe "string concat" do
|
||||||
|
# it "concats two strings with spaces" do
|
||||||
|
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
RSpec.describe ForumsHelper, type: :helper do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Forum, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
Loading…
Reference in new issue