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