feat: Add API to get the active contacts (#1313)

This commit is contained in:
Sojan Jose 2020-10-05 23:30:27 +05:30 committed by GitHub
parent 11725de09a
commit 88b2469dc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 2 deletions

View file

@ -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

View file

@ -3,6 +3,10 @@ class ContactPolicy < ApplicationPolicy
true
end
def active?
true
end
def search?
true
end

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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