Browse Source

Merge pull request #1480 from nextcloud/feature/1336/clearChatHistory

add ability to clear chat history
Andy Scherzinger 3 years ago
parent
commit
c124c3b927

+ 3 - 0
app/src/main/java/com/nextcloud/talk/api/NcApi.java

@@ -415,4 +415,7 @@ public interface NcApi {
                                                         @Field("objectType") String objectType,
                                                         @Field("objectId") String objectId,
                                                         @Field("metaData") String metaData);
+
+    @DELETE
+    Observable<GenericOverall> clearChatHistory(@Header("Authorization") String authorization, @Url String url);
 }

+ 8 - 1
app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

@@ -1805,7 +1805,7 @@ class ChatController(args: Bundle) :
                         }
 
                         override fun onError(e: Throwable) {
-                            // unused atm
+                            Log.e(TAG, "failed to pull chat messages", e)
                         }
 
                         override fun onComplete() {
@@ -1877,6 +1877,13 @@ class ChatController(args: Bundle) :
             val chatOverall = response.body() as ChatOverall?
             val chatMessageList = setDeletionFlagsAndRemoveInfomessages(chatOverall?.ocs!!.data)
 
+            if (chatMessageList.isNotEmpty() &&
+                ChatMessage.SystemMessageType.CLEARED_CHAT == chatMessageList[0].systemMessageType
+            ) {
+                adapter?.clear()
+                adapter?.notifyDataSetChanged()
+            }
+
             if (isFirstMessagesProcessing) {
                 cancelNotificationsForCurrentConversation()
 

+ 59 - 0
app/src/main/java/com/nextcloud/talk/controllers/ConversationInfoController.kt

@@ -30,6 +30,7 @@ import android.text.TextUtils
 import android.util.Log
 import android.view.MenuItem
 import android.view.View
+import android.widget.Toast
 import androidx.appcompat.widget.SwitchCompat
 import androidx.core.content.ContextCompat
 import androidx.work.Data
@@ -165,6 +166,7 @@ class ConversationInfoController(args: Bundle) :
 
         binding.deleteConversationAction.setOnClickListener { showDeleteConversationDialog(null) }
         binding.leaveConversationAction.setOnClickListener { leaveConversation() }
+        binding.clearConversationHistory.setOnClickListener { showClearHistoryDialog(null) }
         binding.addParticipantsAction.setOnClickListener { addParticipants() }
 
         fetchRoomInfo()
@@ -299,6 +301,7 @@ class ConversationInfoController(args: Bundle) :
     private fun showLovelyDialog(dialogId: Int, savedInstanceState: Bundle) {
         when (dialogId) {
             ID_DELETE_CONVERSATION_DIALOG -> showDeleteConversationDialog(savedInstanceState)
+            ID_CLEAR_CHAT_DIALOG -> showClearHistoryDialog(savedInstanceState)
             else -> {
             }
         }
@@ -488,6 +491,55 @@ class ConversationInfoController(args: Bundle) :
         }
     }
 
+    private fun showClearHistoryDialog(savedInstanceState: Bundle?) {
+        if (activity != null) {
+            LovelyStandardDialog(activity, LovelyStandardDialog.ButtonLayout.HORIZONTAL)
+                .setTopColorRes(R.color.nc_darkRed)
+                .setIcon(
+                    DisplayUtils.getTintedDrawable(
+                        context!!.resources,
+                        R.drawable.ic_delete_black_24dp, R.color.bg_default
+                    )
+                )
+                .setPositiveButtonColor(context!!.resources.getColor(R.color.nc_darkRed))
+                .setTitle(R.string.nc_clear_history)
+                .setMessage(R.string.nc_clear_history_warning)
+                .setPositiveButton(R.string.nc_delete) { clearHistory() }
+                .setNegativeButton(R.string.nc_cancel, null)
+                .setInstanceStateHandler(ID_CLEAR_CHAT_DIALOG, saveStateHandler!!)
+                .setSavedInstanceState(savedInstanceState)
+                .show()
+        }
+    }
+
+    private fun clearHistory() {
+        val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1))
+
+        ncApi?.clearChatHistory(
+            credentials,
+            ApiUtils.getUrlForChat(apiVersion, conversationUser!!.baseUrl, conversationToken)
+        )
+            ?.subscribeOn(Schedulers.io())
+            ?.observeOn(AndroidSchedulers.mainThread())
+            ?.subscribe(object : Observer<GenericOverall> {
+                override fun onSubscribe(d: Disposable) {
+                }
+
+                override fun onNext(genericOverall: GenericOverall) {
+                    Toast.makeText(context, context?.getString(R.string.nc_clear_history_success), Toast.LENGTH_LONG)
+                        .show()
+                }
+
+                override fun onError(e: Throwable) {
+                    Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()
+                    Log.e(TAG, "failed to clear chat history", e)
+                }
+
+                override fun onComplete() {
+                }
+            })
+    }
+
     private fun deleteConversation() {
         workerData?.let {
             WorkManager.getInstance().enqueue(
@@ -529,8 +581,14 @@ class ConversationInfoController(args: Bundle) :
 
                         if (conversationCopy!!.canModerate(conversationUser)) {
                             binding.addParticipantsAction.visibility = View.VISIBLE
+                            if (CapabilitiesUtil.hasSpreedFeatureCapability(conversationUser, "clear-history")) {
+                                binding.clearConversationHistory.visibility = View.VISIBLE
+                            } else {
+                                binding.clearConversationHistory.visibility = View.GONE
+                            }
                         } else {
                             binding.addParticipantsAction.visibility = View.GONE
+                            binding.clearConversationHistory.visibility = View.GONE
                         }
 
                         if (isAttached && (!isBeingDestroyed || !isDestroyed)) {
@@ -1002,6 +1060,7 @@ class ConversationInfoController(args: Bundle) :
     companion object {
         private const val TAG = "ConversationInfControll"
         private const val ID_DELETE_CONVERSATION_DIALOG = 0
+        private const val ID_CLEAR_CHAT_DIALOG = 1
         private val LOW_EMPHASIS_OPACITY: Float = 0.38f
     }
 

+ 2 - 1
app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java

@@ -646,6 +646,7 @@ public class ChatMessage implements MessageContentType, MessageContentType.Image
         MATTERBRIDGE_CONFIG_EDITED,
         MATTERBRIDGE_CONFIG_REMOVED,
         MATTERBRIDGE_CONFIG_ENABLED,
-        MATTERBRIDGE_CONFIG_DISABLED
+        MATTERBRIDGE_CONFIG_DISABLED,
+        CLEARED_CHAT
     }
 }

+ 7 - 1
app/src/main/java/com/nextcloud/talk/models/json/converters/EnumSystemMessageTypeConverter.kt

@@ -3,8 +3,10 @@
  *
  * @author Mario Danic
  * @author Tim Krüger
- * Copyright (C) 2021 Tim Krüger <t@timkrueger.me>
+ * @author Marcel Hibbe
  * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
+ * Copyright (C) 2021 Tim Krüger <t@timkrueger.me>
+ * Copyright (C) 2021 Marcel Hibbe <dev@mhibbe.de>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,6 +32,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CALL_LE
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CALL_MISSED
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CALL_STARTED
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CALL_TRIED
+import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CLEARED_CHAT
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CONVERSATION_CREATED
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.CONVERSATION_RENAMED
 import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.DESCRIPTION_REMOVED
@@ -92,6 +95,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage.SystemMessageType.USER_RE
 * `guest_moderator_promoted` - {actor} promoted {user} to moderator
 * `guest_moderator_demoted` - {actor} demoted {user} from moderator
 * `message_deleted` - Message deleted by {actor} (Should not be shown to the user)
+* `history_cleared` - {actor} cleared the history of the conversation
 * `file_shared` - {file}
 * `object_shared` - {object}
 * `matterbridge_config_added` - {actor} set up Matterbridge to synchronize this conversation with other chats
@@ -139,6 +143,7 @@ class EnumSystemMessageTypeConverter : StringBasedTypeConverter<ChatMessage.Syst
             "matterbridge_config_removed" -> return MATTERBRIDGE_CONFIG_REMOVED
             "matterbridge_config_enabled" -> return MATTERBRIDGE_CONFIG_ENABLED
             "matterbridge_config_disabled" -> return MATTERBRIDGE_CONFIG_DISABLED
+            "history_cleared" -> return CLEARED_CHAT
             else -> return DUMMY
         }
     }
@@ -186,6 +191,7 @@ class EnumSystemMessageTypeConverter : StringBasedTypeConverter<ChatMessage.Syst
             MATTERBRIDGE_CONFIG_REMOVED -> return "matterbridge_config_removed"
             MATTERBRIDGE_CONFIG_ENABLED -> return "matterbridge_config_enabled"
             MATTERBRIDGE_CONFIG_DISABLED -> return "matterbridge_config_disabled"
+            CLEARED_CHAT -> return "clear_history"
             else -> return ""
         }
     }

+ 8 - 0
app/src/main/res/layout/controller_conversation_info.xml

@@ -171,6 +171,14 @@
                     apc:mp_icon_tint="@color/grey_600"
                     apc:mp_title="@string/nc_leave" />
 
+                <com.yarolegovich.mp.MaterialStandardPreference
+                    android:id="@+id/clearConversationHistory"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    apc:mp_icon="@drawable/ic_delete_black_24dp"
+                    apc:mp_icon_tint="@color/grey_600"
+                    apc:mp_title="@string/nc_clear_history" />
+
                 <com.yarolegovich.mp.MaterialStandardPreference
                     android:id="@+id/deleteConversationAction"
                     android:layout_width="match_parent"

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -167,6 +167,9 @@
     <string name="nc_start_conversation">Start a conversation</string>
     <string name="nc_configure_room">Configure conversation</string>
     <string name="nc_leave">Leave conversation</string>
+    <string name="nc_clear_history">Clear history</string>
+    <string name="nc_clear_history_warning">The chat history will be deleted.</string>
+    <string name="nc_clear_history_success">Chat history was cleared</string>
     <string name="nc_rename">Rename conversation</string>
     <string name="nc_set_password">Set a password</string>
     <string name="nc_change_password">Change password</string>