ios: use single call to initialize chat controller (#1110)

* ios: use single call to initialize chat controller

* update logs

* comments
This commit is contained in:
Evgeny Poberezkin
2022-09-24 09:28:22 +01:00
committed by GitHub
parent c94691c89e
commit ab7fed1628
5 changed files with 12 additions and 20 deletions

View File

@@ -117,7 +117,6 @@ func chatRecvMsg() async -> ChatResponse? {
}
func apiGetActiveUser() throws -> User? {
let _ = getChatCtrl()
let r = chatSendCmdSync(.showActiveUser)
switch r {
case let .activeUser(user): return user
@@ -725,13 +724,12 @@ func apiUpdateGroup(_ groupId: Int64, _ groupProfile: GroupProfile) async throws
func initializeChat(start: Bool, dbKey: String? = nil) throws {
logger.debug("initializeChat")
let m = ChatModel.shared
(m.chatDbEncrypted, m.chatDbStatus) = migrateChatDatabase(dbKey)
(m.chatDbEncrypted, m.chatDbStatus) = chatMigrateInit(dbKey)
if m.chatDbStatus != .ok { return }
// If we migrated successfully means previous re-encryption process on database level finished successfully too
if encryptionStartedDefault.get() {
encryptionStartedDefault.set(false)
}
let _ = getChatCtrl(dbKey)
try apiSetFilesFolder(filesFolder: getAppFilesDirectory().path)
try apiSetIncognito(incognito: incognitoGroupDefault.get())
m.chatInitialized = true

View File

@@ -91,6 +91,7 @@ struct DatabaseErrorView: View {
private func runChat() {
do {
resetChatCtrl()
try initializeChat(start: m.v3DBMigration.startChat, dbKey: dbKey)
if let s = m.chatDbStatus {
status = s

View File

@@ -158,7 +158,7 @@ var chatStarted = false
func startChat() -> DBMigrationResult? {
hs_init(0, nil)
if chatStarted { return .ok }
let (_, dbStatus) = migrateChatDatabase()
let (_, dbStatus) = chatMigrateInit()
if dbStatus != .ok { return dbStatus }
if let user = apiGetActiveUser() {
logger.debug("active user \(String(describing: user))")
@@ -230,7 +230,6 @@ func receivedMsgNtf(_ res: ChatResponse) async -> (String, UNMutableNotification
}
func apiGetActiveUser() -> User? {
let _ = getChatCtrl()
let r = sendSimpleXCmd(.showActiveUser)
logger.debug("apiGetActiveUser sendSimpleXCmd responce: \(String(describing: r))")
switch r {

View File

@@ -14,19 +14,12 @@ private var migrationResult: (Bool, DBMigrationResult)?
public func getChatCtrl(_ useKey: String? = nil) -> chat_ctrl {
if let controller = chatController { return controller }
let dbPath = getAppDatabasePath().path
let dbKey = useKey ?? getDatabaseKey() ?? ""
logger.debug("getChatCtrl DB path: \(dbPath)")
var cPath = dbPath.cString(using: .utf8)!
var cKey = dbKey.cString(using: .utf8)!
chatController = chat_init_key(&cPath, &cKey)
logger.debug("getChatCtrl: chat_init_key")
return chatController!
fatalError("chat controller not initialized")
}
public func migrateChatDatabase(_ useKey: String? = nil) -> (Bool, DBMigrationResult) {
public func chatMigrateInit(_ useKey: String? = nil) -> (Bool, DBMigrationResult) {
if let res = migrationResult { return res }
logger.debug("migrateChatDatabase \(storeDBPassphraseGroupDefault.get())")
logger.debug("chatMigrateInit \(storeDBPassphraseGroupDefault.get())")
let dbPath = getAppDatabasePath().path
var dbKey = ""
let useKeychain = storeDBPassphraseGroupDefault.get()
@@ -40,11 +33,12 @@ public func migrateChatDatabase(_ useKey: String? = nil) -> (Bool, DBMigrationRe
dbKey = key
}
}
logger.debug("migrateChatDatabase DB path: \(dbPath)")
// logger.debug("migrateChatDatabase DB key: \(dbKey)")
logger.debug("chatMigrateInit DB path: \(dbPath)")
logger.debug("chatMigrateInit DB key: \(dbKey)")
var cPath = dbPath.cString(using: .utf8)!
var cKey = dbKey.cString(using: .utf8)!
let cjson = chat_migrate_db(&cPath, &cKey)!
// the last parameter of chat_migrate_init is used to return the pointer to chat controller
let cjson = chat_migrate_init(&cPath, &cKey, &chatController)!
let dbRes = dbMigrationResult(fromCString(cjson))
let encrypted = dbKey != ""
let keychainErr = dbRes == .ok && useKeychain && encrypted && !setDatabaseKey(dbKey)

View File

@@ -15,8 +15,8 @@ extern void hs_init(int argc, char **argv[]);
typedef void* chat_ctrl;
extern char *chat_migrate_db(char *path, char *key);
extern chat_ctrl chat_init_key(char *path, char *key);
// the last parameter is used to return the pointer to chat controller
extern char *chat_migrate_init(char *path, char *key, chat_ctrl *ctrl);
extern char *chat_send_cmd(chat_ctrl ctl, char *cmd);
extern char *chat_recv_msg(chat_ctrl ctl);
extern char *chat_recv_msg_wait(chat_ctrl ctl, int wait);