Skip to content

Commit e872114

Browse files
Merge pull request #1446 from matrix-org/steve/6070_beacon_info_stop
[Location sharing] Handle m.beacon_info stop
2 parents e373d15 + 77e3b6c commit e872114

15 files changed

+364
-105
lines changed

MatrixSDK/Aggregations/LocationSharing/MXBeaconAggregations.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,17 @@ public class MXBeaconAggregations: NSObject {
125125

126126
var beaconInfoSummary: MXBeaconInfoSummary?
127127

128-
if let existingBeaconInfoSummary = self.getBeaconInfoSummary(withIdentifier: eventId, inRoomWithId: roomId) {
128+
// A new beacon info is emitted to set a current one to stop state. This beacon info have a different event id.
129+
if beaconInfo.isLive == false {
130+
131+
// If no corresponding BeaconInfoSummary exists, discard this beacon info
132+
if let existingBeaconInfoSummary = self.getBeaconInfoSummary(withStoppedBeaconInfo: beaconInfo, inRoomWithId: roomId), existingBeaconInfoSummary.hasStopped == false {
133+
134+
existingBeaconInfoSummary.updateWithBeaconInfo(beaconInfo)
135+
beaconInfoSummary = existingBeaconInfoSummary
136+
}
137+
138+
} else if let existingBeaconInfoSummary = self.getBeaconInfoSummary(withIdentifier: eventId, inRoomWithId: roomId) {
129139

130140
// If beacon info is older than existing one, do not take it into account
131141
if beaconInfo.timestamp > existingBeaconInfoSummary.beaconInfo.timestamp {
@@ -168,4 +178,17 @@ public class MXBeaconAggregations: NSObject {
168178
private func getBeaconInfoSummary(withIdentifier identifier: String, inRoomWithId roomId: String) -> MXBeaconInfoSummary? {
169179
return self.beaconInfoSummaryStore.getBeaconInfoSummary(withIdentifier: identifier, inRoomWithId: roomId)
170180
}
181+
182+
private func getBeaconInfoSummary(withStoppedBeaconInfo beaconInfo: MXBeaconInfo, inRoomWithId roomId: String) -> MXBeaconInfoSummary? {
183+
184+
guard beaconInfo.isLive == false else {
185+
return nil
186+
}
187+
188+
guard let userId = beaconInfo.userId else {
189+
return nil
190+
}
191+
192+
return self.beaconInfoSummaryStore.getBeaconInfoSummary(withUserId: userId, description: beaconInfo.desc, timeout: beaconInfo.timeout, timestamp: beaconInfo.timestamp, inRoomWithId: roomId)
193+
}
171194
}

MatrixSDK/Aggregations/LocationSharing/MXBeaconInfoSummaryStore.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class MXBeaconInfoSummaryMemoryStore: NSObject, MXBeaconInfoSummaryStoreP
2727

2828
public func addOrUpdateBeaconInfoSummary(_ beaconInfoSummary: MXBeaconInfoSummary, inRoomWithId roomId: String) {
2929

30-
var beaconInfoSummaries: [MXBeaconInfoSummary] = self.beaconInfoSummaries[roomId] ?? []
30+
var beaconInfoSummaries = self.getAllBeaconInfoSummaries(inRoomWithId: roomId)
3131

3232
let existingIndex = beaconInfoSummaries.firstIndex { summary in
3333
return summary.id == beaconInfoSummary.id
@@ -52,12 +52,34 @@ public class MXBeaconInfoSummaryMemoryStore: NSObject, MXBeaconInfoSummaryStoreP
5252
}
5353
}
5454

55+
public func getAllBeaconInfoSummaries(inRoomWithId roomId: String) -> [MXBeaconInfoSummary] {
56+
return self.beaconInfoSummaries[roomId] ?? []
57+
}
58+
5559
public func deleteAllBeaconInfoSummaries(inRoomWithId roomId: String) {
5660
self.beaconInfoSummaries[roomId] = nil
5761
}
5862

5963
public func deleteAllBeaconInfoSummaries() {
6064
self.beaconInfoSummaries = [:]
6165
}
66+
67+
public func getBeaconInfoSummary(withUserId userId: String,
68+
description: String?,
69+
timeout: UInt64,
70+
timestamp: UInt64,
71+
inRoomWithId roomId: String) -> MXBeaconInfoSummary? {
72+
73+
let beaconInfoSummaries = self.getAllBeaconInfoSummaries(inRoomWithId: roomId)
74+
75+
return beaconInfoSummaries.first { beaconInfoSummary in
76+
let beaconInfo = beaconInfoSummary.beaconInfo
77+
78+
return beaconInfo.userId == userId
79+
&& beaconInfo.desc == description
80+
&& beaconInfo.timeout == timeout
81+
&& beaconInfo.timestamp == timestamp
82+
}
83+
}
6284
}
6385

MatrixSDK/Aggregations/LocationSharing/MXBeaconInfoSummaryStoreProtocol.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import Foundation
2525
/// Get a MXBeaconInfoSummary from his identifier in a given room. The identifier is the first beacon info event id.
2626
func getBeaconInfoSummary(withIdentifier identifier: String, inRoomWithId roomId: String) -> MXBeaconInfoSummary?
2727

28+
/// Get a MXBeaconInfoSummary from exact property values
29+
func getBeaconInfoSummary(withUserId userId: String,
30+
description: String?,
31+
timeout: UInt64,
32+
timestamp: UInt64,
33+
inRoomWithId roomId: String) -> MXBeaconInfoSummary?
34+
35+
/// Get all MXBeaconInfoSummary in a room
36+
func getAllBeaconInfoSummaries(inRoomWithId roomId: String) -> [MXBeaconInfoSummary]
37+
2838
/// Delete all MXBeaconInfoSummary in a room
2939
func deleteAllBeaconInfoSummaries(inRoomWithId roomId: String)
3040

MatrixSDK/JSONModels/Location/MXBeaconInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ NS_ASSUME_NONNULL_BEGIN
6969
/// @param event The m.beacon_info event.
7070
- (nullable instancetype)initWithMXEvent:(MXEvent*)event;
7171

72+
#pragma mark - Public
73+
74+
/// Get the stopped beacon info version
75+
/// Keep original event as is and update the `isLive` property to false
76+
- (MXBeaconInfo*)stopped;
77+
7278
@end
7379

7480
NS_ASSUME_NONNULL_END

MatrixSDK/JSONModels/Location/MXBeaconInfo.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (instancetype)initWithUserId:(NSString *)userId
4949
_desc = desc;
5050
_timeout = timeout;
5151
_isLive = isLive;
52-
_assetType = MXEventAssetTypeLiveLocation;
52+
_assetType = MXEventAssetTypeUser;
5353
_timestamp = timestamp;
5454
_originalEvent = originalEvent;
5555
}
@@ -91,6 +91,16 @@ - (nullable instancetype)initWithMXEvent:(MXEvent*)event
9191
originalEvent:event];
9292
}
9393

94+
- (MXBeaconInfo*)stopped
95+
{
96+
return [[[self class] alloc] initWithUserId:self.userId
97+
description:self.desc
98+
timeout:self.timeout
99+
isLive:NO
100+
timestamp:self.timestamp
101+
originalEvent:self.originalEvent];
102+
}
103+
94104
#pragma mark - Overrides
95105

96106
+ (id)modelFromJSON:(NSDictionary *)jsonDictionary
@@ -112,7 +122,7 @@ + (id)modelFromJSON:(NSDictionary *)jsonDictionary
112122

113123
MXJSONModelSetString(assetTypeString, assetDictionary[kMXMessageContentKeyExtensibleAssetType]);
114124

115-
isAssetTypeValid = [assetTypeString isEqualToString:kMXMessageContentKeyExtensibleAssetTypeLiveLocation];
125+
isAssetTypeValid = [assetTypeString isEqualToString:kMXMessageContentKeyExtensibleAssetTypeUser];
116126
}
117127

118128
MXJSONModelSetNumber(timestampNumber, jsonDictionary[kMXMessageContentKeyExtensibleTimestampMSC3488])
@@ -134,7 +144,7 @@ + (id)modelFromJSON:(NSDictionary *)jsonDictionary
134144

135145
beaconInfo->_timestamp = [timestampNumber unsignedLongLongValue];
136146

137-
beaconInfo->_assetType = MXEventAssetTypeLiveLocation;
147+
beaconInfo->_assetType = MXEventAssetTypeUser;
138148
}
139149

140150
return beaconInfo;
@@ -156,7 +166,7 @@ - (NSDictionary *)JSONDictionary
156166
// Asset type
157167

158168
content[kMXMessageContentKeyExtensibleAssetMSC3488] = @{
159-
kMXMessageContentKeyExtensibleAssetType: kMXMessageContentKeyExtensibleAssetTypeLiveLocation
169+
kMXMessageContentKeyExtensibleAssetType: kMXMessageContentKeyExtensibleAssetTypeUser
160170
};
161171

162172
return content;

MatrixSDK/JSONModels/MXEvent.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetMSC3488;
294294
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetType;
295295
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetTypeUser;
296296
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetTypePin;
297-
// live user location tracking
298-
FOUNDATION_EXPORT NSString *const kMXMessageContentKeyExtensibleAssetTypeLiveLocation;
299297

300298
// Join Rules
301299

MatrixSDK/JSONModels/MXEvent.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@
195195
NSString *const kMXMessageContentKeyExtensibleAssetType = @"type";
196196
NSString *const kMXMessageContentKeyExtensibleAssetTypeUser = @"m.self";
197197
NSString *const kMXMessageContentKeyExtensibleAssetTypePin = @"m.pin";
198-
NSString *const kMXMessageContentKeyExtensibleAssetTypeLiveLocation = @"m.self.live";
199198

200199
// Join Rules
201200

MatrixSDK/JSONModels/MXEventAssetType.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
typedef NS_ENUM(NSUInteger, MXEventAssetType)
2020
{
2121
MXEventAssetTypeGeneric, // A generic location without context
22-
MXEventAssetTypeUser, // Static user location
23-
MXEventAssetTypePin, // Pin location (POI)
24-
MXEventAssetTypeLiveLocation // User live location
22+
MXEventAssetTypeUser, // User location, it can be static or live depending of event type
23+
MXEventAssetTypePin // Pin location (POI)
2524
};

MatrixSDK/LocationSharing/MXBeaconInfoSummary.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,33 @@ public class MXBeaconInfoSummary: NSObject, MXBeaconInfoSummaryProtocol {
6565
@discardableResult
6666
func updateWithBeaconInfo(_ beaconInfo: MXBeaconInfo) -> Bool {
6767

68-
// TODO: Update this check for beacon info stop state event
69-
guard let beaconInfoEventId = beaconInfo.originalEvent?.eventId, beaconInfoEventId == self.id else {
68+
guard let beaconInfoEventId = beaconInfo.originalEvent?.eventId else {
7069
return false
7170
}
7271

73-
self.beaconInfo = beaconInfo
74-
return true
72+
guard beaconInfo.userId == self.userId else {
73+
return false
74+
}
75+
76+
if beaconInfoEventId == self.id {
77+
self.beaconInfo = beaconInfo
78+
return true
79+
} else if beaconInfoEventId != self.id
80+
&& self.beaconInfo.isLive == true
81+
&& beaconInfo.isLive == false
82+
&& beaconInfo.desc == self.beaconInfo.desc
83+
&& beaconInfo.timeout == self.beaconInfo.timeout
84+
&& beaconInfo.timestamp == self.beaconInfo.timestamp {
85+
86+
// Beacon info with a different event id is only allowed when the beacon info is representing the stop state
87+
88+
// Update current beacon info with `isLive` property to false
89+
self.beaconInfo = self.beaconInfo.stopped()
90+
91+
return true
92+
}
93+
94+
return false
7595
}
7696

7797
@discardableResult

0 commit comments

Comments
 (0)