diff --git a/Sources/Integration/Session/Session+UD.swift b/Sources/Integration/Session/Session+UD.swift index 6dd7472fedb8b3c9fd798134d8ede35a29ce3804..215147413619a23c55d29a72910d9fdd037c551c 100644 --- a/Sources/Integration/Session/Session+UD.swift +++ b/Sources/Integration/Session/Session+UD.swift @@ -41,20 +41,14 @@ extension Session { switch $0 { case .success(_): - _ = try? self.dbManager.saveContact(.init( - id: self.client.bindings.myId, - marshaled: self.client.bindings.meMarshalled, - username: value, - email: nil, - phone: nil, - nickname: nil, - photo: nil, - authStatus: .friend, - isRecent: false, - createdAt: Date() - )) - self.username = value + let query = Contact.Query(id: [self.client.bindings.myId]) + + if var myself = try? self.dbManager.fetchContacts(query).first { + myself.username = value + _ = try? self.dbManager.saveContact(myself) + } + completion(.success(nil)) case .failure(let error): completion(.failure(error)) diff --git a/Sources/Integration/Session/Session.swift b/Sources/Integration/Session/Session.swift index 51234cabf4aa2f29f29bc41bfc4166cd5afa2ab9..fc69e656da1ad8611d01907b1751cf8493ddafaa 100644 --- a/Sources/Integration/Session/Session.swift +++ b/Sources/Integration/Session/Session.swift @@ -194,6 +194,26 @@ public final class Session: SessionType { } .store(in: &cancellables) + /// Create a contact for myself, for foreign key purposes + /// + if var myself = try? dbManager.fetchContacts(.init(id: [client.bindings.myId])).first { + myself.username = username + _ = try? dbManager.saveContact(myself) + } else { + _ = try? dbManager.saveContact(.init( + id: client.bindings.myId, + marshaled: client.bindings.meMarshalled, + username: username, + email: email, + phone: phone, + nickname: nil, + photo: nil, + authStatus: .friend, + isRecent: false, + createdAt: Date() + )) + } + registerUnfinishedUploadTransfers() registerUnfinishedDownloadTransfers()