Update store mechanism for call invites

This commit is contained in:
ismailgulek
2021-05-10 17:24:50 +03:00
parent 4a704262d4
commit f132070fc7
3 changed files with 27 additions and 42 deletions

View File

@@ -554,16 +554,16 @@ Matrix session observer used to detect new opened sessions.
if (@available(iOS 13.0, *))
{
// for iOS 13, we'll just report the incoming call in the same runloop. It means we cannot call an async API here.
MXEvent *lastCallInvite = _pushNotificationStore.lastCallInvite;
MXEvent *callInvite = [_pushNotificationStore callInviteForEventId:eventId];
// remove event
_pushNotificationStore.lastCallInvite = nil;
[_pushNotificationStore removeCallInviteWithEventId:eventId];
MXSession *session = [AppDelegate theDelegate].mxSessions.firstObject;
// when we have a VoIP push while the application is killed, session.callManager will not be ready yet. Configure it.
[[AppDelegate theDelegate] configureCallManagerIfRequiredForSession:session];
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: lastCallInvite: %@", lastCallInvite);
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: callInvite: %@", callInvite);
if ([lastCallInvite.eventId isEqualToString:eventId])
if (callInvite)
{
// We're using this dispatch_group to continue event stream after cache fully processed.
dispatch_group_t dispatchGroup = dispatch_group_create();
@@ -575,18 +575,11 @@ Matrix session observer used to detect new opened sessions.
dispatch_group_leave(dispatchGroup);
}];
if (lastCallInvite.isEncrypted && ![session decryptEvent:lastCallInvite inTimeline:nil])
{
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: Failed to decrypt the call invite event: %@", eventId);
completion();
return;
}
if (lastCallInvite.eventType == MXEventTypeCallInvite)
if (callInvite.eventType == MXEventTypeCallInvite)
{
// process the call invite synchronously
[session.callManager handleCallEvent:lastCallInvite];
MXCallInviteEventContent *content = [MXCallInviteEventContent modelFromJSON:lastCallInvite.content];
[session.callManager handleCallEvent:callInvite];
MXCallInviteEventContent *content = [MXCallInviteEventContent modelFromJSON:callInvite.content];
MXCall *call = [session.callManager callWithCallId:content.callId];
if (call)
{
@@ -604,10 +597,10 @@ Matrix session observer used to detect new opened sessions.
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: Error on call object on room %@ for the event: %@", roomId, eventId);
}
}
else if ([lastCallInvite.type isEqualToString:kWidgetMatrixEventTypeString] ||
[lastCallInvite.type isEqualToString:kWidgetModularEventTypeString])
else if ([callInvite.type isEqualToString:kWidgetMatrixEventTypeString] ||
[callInvite.type isEqualToString:kWidgetModularEventTypeString])
{
[[AppDelegate theDelegate].callPresenter processWidgetEvent:lastCallInvite
[[AppDelegate theDelegate].callPresenter processWidgetEvent:callInvite
inSession:session];
}
else
@@ -619,7 +612,7 @@ Matrix session observer used to detect new opened sessions.
else
{
// It's a serious error. There is nothing to avoid iOS to kill us here.
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: iOS 13 and in bg, but we don't have the last callInvite event for the event %@. There is something wrong.", eventId);
NSLog(@"[PushNotificationService] didReceiveIncomingPushWithPayload: iOS 13 and in bg, but we don't have the callInvite event for the eventId: %@. There is something wrong.", eventId);
}
}
else

View File

@@ -29,7 +29,6 @@ final class PushNotificationStore: NSObject {
private struct StoreKeys {
static let pushToken: String = "pushtoken"
static let lastCallInvite: String = "lastCallInvite"
}
/// Store. Defaults to `KeychainStore`
@@ -59,28 +58,20 @@ final class PushNotificationStore: NSObject {
}
}
var lastCallInvite: MXEvent? {
get {
do {
guard let data = try store.data(forKey: StoreKeys.lastCallInvite) else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: data) as? MXEvent
} catch let error {
NSLog("[PinCodePreferences] Error when reading push token from store: \(error)")
return nil
}
} set {
do {
guard let newValue = newValue else {
return try store.removeObject(forKey: StoreKeys.lastCallInvite)
}
let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
try store.set(data, forKey: StoreKeys.lastCallInvite)
} catch let error {
NSLog("[PinCodePreferences] Error when storing push token to the store: \(error)")
}
func callInvite(forEventId eventId: String) -> MXEvent? {
guard let data = try? store.data(forKey: eventId) else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: data) as? MXEvent
}
func storeCallInvite(_ event: MXEvent) {
let data = NSKeyedArchiver.archivedData(withRootObject: event)
try? store.set(data, forKey: event.eventId)
}
func removeCallInvite(withEventId eventId: String) {
try? store.removeObject(forKey: eventId)
}
func reset() {

View File

@@ -587,9 +587,10 @@ class NotificationService: UNNotificationServiceExtension {
}
if event.isEncrypted {
pushNotificationStore.lastCallInvite = event.clear
if #available(iOS 13.0, *) {
pushNotificationStore.storeCallInvite(event.clear)
} else {
pushNotificationStore.lastCallInvite = event
pushNotificationStore.storeCallInvite(event)
}
ongoingVoIPPushRequests[event.eventId] = true