customer_service_im 0.3.0 → 0.7.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: 41fabd8cba3961e2a8f41f7018e5556e0b1b4f0eca4701da54475b3d79635ae2
4
- data.tar.gz: e77bec68a20f57ff6da3715d112de8654a4b62b8da145d9cb55925fe07f95d38
3
+ metadata.gz: d1c17798597ac31c4ff215502ab3766fc668c5378d1b82b7611eafe72030c76e
4
+ data.tar.gz: 72ecaff85227c5c8aea3a477d5ccb18e1aff1e3d3e4eb6f81f27df41114d5074
5
5
  SHA512:
6
- metadata.gz: c004f6fde779fb1ccd13cd1b1cb5b8f129e018fbb90f4d8655b2485ca0e4fda476a58418d228668cd5401a7cf0e1b403a4bb96db4ce3c765e6ed41f819262fea
7
- data.tar.gz: ed92adf642e25fa6a7ac1facecf2e7abfac77820460c073881d1cde56c5de8e7fbb03c154d4360f963115b3e4977a0416a0361418d583857d6bbce0dba704b67
6
+ metadata.gz: 2cbc740a59fb1f26851047f65838fe1937f3e30a6b61d3e510cab6c26f49cae4c332e8ab0923ca983938eb0320f5623c40e42cc2c5d305c9515e718dd0ff4225
7
+ data.tar.gz: eba1e756eeb41c8fad3a9fa7b7d253396b77e4eef7696fbdda90374d6f0b643fadb1c25251a8584288579a79efc78f53edb2d7c582b265eeef827312f8c86603
@@ -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.3.0'
2
+ VERSION = '0.7.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.3.0
4
+ version: 0.7.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
@@ -114,6 +114,34 @@ dependencies:
114
114
  - - ">="
115
115
  - !ruby/object:Gem::Version
116
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'
131
+ - !ruby/object:Gem::Dependency
132
+ name: bcrypt
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :runtime
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
117
145
  description: Description of CustomerServiceIm.
118
146
  email:
119
147
  - ianlynxk@gmail.com
@@ -132,6 +160,8 @@ files:
132
160
  - app/controllers/customer_service_im/application_controller.rb
133
161
  - app/controllers/customer_service_im/conversations_controller.rb
134
162
  - app/controllers/customer_service_im/messages_controller.rb
163
+ - app/controllers/customer_service_im/staffs_controller.rb
164
+ - app/controllers/customer_service_im/users_controller.rb
135
165
  - app/jobs/customer_service_im/application_job.rb
136
166
  - app/mailers/customer_service_im/application_mailer.rb
137
167
  - app/models/customer_service_im/application_record.rb
@@ -143,9 +173,11 @@ files:
143
173
  - app/views/customer_service_im/conversations/_message.json.jbuilder
144
174
  - app/views/customer_service_im/conversations/index.json.jbuilder
145
175
  - app/views/customer_service_im/conversations/messages.json.jbuilder
176
+ - app/views/customer_service_im/conversations/show.json.jbuilder
146
177
  - app/views/customer_service_im/messages/_message.json.jbuilder
147
178
  - app/views/customer_service_im/messages/index.json.jbuilder
148
179
  - config/cable.yml
180
+ - config/initializers/mini_program.rb
149
181
  - config/initializers/tx_asr.rb
150
182
  - config/routes.rb
151
183
  - db/migrate/20210927065745_create_customer_service_im_staffs.rb
@@ -154,6 +186,8 @@ files:
154
186
  - db/migrate/20210928084420_add_state_to_customer_service_im_messages.rb
155
187
  - db/migrate/20210928085514_add_last_messaged_on_to_customer_service_im_conversations.rb
156
188
  - db/migrate/20210930025215_add_unread_message_count_to_customer_service_im_conversations.rb
189
+ - db/migrate/20211026062727_add_password_digest_to_customer_service_im_staffs.rb
190
+ - db/migrate/20211026062951_add_email_to_customer_service_im_staffs.rb
157
191
  - lib/customer_service_im.rb
158
192
  - lib/customer_service_im/core_ext.rb
159
193
  - lib/customer_service_im/engine.rb