Skip to content

Commit

Permalink
Set lastVisibleRowId on threads and wasRead on messages when restoring
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-signal authored Nov 1, 2024
1 parent 2a88f9e commit 03d9cc0
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 17 deletions.
1 change: 1 addition & 0 deletions SignalServiceKit/Environment/AppSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ public class AppSetup {
plaintextStreamProvider: MessageBackupPlaintextProtoStreamProviderImpl(),
postFrameRestoreActionManager: MessageBackupPostFrameRestoreActionManager(
interactionStore: backupInteractionStore,
lastVisibleInteractionStore: lastVisibleInteractionStore,
recipientDatabaseTable: recipientDatabaseTable,
sskPreferences: MessageBackupPostFrameRestoreActionManager.Wrappers.SSKPreferences(),
threadStore: backupThreadStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ extension MessageBackup {
public struct PostFrameRestoreActions {
var isPinned: Bool
var lastVisibleInteractionRowId: Int64?
var hadAnyUnreadMessages: Bool = false

var shouldBeMarkedVisible: Bool {
isPinned || lastVisibleInteractionRowId != nil
Expand All @@ -234,6 +235,7 @@ extension MessageBackup {

func updateLastVisibleInteractionRowId(
interactionRowId: Int64,
wasRead: Bool,
chatId: ChatId
) {
var actions = postFrameRestoreActions[chatId] ?? .default
Expand All @@ -245,6 +247,7 @@ extension MessageBackup {
{
actions.lastVisibleInteractionRowId = interactionRowId
}
actions.hadAnyUnreadMessages = actions.hadAnyUnreadMessages || !wasRead
postFrameRestoreActions[chatId] = actions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,20 @@ final class MessageBackupGroupCallArchiver {
thread: groupThread,
sentAtTimestamp: chatItem.dateSent
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
groupCallInteraction,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,20 @@ final class MessageBackupIndividualCallArchiver {
thread: contactThread,
sentAtTimestamp: chatItem.dateSent
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
individualCallInteraction,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,20 @@ final class MessageBackupGroupUpdateMessageArchiver {
groupThread: groupThread,
updateItems: persistableUpdates
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
infoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ final class MessageBackupExpirationTimerChatUpdateArchiver {
configurationDurationSeconds: UInt32(clamping: expiresInSeconds), // Safe to clamp, we checked for overflow above
createdByRemoteName: createdByRemoteName
)

guard let directionalDetails = chatItem.directionalDetails else {
return invalidProtoData(.chatItemMissingDirectionalDetails)
}

do {
try interactionStore.insert(
dmUpdateInfoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,20 @@ final class MessageBackupLearnedProfileChatUpdateArchiver {
timestamp: chatItem.dateSent,
displayNameBefore: displayNameBefore
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
learnedProfileKeyInfoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ final class MessageBackupProfileChangeChatUpdateArchiver {
)
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
profileChangeInfoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ final class MessageBackupSessionSwitchoverChatUpdateArchiver {
phoneNumber: e164.stringValue
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
sessionSwitchoverInfoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,28 +564,55 @@ final class MessageBackupSimpleChatUpdateArchiver {
simpleChatUpdateInteraction = .simpleInfoMessage(.acceptedMessageRequest)
}

let interactionToInsert: TSInteraction = switch simpleChatUpdateInteraction {
guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

switch simpleChatUpdateInteraction {
case .simpleInfoMessage(let infoMessageType):
TSInfoMessage(
let infoMessage = TSInfoMessage(
thread: thread,
messageType: infoMessageType,
timestamp: chatItem.dateSent
)
do {
try interactionStore.insert(
infoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
return .messageFailure([.restoreFrameError(.databaseInsertionFailed(error), chatItem.id)])
}
case .prebuiltInfoMessage(let infoMessage):
infoMessage
do {
try interactionStore.insert(
infoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
return .messageFailure([.restoreFrameError(.databaseInsertionFailed(error), chatItem.id)])
}
case .errorMessage(let errorMessage):
errorMessage
}

do {
try interactionStore.insert(
interactionToInsert,
in: chatThread,
chatId: chatItem.typedChatId,
context: context
)
} catch let error {
return .messageFailure([.restoreFrameError(.databaseInsertionFailed(error), chatItem.id)])
do {
try interactionStore.insert(
errorMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
return .messageFailure([.restoreFrameError(.databaseInsertionFailed(error), chatItem.id)])
}
}

return .success(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ final class MessageBackupThreadMergeChatUpdateArchiver {
previousE164: previousE164.stringValue
)

guard let directionalDetails = chatItem.directionalDetails else {
return .messageFailure([.restoreFrameError(
.invalidProtoData(.chatItemMissingDirectionalDetails),
chatItem.id
)])
}

do {
try interactionStore.insert(
threadMergeInfoMessage,
in: chatThread,
chatId: chatItem.typedChatId,
directionalDetails: directionalDetails,
context: context
)
} catch let error {
Expand Down
Loading

0 comments on commit 03d9cc0

Please sign in to comment.