diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index 34ac91b3a..a82a30414 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -8,6 +8,12 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController @contacts = Current.account.contacts end + # returns online contacts + def active + @contacts = Current.account.contacts.where(id: ::OnlineStatusTracker + .get_available_contact_ids(Current.account.id)) + end + def show; end def create diff --git a/app/policies/contact_policy.rb b/app/policies/contact_policy.rb index 722e70e0b..a6e1590fb 100644 --- a/app/policies/contact_policy.rb +++ b/app/policies/contact_policy.rb @@ -3,6 +3,10 @@ class ContactPolicy < ApplicationPolicy true end + def active? + true + end + def search? true end diff --git a/app/views/api/v1/accounts/contacts/active.json.jbuilder b/app/views/api/v1/accounts/contacts/active.json.jbuilder new file mode 100644 index 000000000..b033133ed --- /dev/null +++ b/app/views/api/v1/accounts/contacts/active.json.jbuilder @@ -0,0 +1,5 @@ +json.payload do + json.array! @contacts do |contact| + json.partial! 'api/v1/models/contact.json.jbuilder', resource: contact, with_contact_inboxes: true + end +end diff --git a/config/routes.rb b/config/routes.rb index 8db4acc4a..76884e310 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,6 +64,7 @@ Rails.application.routes.draw do resources :contacts, only: [:index, :show, :update, :create] do collection do + get :active get :search end scope module: :contacts do diff --git a/lib/online_status_tracker.rb b/lib/online_status_tracker.rb index 622585dff..42bc0dca0 100644 --- a/lib/online_status_tracker.rb +++ b/lib/online_status_tracker.rb @@ -37,9 +37,13 @@ module OnlineStatusTracker format(::Redis::Alfred::ONLINE_STATUS, account_id: account_id) end + def self.get_available_contact_ids(account_id) + ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i) + end + def self.get_available_contacts(account_id) - contact_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i) - contact_ids.index_with { |_id| 'online' } + # returns {id1: 'online', id2: 'online'} + get_available_contact_ids(account_id).index_with { |_id| 'online' } end def self.get_available_users(account_id) diff --git a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb index 04b9547bf..c2ad6fec0 100644 --- a/spec/controllers/api/v1/accounts/contacts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/contacts_controller_spec.rb @@ -30,6 +30,41 @@ RSpec.describe 'Contacts API', type: :request do end end + describe 'GET /api/v1/accounts/{account.id}/contacts/active' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get "/api/v1/accounts/#{account.id}/contacts/active" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + let!(:contact) { create(:contact, account: account) } + + it 'returns no contacts if no are online' do + get "/api/v1/accounts/#{account.id}/contacts/active", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).not_to include(contact.email) + end + + it 'returns all contacts who are online' do + allow(::OnlineStatusTracker).to receive(:get_available_contact_ids).and_return([contact.id]) + + get "/api/v1/accounts/#{account.id}/contacts/active", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(contact.email) + end + end + end + describe 'GET /api/v1/accounts/{account.id}/contacts/search' do context 'when it is an unauthenticated user' do it 'returns unauthorized' do