Commit 34ceed01 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

database: add interaction direction

Add a new column to check if an interaction is outgoing or incoming.

Change-Id: I0b403ddb345ac98a8898ecf398beb8854703988f
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent e8a6b2b0
...@@ -29,7 +29,8 @@ typealias Interaction = ( ...@@ -29,7 +29,8 @@ typealias Interaction = (
body: String, body: String,
type: String, type: String,
status: String, status: String,
daemonID: String daemonID: String,
incoming: Bool
) )
final class InteractionDataHelper { final class InteractionDataHelper {
...@@ -44,6 +45,7 @@ final class InteractionDataHelper { ...@@ -44,6 +45,7 @@ final class InteractionDataHelper {
let type = Expression<String>("type") let type = Expression<String>("type")
let status = Expression<String>("status") let status = Expression<String>("status")
let daemonId = Expression<String>("daemon_id") let daemonId = Expression<String>("daemon_id")
let incoming = Expression<Bool>("incoming")
func createTable() throws { func createTable() throws {
guard let dataBase = RingDB.instance.ringDB else { guard let dataBase = RingDB.instance.ringDB else {
...@@ -60,6 +62,7 @@ final class InteractionDataHelper { ...@@ -60,6 +62,7 @@ final class InteractionDataHelper {
table.column(type) table.column(type)
table.column(status) table.column(status)
table.column(daemonId) table.column(daemonId)
table.column(incoming)
table.foreignKey(accountId, table.foreignKey(accountId,
references: RingDB.instance.tableProfiles, id, delete: .noAction) references: RingDB.instance.tableProfiles, id, delete: .noAction)
table.foreignKey(authorId, table.foreignKey(authorId,
...@@ -85,7 +88,8 @@ final class InteractionDataHelper { ...@@ -85,7 +88,8 @@ final class InteractionDataHelper {
body <- item.body, body <- item.body,
type <- item.type, type <- item.type,
status <- item.status, status <- item.status,
daemonId <- item.daemonID) daemonId <- item.daemonID,
incoming <- item.incoming)
do { do {
let rowId = try dataBase.run(query) let rowId = try dataBase.run(query)
guard rowId > 0 else { guard rowId > 0 else {
...@@ -129,7 +133,8 @@ final class InteractionDataHelper { ...@@ -129,7 +133,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId]) daemonID: item[daemonId],
incoming: item[incoming])
} }
return nil return nil
} }
...@@ -149,7 +154,8 @@ final class InteractionDataHelper { ...@@ -149,7 +154,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId])) daemonID: item[daemonId],
incoming: item[incoming]))
} }
return interactions return interactions
} }
...@@ -170,7 +176,8 @@ final class InteractionDataHelper { ...@@ -170,7 +176,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId])) daemonID: item[daemonId],
incoming: item[incoming]))
} }
return interactions return interactions
} }
...@@ -191,7 +198,8 @@ final class InteractionDataHelper { ...@@ -191,7 +198,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId])) daemonID: item[daemonId],
incoming: item[incoming]))
} }
return interactions return interactions
} }
...@@ -213,7 +221,8 @@ final class InteractionDataHelper { ...@@ -213,7 +221,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId])) daemonID: item[daemonId],
incoming: item[incoming]))
} }
return interactions return interactions
} }
...@@ -234,7 +243,8 @@ final class InteractionDataHelper { ...@@ -234,7 +243,8 @@ final class InteractionDataHelper {
body: item[body], body: item[body],
type: item[type], type: item[type],
status: item[status], status: item[status],
daemonID: item[daemonId])) daemonID: item[daemonId],
incoming: item[incoming]))
} }
if interactions.isEmpty { if interactions.isEmpty {
return nil return nil
......
...@@ -31,10 +31,6 @@ enum ProfileStatus: String { ...@@ -31,10 +31,6 @@ enum ProfileStatus: String {
case untrasted = "UNTRUSTED" case untrasted = "UNTRUSTED"
} }
enum MessageDirection {
case incoming
case outgoing
}
enum InteractionStatus: String { enum InteractionStatus: String {
case invalid = "INVALID" case invalid = "INVALID"
case unknown = "UNKNOWN" case unknown = "UNKNOWN"
...@@ -116,7 +112,7 @@ class DBManager { ...@@ -116,7 +112,7 @@ class DBManager {
try interactionHepler.createTable() try interactionHepler.createTable()
} }
func saveMessage(for accountUri: String, with contactUri: String, message: MessageModel, type: MessageDirection) -> Completable { func saveMessage(for accountUri: String, with contactUri: String, message: MessageModel, incoming: Bool) -> Completable {
//create completable which will be executed on background thread //create completable which will be executed on background thread
return Completable.create { [weak self] completable in return Completable.create { [weak self] completable in
...@@ -137,11 +133,11 @@ class DBManager { ...@@ -137,11 +133,11 @@ class DBManager {
} }
var author: Int64 var author: Int64
switch type {
case .incoming: if incoming {
author = contactProfile.id author = contactProfile.id
case .outgoing: } else {
author = accountProfile.id author = accountProfile.id
} }
guard let conversationsID = try self?.getConversationsIDBetween(accountProfileID: accountProfile.id, guard let conversationsID = try self?.getConversationsIDBetween(accountProfileID: accountProfile.id,
...@@ -379,7 +375,8 @@ class DBManager { ...@@ -379,7 +375,8 @@ class DBManager {
let message = MessageModel(withId: interaction.daemonID, let message = MessageModel(withId: interaction.daemonID,
receivedDate: date, receivedDate: date,
content: interaction.body, content: interaction.body,
author: author) author: author,
incoming: interaction.incoming)
message.isGenerated = self.isGenerated(message: message) message.isGenerated = self.isGenerated(message: message)
if let status: InteractionStatus = InteractionStatus(rawValue: interaction.status) { if let status: InteractionStatus = InteractionStatus(rawValue: interaction.status) {
message.status = status.toMessageStatus() message.status = status.toMessageStatus()
...@@ -396,7 +393,7 @@ class DBManager { ...@@ -396,7 +393,7 @@ class DBManager {
let interaction = Interaction(defaultID, accountProfileID, authorProfileID, let interaction = Interaction(defaultID, accountProfileID, authorProfileID,
conversationID, Int64(timeInterval), conversationID, Int64(timeInterval),
message.content, InteractionType.text.rawValue, message.content, InteractionType.text.rawValue,
InteractionStatus.unknown.rawValue, message.daemonId) InteractionStatus.unknown.rawValue, message.daemonId, message.incoming )
return self.interactionHepler.insert(item: interaction) return self.interactionHepler.insert(item: interaction)
} }
......
...@@ -89,13 +89,17 @@ class ContactRequestsViewModel: Stateable, ViewModel { ...@@ -89,13 +89,17 @@ class ContactRequestsViewModel: Stateable, ViewModel {
let acceptCompleted = self.contactsService.accept(contactRequest: item.contactRequest, withAccount: self.accountsService.currentAccount!) let acceptCompleted = self.contactsService.accept(contactRequest: item.contactRequest, withAccount: self.accountsService.currentAccount!)
let accountHelper = AccountModelHelper(withAccount: self.accountsService.currentAccount!) let accountHelper = AccountModelHelper(withAccount: self.accountsService.currentAccount!)
self.conversationService.saveMessage(withId: "",
withContent: GeneratedMessageType.contactRequestAccepted.rawValue, let message = MessageModel(withId: "",
byAuthor: accountHelper.ringId!, receivedDate: Date(),
content: GeneratedMessageType.contactRequestAccepted.rawValue,
author: accountHelper.ringId!,
incoming: true)
message.isGenerated = true
self.conversationService.saveMessage(message: message,
toConversationWith: item.contactRequest.ringId, toConversationWith: item.contactRequest.ringId,
toAccountId: (self.accountsService.currentAccount?.id)!, toAccountId: (self.accountsService.currentAccount?.id)!,
toAccountUri: accountHelper.ringId!, toAccountUri: accountHelper.ringId!,
generated: true,
shouldRefreshConversations: true) shouldRefreshConversations: true)
.subscribe(onCompleted: { [unowned self] in .subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved") self.log.debug("Message saved")
......
...@@ -98,12 +98,11 @@ class MessageViewModel { ...@@ -98,12 +98,11 @@ class MessageViewModel {
if self.message.isGenerated { if self.message.isGenerated {
return .generated return .generated
} }
let accountHelper = AccountModelHelper(withAccount: accountService.currentAccount!)
if self.message.author == accountHelper.ringId! { if self.message.incoming {
return .sent
} else {
return.received return.received
} else {
return .sent
} }
} }
} }
...@@ -27,12 +27,13 @@ class MessageModel { ...@@ -27,12 +27,13 @@ class MessageModel {
var author: String = "" var author: String = ""
var status: MessageStatus = .unknown var status: MessageStatus = .unknown
var isGenerated: Bool = false var isGenerated: Bool = false
var incoming: Bool
convenience init(withId id: String, receivedDate: Date, content: String, author: String) { init(withId id: String, receivedDate: Date, content: String, author: String, incoming: Bool) {
self.init()
self.daemonId = id self.daemonId = id
self.receivedDate = receivedDate self.receivedDate = receivedDate
self.content = content self.content = content
self.author = author self.author = author
self.incoming = incoming
} }
} }
...@@ -86,14 +86,15 @@ class ConversationsManager: MessagesAdapterDelegate { ...@@ -86,14 +86,15 @@ class ConversationsManager: MessagesAdapterDelegate {
if currentAccountUri == messageAccountUri { if currentAccountUri == messageAccountUri {
shouldUpdateConversationsList = true shouldUpdateConversationsList = true
} }
let message = self.conversationService.createMessage(withId: "",
self.conversationService.saveMessage(withId: "", withContent: content,
withContent: content, byAuthor: senderAccount,
byAuthor: senderAccount, generated: false,
incoming: true)
self.conversationService.saveMessage(message: message,
toConversationWith: senderAccount, toConversationWith: senderAccount,
toAccountId: receiverAccountId, toAccountId: receiverAccountId,
toAccountUri: messageAccountUri, toAccountUri: messageAccountUri,
generated: false,
shouldRefreshConversations: shouldUpdateConversationsList) shouldRefreshConversations: shouldUpdateConversationsList)
.subscribe() .subscribe()
.disposed(by: self.disposeBag) .disposed(by: self.disposeBag)
......
...@@ -105,14 +105,16 @@ class ConversationsService { ...@@ -105,14 +105,16 @@ class ConversationsService {
let messageId = String(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) let accountHelper = AccountModelHelper(withAccount: senderAccount)
if accountHelper.ringId! != recipientRingId { if accountHelper.ringId! != recipientRingId {
_ = self.saveMessage(withId: messageId, let message = self.createMessage(withId: messageId,
withContent: content, withContent: content,
byAuthor: accountHelper.ringId!, byAuthor: accountHelper.ringId!,
toConversationWith: recipientRingId, generated: false,
toAccountId: senderAccount.id, incoming: false)
toAccountUri: accountHelper.ringId!, self.saveMessage(message: message,
generated: false, toConversationWith: recipientRingId,
shouldRefreshConversations: true) toAccountId: senderAccount.id,
toAccountUri: accountHelper.ringId!,
shouldRefreshConversations: true)
.subscribe(onCompleted: { [unowned self] in .subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved") self.log.debug("Message saved")
}) })
...@@ -125,51 +127,47 @@ class ConversationsService { ...@@ -125,51 +127,47 @@ class ConversationsService {
}) })
} }
// swiftlint:disable function_parameter_count func createMessage(withId messageId: String,
func saveMessage(withId messageId: String, withContent content: String,
withContent content: String, byAuthor author: String,
byAuthor author: String, generated: Bool?,
incoming: Bool) -> MessageModel {
let message = MessageModel(withId: messageId, receivedDate: Date(), content: content, author: author, incoming: incoming)
if let generated = generated {
message.isGenerated = generated
}
return message
}
func saveMessage(message: MessageModel,
toConversationWith recipientRingId: String, toConversationWith recipientRingId: String,
toAccountId: String, toAccountId: String,
toAccountUri: String, toAccountUri: String,
generated: Bool?,
shouldRefreshConversations: Bool) -> Completable { shouldRefreshConversations: Bool) -> Completable {
return Completable.create(subscribe: { [unowned self] completable in return Completable.create(subscribe: { [unowned self] completable in
let message = MessageModel(withId: messageId, receivedDate: Date(), content: content, author: author)
if let generated = generated {
message.isGenerated = generated
}
var messageDirection = MessageDirection.incoming
if author == toAccountUri {
messageDirection = MessageDirection.outgoing
}
self.dbManager.saveMessage(for: toAccountUri, self.dbManager.saveMessage(for: toAccountUri,
with: recipientRingId, with: recipientRingId,
message: message, message: message,
type: messageDirection) incoming: message.incoming)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background)) .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe(onCompleted: { [weak self] in .subscribe(onCompleted: { [weak self] in
if shouldRefreshConversations { if shouldRefreshConversations {
self?.dbManager.getConversationsObservable(for: toAccountId, accountURI: toAccountUri) self?.dbManager.getConversationsObservable(for: toAccountId, accountURI: toAccountUri)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background)) .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe(onNext: { [weak self] conversationsModels in .subscribe(onNext: { [weak self] conversationsModels in
self?.conversations.value = conversationsModels self?.conversations.value = conversationsModels
}) })
.disposed(by: (self?.disposeBag)!) .disposed(by: (self?.disposeBag)!)
} }
completable(.completed) completable(.completed)
}, onError: { error in }, onError: { error in
completable(.error(error)) completable(.error(error))
}).disposed(by: self.disposeBag) }).disposed(by: self.disposeBag)
return Disposables.create { } return Disposables.create { }
}) })
} }
// swiftlint:enable function_parameter_count
func findConversation(withRingId ringId: String, func findConversation(withRingId ringId: String,
withAccountId accountId: String) -> ConversationModel? { withAccountId accountId: String) -> ConversationModel? {
...@@ -193,17 +191,21 @@ class ConversationsService { ...@@ -193,17 +191,21 @@ class ConversationsService {
let uri = AccountModelHelper(withAccount: account).ringId let uri = AccountModelHelper(withAccount: account).ringId
let accountHelper = AccountModelHelper(withAccount: account) let accountHelper = AccountModelHelper(withAccount: account)
self.saveMessage(withId: "",
withContent: messageType.rawValue, let message = self.createMessage(withId: "",
byAuthor: accountHelper.ringId!, withContent: messageType.rawValue,
byAuthor: accountHelper.ringId!,
generated: true,
incoming: true)
self.saveMessage(message: message,
toConversationWith: ringId, toConversationWith: ringId,
toAccountId: account.id, toAccountId: account.id,
toAccountUri: uri!, toAccountUri: uri!,
generated: true,
shouldRefreshConversations: true) shouldRefreshConversations: true)
.subscribe(onCompleted: { [unowned self] in .subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved") self.log.debug("Message saved")
}) })
.disposed(by: disposeBag) .disposed(by: disposeBag)
} }
...@@ -226,7 +228,7 @@ class ConversationsService { ...@@ -226,7 +228,7 @@ class ConversationsService {
//Filter unread messages //Filter unread messages
let unreadMessages = conversation.messages.filter({ messages in let unreadMessages = conversation.messages.filter({ messages in
return messages.status != .read return messages.status != .read && messages.incoming
}) })
let messagesIds = unreadMessages.map({$0.messageId}).filter({$0 >= 0}) let messagesIds = unreadMessages.map({$0.messageId}).filter({$0 >= 0})
......
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