From 045e5a80ed743ad04c6cae084410a40650a74351 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 26 Sep 2018 19:07:22 +0200 Subject: [PATCH] Blocked domains - 2 --- .../asynctasks/DeleteDomainsAsyncTask.java | 57 +++++++++++++++++++ .../fr/gouv/etalab/mastodon/client/API.java | 32 ++--------- .../mastodon/drawers/DomainsListAdapter.java | 8 +++ .../mastodon/drawers/StatusListAdapter.java | 18 +++++- .../DisplayMutedInstanceFragment.java | 4 ++ .../OnRetrieveDomainsInterface.java | 1 + app/src/main/res/menu/option_toot.xml | 4 ++ app/src/main/res/values/strings.xml | 3 + 8 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/DeleteDomainsAsyncTask.java diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/DeleteDomainsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/DeleteDomainsAsyncTask.java new file mode 100644 index 000000000..78a27ef9a --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/DeleteDomainsAsyncTask.java @@ -0,0 +1,57 @@ +/* Copyright 2018 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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 the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab 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 General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; + +import java.lang.ref.WeakReference; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.interfaces.OnRetrieveDomainsInterface; + + +/** + * Created by Thomas on 26/09/2018. + * Delete a blocked instance + */ + +public class DeleteDomainsAsyncTask extends AsyncTask { + + + private OnRetrieveDomainsInterface listener; + private WeakReference contextReference; + private int response; + private String domainName; + + public DeleteDomainsAsyncTask(Context context, String domainName, OnRetrieveDomainsInterface onRetrieveDomainsInterface){ + this.contextReference = new WeakReference<>(context); + this.listener = onRetrieveDomainsInterface; + this.domainName = domainName; + } + + @Override + protected Void doInBackground(Void... params) { + API api = new API(this.contextReference.get()); + response = api.deleteBlockedDomain(domainName); + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onRetrieveDomainsDeleted(response); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 0361d5501..3e0df6016 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -89,7 +89,8 @@ public class API { ENDORSE, UNENDORSE, SHOW_BOOST, - HIDE_BOOST + HIDE_BOOST, + BLOCK_DOMAIN } public enum accountPrivacy { @@ -811,32 +812,6 @@ public class API { return apiResponse; } - /** - * Add a blocked domains for the authenticated account *synchronously* - * @param domain String domain name - */ - @SuppressWarnings("SameParameterValue") - public int addBlockedDomain(String domain){ - - HashMap params = new HashMap<>(); - params.put("domain",domain); - domains = new ArrayList<>(); - HttpsConnection httpsConnection; - try { - httpsConnection = new HttpsConnection(context); - httpsConnection.post(getAbsoluteUrl("/domain_blocks"), 60, params, prefKeyOauthTokenT); - actionCode = httpsConnection.getActionCode(); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } - return actionCode; - } /** * Delete a blocked domains for the authenticated account *synchronously* @@ -1053,6 +1028,9 @@ public class API { case BLOCK: action = String.format("/accounts/%s/block", targetedId); break; + case BLOCK_DOMAIN: + action = String.format("/domain_blocks/%s", targetedId); + break; case UNBLOCK: action = String.format("/accounts/%s/unblock", targetedId); break; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/DomainsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/DomainsListAdapter.java index 13a4c0aac..94b3d3874 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/DomainsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/DomainsListAdapter.java @@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.drawers; import android.content.Context; import android.content.DialogInterface; +import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; @@ -28,6 +29,7 @@ import android.widget.TextView; import java.util.List; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.DeleteDomainsAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveDomainsInterface; @@ -77,6 +79,7 @@ public class DomainsListAdapter extends RecyclerView.Adapter implements OnRetrie public void onClick(DialogInterface dialog, int which) { domains.remove(domain); domainsListAdapter.notifyItemRemoved(holder.getAdapterPosition()); + new DeleteDomainsAsyncTask(context, domain, DomainsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); if( domains.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE) textviewNoAction.setVisibility(View.VISIBLE); dialog.dismiss(); @@ -112,6 +115,11 @@ public class DomainsListAdapter extends RecyclerView.Adapter implements OnRetrie } + @Override + public void onRetrieveDomainsDeleted(int response) { + + } + private class ViewHolder extends RecyclerView.ViewHolder{ TextView domain_name; ImageView domain_delete; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index a57342fe9..c1718e1a3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -1187,10 +1187,14 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct popup.getMenu().findItem(R.id.action_mute).setVisible(false); popup.getMenu().findItem(R.id.action_report).setVisible(false); popup.getMenu().findItem(R.id.action_timed_mute).setVisible(false); + popup.getMenu().findItem(R.id.action_block_domain).setVisible(false); stringArrayConf = context.getResources().getStringArray(R.array.more_action_owner_confirm); }else { popup.getMenu().findItem(R.id.action_redraft).setVisible(false); popup.getMenu().findItem(R.id.action_remove).setVisible(false); + //Same instance + if(status.getAccount().getAcct().split("#").length <2 ) + popup.getMenu().findItem(R.id.action_block_domain).setVisible(false); stringArrayConf = context.getResources().getStringArray(R.array.more_action_confirm); if( type != RetrieveFeedsAsyncTask.Type.HOME){ popup.getMenu().findItem(R.id.action_timed_mute).setVisible(false); @@ -1225,6 +1229,13 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //noinspection deprecation builderInner.setMessage(Html.fromHtml(status.getContent())); break; + case R.id.action_block_domain: + builderInner = new AlertDialog.Builder(context); + builderInner.setTitle(stringArrayConf[3]); + doAction = API.StatusAction.BLOCK_DOMAIN; + String domain = status.getAccount().getAcct().split("@")[1]; + builderInner.setMessage(context.getString(R.string.block_domain_confirm_message, domain)); + break; case R.id.action_mute: builderInner = new AlertDialog.Builder(context); builderInner.setTitle(stringArrayConf[0]); @@ -1507,7 +1518,12 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct comment = finalInput.getText().toString(); new PostActionAsyncTask(context, doAction, status.getId(), status, comment, StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }else{ - String targetedId = status.getAccount().getId(); + String targetedId; + if( item.getItemId() == R.id.action_block_domain){ + targetedId = status.getAccount().getAcct().split("@")[1]; + }else { + targetedId = status.getAccount().getId(); + } new PostActionAsyncTask(context, doAction, targetedId, StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } dialog.dismiss(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayMutedInstanceFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayMutedInstanceFragment.java index f0625e250..821a3a378 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayMutedInstanceFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayMutedInstanceFragment.java @@ -200,6 +200,10 @@ public class DisplayMutedInstanceFragment extends Fragment implements OnRetrieve firstLoad = false; } + @Override + public void onRetrieveDomainsDeleted(int response) { + + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveDomainsInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveDomainsInterface.java index 980a1c772..63e6f0cea 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveDomainsInterface.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveDomainsInterface.java @@ -23,4 +23,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse; */ public interface OnRetrieveDomainsInterface { void onRetrieveDomains(APIResponse apiResponse); + void onRetrieveDomainsDeleted(int response); } diff --git a/app/src/main/res/menu/option_toot.xml b/app/src/main/res/menu/option_toot.xml index 63e1c67fe..4aa302a19 100644 --- a/app/src/main/res/menu/option_toot.xml +++ b/app/src/main/res/menu/option_toot.xml @@ -29,6 +29,10 @@ android:id="@+id/action_report" android:title="@string/more_action_3" app:showAsAction="never" /> + Mute this account? Block this account? Report this toot? + Block this domain? @@ -625,7 +626,9 @@ No blocked domains! Unblock domain Are you sure to unblock %s? + Are you sure to block %s? Blocked domains + Block domain Never