Commit 41dc9734 authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Kateryna Kostiuk

contact request: create conversation

Create conversation and generate messages when:
- request from peer is received

Change-Id: Ibe56cba805c88e5517ed1fb3fc3f21bf36259970
Reviewed-by: Kateryna Kostiuk's avatarKateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
parent 1a0488ab
......@@ -37,6 +37,17 @@ class ContactRequestsViewModel: ViewModel {
self.accountsService = injectionBag.accountService
self.conversationService = injectionBag.conversationsService
self.nameService = injectionBag.nameService
self.contactsService.contactRequests
.asObservable()
.subscribe(onNext: {[unowned self] contactRequests in
guard let account = self.accountsService.currentAccount else { return }
guard let ringId = contactRequests.last?.ringId else { return }
self.conversationService.generateMessage(ofType: GeneratedMessageType.receivedContactRequest,
forRindId: ringId,
forAccount: account)
})
.disposed(by: self.disposeBag)
}
lazy var contactRequestItems: Observable<[ContactRequestItem]> = {
......@@ -66,15 +77,6 @@ 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(withId: "",
withContent: GeneratedMessageType.receivedContactRequest.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: item.contactRequest.ringId,
currentAccountId: (self.accountsService.currentAccount?.id)!, generated: true)
.subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved")
})
.disposed(by: disposeBag)
self.conversationService.saveMessage(withId: "",
withContent: GeneratedMessageType.contactRequestAccepted.rawValue,
byAuthor: accountHelper.ringId!,
......
......@@ -283,24 +283,8 @@ class ConversationViewModel: ViewModel {
}
func generateMessage(ofType messageType: GeneratedMessageType) {
if self.generatedMessageExists(ofType: messageType) {
return
}
let accountHelper = AccountModelHelper(withAccount: self.accountService.currentAccount!)
self.saveMessage(withId: "",
withContent: messageType.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: self.conversation.recipientRingId,
generated: true)
}
func generatedMessageExists(ofType messageType: GeneratedMessageType) -> Bool {
for message in self.conversation.messages
where message.content == messageType.rawValue {
return true
}
return false
self.conversationsService.generateMessage(ofType: messageType,
forRindId: self.conversation.recipientRingId,
forAccount: self.accountService.currentAccount!)
}
}
......@@ -38,7 +38,7 @@ enum MessageSequencing {
enum GeneratedMessageType: String {
case sendContactRequest = "The invitation has been sent"
case receivedContactRequest = "Contact request received"
case contactRequestAccepted = "ACCEPTED"
case contactRequestAccepted = "Contact accepted"
}
class MessageViewModel {
......
......@@ -171,6 +171,47 @@ class ConversationsService: MessagesAdapterDelegate {
})
}
func findConversation(withRingId ringId: String,
withAccountId accountId: String) -> ConversationModel? {
return self.results
.filter({ conversation in
return conversation.recipientRingId == ringId && conversation.accountId == accountId
})
.first
}
func generateMessage(ofType messageType: GeneratedMessageType,
forRindId ringId: String,
forAccount account: AccountModel) {
if let conversation = self.findConversation(withRingId: ringId, withAccountId: account.id) {
if self.generatedMessageExists(ofType: messageType, inConversation: conversation) {
return
}
}
let accountHelper = AccountModelHelper(withAccount: account)
self.saveMessage(withId: "",
withContent: messageType.rawValue,
byAuthor: accountHelper.ringId!,
toConversationWith: ringId,
currentAccountId: account.id,
generated: true)
.subscribe(onCompleted: { [unowned self] in
self.log.debug("Message saved")
})
.disposed(by: disposeBag)
}
func generatedMessageExists(ofType messageType: GeneratedMessageType,
inConversation conversation: ConversationModel) -> Bool {
for message in conversation.messages
where message.content == messageType.rawValue {
return true
}
return false
}
func status(forMessageId messageId: String) -> MessageStatus {
return self.messageAdapter.status(forMessageId: UInt64(messageId)!)
}
......
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