From c98907db499563a4dd08761c69b7ead59d21be17 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sat, 4 Jul 2020 11:42:47 +0530 Subject: [PATCH] Feature: Availability Statuses (#874) Co-authored-by: Pranav Raj S --- app/channels/room_channel.rb | 40 ++++++++++++-- app/controllers/api/v1/profiles_controller.rb | 2 +- app/javascript/dashboard/api/auth.js | 17 +++--- .../dashboard/components/layout/Sidebar.vue | 6 ++- .../components/widgets/Thumbnail.vue | 25 ++++++--- .../widgets/conversation/ConversationCard.vue | 1 + .../conversation/ConversationHeader.vue | 1 + .../dashboard/helper/actionCable.js | 6 +++ .../dashboard/i18n/locale/en/settings.json | 17 ++++++ .../dashboard/settings/profile/Index.vue | 29 +++++++--- .../dashboard/store/modules/agents.js | 8 +++ .../dashboard/store/modules/contacts.js | 19 +++++++ .../modules/specs/agents/actions.spec.js | 12 +++++ .../modules/specs/agents/mutations.spec.js | 36 +++++++++++++ .../dashboard/store/mutation-types.js | 2 + .../helpers/BaseActionCableConnector.js | 13 ++++- .../shared/helpers/vuex/mutationHelpers.js | 11 ++++ app/javascript/widget/helpers/actionCable.js | 5 ++ app/javascript/widget/store/modules/agent.js | 5 ++ .../store/modules/specs/agent/actions.spec.js | 5 ++ .../modules/specs/agent/mutations.spec.js | 33 +++++++++++- .../concerns/availability_statusable.rb | 15 ++++-- app/models/user.rb | 15 +++++- .../v1/accounts/contacts/index.json.jbuilder | 7 +-- .../v1/accounts/contacts/show.json.jbuilder | 8 +-- .../v1/accounts/contacts/update.json.jbuilder | 7 +-- .../api/v1/models/_contact.json.jbuilder | 7 +++ app/views/api/v1/models/_user.json.jbuilder | 1 + config/cable.yml | 15 +++--- ...20200629122646_add_availability_to_user.rb | 5 ++ db/schema.rb | 3 +- lib/online_status_tracker.rb | 53 +++++++++++++++---- lib/redis/alfred.rb | 45 ++++++++++++++++ spec/channels/room_channel_spec.rb | 6 +-- .../api/v1/profiles_controller_spec.rb | 10 ++++ 35 files changed, 413 insertions(+), 77 deletions(-) create mode 100644 app/views/api/v1/models/_contact.json.jbuilder create mode 100644 db/migrate/20200629122646_add_availability_to_user.rb diff --git a/app/channels/room_channel.rb b/app/channels/room_channel.rb index df1a05d6b..1a0cbb434 100644 --- a/app/channels/room_channel.rb +++ b/app/channels/room_channel.rb @@ -1,10 +1,42 @@ class RoomChannel < ApplicationCable::Channel def subscribed - stream_from params[:pubsub_token] - ::OnlineStatusTracker.add_subscription(params[:pubsub_token]) + ensure_stream + current_user + current_account + update_subscription end - def unsubscribed - ::OnlineStatusTracker.remove_subscription(params[:pubsub_token]) + def update_presence + update_subscription + data = { account_id: @current_account.id, users: ::OnlineStatusTracker.get_available_users(@current_account.id) } + data[:contacts] = ::OnlineStatusTracker.get_available_contacts(@current_account.id) if @current_user.is_a? User + ActionCable.server.broadcast(@pubsub_token, { event: 'presence.update', data: data }) + end + + private + + def ensure_stream + @pubsub_token = params[:pubsub_token] + stream_from @pubsub_token + end + + def update_subscription + ::OnlineStatusTracker.update_presence(@current_account.id, @current_user.class.name, @current_user.id) + end + + def current_user + @current_user ||= if params[:user_id].blank? + Contact.find_by!(pubsub_token: @pubsub_token) + else + User.find_by!(pubsub_token: @pubsub_token, id: params[:user_id]) + end + end + + def current_account + @current_account ||= if @current_user.is_a? Contact + @current_user.account + else + @current_user.accounts.find(params[:account_id]) + end end end diff --git a/app/controllers/api/v1/profiles_controller.rb b/app/controllers/api/v1/profiles_controller.rb index bd415ffeb..38f1a9b3b 100644 --- a/app/controllers/api/v1/profiles_controller.rb +++ b/app/controllers/api/v1/profiles_controller.rb @@ -16,6 +16,6 @@ class Api::V1::ProfilesController < Api::BaseController end def profile_params - params.require(:profile).permit(:email, :name, :password, :password_confirmation, :avatar) + params.require(:profile).permit(:email, :name, :password, :password_confirmation, :avatar, :availability) end end diff --git a/app/javascript/dashboard/api/auth.js b/app/javascript/dashboard/api/auth.js index f247089cd..42d1fbc41 100644 --- a/app/javascript/dashboard/api/auth.js +++ b/app/javascript/dashboard/api/auth.js @@ -118,21 +118,18 @@ export default { return axios.post(urlData.url, { email }); }, - profileUpdate({ name, email, password, password_confirmation, avatar }) { + profileUpdate({ password, password_confirmation, ...profileAttributes }) { const formData = new FormData(); - if (name) { - formData.append('profile[name]', name); - } - if (email) { - formData.append('profile[email]', email); - } + Object.keys(profileAttributes).forEach(key => { + const value = profileAttributes[key]; + if (value) { + formData.append(`profile[${key}]`, value); + } + }); if (password && password_confirmation) { formData.append('profile[password]', password); formData.append('profile[password_confirmation]', password_confirmation); } - if (avatar) { - formData.append('profile[avatar]', avatar); - } return axios.put(endPoints('profileUpdate').url, formData); }, }; diff --git a/app/javascript/dashboard/components/layout/Sidebar.vue b/app/javascript/dashboard/components/layout/Sidebar.vue index 4ec6c239f..d6ab8a75d 100644 --- a/app/javascript/dashboard/components/layout/Sidebar.vue +++ b/app/javascript/dashboard/components/layout/Sidebar.vue @@ -56,7 +56,11 @@
- +

{{ currentUser.name }} diff --git a/app/javascript/dashboard/components/widgets/Thumbnail.vue b/app/javascript/dashboard/components/widgets/Thumbnail.vue index 25b707c04..2d1c3e200 100644 --- a/app/javascript/dashboard/components/widgets/Thumbnail.vue +++ b/app/javascript/dashboard/components/widgets/Thumbnail.vue @@ -21,11 +21,6 @@ :style="badgeStyle" src="~dashboard/assets/images/fb-badge.png" /> -
- +