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

conversations: save the message id for outgoing messages

- Recuperates the generated message id returned by sendMessage,
  and saves it to the db.

- Removes excess message saving done by ConversationViewModel
  when sending, as it is already done in ConversationService's
  sendMessage.

Change-Id: I1d45ee91778b65478a305fd9d1f75d37312f3e05
Reviewed-by: Kateryna Kostiuk's avatarKateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
parent 826a5cef
......@@ -66,8 +66,8 @@ class ContactRequestsViewModel: ViewModel {
let acceptCompleted = self.contactsService.accept(contactRequest: item.contactRequest, withAccount: self.accountsService.currentAccount!)
let accountHelper = AccountModelHelper(withAccount: self.accountsService.currentAccount!)
self.conversationService.saveMessage(withContent:
GeneratedMessageType.receivedContactRequest.rawValue,
self.conversationService.saveMessage(withId: "",
withContent: GeneratedMessageType.receivedContactRequest.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: item.contactRequest.ringId,
currentAccountId: (self.accountsService.currentAccount?.id)!, generated: true)
......@@ -75,8 +75,8 @@ class ContactRequestsViewModel: ViewModel {
self.log.debug("Message saved")
})
.disposed(by: disposeBag)
self.conversationService.saveMessage(withContent:
GeneratedMessageType.contactRequestAccepted.rawValue,
self.conversationService.saveMessage(withId: "",
withContent: GeneratedMessageType.contactRequestAccepted.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: item.contactRequest.ringId,
currentAccountId: (self.accountsService.currentAccount?.id)!, generated: true)
......
......@@ -223,14 +223,21 @@ class ConversationViewModel: ViewModel {
from: accountService.currentAccount!,
to: self.conversation.recipientRingId)
.subscribe(onCompleted: { [unowned self] in
let accountHelper = AccountModelHelper(withAccount: self.accountService.currentAccount!)
self.saveMessage(withContent: content, byAuthor: accountHelper.ringId!, toConversationWith: self.conversation.recipientRingId, generated: false)
self.log.debug("Message sent")
}).disposed(by: self.disposeBag)
}
fileprivate func saveMessage(withContent content: String, byAuthor author: String, toConversationWith account: String, generated: Bool) {
fileprivate func saveMessage(withId messageId: String,
withContent content: String,
byAuthor author: String,
toConversationWith account: String,
generated: Bool) {
self.conversationsService
.saveMessage(withContent: content, byAuthor: author, toConversationWith: account, currentAccountId: (accountService.currentAccount?.id)!, generated: generated)
.saveMessage(withId: messageId,
withContent: content,
byAuthor: author,
toConversationWith: account,
currentAccountId: (accountService.currentAccount?.id)!, generated: generated)
.subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved")
})
......@@ -281,8 +288,11 @@ class ConversationViewModel: ViewModel {
}
let accountHelper = AccountModelHelper(withAccount: self.accountService.currentAccount!)
self.saveMessage(withContent:
messageType.rawValue, byAuthor: accountHelper.ringId!, toConversationWith: self.conversation.recipientRingId, generated: true)
self.saveMessage(withId: "",
withContent: messageType.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: self.conversation.recipientRingId,
generated: true)
}
func generatedMessageExists(ofType messageType: GeneratedMessageType) -> Bool {
......
......@@ -63,8 +63,8 @@ class MessageViewModel {
return self.message.receivedDate
}
var id: Int64 {
return self.message.id
var id: UInt64 {
return UInt64(self.message.id)!
}
var status: MessageStatus {
......
......@@ -22,14 +22,14 @@ import RealmSwift
class MessageModel: Object {
dynamic var id: Int64 = 0
dynamic var id: String = ""
dynamic var receivedDate: Date = Date()
dynamic var content: String = ""
dynamic var author: String = ""
dynamic var status: MessageStatus = .unknown
dynamic var isGenerated: Bool = false
convenience init(withId id: Int64, receivedDate: Date, content: String, author: String) {
convenience init(withId id: String, receivedDate: Date, content: String, author: String) {
self.init()
self.id = id
self.receivedDate = receivedDate
......
......@@ -60,16 +60,19 @@ class ConversationsService: MessagesAdapterDelegate {
return Completable.create(subscribe: { [unowned self] completable in
let contentDict = [self.textPlainMIMEType: content]
self.messageAdapter.sendMessage(withContent: contentDict, withAccountId: senderAccount.id, to: recipientRingId)
let messageId = String(self.messageAdapter.sendMessage(withContent: contentDict, withAccountId: senderAccount.id, to: recipientRingId))
let accountHelper = AccountModelHelper(withAccount: senderAccount)
if accountHelper.ringId! != recipientRingId {
_ = self.saveMessage(withContent: content,
_ = self.saveMessage(withId: messageId,
withContent: content,
byAuthor: accountHelper.ringId!,
toConversationWith: recipientRingId,
currentAccountId: senderAccount.id,
generated: false)
.subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved")
})
.disposed(by: self.disposeBag)
}
completable(.completed)
......@@ -93,14 +96,15 @@ class ConversationsService: MessagesAdapterDelegate {
})
}
func saveMessage(withContent content: String,
func saveMessage(withId messageId: String,
withContent content: String,
byAuthor author: String,
toConversationWith recipientRingId: String,
currentAccountId: String,
generated: Bool?) -> Completable {
return Completable.create(subscribe: { [unowned self] completable in
let message = MessageModel(withId: 0, receivedDate: Date(), content: content, author: author)
let message = MessageModel(withId: messageId, receivedDate: Date(), content: content, author: author)
if let generated = generated {
message.isGenerated = generated
}
......@@ -192,7 +196,8 @@ class ConversationsService: MessagesAdapterDelegate {
to receiverAccountId: String) {
if let content = message[textPlainMIMEType] {
self.saveMessage(withContent: content,
self.saveMessage(withId: "",
withContent: content,
byAuthor: senderAccount,
toConversationWith: senderAccount,
currentAccountId: receiverAccountId,
......
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