@ -33,11 +33,7 @@ internal extension SessionUtil {
let userED25519KeyPair : KeyPair = Identity . fetchUserEd25519KeyPair ( db , using : dependencies )
else { throw MessageSenderError . noKeyPair }
// T h e r e w i l l p r o b a b l y b e c u s t o m i n i t f u n c t i o n s , w i l l n e e d a w a y t o s a v e t h e c o n f i n t o
// t h e i n - m e m o r y s t a t e a f t e r i n i t t h o u g h
var secretKey : [ UInt8 ] = userED25519KeyPair . secretKey
var groupIdentityPublicKey : [ UInt8 ] = groupIdentityKeyPair . publicKey
var groupIdentityPrivateKey : [ UInt8 ] = groupIdentityKeyPair . secretKey
// P r e p t h e r e l e v a n t d e t a i l s
let groupSessionId : SessionId = SessionId ( . group , publicKey : groupIdentityKeyPair . publicKey )
let creationTimestamp : TimeInterval = TimeInterval (
SnodeAPI . currentOffsetTimestampMs ( using : dependencies ) / 1000
@ -46,43 +42,17 @@ internal extension SessionUtil {
let currentUserProfile : Profile ? = Profile . fetchOrCreateCurrentUser ( db , using : dependencies )
// C r e a t e t h e n e w c o n f i g o b j e c t s
var groupKeysConf : UnsafeMutablePointer < config_group_keys > ? = nil
var groupInfoConf : UnsafeMutablePointer < config_object > ? = nil
var groupMembersConf : UnsafeMutablePointer < config_object > ? = nil
var error : [ CChar ] = [ CChar ] ( repeating : 0 , count : 256 )
try groups_info_init (
& groupInfoConf ,
& groupIdentityPublicKey ,
& groupIdentityPrivateKey ,
nil ,
0 ,
& error
) . orThrow ( error : error )
try groups_members_init (
& groupMembersConf ,
& groupIdentityPublicKey ,
& groupIdentityPrivateKey ,
nil ,
0 ,
& error
) . orThrow ( error : error )
try groups_keys_init (
& groupKeysConf ,
& secretKey ,
& groupIdentityPublicKey ,
& groupIdentityPrivateKey ,
groupInfoConf ,
groupMembersConf ,
nil ,
0 ,
& error
) . orThrow ( error : error )
let groupState : [ ConfigDump . Variant : Config ] = try createGroupState (
groupSessionId : groupSessionId ,
userED25519KeyPair : userED25519KeyPair ,
groupIdentityPrivateKey : Data ( groupIdentityKeyPair . secretKey ) ,
authData : nil ,
shouldLoadState : false , // W e m a n u a l l y l o a d t h e s t a t e a f t e r p o p u l a t i n g t h e c o n f i g s
using : dependencies
)
guard
let keysConf : UnsafeMutablePointer < config_group_keys > = groupKeysConf ,
let infoConf : UnsafeMutablePointer < config_object > = groupInfoConf ,
let membersConf : UnsafeMutablePointer < config_object > = groupMembersConf
else {
// E x t r a c t t h e c o n f o b j e c t s f r o m t h e s t a t e t o l o a d i n t h e i n i t i a l d a t a
guard case . groupKeys ( _ , let groupInfoConf , let membersConf ) = groupState [ . groupKeys ] else {
SNLog ( " [SessionUtil Error] Group config objects were null " )
throw SessionUtilError . unableToCreateConfigObject
}
@ -133,13 +103,8 @@ internal extension SessionUtil {
groups_members_set ( membersConf , & member )
}
}
// D e f i n e t h e c o n f i g s t a t e m a p a n d l o a d i t i n t o m e m o r y
let groupState : [ ConfigDump . Variant : Config ] = [
. groupKeys : . groupKeys ( keysConf , info : infoConf , members : membersConf ) ,
. groupInfo : . object ( infoConf ) ,
. groupMembers : . object ( membersConf ) ,
]
// N o w t h a t e v e r y t h i n g h a s b e e n p o p u l a t e d c o r r e c t l y w e c a n l o a d t h e s t a t e i n t o m e m o r y
dependencies . mutate ( cache : . sessionUtil ) { cache in
groupState . forEach { variant , config in
cache . setConfig ( for : variant , sessionId : groupSessionId , to : config )
@ -147,7 +112,7 @@ internal extension SessionUtil {
}
return (
SessionId( . group , publicKey : groupIdentityPublicKey ) ,
groupSessionId ,
groupIdentityKeyPair ,
groupState ,
ClosedGroup (
@ -158,7 +123,8 @@ internal extension SessionUtil {
displayPictureFilename : displayPictureFilename ,
displayPictureEncryptionKey : displayPictureEncryptionKey ,
lastDisplayPictureUpdate : creationTimestamp ,
groupIdentityPrivateKey : Data ( groupIdentityPrivateKey ) ,
shouldPoll : true ,
groupIdentityPrivateKey : Data ( groupIdentityKeyPair . secretKey ) ,
invited : false
) ,
finalMembers . map { memberId , info -> GroupMember in
@ -166,6 +132,7 @@ internal extension SessionUtil {
groupId : groupSessionId . hexString ,
profileId : memberId ,
role : ( info . isAdmin ? . admin : . standard ) ,
roleStatus : ( memberId = = userSessionId . hexString ? . accepted : . pending ) ,
isHidden : false
)
}
@ -222,6 +189,7 @@ internal extension SessionUtil {
userED25519KeyPair : KeyPair ,
groupIdentityPrivateKey : Data ? ,
authData : Data ? ,
shouldLoadState : Bool ,
using dependencies : Dependencies
) throws -> [ ConfigDump . Variant : Config ] {
var secretKey : [ UInt8 ] = userED25519KeyPair . secretKey
@ -277,9 +245,14 @@ internal extension SessionUtil {
. groupMembers : . object ( membersConf ) ,
]
dependencies . mutate ( cache : . sessionUtil ) { cache in
groupState . forEach { variant , config in
cache . setConfig ( for : variant , sessionId : groupSessionId , to : config )
// O n l y l o a d t h e s t a t e i f s p e c i f i e d ( d u r i n g i n i t i a l g r o u p c r e a t i o n w e w a n t t o
// l o a d t h e s t a t e a f t e r p o p u l a t i n g t h e d i f f e r e n t c o n f i g s i n c a s e i n v a l i d d a t a
// w a s p r o v i d e d )
if shouldLoadState {
dependencies . mutate ( cache : . sessionUtil ) { cache in
groupState . forEach { variant , config in
cache . setConfig ( for : variant , sessionId : groupSessionId , to : config )
}
}
}