Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/message #1050

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Mixin/Service/Job/RefreshOffsetJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ class RefreshOffsetJob: BaseJob {
}
switch MessageAPI.messageStatus(offset: statusOffset) {
case let .success(blazeMessages):
guard blazeMessages.count > 0, let lastStatusOffset = blazeMessages.last?.updatedAt.toUTCDate().nanosecond() else {
guard let lastStatusOffset = blazeMessages.last?.updatedAt.toUTCDate().nanosecond() else {
Logger.general.debug(category: "RefreshOffsetJob", message: "Early returned for empty data")
return
}
for data in blazeMessages {
MessageDAO.shared.updateMessageStatus(messageId: data.messageId, status: data.status, from: "messageStatus")
AppGroupUserDefaults.Crypto.Offset.status = data.updatedAt.toUTCDate().nanosecond()
ReceiveMessageService.shared.updatePendingMessageStatuses { statuses in
for data in blazeMessages {
let messageExists = MessageDAO.shared.updateMessageStatus(messageId: data.messageId, status: data.status, from: "RefreshOffset")
AppGroupUserDefaults.Crypto.Offset.status = data.updatedAt.toUTCDate().nanosecond()
if !messageExists {
if let status = statuses[data.messageId], MessageStatus.getOrder(messageStatus: status) >= MessageStatus.getOrder(messageStatus: data.status) {
// Don't replace it with a new incoming but low ordered status
continue
} else {
statuses[data.messageId] = data.status
Logger.general.debug(category: "RefreshOffsetJob", message: "Saved status for inexisted message: \(data.messageId), status: \(data.status)")
}
}
}
}
if lastStatusOffset == statusOffset {
return
Expand Down
58 changes: 31 additions & 27 deletions MixinServices/MixinServices/Database/User/DAO/MessageDAO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,34 +273,37 @@ public final class MessageDAO: UserDatabaseDAO {
UNUserNotificationCenter.current().removeNotifications(withIdentifiers: mentionMessageIds)
}

public func updateMessageStatus(messageId: String, status: String, from: String) {
guard let oldMessage: Message = db.select(where: Message.column(of: .messageId) == messageId) else {
return
}
guard oldMessage.status != MessageStatus.FAILED.rawValue else {
let error = MixinServicesError.badMessageData(id: messageId, status: status, from: from)
reporter.report(error: error)
return
}
guard MessageStatus.getOrder(messageStatus: status) > MessageStatus.getOrder(messageStatus: oldMessage.status) else {
return
}

let conversationId = oldMessage.conversationId

let completion: ((GRDB.Database) -> Void)?
if isAppExtension {
completion = nil
} else {
completion = { _ in
let status = MessageStatus(rawValue: status) ?? .UNKNOWN
let change = ConversationChange(conversationId: conversationId,
action: .updateMessageStatus(messageId: messageId, newStatus: status))
NotificationCenter.default.post(onMainThread: conversationDidChangeNotification, object: change)
}
}

// Return true if message exists, false if not exists
@discardableResult
public func updateMessageStatus(messageId: String, status: String, from: String) -> Bool {
var messageExists = true
db.write { db in
guard let current = try Message.filter(Message.column(of: .messageId) == messageId).fetchOne(db) else {
messageExists = false
return
}
guard current.status != MessageStatus.FAILED.rawValue else {
let error = MixinServicesError.badMessageData(id: messageId, status: status, from: from)
reporter.report(error: error)
return
}
guard MessageStatus.getOrder(messageStatus: status) > MessageStatus.getOrder(messageStatus: current.status) else {
return
}

let conversationId = current.conversationId
let completion: ((GRDB.Database) -> Void)?
if isAppExtension {
completion = nil
} else {
completion = { _ in
let status = MessageStatus(rawValue: status) ?? .UNKNOWN
let change = ConversationChange(conversationId: conversationId,
action: .updateMessageStatus(messageId: messageId, newStatus: status))
NotificationCenter.default.post(onMainThread: conversationDidChangeNotification, object: change)
}
}

try Message
.filter(Message.column(of: .messageId) == messageId)
.updateAll(db, [Message.column(of: .status).set(to: status)])
Expand All @@ -311,6 +314,7 @@ public final class MessageDAO: UserDatabaseDAO {
db.afterNextTransactionCommit(completion)
}
}
return messageExists
}

public func updateUnseenMessageCount(database: GRDB.Database, conversationId: String) throws {
Expand Down
3 changes: 3 additions & 0 deletions MixinServices/MixinServices/Services/LoginManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public final class LoginManager {
SignalDatabase.current.erase()
PropertiesDAO.shared.removeValue(forKey: .iterator)
AppGroupUserDefaults.Crypto.clearAll()
ReceiveMessageService.shared.updatePendingMessageStatuses { statuses in
statuses.removeAll()
}
NotificationCenter.default.post(name: LoginManager.didLogoutNotification, object: self)
}
}
Expand Down
Loading