@ -10,15 +10,19 @@ import SessionSnodeKit
// N o t e : L o o k s l i k e t h e o l d e s t i O S d e v i c e w e s u p p o r t ( m i n i O S 1 3 . 0 ) h a s 2 G b o f R A M , p r o c e s s i n g
// ~ 2 5 0 k m e s s a g e s a n d ~ 1 0 0 0 t h r e a d s s e e m s t o t a k e u p
enum _003_YDBToGRDBMigration : Migration {
static let target : TargetMigrations . Identifier = . messagingKit
static let identifier : String = " YDBToGRDBMigration "
static let minExpectedRunDuration : TimeInterval = 20
static let needsConfigSync : Bool = true
static let minExpectedRunDuration : TimeInterval = 20
static func migrate ( _ db : Database ) throws {
let targetIdentifier : TargetMigrations . Identifier = . messagingKit
guard let dbConnection : YapDatabaseConnection = SUKLegacy . newDatabaseConnection ( ) else {
SNLog ( " [Migration Warning] No legacy database, skipping \( target . key ( with : self ) ) " )
return
}
// MARK: - R e a d f r o m L e g a c y D a t a b a s e
// MARK: - P r o c e s s C o n t a c t s , T h r e a d s & I n t e r a c t i o n s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - SessionMessagingKit migration - Start " )
var shouldFailMigration : Bool = false
var legacyMigrations : Set < SMKLegacy . _DBMigration > = [ ]
var contacts : Set < SMKLegacy . _Contact > = [ ]
@ -48,89 +52,24 @@ enum _003_YDBToGRDBMigration: Migration {
var outgoingReadReceiptsTimestampsMs : [ String : Set < Int64 > ] = [ : ]
var receivedMessageTimestamps : Set < UInt64 > = [ ]
// M a p t h e L e g a c y t y p e s f o r t h e N S K e y e d U n a r c h i v e r
NSKeyedUnarchiver . setClass (
SMKLegacy . _Thread . self ,
forClassName : " TSThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ContactThread . self ,
forClassName : " TSContactThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _GroupThread . self ,
forClassName : " TSGroupThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _GroupModel . self ,
forClassName : " TSGroupModel "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Contact . self ,
forClassName : " SNContact "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBInteraction . self ,
forClassName : " TSInteraction "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBMessage . self ,
forClassName : " TSMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBQuotedMessage . self ,
forClassName : " TSQuotedMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBQuotedMessage . _DBAttachmentInfo . self ,
forClassName : " OWSAttachmentInfo "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBLinkPreview . self ,
forClassName : " SessionServiceKit.OWSLinkPreview " // V e r y o l d l e g a c y n a m e
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBLinkPreview . self ,
forClassName : " SessionMessagingKit.OWSLinkPreview "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBIncomingMessage . self ,
forClassName : " TSIncomingMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBOutgoingMessage . self ,
forClassName : " TSOutgoingMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBOutgoingMessageRecipientState . self ,
forClassName : " TSOutgoingMessageRecipientState "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBInfoMessage . self ,
forClassName : " TSInfoMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DisappearingConfigurationUpdateInfoMessage . self ,
forClassName : " OWSDisappearingConfigurationUpdateInfoMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Attachment . self ,
forClassName : " TSAttachment "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentStream . self ,
forClassName : " TSAttachmentStream "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentPointer . self ,
forClassName : " TSAttachmentPointer "
)
var notifyPushServerJobs : Set < SMKLegacy . _NotifyPNServerJob > = [ ]
var messageReceiveJobs : Set < SMKLegacy . _MessageReceiveJob > = [ ]
var messageSendJobs : Set < SMKLegacy . _MessageSendJob > = [ ]
var attachmentUploadJobs : Set < SMKLegacy . _AttachmentUploadJob > = [ ]
var attachmentDownloadJobs : Set < SMKLegacy . _AttachmentDownloadJob > = [ ]
var legacyPreferences : [ String : Any ] = [ : ]
// M a p t h e L e g a c y t y p e s f o r t h e N S K e y e d U n a r c h i v e z
self . mapLegacyTypesForNSKeyedUnarchiver ( )
Storage . read { transaction in
dbConnection . read { transaction in
// MARK: - - M i g r a t i o n s
// P r o c e s s t h e m i g r a t i o n s ( w e d o n ' t w a n t t o b o t h e r r u n n i n g t h e o l d m i g r a t i o n s a s i t w o u l d b e
// a w a s t e o f t i m e , r a t h e r w e i n c l u d e t h e l o g i c f r o m t h e o l d m i g r a t i o n s i n h e r e a n d m a k e t h e
// s a m e c h a n g e s i f t h e m i g r a t i o n h a s n ' t a l r e a d y r u n )
transaction . enumerate Row s( inCollection : SMKLegacy . databaseMigrationCollection ) { key , _ , _ , _ in
transaction . enumerateKeys ( inCollection : SMKLegacy . databaseMigrationCollection ) { key , _ in
guard let legacyMigration : SMKLegacy . _DBMigration = SMKLegacy . _DBMigration ( rawValue : key ) else {
SNLog ( " [Migration Error] Found unknown migration " )
shouldFailMigration = true
@ -139,23 +78,28 @@ enum _003_YDBToGRDBMigration: Migration {
legacyMigrations . insert ( legacyMigration )
}
GRDBStorage . shared . update ( progress : 0.01 , for : self , in : target )
// MARK: - - C o n t a c t s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Contacts " )
// P r o c e s s t h e C o n t a c t s
transaction . enumerateRows ( inCollection : SMKLegacy . contactCollection ) { _ , object , _ , _ in
guard let contact = object as ? SMKLegacy . _Contact else { return }
contacts . insert ( contact )
validProfileIds . insert ( contact . sessionID )
}
// P r o c e s s l e g a c y b l o c k e d c o n t a c t s
legacyBlockedSessionIds = Set ( transaction . object (
forKey : SMKLegacy . blockedPhoneNumbersKey ,
inCollection : SMKLegacy . blockListCollection
) as ? [ String ] ? ? [ ] )
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process threads - Start " )
GRDBStorage . shared . update ( progress : 0.02 , for : self , in : target )
// MARK: - - T h r e a d s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Threads " )
// P r o c e s s t h e t h r e a d s
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . threadCollection ) { key , object , _ in
guard let thread : SMKLegacy . _Thread = object as ? SMKLegacy . _Thread else { return }
@ -237,10 +181,12 @@ enum _003_YDBToGRDBMigration: Migration {
openGroupLastDeletionServerId [ thread . uniqueId ] = transaction . object ( forKey : openGroup . id , inCollection : SMKLegacy . openGroupLastDeletionServerIDCollection ) as ? Int64
}
}
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process threads - End " )
GRDBStorage . shared . update ( progress : 0.04 , for : self , in : target )
// MARK: - - I n t e r a c t i o n s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Interactions " )
// P r o c e s s i n t e r a c t i o n s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process interactions - Start " )
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . interactionCollection ) { _ , object , _ in
guard let interaction : SMKLegacy . _DBInteraction = object as ? SMKLegacy . _DBInteraction else {
SNLog ( " [Migration Error] Unable to process interaction " )
@ -251,10 +197,12 @@ enum _003_YDBToGRDBMigration: Migration {
interactions [ interaction . uniqueThreadId ] = ( interactions [ interaction . uniqueThreadId ] ? ? [ ] )
. appending ( interaction )
}
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process interactions - End " )
GRDBStorage . shared . update ( progress : 0.19 , for : self , in : target )
// MARK: - - A t t a c h m e n t s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Attachments " )
// P r o c e s s a t t a c h m e n t s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process attachments - Start " )
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . attachmentsCollection ) { key , object , _ in
guard let attachment : SMKLegacy . _Attachment = object as ? SMKLegacy . _Attachment else {
SNLog ( " [Migration Error] Unable to process attachment " )
@ -264,9 +212,10 @@ enum _003_YDBToGRDBMigration: Migration {
attachments [ key ] = attachment
}
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process attachments - End " )
GRDBStorage . shared . update ( progress : 0.21 , for : self , in : target )
// MARK: - - R e a d R e c e i p t s
// P r o c e s s r e a d r e c e i p t s
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . outgoingReadReceiptManagerCollection ) { key , object , _ in
guard let timestampsMs : Set < Int64 > = object as ? Set < Int64 > else { return }
@ -284,6 +233,84 @@ enum _003_YDBToGRDBMigration: Migration {
. defaulting ( to : [ ] )
. asSet ( )
)
// MARK: - - J o b s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Jobs " )
transaction . enumerateRows ( inCollection : SMKLegacy . notifyPushServerJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _NotifyPNServerJob else { return }
notifyPushServerJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . messageReceiveJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _MessageReceiveJob else { return }
messageReceiveJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . messageSendJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _MessageSendJob else { return }
messageSendJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . attachmentUploadJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _AttachmentUploadJob else { return }
attachmentUploadJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . attachmentDownloadJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _AttachmentDownloadJob else { return }
attachmentDownloadJobs . insert ( job )
}
GRDBStorage . shared . update ( progress : 0.22 , for : self , in : target )
// MARK: - - P r e f e r e n c e s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Processing Preferences " )
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . preferencesCollection ) { key , object , _ in
legacyPreferences [ key ] = object
}
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . additionalPreferencesCollection ) { key , object , _ in
legacyPreferences [ key ] = object
}
// N o t e : T h e ' i n t ( f o r K e y : i n C o l l e c t i o n : ) ' d e f a u l t s t o ` 0 ` w h i c h i s a n i n c o r r e c t v a l u e
// f o r t h e n o t i f i c a t i o n s o u n d s o c a t c h i t a n d d e f a u l t
let globalNotificationSoundValue : Int32 = transaction . int (
forKey : SMKLegacy . soundsGlobalNotificationKey ,
inCollection : SMKLegacy . soundsStorageNotificationCollection
)
legacyPreferences [ SMKLegacy . soundsGlobalNotificationKey ] = ( globalNotificationSoundValue > 0 ?
Int ( globalNotificationSoundValue ) :
Preferences . Sound . defaultNotificationSound . rawValue
)
legacyPreferences [ SMKLegacy . readReceiptManagerAreReadReceiptsEnabled ] = transaction . bool (
forKey : SMKLegacy . readReceiptManagerAreReadReceiptsEnabled ,
inCollection : SMKLegacy . readReceiptManagerCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . typingIndicatorsEnabledKey ] = transaction . bool (
forKey : SMKLegacy . typingIndicatorsEnabledKey ,
inCollection : SMKLegacy . typingIndicatorsCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . screenLockIsScreenLockEnabledKey ] = transaction . bool (
forKey : SMKLegacy . screenLockIsScreenLockEnabledKey ,
inCollection : SMKLegacy . screenLockCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . screenLockScreenLockTimeoutSecondsKey ] = transaction . double (
forKey : SMKLegacy . screenLockScreenLockTimeoutSecondsKey ,
inCollection : SMKLegacy . screenLockCollection ,
defaultValue : ( 15 * 60 )
)
GRDBStorage . shared . update ( progress : 0.23 , for : self , in : target )
}
// W e c a n ' t p r o p e r l y t h r o w w i t h i n t h e ' e n u m e r a t e K e y s A n d O b j e c t s ' b l o c k s o h a v e t o t h r o w h e r e
@ -295,10 +322,14 @@ enum _003_YDBToGRDBMigration: Migration {
// MARK: - I n s e r t C o n t a c t s
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Inserting Contacts " )
try autoreleasepool {
let currentUserPublicKey : String = getUserHexEncodedPublicKey ( db )
// V a l u e s f o r c o n t a c t p r o g r e s s
let contactStartProgress : CGFloat = 0.23
let progressPerContact : CGFloat = ( 0.05 / CGFloat ( contacts . count ) )
try contacts . forEach { legacyContact in
try contacts . enumerated( ) . forEach { index , legacyContact in
let isCurrentUser : Bool = ( legacyContact . sessionID = = currentUserPublicKey )
let contactThreadId : String = SMKLegacy . _ContactThread . threadId ( from : legacyContact . sessionID )
@ -368,12 +399,25 @@ enum _003_YDBToGRDBMigration: Migration {
hasBeenBlocked : ( ! isCurrentUser && ( legacyContact . hasBeenBlocked || legacyContact . isBlocked ) )
) . insert ( db )
}
// I n c r e m e n t t h e p r o g r e s s f o r e a c h c o n t a c t
GRDBStorage . shared . update (
progress : contactStartProgress + ( progressPerContact * CGFloat ( index + 1 ) ) ,
for : self ,
in : target
)
}
}
// C l e a r o u t p r o c e s s e d d a t a ( g i v e t h e m e m o r y a c h a n g e t o b e f r e e d )
contacts = [ ]
legacyBlockedSessionIds = [ ]
contactThreadIds = [ ]
// MARK: - I n s e r t T h r e a d s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process thread inserts - Start " )
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Inserting Threads & Interactions " )
var legacyInteractionToIdMap : [ String : Int64 ] = [ : ]
var legacyInteractionIdentifierToIdMap : [ String : Int64 ] = [ : ]
var legacyInteractionIdentifierToIdFallbackMap : [ String : Int64 ] = [ : ]
@ -411,6 +455,12 @@ enum _003_YDBToGRDBMigration: Migration {
. joined ( separator : " - " )
}
// V a l u e s f o r t h r e a d p r o g r e s s
var interactionCounter : CGFloat = 0
let allInteractionsCount : Int = interactions . map { $0 . value . count } . reduce ( 0 , + )
let threadInteractionsStartProgress : CGFloat = 0.28
let progressPerInteraction : CGFloat = ( 0.70 / CGFloat ( allInteractionsCount ) )
// S o r t b y i d j u s t s o w e c a n m a k e t h e m i g r a t i o n p r o c e s s m o r e d e t e r m i n s t i c
try legacyThreads . sorted ( by : { lhs , rhs in lhs . uniqueId < rhs . uniqueId } ) . forEach { legacyThread in
guard let threadId : String = legacyThreadIdToIdMap [ legacyThread . uniqueId ] else {
@ -921,25 +971,22 @@ enum _003_YDBToGRDBMigration: Migration {
attachmentId : attachmentId
) . insert ( db )
}
}
// I n c r e m e n t t h e p r o g r e s s f o r e a c h c o n t a c t
GRDBStorage . shared . update (
progress : (
threadInteractionsStartProgress +
( progressPerInteraction * ( interactionCounter + 1 ) )
) ,
for : self ,
in : target
)
interactionCounter += 1
}
}
}
// I n s e r t a ' C o n t r o l M e s s a g e P r o c e s s R e c o r d ' f o r a n y r e m a i n i n g ' r e c e i v e d M e s s a g e T i m e s t a m p '
// e n t r i e s a s " l e g a c y "
try ControlMessageProcessRecord . generateLegacyProcessRecords (
db ,
receivedMessageTimestamps : receivedMessageTimestamps . map { Int64 ( $0 ) }
)
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process thread inserts - End " )
// C l e a r o u t p r o c e s s e d d a t a ( g i v e t h e m e m o r y a c h a n g e t o b e f r e e d )
contacts = [ ]
legacyBlockedSessionIds = [ ]
contactThreadIds = [ ]
legacyThreads = [ ]
disappearingMessagesConfiguration = [ : ]
@ -957,150 +1004,24 @@ enum _003_YDBToGRDBMigration: Migration {
interactions = [ : ]
attachments = [ : ]
// MARK: - - R e c e i v e d M e s s a g e T i m e s t a m p s
// I n s e r t a ' C o n t r o l M e s s a g e P r o c e s s R e c o r d ' f o r a n y r e m a i n i n g ' r e c e i v e d M e s s a g e T i m e s t a m p '
// e n t r i e s a s " l e g a c y "
try ControlMessageProcessRecord . generateLegacyProcessRecords (
db ,
receivedMessageTimestamps : receivedMessageTimestamps . map { Int64 ( $0 ) }
)
// C l e a r o u t p r o c e s s e d d a t a ( g i v e t h e m e m o r y a c h a n g e t o b e f r e e d )
receivedMessageTimestamps = [ ]
// MARK: - P r o c e s s L e g a c y J o b s
// MARK: - In s e r t J o b s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process jobs - Start " )
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Inserting Jobs " )
var notifyPushServerJobs : Set < SMKLegacy . _NotifyPNServerJob > = [ ]
var messageReceiveJobs : Set < SMKLegacy . _MessageReceiveJob > = [ ]
var messageSendJobs : Set < SMKLegacy . _MessageSendJob > = [ ]
var attachmentUploadJobs : Set < SMKLegacy . _AttachmentUploadJob > = [ ]
var attachmentDownloadJobs : Set < SMKLegacy . _AttachmentDownloadJob > = [ ]
// M a p t h e L e g a c y t y p e s f o r t h e N S K e y e d U n a r c h i v e r
NSKeyedUnarchiver . setClass (
SMKLegacy . _NotifyPNServerJob . self ,
forClassName : " SessionMessagingKit.NotifyPNServerJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _NotifyPNServerJob . _SnodeMessage . self ,
forClassName : " SessionSnodeKit.SnodeMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageSendJob . self ,
forClassName : " SessionMessagingKit.SNMessageSendJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageReceiveJob . self ,
forClassName : " SessionMessagingKit.MessageReceiveJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentUploadJob . self ,
forClassName : " SessionMessagingKit.AttachmentUploadJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentDownloadJob . self ,
forClassName : " SessionMessagingKit.AttachmentDownloadJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Message . self ,
forClassName : " SNMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _VisibleMessage . self ,
forClassName : " SNVisibleMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Quote . self ,
forClassName : " SNQuote "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _LinkPreview . self ,
forClassName : " SNLinkPreview "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Profile . self ,
forClassName : " SNProfile "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _OpenGroupInvitation . self ,
forClassName : " SNOpenGroupInvitation "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ControlMessage . self ,
forClassName : " SNControlMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ReadReceipt . self ,
forClassName : " SNReadReceipt "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _TypingIndicator . self ,
forClassName : " SNTypingIndicator "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ClosedGroupControlMessage . self ,
forClassName : " SessionMessagingKit.ClosedGroupControlMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ClosedGroupControlMessage . _KeyPairWrapper . self ,
forClassName : " ClosedGroupControlMessage.SNKeyPairWrapper "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DataExtractionNotification . self ,
forClassName : " SessionMessagingKit.DataExtractionNotification "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ExpirationTimerUpdate . self ,
forClassName : " SNExpirationTimerUpdate "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ConfigurationMessage . self ,
forClassName : " SNConfigurationMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _CMClosedGroup . self ,
forClassName : " SNClosedGroup "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _CMContact . self ,
forClassName : " SNConfigurationMessage.SNConfigurationMessageContact "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _UnsendRequest . self ,
forClassName : " SNUnsendRequest "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageRequestResponse . self ,
forClassName : " SNMessageRequestResponse "
)
Storage . read { transaction in
transaction . enumerateRows ( inCollection : SMKLegacy . notifyPushServerJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _NotifyPNServerJob else { return }
notifyPushServerJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . messageReceiveJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _MessageReceiveJob else { return }
messageReceiveJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . messageSendJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _MessageSendJob else { return }
messageSendJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . attachmentUploadJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _AttachmentUploadJob else { return }
attachmentUploadJobs . insert ( job )
}
transaction . enumerateRows ( inCollection : SMKLegacy . attachmentDownloadJobCollection ) { _ , object , _ , _ in
guard let job = object as ? SMKLegacy . _AttachmentDownloadJob else { return }
attachmentDownloadJobs . insert ( job )
}
}
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process jobs - End " )
// MARK: - I n s e r t J o b s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process job inserts - Start " )
// MARK: - - - n o t i f y P u s h S e r v e r
// MARK: - - n o t i f y P u s h S e r v e r
try autoreleasepool {
try notifyPushServerJobs . forEach { legacyJob in
@ -1123,7 +1044,7 @@ enum _003_YDBToGRDBMigration: Migration {
}
}
// MARK: - - -m e s s a g e R e c e i v e
// MARK: - -m e s s a g e R e c e i v e
try autoreleasepool {
try messageReceiveJobs . forEach { legacyJob in
@ -1172,7 +1093,7 @@ enum _003_YDBToGRDBMigration: Migration {
}
}
// MARK: - - -m e s s a g e S e n d
// MARK: - -m e s s a g e S e n d
var messageSendJobLegacyMap : [ String : Job ] = [ : ]
@ -1266,7 +1187,7 @@ enum _003_YDBToGRDBMigration: Migration {
}
}
// MARK: - - -a t t a c h m e n t U p l o a d
// MARK: - -a t t a c h m e n t U p l o a d
try autoreleasepool {
try attachmentUploadJobs . forEach { legacyJob in
@ -1300,7 +1221,7 @@ enum _003_YDBToGRDBMigration: Migration {
}
}
// MARK: - - -a t t a c h m e n t D o w n l o a d
// MARK: - -a t t a c h m e n t D o w n l o a d
try autoreleasepool {
try attachmentDownloadJobs . forEach { legacyJob in
@ -1327,7 +1248,7 @@ enum _003_YDBToGRDBMigration: Migration {
}
}
// MARK: - - -s e n d R e a d R e c e i p t s
// MARK: - -s e n d R e a d R e c e i p t s
try autoreleasepool {
try outgoingReadReceiptsTimestampsMs . forEach { threadId , timestampsMs in
@ -1342,59 +1263,11 @@ enum _003_YDBToGRDBMigration: Migration {
) ? . inserted ( db )
}
}
GRDBStorage . shared . update ( progress : 0.99 , for : self , in : target )
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process job inserts - End " )
// MARK: - P r o c e s s P r e f e r e n c e s
// MARK: - P r e f e r e n c e s
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process preferences inserts - Start " )
var legacyPreferences : [ String : Any ] = [ : ]
Storage . read { transaction in
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . preferencesCollection ) { key , object , _ in
legacyPreferences [ key ] = object
}
transaction . enumerateKeysAndObjects ( inCollection : SMKLegacy . additionalPreferencesCollection ) { key , object , _ in
legacyPreferences [ key ] = object
}
// N o t e : T h e ' i n t ( f o r K e y : i n C o l l e c t i o n : ) ' d e f a u l t s t o ` 0 ` w h i c h i s a n i n c o r r e c t v a l u e
// f o r t h e n o t i f i c a t i o n s o u n d s o c a t c h i t a n d d e f a u l t
let globalNotificationSoundValue : Int32 = transaction . int (
forKey : SMKLegacy . soundsGlobalNotificationKey ,
inCollection : SMKLegacy . soundsStorageNotificationCollection
)
legacyPreferences [ SMKLegacy . soundsGlobalNotificationKey ] = ( globalNotificationSoundValue > 0 ?
Int ( globalNotificationSoundValue ) :
Preferences . Sound . defaultNotificationSound . rawValue
)
legacyPreferences [ SMKLegacy . readReceiptManagerAreReadReceiptsEnabled ] = transaction . bool (
forKey : SMKLegacy . readReceiptManagerAreReadReceiptsEnabled ,
inCollection : SMKLegacy . readReceiptManagerCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . typingIndicatorsEnabledKey ] = transaction . bool (
forKey : SMKLegacy . typingIndicatorsEnabledKey ,
inCollection : SMKLegacy . typingIndicatorsCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . screenLockIsScreenLockEnabledKey ] = transaction . bool (
forKey : SMKLegacy . screenLockIsScreenLockEnabledKey ,
inCollection : SMKLegacy . screenLockCollection ,
defaultValue : false
)
legacyPreferences [ SMKLegacy . screenLockScreenLockTimeoutSecondsKey ] = transaction . double (
forKey : SMKLegacy . screenLockScreenLockTimeoutSecondsKey ,
inCollection : SMKLegacy . screenLockCollection ,
defaultValue : ( 15 * 60 )
)
}
SNLog ( " [Migration Info] \( target . key ( with : self ) ) - Inserting Preferences " )
db [ . defaultNotificationSound ] = Preferences . Sound ( rawValue : legacyPreferences [ SMKLegacy . soundsGlobalNotificationKey ] as ? Int ? ? - 1 )
. defaulting ( to : Preferences . Sound . defaultNotificationSound )
@ -1425,10 +1298,6 @@ enum _003_YDBToGRDBMigration: Migration {
db [ . hasSavedThread ] = ( legacyPreferences [ SMKLegacy . preferencesKeyHasSavedThreadKey ] as ? Bool = = true )
db [ . hasSentAMessage ] = ( legacyPreferences [ SMKLegacy . preferencesKeyHasSentAMessageKey ] as ? Bool = = true )
db [ . isReadyForAppExtensions ] = CurrentAppContext ( ) . appUserDefaults ( ) . bool ( forKey : SMKLegacy . preferencesKeyIsReadyForAppExtensions )
print ( " RAWR [ \( Date ( ) . timeIntervalSince1970 ) ] - Process preferences inserts - End " )
print ( " RAWR Done!!! " )
}
// MARK: - C o n v e n i e n c e
@ -1582,4 +1451,179 @@ enum _003_YDBToGRDBMigration: Migration {
return legacyAttachmentId
}
private static func mapLegacyTypesForNSKeyedUnarchiver ( ) {
NSKeyedUnarchiver . setClass (
SMKLegacy . _Thread . self ,
forClassName : " TSThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ContactThread . self ,
forClassName : " TSContactThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _GroupThread . self ,
forClassName : " TSGroupThread "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _GroupModel . self ,
forClassName : " TSGroupModel "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Contact . self ,
forClassName : " SNContact "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBInteraction . self ,
forClassName : " TSInteraction "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBMessage . self ,
forClassName : " TSMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBQuotedMessage . self ,
forClassName : " TSQuotedMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBQuotedMessage . _DBAttachmentInfo . self ,
forClassName : " OWSAttachmentInfo "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBLinkPreview . self ,
forClassName : " SessionServiceKit.OWSLinkPreview " // V e r y o l d l e g a c y n a m e
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBLinkPreview . self ,
forClassName : " SessionMessagingKit.OWSLinkPreview "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBIncomingMessage . self ,
forClassName : " TSIncomingMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBOutgoingMessage . self ,
forClassName : " TSOutgoingMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBOutgoingMessageRecipientState . self ,
forClassName : " TSOutgoingMessageRecipientState "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DBInfoMessage . self ,
forClassName : " TSInfoMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DisappearingConfigurationUpdateInfoMessage . self ,
forClassName : " OWSDisappearingConfigurationUpdateInfoMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Attachment . self ,
forClassName : " TSAttachment "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentStream . self ,
forClassName : " TSAttachmentStream "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentPointer . self ,
forClassName : " TSAttachmentPointer "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _NotifyPNServerJob . self ,
forClassName : " SessionMessagingKit.NotifyPNServerJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _NotifyPNServerJob . _SnodeMessage . self ,
forClassName : " SessionSnodeKit.SnodeMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageSendJob . self ,
forClassName : " SessionMessagingKit.SNMessageSendJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageReceiveJob . self ,
forClassName : " SessionMessagingKit.MessageReceiveJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentUploadJob . self ,
forClassName : " SessionMessagingKit.AttachmentUploadJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _AttachmentDownloadJob . self ,
forClassName : " SessionMessagingKit.AttachmentDownloadJob "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Message . self ,
forClassName : " SNMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _VisibleMessage . self ,
forClassName : " SNVisibleMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Quote . self ,
forClassName : " SNQuote "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _LinkPreview . self ,
forClassName : " SNLinkPreview "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _Profile . self ,
forClassName : " SNProfile "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _OpenGroupInvitation . self ,
forClassName : " SNOpenGroupInvitation "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ControlMessage . self ,
forClassName : " SNControlMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ReadReceipt . self ,
forClassName : " SNReadReceipt "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _TypingIndicator . self ,
forClassName : " SNTypingIndicator "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ClosedGroupControlMessage . self ,
forClassName : " SessionMessagingKit.ClosedGroupControlMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ClosedGroupControlMessage . _KeyPairWrapper . self ,
forClassName : " ClosedGroupControlMessage.SNKeyPairWrapper "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _DataExtractionNotification . self ,
forClassName : " SessionMessagingKit.DataExtractionNotification "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ExpirationTimerUpdate . self ,
forClassName : " SNExpirationTimerUpdate "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _ConfigurationMessage . self ,
forClassName : " SNConfigurationMessage "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _CMClosedGroup . self ,
forClassName : " SNClosedGroup "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _CMContact . self ,
forClassName : " SNConfigurationMessage.SNConfigurationMessageContact "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _UnsendRequest . self ,
forClassName : " SNUnsendRequest "
)
NSKeyedUnarchiver . setClass (
SMKLegacy . _MessageRequestResponse . self ,
forClassName : " SNMessageRequestResponse "
)
}
}