|
|
|
@ -278,56 +278,65 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate {
|
|
|
|
|
let duration: TimeInterval = self.duration
|
|
|
|
|
let hasStartedConnecting: Bool = self.hasStartedConnecting
|
|
|
|
|
|
|
|
|
|
// Don't know whether there will be an impact if we don't write async
|
|
|
|
|
Storage.shared.write { db in
|
|
|
|
|
guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let updateToMissedIfNeeded: () throws -> () = {
|
|
|
|
|
let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed)
|
|
|
|
|
|
|
|
|
|
guard
|
|
|
|
|
let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8),
|
|
|
|
|
let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode(
|
|
|
|
|
CallMessage.MessageInfo.self,
|
|
|
|
|
from: infoMessageData
|
|
|
|
|
),
|
|
|
|
|
messageInfo.state == .incoming,
|
|
|
|
|
let missedCallInfoData: Data = try? JSONEncoder().encode(missedCallInfo)
|
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
|
|
_ = try interaction
|
|
|
|
|
.with(body: String(data: missedCallInfoData, encoding: .utf8))
|
|
|
|
|
.saved(db)
|
|
|
|
|
}
|
|
|
|
|
let shouldMarkAsRead: Bool = try {
|
|
|
|
|
if duration > 0 { return true }
|
|
|
|
|
if hasStartedConnecting { return true }
|
|
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
|
case .local:
|
|
|
|
|
try updateToMissedIfNeeded()
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
case .remote, .unanswered:
|
|
|
|
|
try updateToMissedIfNeeded()
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
case .answeredElsewhere: return true
|
|
|
|
|
Storage.shared.writeAsync(
|
|
|
|
|
updates: { db in
|
|
|
|
|
guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
guard shouldMarkAsRead else { return }
|
|
|
|
|
|
|
|
|
|
try Interaction.markAsRead(
|
|
|
|
|
db,
|
|
|
|
|
interactionId: interaction.id,
|
|
|
|
|
threadId: interaction.threadId,
|
|
|
|
|
includingOlder: false,
|
|
|
|
|
trySendReadReceipt: false
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
let updateToMissedIfNeeded: () throws -> () = {
|
|
|
|
|
let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed)
|
|
|
|
|
|
|
|
|
|
guard
|
|
|
|
|
let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8),
|
|
|
|
|
let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode(
|
|
|
|
|
CallMessage.MessageInfo.self,
|
|
|
|
|
from: infoMessageData
|
|
|
|
|
),
|
|
|
|
|
messageInfo.state == .incoming,
|
|
|
|
|
let missedCallInfoData: Data = try? JSONEncoder().encode(missedCallInfo)
|
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
|
|
_ = try interaction
|
|
|
|
|
.with(body: String(data: missedCallInfoData, encoding: .utf8))
|
|
|
|
|
.saved(db)
|
|
|
|
|
}
|
|
|
|
|
let shouldMarkAsRead: Bool = try {
|
|
|
|
|
if duration > 0 { return true }
|
|
|
|
|
if hasStartedConnecting { return true }
|
|
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
|
case .local:
|
|
|
|
|
try updateToMissedIfNeeded()
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
case .remote, .unanswered:
|
|
|
|
|
try updateToMissedIfNeeded()
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
case .answeredElsewhere: return true
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
guard shouldMarkAsRead else { return }
|
|
|
|
|
|
|
|
|
|
try Interaction.markAsRead(
|
|
|
|
|
db,
|
|
|
|
|
interactionId: interaction.id,
|
|
|
|
|
threadId: interaction.threadId,
|
|
|
|
|
includingOlder: false,
|
|
|
|
|
trySendReadReceipt: false
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
completion: { _, _ in
|
|
|
|
|
if CurrentAppContext().isInBackground() {
|
|
|
|
|
// Stop all jobs except for message sending and when completed suspend the database
|
|
|
|
|
JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend) {
|
|
|
|
|
NotificationCenter.default.post(name: Database.suspendNotification, object: self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Renderer
|
|
|
|
|