Skip to content

Commit f5da291

Browse files
rafaelmf3Rafael Marinho
andauthored
[CHA-651] feature(CHA-769): add shared location support (#166)
* feature(CHA-769): add shared location support * fix Sorbet type checking error * fix spec * add specs and fix location methods * minor fixes * lint * feat(cha-651): add shared locations support * feat(cha-651): add location sharing support * test(cha-651): fix unit test * fix(cha-651): fix unit tests * fix(cha-651): fix unit test --------- Co-authored-by: Rafael Marinho <rafael.marinho@getstream.io>
1 parent fd85415 commit f5da291

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,33 @@ def query_drafts(user_id, filter: nil, sort: nil, **options)
827827
post('drafts/query', data: data)
828828
end
829829

830+
# Get active_live_locations for the current user
831+
#
832+
# @return [StreamChat::StreamResponse]
833+
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
834+
def get_active_live_locations(user_id)
835+
get('users/live_locations', params: { user_id: user_id })
836+
end
837+
838+
# Update live location
839+
#
840+
# @param [String] user_id The ID of the user to update the location
841+
# @param [String] created_by_device_id The device ID that created the location
842+
# @param [String] message_id The message ID associated with the location
843+
# @param [Float] latitude Optional latitude coordinate
844+
# @param [Float] longitude Optional longitude coordinate
845+
# @param [String] end_at Optional end time for the location sharing
846+
# @return [StreamChat::StreamResponse]
847+
sig { params(user_id: String, created_by_device_id: String, message_id: String, options: T.untyped).returns(StreamChat::StreamResponse) }
848+
def update_location(user_id, created_by_device_id:, message_id:, **options)
849+
data = {
850+
created_by_device_id: created_by_device_id,
851+
message_id: message_id
852+
}
853+
data.merge!(options) if options
854+
put('users/live_locations', data: data, params: { user_id: user_id })
855+
end
856+
830857
# Gets a comamnd.
831858
sig { params(name: String).returns(StreamChat::StreamResponse) }
832859
def get_command(name)

spec/client_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,9 @@ def loop_times(times)
928928
list_resp = @client.list_imports({ limit: 1 })
929929
expect(list_resp['import_tasks'].length).to eq 1
930930
end
931+
end
931932

933+
describe 'drafts' do
932934
it 'can query drafts' do
933935
# Create multiple drafts in different channels
934936
draft1 = { 'text' => 'Draft in channel 1' }
@@ -1215,4 +1217,90 @@ def loop_times(times)
12151217
end
12161218
end
12171219
end
1220+
1221+
describe 'live locations' do
1222+
before(:all) do
1223+
@location_test_user = { id: SecureRandom.uuid }
1224+
@client.upsert_users([@location_test_user])
1225+
@location_channel = @client.channel('messaging', channel_id: SecureRandom.uuid)
1226+
@location_channel.create(@location_test_user[:id])
1227+
@location_channel.update_partial({ config_overrides: { shared_locations: true } })
1228+
@location_message = @location_channel.send_message({ text: 'Location sharing message' }, @location_test_user[:id])
1229+
end
1230+
1231+
after(:all) do
1232+
@location_channel.delete
1233+
@client.delete_user(@location_test_user[:id])
1234+
end
1235+
1236+
it 'can create and update a location' do
1237+
location = {
1238+
created_by_device_id: SecureRandom.uuid,
1239+
latitude: 40.7128,
1240+
longitude: -74.0060,
1241+
end_at: (Time.now + 3600).iso8601
1242+
}
1243+
1244+
response = @location_channel.send_message(
1245+
{
1246+
text: 'Location sharing message',
1247+
shared_location: location
1248+
}, @location_test_user[:id]
1249+
)
1250+
1251+
expect(response['message']).to include 'shared_location'
1252+
expect(response['message']['shared_location']['created_by_device_id']).to eq(location[:created_by_device_id])
1253+
expect(response['message']['shared_location']['latitude']).to eq(location[:latitude])
1254+
expect(response['message']['shared_location']['longitude']).to eq(location[:longitude])
1255+
1256+
new_latitude = location[:latitude] + 10
1257+
response = @client.update_location(
1258+
response['message']['user']['id'],
1259+
created_by_device_id: location[:created_by_device_id],
1260+
message_id: response['message']['id'],
1261+
latitude: new_latitude,
1262+
longitude: location[:longitude],
1263+
end_at: location[:end_at]
1264+
)
1265+
1266+
expect(response['created_by_device_id']).to eq(location[:created_by_device_id])
1267+
expect(response['latitude']).to eq(new_latitude)
1268+
expect(response['longitude']).to eq(location[:longitude])
1269+
end
1270+
1271+
it 'can get active live locations' do
1272+
device_id = SecureRandom.uuid
1273+
latitude = 40.7128
1274+
longitude = -74.0060
1275+
end_at = (Time.now + 3600).iso8601
1276+
1277+
@location_channel.send_message(
1278+
{
1279+
text: 'Location sharing message',
1280+
shared_location: {
1281+
created_by_device_id: device_id,
1282+
latitude: latitude,
1283+
longitude: longitude,
1284+
end_at: end_at
1285+
}
1286+
}, @location_test_user[:id]
1287+
)
1288+
1289+
response = @client.get_active_live_locations(@location_test_user[:id])
1290+
expect(response).to include 'active_live_locations'
1291+
expect(response['active_live_locations']).to be_an(Array)
1292+
expect(response['active_live_locations'].length).to be >= 1
1293+
location = response['active_live_locations'].find { |loc| loc['created_by_device_id'] == device_id }
1294+
expect(location).not_to be_nil
1295+
expect(location['latitude']).to eq(latitude)
1296+
expect(location['longitude']).to eq(longitude)
1297+
expect(DateTime.parse(location['end_at']).iso8601).to eq(end_at)
1298+
end
1299+
1300+
it 'should have active live locations on the channel' do
1301+
response = @location_channel.query
1302+
expect(response).to include 'active_live_locations'
1303+
expect(response['active_live_locations'].length).to be >= 1
1304+
end
1305+
end
12181306
end

0 commit comments

Comments
 (0)