Fixed an issue where subscribing to a new legacy group wouldn't have worked

pull/856/head
Morgan Pretty 2 years ago
parent 01d77a515c
commit c7d090251a

@ -192,11 +192,22 @@ extension MessageReceiver {
// Start polling
ClosedGroupPoller.shared.startIfNeeded(for: groupPublicKey)
// Subscribe for push notifications
// Resubscribe for group push notifications
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db)
PushNotificationAPI
.subscribeToLegacyGroup(
legacyGroupId: groupPublicKey,
currentUserPublicKey: getUserHexEncodedPublicKey(db)
.subscribeToLegacyGroups(
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
)
.asRequest(of: String.self)
.fetchSet(db)
.inserting(groupPublicKey) // Insert the new key just to be sure
)
.sinkUntilComplete()
}

@ -118,9 +118,18 @@ extension MessageSender {
.map { MessageSender.sendImmediate(preparedSendData: $0) }
.appending(
// Subscribe for push notifications (if enabled)
PushNotificationAPI.subscribeToLegacyGroup(
legacyGroupId: groupPublicKey,
currentUserPublicKey: userPublicKey
PushNotificationAPI.subscribeToLegacyGroups(
currentUserPublicKey: userPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == userPublicKey)
)
.asRequest(of: String.self)
.fetchSet(db)
.inserting(groupPublicKey) // Insert the new key just to be sure
)
)
)

@ -79,7 +79,7 @@ public enum PushNotificationAPI {
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == getUserHexEncodedPublicKey(db))
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
)
.asRequest(of: String.self)
.fetchSet(db)
@ -118,35 +118,12 @@ public enum PushNotificationAPI {
.map { _ in () }
.eraseToAnyPublisher(),
// FIXME: Remove this once legacy groups are deprecated
PushNotificationAPI
.send(
request: PushNotificationAPIRequest(
endpoint: .legacyGroupsOnlySubscribe,
body: LegacyGroupOnlyRequest(
token: hexEncodedToken,
pubKey: currentUserPublicKey,
device: "ios",
legacyGroupPublicKeys: legacyGroupIds
)
)
)
.decoded(as: LegacyPushServerResponse.self, using: dependencies)
.retry(maxRetryCount)
.handleEvents(
receiveOutput: { _, response in
guard response.code != 0 else {
return SNLog("Couldn't subscribe for legacy groups due to error: \(response.message ?? "nil").")
}
},
receiveCompletion: { result in
switch result {
case .finished: break
case .failure: SNLog("Couldn't subscribe for legacy groups.")
}
}
)
.map { _ in () }
.eraseToAnyPublisher()
PushNotificationAPI.subscribeToLegacyGroups(
forced: true,
token: hexEncodedToken,
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: legacyGroupIds
)
]
)
.collect()
@ -284,14 +261,20 @@ public enum PushNotificationAPI {
// MARK: - Legacy Groups
// FIXME: Remove this once legacy groups are deprecated
public static func subscribeToLegacyGroup(
legacyGroupId: String,
public static func subscribeToLegacyGroups(
forced: Bool = false,
token: String? = nil,
currentUserPublicKey: String,
legacyGroupIds: Set<String>,
using dependencies: SSKDependencies = SSKDependencies()
) -> AnyPublisher<Void, Error> {
let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs]
guard isUsingFullAPNs else {
// Only continue if PNs are enabled and we have a device token
guard
(forced || isUsingFullAPNs),
let deviceToken: String = (token ?? UserDefaults.standard[.deviceToken])
else {
return Just(())
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
@ -300,10 +283,12 @@ public enum PushNotificationAPI {
return PushNotificationAPI
.send(
request: PushNotificationAPIRequest(
endpoint: .legacyGroupSubscribe,
body: LegacyGroupRequest(
endpoint: .legacyGroupsOnlySubscribe,
body: LegacyGroupOnlyRequest(
token: deviceToken,
pubKey: currentUserPublicKey,
closedGroupPublicKey: legacyGroupId
device: "ios",
legacyGroupPublicKeys: legacyGroupIds
)
)
)
@ -312,13 +297,13 @@ public enum PushNotificationAPI {
.handleEvents(
receiveOutput: { _, response in
guard response.code != 0 else {
return SNLog("Couldn't subscribe for legacy group: \(legacyGroupId) due to error: \(response.message ?? "nil").")
return SNLog("Couldn't subscribe for legacy groups due to error: \(response.message ?? "nil").")
}
},
receiveCompletion: { result in
switch result {
case .finished: break
case .failure: SNLog("Couldn't subscribe for legacy group: \(legacyGroupId).")
case .failure: SNLog("Couldn't subscribe for legacy groups.")
}
}
)
@ -332,8 +317,6 @@ public enum PushNotificationAPI {
currentUserPublicKey: String,
using dependencies: SSKDependencies = SSKDependencies()
) -> AnyPublisher<Void, Error> {
let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs]
return PushNotificationAPI
.send(
request: PushNotificationAPIRequest(

Loading…
Cancel
Save