@ -53,27 +53,65 @@ public class ConversationViewModel: OWSAudioPlayerDelegate {
// MARK: - I n i t i a l i z a t i o n
init ( threadId : String , threadVariant : SessionThread . Variant , focusedInteractionId : Int64 ? ) {
// I f w e h a v e a s p e c i f i e d ' f o c u s e d I n t e r a c t i o n I d ' t h e n u s e t h a t , o t h e r w i s e r e t r i e v e t h e o l d e s t
// u n r e a d i n t e r a c t i o n a n d s t a r t f o c u s e d a r o u n d t h a t o n e
let targetInteractionId : Int64 ? = {
if let focusedInteractionId : Int64 = focusedInteractionId { return focusedInteractionId }
typealias InitialData = (
targetInteractionId : Int64 ? ,
currentUserIsClosedGroupMember : Bool ? ,
openGroupPermissions : OpenGroup . Permissions ? ,
blindedKey : String ?
)
let initialData : InitialData ? = Storage . shared . read { db -> InitialData in
let interaction : TypedTableAlias < Interaction > = TypedTableAlias ( )
let groupMember : TypedTableAlias < GroupMember > = TypedTableAlias ( )
return Storage . shared . read { db in
let interaction : TypedTableAlias < Interaction > = TypedTableAlias ( )
return try Interaction
// I f w e h a v e a s p e c i f i e d ' f o c u s e d I n t e r a c t i o n I d ' t h e n u s e t h a t , o t h e r w i s e r e t r i e v e t h e o l d e s t
// u n r e a d i n t e r a c t i o n a n d s t a r t f o c u s e d a r o u n d t h a t o n e
let targetInteractionId : Int64 ? = ( focusedInteractionId != nil ? focusedInteractionId :
try Interaction
. select ( . id )
. filter ( interaction [ . wasRead ] = = false )
. filter ( interaction [ . threadId ] = = threadId )
. order ( interaction [ . timestampMs ] . asc )
. asRequest ( of : Int64 . self )
. fetchOne ( db )
}
} ( )
)
let currentUserIsClosedGroupMember : Bool ? = ( threadVariant != . closedGroup ? nil :
try GroupMember
. filter ( groupMember [ . groupId ] = = threadId )
. filter ( groupMember [ . profileId ] = = getUserHexEncodedPublicKey ( db ) )
. filter ( groupMember [ . role ] = = GroupMember . Role . standard )
. isNotEmpty ( db )
)
let openGroupPermissions : OpenGroup . Permissions ? = ( threadVariant != . openGroup ? nil :
try OpenGroup
. filter ( id : threadId )
. select ( . permissions )
. asRequest ( of : OpenGroup . Permissions . self )
. fetchOne ( db )
)
let blindedKey : String ? = SessionThread . getUserHexEncodedBlindedKey (
db ,
threadId : threadId ,
threadVariant : threadVariant
)
return (
targetInteractionId ,
currentUserIsClosedGroupMember ,
openGroupPermissions ,
blindedKey
)
}
self . threadId = threadId
self . initialThreadVariant = threadVariant
self . focusedInteractionId = targetInteractionId
self . focusedInteractionId = initialData ? . targetInteractionId
self . threadData = SessionThreadViewModel (
threadId : threadId ,
threadVariant : threadVariant ,
currentUserIsClosedGroupMember : initialData ? . currentUserIsClosedGroupMember ,
openGroupPermissions : initialData ? . openGroupPermissions
) . populatingCurrentUserBlindedKey ( currentUserBlindedPublicKeyForThisThread : initialData ? . blindedKey )
self . pagedDataObserver = nil
// N o t e : S i n c e t h i s r e f e r e n c e s s e l f w e n e e d t o f i n i s h i n i t i a l i z i n g b e f o r e s e t t i n g i t , w e
@ -93,7 +131,7 @@ public class ConversationViewModel: OWSAudioPlayerDelegate {
DispatchQueue . global ( qos : . userInitiated ) . async { [ weak self ] in
// I f w e d o n ' t h a v e a ` i n i t i a l F o c u s e d I d ` t h e n d e f a u l t t o ` . p a g e B e f o r e ` ( i t ' l l q u e r y
// f r o m a ` 0 ` o f f s e t )
guard let initialFocusedId : Int64 = targetInteractionId else {
guard let initialFocusedId : Int64 = initialData? . targetInteractionId else {
self ? . pagedDataObserver ? . load ( . pageBefore )
return
}
@ -105,21 +143,7 @@ public class ConversationViewModel: OWSAudioPlayerDelegate {
// MARK: - T h r e a d D a t a
// / T h i s v a l u e i s t h e c u r r e n t s t a t e o f t h e v i e w
public private ( set ) lazy var threadData : SessionThreadViewModel = SessionThreadViewModel (
threadId : self . threadId ,
threadVariant : self . initialThreadVariant ,
currentUserIsClosedGroupMember : ( self . initialThreadVariant != . closedGroup ?
nil :
Storage . shared . read { db in
try GroupMember
. filter ( GroupMember . Columns . groupId = = self . threadId )
. filter ( GroupMember . Columns . profileId = = getUserHexEncodedPublicKey ( db ) )
. filter ( GroupMember . Columns . role = = GroupMember . Role . standard )
. isNotEmpty ( db )
}
)
)
. populatingCurrentUserBlindedKey ( )
public private ( set ) var threadData : SessionThreadViewModel
// / T h i s i s a l l t h e d a t a t h e s c r e e n n e e d s t o p o p u l a t e i t s e l f , p l e a s e s e e t h e f o l l o w i n g l i n k f o r t i p s t o h e l p o p t i m i s e
// / p e r f o r m a n c e h t t p s : / / g i t h u b . c o m / g r o u e / G R D B . s w i f t # v a l u e o b s e r v a t i o n - p e r f o r m a n c e