From 3a94ca5902027d98fecc47ff1b33df5493dd1efe Mon Sep 17 00:00:00 2001
From: ialokim <ialokim@mailbox.org>
Date: Thu, 9 Mar 2023 00:40:37 +0100
Subject: [PATCH] send and react to specific event when forum invitation is
 sent

---
 .../conversation/ConversationViewModel.kt     |  6 +++-
 .../sharing/ForumInvitationSentEvent.kt       | 30 +++++++++++++++++++
 .../forums/sharing/ForumSharingViewModel.kt   |  5 +++-
 3 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumInvitationSentEvent.kt

diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
index 3c28300908..17e1975506 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/conversation/ConversationViewModel.kt
@@ -1,6 +1,6 @@
 /*
  * Briar Desktop
- * Copyright (C) 2021-2022 The Briar Project
+ * Copyright (C) 2021-2023 The Briar Project
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -64,6 +64,7 @@ import org.briarproject.briar.desktop.contact.ContactItem
 import org.briarproject.briar.desktop.contact.loadContactItem
 import org.briarproject.briar.desktop.conversation.ConversationRequestItem.RequestType.FORUM
 import org.briarproject.briar.desktop.conversation.ConversationRequestItem.RequestType.INTRODUCTION
+import org.briarproject.briar.desktop.forums.sharing.ForumInvitationSentEvent
 import org.briarproject.briar.desktop.threading.BriarExecutors
 import org.briarproject.briar.desktop.utils.KLoggerUtils.e
 import org.briarproject.briar.desktop.utils.KLoggerUtils.i
@@ -375,6 +376,9 @@ constructor(
                 }
             }
 
+            is ForumInvitationSentEvent ->
+                if (_contactId.value in e.contactIds) reloadMessages()
+
             is ContactConnectedEvent -> {
                 if (e.contactId == _contactId.value) {
                     LOG.i { "Contact connected" }
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumInvitationSentEvent.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumInvitationSentEvent.kt
new file mode 100644
index 0000000000..016dfd976f
--- /dev/null
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumInvitationSentEvent.kt
@@ -0,0 +1,30 @@
+/*
+ * Briar Desktop
+ * Copyright (C) 2021-2023 The Briar Project
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package org.briarproject.briar.desktop.forums.sharing
+
+import org.briarproject.bramble.api.contact.ContactId
+import org.briarproject.bramble.api.event.Event
+
+/**
+ * An event that is broadcast when a forum invitation
+ * is sent to a contact.
+ */
+data class ForumInvitationSentEvent(
+    val contactIds: List<ContactId>,
+) : Event()
diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumSharingViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumSharingViewModel.kt
index c7602af618..03dc7a57be 100644
--- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumSharingViewModel.kt
+++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forums/sharing/ForumSharingViewModel.kt
@@ -1,6 +1,6 @@
 /*
  * Briar Desktop
- * Copyright (C) 2021-2022 The Briar Project
+ * Copyright (C) 2021-2023 The Briar Project
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -149,6 +149,9 @@ class ForumSharingViewModel @Inject constructor(
         _shareableSelected.value.forEach { contactId ->
             forumSharingManager.sendInvitation(txn, groupId, contactId, message)
         }
+        // send custom event to force message reload if private chat is open for contactId
+        // todo: switch to a more generic approach where every locally sent message broadcasts an event per default
+        txn.attach(ForumInvitationSentEvent(_shareableSelected.value.toList()))
         txn.attach { reload() }
     }
 
-- 
GitLab