customer_service_im 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 754772f211d22ecdfa10dff02258ee267011180f4db205900046454b68303bb8
4
- data.tar.gz: 378c649443b5f2c24ccf4c823f1af9d3d6fa8ab1f4efd230bf7abea6c6924ce9
3
+ metadata.gz: 51614b5350dfdda2b865dde53f6781b7d0e01a61fd4d9a52f807bacd0d4c1535
4
+ data.tar.gz: 546af3c9b523ce2ea30292fae46fc542ebe0b8c1bed3576192193bfd5b29dba6
5
5
  SHA512:
6
- metadata.gz: 27b0ccd8552c84377ec34ebdfe000e999ab4d3707b6d49b99c2a591e93610f8108d7149cb805e134d8d32a444d85af6e9b730ee69f49c6ae05c6eb293babdba9
7
- data.tar.gz: 5a13da61de2510fa5122eb9171e2e0c9cac0d0ed52036ba1fc36f703c9fedb33f301cc1432cc0b557beaf4ee8926cc1b568bd78635523008b8d5ebe1939dbcd8
6
+ metadata.gz: a76f3ff1b5673fd1ba393d123d95cb79408ac402a6fba35b35146513102dc0ecca49e2cf8b89709baec5fa31d2ce8df867832fd60950758d33a91b7aec083de5
7
+ data.tar.gz: 1a0ab5cd159159668367664ea402c7d0c9b82cfd1e9753e36c226237430c01c249875cb2323126a8e9a78ceebf63b4abe3922eda53444f5c173bb48cd995f602
@@ -10,6 +10,10 @@ module CustomerServiceIm
10
10
  render json: { errors: errors }, status: :unprocessable_entity
11
11
  end
12
12
 
13
+ def render_401(errors: nil)
14
+ render json: { errors: errors }, status: :unauthorized
15
+ end
16
+
13
17
  def render_200(data)
14
18
  render json: data
15
19
  end
@@ -17,7 +21,7 @@ module CustomerServiceIm
17
21
  private
18
22
 
19
23
  def token_user(user)
20
- payload = { user_type: user.class.name, user_id: user.id }
24
+ payload = { data: { user_type: user.class.name, user_id: user.id } }
21
25
  JWT.encode(payload, hmac_secret, 'HS256')
22
26
  end
23
27
 
@@ -34,9 +38,10 @@ module CustomerServiceIm
34
38
  token = request.headers[:token]
35
39
  payload, = JWT.decode(token, hmac_secret, true, { algorithm: 'HS256' })
36
40
  rescue JWT::DecodeError => e
41
+ # binding.pry
37
42
  logger = Logger.new Rails.root.join("log/jwt.log")
38
43
  logger.error("JWT Decode Error: #{e}")
39
- raise e
44
+ return false
40
45
  end
41
46
 
42
47
  payload["data"]["user_type"].classify.constantize.find(payload["data"]["user_id"])
@@ -1,6 +1,7 @@
1
1
 
2
2
  module CustomerServiceIm
3
3
  class ConversationsController < ApplicationController
4
+ before_action :authenticate_user!
4
5
  before_action :set_conversation, only: [:messages, :all_messages_read]
5
6
 
6
7
  def index
@@ -8,6 +9,18 @@ module CustomerServiceIm
8
9
  @total_count = @conversations.size
9
10
  end
10
11
 
12
+ def show; end
13
+
14
+ def create
15
+ @conversation = Conversation.new(user: logged_user)
16
+
17
+ if @conversation.save
18
+ render :show
19
+ else
20
+ render_500 errors: @conversation.errors
21
+ end
22
+ end
23
+
11
24
  def messages
12
25
  @messages = @conversation.messages
13
26
  .order('created_at desc')
@@ -1,5 +1,6 @@
1
1
  module CustomerServiceIm
2
2
  class MessagesController < ApplicationController
3
+ before_action :authenticate_user!
3
4
  before_action :set_conversation, only: [ :index ]
4
5
  before_action :set_message, only: [ :receive ]
5
6
 
@@ -0,0 +1,13 @@
1
+
2
+ module CustomerServiceIm
3
+ class StaffsController < CustomerServiceIm::ApplicationController
4
+ def login
5
+ staff = Staff.find_by!(email: params[:email])
6
+ if staff.authenticate(params[:password])
7
+ render_200 staff.as_json.merge({ token: token_user(staff) })
8
+ else
9
+ render_401(errors: "邮箱或密码错误")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module CustomerServiceIm
3
+ class UsersController < ApplicationController
4
+ def login
5
+ mp = MiniProgram::Client.new(appid: CustomerServiceIm.appid, app_secret: CustomerServiceIm.app_secret )
6
+ result = mp.login(params.require(:code))
7
+
8
+ if result.failure?
9
+ render_500 errors: result.errors
10
+ end
11
+
12
+ open_id = result.data[:openid]
13
+
14
+ user = User.find_or_create_mp_user(open_id: open_id)
15
+
16
+ render_200 user.as_json.merge({ token: token_user(user), conversation_id: user.conversation_ids.first })
17
+ end
18
+
19
+ def update
20
+ user = logged_user
21
+ if user.update(user_params)
22
+ render_200 result: :ok
23
+ else
24
+ render_500 errors: user.errors
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def user_params
31
+ params.require(:user).permit(:nickname, :avatar)
32
+ end
33
+ end
34
+ end
@@ -71,5 +71,14 @@ module CustomerServiceIm::Concerns::Customer
71
71
  def online_customer_list_in_redis
72
72
  redis.smembers online_customers_redis_key
73
73
  end
74
+
75
+ def find_or_create_mp_user(open_id)
76
+ return if open_id.blank?
77
+
78
+ User.create_with(
79
+ email: "#{SecureRandom.uuid}@mp.com",
80
+ password: Digest::MD5.hexdigest(open_id)
81
+ ).find_or_create_by(open_id: open_id)
82
+ end
74
83
  end
75
84
  end
@@ -5,6 +5,8 @@ module CustomerServiceIm
5
5
 
6
6
  validates_presence_of :name
7
7
 
8
+ has_secure_password
9
+
8
10
  def join_conversation(conversation)
9
11
  conversation.update(staff: self)
10
12
 
@@ -0,0 +1,2 @@
1
+
2
+ json.partial! "conversation", conversation: @conversation
@@ -0,0 +1,5 @@
1
+
2
+ MiniProgram.setup do |config|
3
+ config.appid = CustomerServiceIm.appid
4
+ config.app_secret = CustomerServiceIm.app_secret
5
+ end
data/config/routes.rb CHANGED
@@ -1,6 +1,12 @@
1
1
  CustomerServiceIm::Engine.routes.draw do
2
2
  mount ActionCable.server => "/cable"
3
3
 
4
+ resources :users, defaults: { format: :json } do
5
+ collection do
6
+ post :login
7
+ end
8
+ end
9
+
4
10
  resources :conversations, defaults: { format: :json } do
5
11
  member do
6
12
  get :messages
@@ -20,4 +26,10 @@ CustomerServiceIm::Engine.routes.draw do
20
26
  post :reply
21
27
  end
22
28
  end
29
+
30
+ resources :staffs do
31
+ collection do
32
+ post :login
33
+ end
34
+ end
23
35
  end
@@ -0,0 +1,5 @@
1
+ class AddPasswordDigestToCustomerServiceImStaffs < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :customer_service_im_staffs, :password_digest, :string
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddEmailToCustomerServiceImStaffs < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :customer_service_im_staffs, :email, :string
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module CustomerServiceIm
2
- VERSION = '0.2.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -1,10 +1,12 @@
1
1
  require "customer_service_im/version"
2
2
  require "customer_service_im/engine"
3
3
  require "customer_service_im/core_ext"
4
- require 'shoulda'
4
+ # require 'shoulda'
5
5
  require "jwt"
6
6
  require 'jbuilder'
7
7
  require "tx_asr"
8
+ require "redis"
9
+ require 'mini_program'
8
10
 
9
11
  module CustomerServiceIm
10
12
  # Your code goes here...
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: customer_service_im
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-18 00:00:00.000000000 Z
11
+ date: 2021-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -31,19 +31,19 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 6.1.4.1
33
33
  - !ruby/object:Gem::Dependency
34
- name: shoulda
34
+ name: shoulda-matchers
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '4.0'
39
+ version: 4.5.1
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '4.0'
46
+ version: 4.5.1
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jwt
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +100,34 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: redis
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: mini_program
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
103
131
  description: Description of CustomerServiceIm.
104
132
  email:
105
133
  - ianlynxk@gmail.com
@@ -118,6 +146,8 @@ files:
118
146
  - app/controllers/customer_service_im/application_controller.rb
119
147
  - app/controllers/customer_service_im/conversations_controller.rb
120
148
  - app/controllers/customer_service_im/messages_controller.rb
149
+ - app/controllers/customer_service_im/staffs_controller.rb
150
+ - app/controllers/customer_service_im/users_controller.rb
121
151
  - app/jobs/customer_service_im/application_job.rb
122
152
  - app/mailers/customer_service_im/application_mailer.rb
123
153
  - app/models/customer_service_im/application_record.rb
@@ -129,9 +159,11 @@ files:
129
159
  - app/views/customer_service_im/conversations/_message.json.jbuilder
130
160
  - app/views/customer_service_im/conversations/index.json.jbuilder
131
161
  - app/views/customer_service_im/conversations/messages.json.jbuilder
162
+ - app/views/customer_service_im/conversations/show.json.jbuilder
132
163
  - app/views/customer_service_im/messages/_message.json.jbuilder
133
164
  - app/views/customer_service_im/messages/index.json.jbuilder
134
165
  - config/cable.yml
166
+ - config/initializers/mini_program.rb
135
167
  - config/initializers/tx_asr.rb
136
168
  - config/routes.rb
137
169
  - db/migrate/20210927065745_create_customer_service_im_staffs.rb
@@ -140,6 +172,8 @@ files:
140
172
  - db/migrate/20210928084420_add_state_to_customer_service_im_messages.rb
141
173
  - db/migrate/20210928085514_add_last_messaged_on_to_customer_service_im_conversations.rb
142
174
  - db/migrate/20210930025215_add_unread_message_count_to_customer_service_im_conversations.rb
175
+ - db/migrate/20211026062727_add_password_digest_to_customer_service_im_staffs.rb
176
+ - db/migrate/20211026062951_add_email_to_customer_service_im_staffs.rb
143
177
  - lib/customer_service_im.rb
144
178
  - lib/customer_service_im/core_ext.rb
145
179
  - lib/customer_service_im/engine.rb