Skip to content

Commit a843cc0

Browse files
author
Rafael Marinho
committed
add specs and fix location methods
1 parent 785d723 commit a843cc0

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

lib/stream-chat/client.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,21 +810,22 @@ def query_drafts(user_id, filter: nil, sort: nil, **options)
810810
# Get active_live_locations for the current user
811811
#
812812
# @return [StreamChat::StreamResponse]
813-
sig { returns(StreamChat::StreamResponse) }
814-
def get_active_live_locations
815-
get('users/locations')
813+
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
814+
def get_active_live_locations(user_id)
815+
get('users/locations', params: { user_id: user_id })
816816
end
817817

818818
# Update live location
819819
#
820+
# @param [String] user_id The ID of the user to update the location
820821
# @param [String] created_by_device_id The device ID that created the location
821822
# @param [String] message_id The message ID associated with the location
822823
# @param [Float] latitude Optional latitude coordinate
823824
# @param [Float] longitude Optional longitude coordinate
824825
# @param [String] end_at Optional end time for the location sharing
825826
# @return [StreamChat::StreamResponse]
826-
sig { params(created_by_device_id: String, message_id: String, latitude: T.nilable(Float), longitude: T.nilable(Float), end_at: T.nilable(String)).returns(StreamChat::StreamResponse) }
827-
def update_location(created_by_device_id:, message_id:, latitude: nil, longitude: nil, end_at: nil)
827+
sig { params(user_id: String, created_by_device_id: String, message_id: String, latitude: T.nilable(Float), longitude: T.nilable(Float), end_at: T.nilable(String)).returns(StreamChat::StreamResponse) }
828+
def update_location(user_id, created_by_device_id:, message_id:, latitude: nil, longitude: nil, end_at: nil)
828829
data = {
829830
created_by_device_id: created_by_device_id,
830831
message_id: message_id
@@ -833,7 +834,7 @@ def update_location(created_by_device_id:, message_id:, latitude: nil, longitude
833834
data[:longitude] = longitude if longitude
834835
data[:end_at] = end_at if end_at
835836

836-
put('users/location', data: data)
837+
put('users/location', data: data, params: { user_id: user_id })
837838
end
838839

839840
# Gets a comamnd.

spec/client_spec.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,9 @@ def loop_times(times)
825825
list_resp = @client.list_imports({ limit: 1 })
826826
expect(list_resp['import_tasks'].length).to eq 1
827827
end
828+
end
828829

830+
describe 'drafts' do
829831
it 'can query drafts' do
830832
# Create multiple drafts in different channels
831833
draft1 = { 'text' => 'Draft in channel 1' }
@@ -1020,4 +1022,86 @@ def loop_times(times)
10201022
expect(response['threads'].length).to be >= 1
10211023
end
10221024
end
1025+
1026+
describe 'live locations' do
1027+
before(:all) do
1028+
@location_test_user = { id: SecureRandom.uuid }
1029+
@client.upsert_users([@location_test_user])
1030+
@location_channel = @client.channel('messaging', channel_id: SecureRandom.uuid)
1031+
@location_channel.create(@location_test_user[:id])
1032+
@location_message = @location_channel.send_message({ text: 'Location sharing message' }, @location_test_user[:id])
1033+
end
1034+
1035+
after(:all) do
1036+
@location_channel.delete
1037+
@client.delete_user(@location_test_user[:id])
1038+
end
1039+
1040+
it 'can create and update a location' do
1041+
location = {
1042+
created_by_device_id: SecureRandom.uuid,
1043+
latitude: 40.7128,
1044+
longitude: -74.0060,
1045+
end_at: (Time.now + 3600).iso8601
1046+
}
1047+
1048+
response = @channel.send_message({
1049+
text: 'Location sharing message',
1050+
shared_location: location,
1051+
}, @location_test_user[:id])
1052+
1053+
expect(response['message']).to include 'shared_location'
1054+
expect(response['message']['shared_location']['created_by_device_id']).to eq(location[:created_by_device_id])
1055+
expect(response['message']['shared_location']['latitude']).to eq(location[:latitude])
1056+
expect(response['message']['shared_location']['longitude']).to eq(location[:longitude])
1057+
1058+
new_latitude = location[:latitude] + 10
1059+
response = @client.update_location(
1060+
response['message']['user']['id'],
1061+
created_by_device_id: location[:created_by_device_id],
1062+
message_id: response['message']['id'],
1063+
latitude: new_latitude,
1064+
longitude: location[:longitude],
1065+
end_at: location[:end_at],
1066+
)
1067+
1068+
expect(response['created_by_device_id']).to eq(location[:created_by_device_id])
1069+
expect(response['latitude']).to eq(new_latitude)
1070+
expect(response['longitude']).to eq(location[:longitude])
1071+
end
1072+
1073+
it 'can get active live locations' do
1074+
device_id = SecureRandom.uuid
1075+
latitude = 40.7128
1076+
longitude = -74.0060
1077+
end_at = (Time.now + 3600).iso8601
1078+
1079+
response = @location_channel.send_message({
1080+
text: 'Location sharing message',
1081+
shared_location: {
1082+
created_by_device_id: device_id,
1083+
latitude: latitude,
1084+
longitude: longitude,
1085+
end_at: end_at
1086+
}
1087+
}, @location_test_user[:id])
1088+
1089+
response = @client.get_active_live_locations(@location_test_user[:id])
1090+
expect(response).to include 'active_live_locations'
1091+
expect(response['active_live_locations']).to be_an(Array)
1092+
expect(response['active_live_locations'].length).to be >= 1
1093+
1094+
location = response['active_live_locations'].find { |loc| loc['created_by_device_id'] == device_id }
1095+
expect(location).not_to be_nil
1096+
expect(location['latitude']).to eq(latitude)
1097+
expect(location['longitude']).to eq(longitude)
1098+
expect(DateTime.parse(location['end_at']).iso8601).to eq(end_at)
1099+
end
1100+
1101+
it 'should have active live locations on the channel' do
1102+
response = @channel.query()
1103+
expect(response).to include 'active_live_locations'
1104+
expect(response['active_live_locations'].length).to be >= 1
1105+
end
1106+
end
10231107
end

0 commit comments

Comments
 (0)