Commit 3adddfa3 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

contacts: save profile in database

This patch saves profile to database when contact request is accepted.

Change-Id: I6a43e63d5c9bbeed4e2c3900ec155adcd74fd098
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 416d55ee
......@@ -105,4 +105,23 @@ class VCardUtils {
return path
}
}
class func getName(from vCard: CNContact?) -> String {
guard let vCard = vCard else {
return ""
}
var name = ""
if !vCard.givenName.isEmpty {
name = vCard.givenName
}
if !vCard.familyName.isEmpty {
if !name.isEmpty {
name += " "
}
name += vCard.familyName
}
return name
}
}
......@@ -136,4 +136,29 @@ final class ProfileDataHelper {
}
return nil
}
func insertOrUpdateProfile(item: Profile) throws {
guard let dataBase = RingDB.instance.ringDB else {
throw DataAccessError.datastoreConnectionError
}
try dataBase.transaction {
let selectQuery = table.filter(uri == item.uri)
let rows = try dataBase.run(selectQuery.update(alias <- item.alias,
photo <- item.photo,
status <- item.status))
if rows > 0 {
return
}
let insertQuery = table.insert(uri <- item.uri,
alias <- item.alias,
photo <- item.photo,
type <- item.type,
status <- item.status)
let rowId = try dataBase.run(insertQuery)
guard rowId > 0 else {
throw DataAccessError.databaseError
}
return
}
}
}
......@@ -413,6 +413,17 @@ class DBManager {
return self.interactionHepler.insertIfNotExist(item: interaction)
}
func createOrUpdateRingProfile(profileUri: String, alias: String?, image: String?, status: ProfileStatus) -> Bool {
let profile = Profile(defaultID, profileUri, alias, image, ProfileType.ring.rawValue,
status.rawValue)
do {
try self.profileHepler.insertOrUpdateProfile(item: profile)
} catch {
return false
}
return true
}
private func getProfile(for profileUri: String, createIfNotExists: Bool) throws -> Profile? {
if let profile = try self.profileHepler.selectProfile(accountURI: profileUri) {
return profile
......
......@@ -79,15 +79,7 @@ class ContactRequestsViewModel: Stateable, ViewModel {
self.presenceService.subscribeBuddy(withAccountId: (self.accountsService.currentAccount?.id)!,
withUri: item.contactRequest.ringId,
withFlag: true)
if let vCard = item.contactRequest.vCard {
let saveVCardCompleted = self.contactsService.saveVCard(vCard: vCard, forContactWithRingId: item.contactRequest.ringId)
return Observable<Void>.zip(acceptCompleted, saveVCardCompleted) { _, _ in
return
}
} else {
return acceptCompleted.asObservable()
}
return acceptCompleted.asObservable()
}
func discard(withItem item: ContactRequestItem) -> Observable<Void> {
......
......@@ -81,15 +81,21 @@ class ConversationViewModel: ViewModel {
let contact = self.contactsService.contact(withRingId: contactRingId)
self.contactsService.loadVCard(forContactWithRingId: contactRingId)
.subscribe(onSuccess: { vCard in
guard let imageData = vCard.imageData else {
self.log.warning("vCard for ringId: \(contactRingId) has no image")
return
}
self.profileImageData = imageData
})
.disposed(by: self.disposeBag)
if let profile = conversation.value.participantProfile, let photo = profile.photo {
if let data = NSData(base64Encoded: photo, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) as Data? {
self.profileImageData = data
}
} else {
self.contactsService.loadVCard(forContactWithRingId: contactRingId)
.subscribe(onSuccess: { vCard in
guard let imageData = vCard.imageData else {
self.log.warning("vCard for ringId: \(contactRingId) has no image")
return
}
self.profileImageData = imageData
})
.disposed(by: self.disposeBag)
}
// invite and block buttons
if let contact = contact {
......
......@@ -43,6 +43,7 @@ class ContactsService {
fileprivate let responseStream = PublishSubject<ServiceEvent>()
var sharedResponseStream: Observable<ServiceEvent>
let dbManager = DBManager(profileHepler: ProfileDataHelper(), conversationHelper: ConversationDataHelper(), interactionHepler: InteractionDataHelper())
init(withContactsAdapter contactsAdapter: ContactsAdapter) {
self.contactsAdapter = contactsAdapter
......@@ -105,6 +106,16 @@ class ContactsService {
let success = self.contactsAdapter.acceptTrustRequest(fromContact: contactRequest.ringId,
withAccountId: account.id)
if success {
var stringImage: String?
if let vCard = contactRequest.vCard, let image = vCard.imageData {
stringImage = image.base64EncodedString()
}
let name = VCardUtils.getName(from: contactRequest.vCard)
_ = self.dbManager
.createOrUpdateRingProfile(profileUri: contactRequest.ringId,
alias: name,
image: stringImage,
status: ProfileStatus.trusted)
var event = ServiceEvent(withEventType: .contactAdded)
event.addEventInput(.accountId, value: account.id)
event.addEventInput(.state, value: true)
......@@ -202,7 +213,7 @@ extension ContactsService: ContactsAdapterDelegate {
vCard = nil
log.error("Unable to parse the vCard :\(error)")
}
//Update trust request list
if self.contactRequest(withRingId: senderAccount) == nil {
let contactRequest = ContactRequestModel(withRingId: senderAccount,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment