Fix issue #410 - Remove messages from cache and in timelines for blocked accounts

This commit is contained in:
Thomas 2022-11-06 18:08:54 +01:00
parent 51e031f065
commit c527e3ae25
5 changed files with 63 additions and 0 deletions

View File

@ -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

View File

@ -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";

View File

@ -67,12 +67,14 @@ public class FragmentMastodonNotification extends Fragment implements Notificati
private boolean flagLoading;
private List<Notification> 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<Notification> 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;

View File

@ -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<Status> 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);
}
}
}
}
}

View File

@ -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 <http://www.gnu.org/licenses>. */
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<RelationShip> 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();