From c527e3ae25ee50d04f8ff034a7e212c9c8719c32 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 6 Nov 2022 18:08:54 +0100 Subject: [PATCH] Fix issue #410 - Remove messages from cache and in timelines for blocked accounts --- .../client/entities/app/StatusCache.java | 20 +++++++++++++++++++ .../app/fedilab/android/helper/Helper.java | 1 + .../FragmentMastodonNotification.java | 19 ++++++++++++++++++ .../timeline/FragmentMastodonTimeline.java | 17 ++++++++++++++++ .../viewmodel/mastodon/AccountsVM.java | 6 ++++++ 5 files changed, 63 insertions(+) diff --git a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java index 83c04a38..254a8197 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java +++ b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java @@ -493,6 +493,26 @@ public class StatusCache { } } + /** + * delete statuses in db for a user that wrote them + * + * @param instance - String instance + * @param userid - String status id + * @param targetedUser - String id of the user that wrote them + * @throws DBException exception with database + */ + public void deleteStatusForTargetedAccount(String instance, String userid, String targetedUser) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + db.delete(Sqlite.TABLE_STATUS_CACHE, + Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_STATUS + " LIKE ?", + new String[]{userid, instance, "%\"id\":\"" + targetedUser + "\"%" }); + } catch (Exception e) { + e.printStackTrace(); + } + } /** * Get paginated notifications from db diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index cdb7338b..61843940 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -217,6 +217,7 @@ public class Helper { public static final String ARG_STATUS_POSTED = "ARG_STATUS_POSTED"; public static final String ARG_STATUS_ACTION = "ARG_STATUS_ACTION"; + public static final String ARG_DELETE_ALL_FOR_ACCOUNT_ID = "ARG_DELETE_ALL_FOR_ACCOUNT_ID"; public static final String ARG_STATUS_ACCOUNT_ID_DELETED = "ARG_STATUS_ACCOUNT_ID_DELETED"; public static final String ARG_STATUS_DRAFT = "ARG_STATUS_DRAFT"; diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index 9aee9299..80e9a9e5 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -67,12 +67,14 @@ public class FragmentMastodonNotification extends Fragment implements Notificati private boolean flagLoading; private List notificationList; private NotificationAdapter notificationAdapter; + private final BroadcastReceiver receive_action = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle b = intent.getExtras(); if (b != null) { Status receivedStatus = (Status) b.getSerializable(Helper.ARG_STATUS_ACTION); + String delete_all_for_account_id = b.getString(Helper.ARG_DELETE_ALL_FOR_ACCOUNT_ID); if (receivedStatus != null && notificationAdapter != null) { int position = getPosition(receivedStatus); if (position >= 0) { @@ -86,10 +88,27 @@ public class FragmentMastodonNotification extends Fragment implements Notificati notificationAdapter.notifyItemChanged(position); } } + } else if (delete_all_for_account_id != null) { + List toRemove = new ArrayList<>(); + if (notificationList != null) { + for (int position = 0; position < notificationList.size(); position++) { + if (notificationList.get(position).account.id.equals(delete_all_for_account_id)) { + toRemove.add(notificationList.get(position)); + } + } + } + if (toRemove.size() > 0) { + for (int i = 0; i < toRemove.size(); i++) { + int position = getPosition(toRemove.get(i)); + notificationList.remove(position); + notificationAdapter.notifyItemRemoved(position); + } + } } } } }; + private boolean isViewInitialized; private Notifications initialNotifications; private String max_id, min_id, min_id_fetch_more, max_id_fetch_more; diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index 6e3d55fb..0e82effc 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -89,6 +89,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. if (b != null) { Status receivedStatus = (Status) b.getSerializable(Helper.ARG_STATUS_ACTION); String delete_statuses_for_user = b.getString(Helper.ARG_STATUS_ACCOUNT_ID_DELETED); + String delete_all_for_account_id = b.getString(Helper.ARG_DELETE_ALL_FOR_ACCOUNT_ID); Status status_to_delete = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED); Status status_to_update = (Status) b.getSerializable(Helper.ARG_STATUS_UPDATED); Status statusPosted = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED); @@ -132,6 +133,22 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. } else if (statusPosted != null && statusAdapter != null && timelineType == Timeline.TimeLineEnum.HOME) { timelineStatuses.add(0, statusPosted); statusAdapter.notifyItemInserted(0); + } else if (delete_all_for_account_id != null) { + List toRemove = new ArrayList<>(); + if (timelineStatuses != null) { + for (int position = 0; position < timelineStatuses.size(); position++) { + if (timelineStatuses.get(position).account.id.equals(delete_all_for_account_id)) { + toRemove.add(timelineStatuses.get(position)); + } + } + } + if (toRemove.size() > 0) { + for (int i = 0; i < toRemove.size(); i++) { + int position = getPosition(toRemove.get(i)); + timelineStatuses.remove(position); + statusAdapter.notifyItemRemoved(position); + } + } } } } diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java index 69e6cd05..06f299f2 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AccountsVM.java @@ -14,6 +14,8 @@ package app.fedilab.android.viewmodel.mastodon; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction; + import android.app.Application; import android.net.Uri; import android.os.Handler; @@ -33,6 +35,7 @@ import java.util.concurrent.TimeUnit; import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; +import app.fedilab.android.activities.MainActivity; import app.fedilab.android.client.endpoints.MastodonAccountsService; import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.client.entities.api.Accounts; @@ -50,6 +53,7 @@ import app.fedilab.android.client.entities.api.Status; import app.fedilab.android.client.entities.api.Statuses; import app.fedilab.android.client.entities.api.Tag; import app.fedilab.android.client.entities.api.Token; +import app.fedilab.android.client.entities.app.StatusCache; import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import okhttp3.MultipartBody; @@ -627,6 +631,8 @@ public class AccountsVM extends AndroidViewModel { Response blockResponse = blockCall.execute(); if (blockResponse.isSuccessful()) { relationShip = blockResponse.body(); + sendAction(getApplication().getApplicationContext(), Helper.ARG_DELETE_ALL_FOR_ACCOUNT_ID, null, id); + new StatusCache(getApplication().getApplicationContext()).deleteStatusForTargetedAccount(MainActivity.currentInstance, MainActivity.currentUserID, id); } } catch (Exception e) { e.printStackTrace();