mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2025-01-07 00:20:08 +02:00
Improve blocked domains for admins
This commit is contained in:
parent
5d0d838471
commit
66fb64c5d1
6 changed files with 148 additions and 6 deletions
|
@ -18,6 +18,10 @@ import static app.fedilab.android.activities.admin.AdminActionActivity.AdminEnum
|
|||
import static app.fedilab.android.activities.admin.AdminActionActivity.AdminEnum.DOMAIN;
|
||||
import static app.fedilab.android.activities.admin.AdminActionActivity.AdminEnum.REPORT;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
|
@ -29,11 +33,13 @@ import androidx.appcompat.app.AlertDialog;
|
|||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import app.fedilab.android.R;
|
||||
import app.fedilab.android.activities.BaseActivity;
|
||||
import app.fedilab.android.client.entities.api.admin.AdminDomainBlock;
|
||||
import app.fedilab.android.databinding.ActivityAdminActionsBinding;
|
||||
import app.fedilab.android.databinding.PopupAdminFilterAccountsBinding;
|
||||
import app.fedilab.android.databinding.PopupAdminFilterReportsBinding;
|
||||
|
@ -53,13 +59,32 @@ public class AdminActionActivity extends BaseActivity {
|
|||
private FragmentAdminAccount fragmentAdminAccount;
|
||||
private FragmentAdminDomain fragmentAdminDomain;
|
||||
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Bundle b = intent.getExtras();
|
||||
if (b != null) {
|
||||
AdminDomainBlock adminDomainBlock = (AdminDomainBlock) b.getSerializable(Helper.ARG_ADMIN_DOMAINBLOCK);
|
||||
AdminDomainBlock adminDomainBlockDelete = (AdminDomainBlock) b.getSerializable(Helper.ARG_ADMIN_DOMAINBLOCK_DELETE);
|
||||
if (adminDomainBlock != null && adminDomainBlock.domain != null && fragmentAdminDomain != null) {
|
||||
fragmentAdminDomain.update(adminDomainBlock);
|
||||
}
|
||||
if (adminDomainBlockDelete != null && fragmentAdminDomain != null) {
|
||||
fragmentAdminDomain.delete(adminDomainBlockDelete);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
ThemeHelper.applyThemeBar(this);
|
||||
binding = ActivityAdminActionsBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(Helper.BROADCAST_DATA));
|
||||
if (getSupportActionBar() != null) {
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.cyanea_primary)));
|
||||
|
@ -68,6 +93,7 @@ public class AdminActionActivity extends BaseActivity {
|
|||
binding.reports.setOnClickListener(v -> displayTimeline(REPORT));
|
||||
binding.accounts.setOnClickListener(v -> displayTimeline(ACCOUNT));
|
||||
binding.domains.setOnClickListener(v -> displayTimeline(DOMAIN));
|
||||
|
||||
}
|
||||
|
||||
private void displayTimeline(AdminEnum type) {
|
||||
|
@ -290,6 +316,14 @@ public class AdminActionActivity extends BaseActivity {
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mReceiver != null) {
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (canGoBack) {
|
||||
|
|
|
@ -15,15 +15,22 @@ package app.fedilab.android.activities.admin;
|
|||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
|
||||
import static app.fedilab.android.helper.Helper.BROADCAST_DATA;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import app.fedilab.android.R;
|
||||
import app.fedilab.android.activities.BaseActivity;
|
||||
|
@ -104,18 +111,47 @@ public class AdminDomainBlockActivity extends BaseActivity {
|
|||
} else {
|
||||
Toasty.error(AdminDomainBlockActivity.this, getString(R.string.toast_error), Toasty.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
Intent intent = new Intent(BROADCAST_DATA).putExtra(Helper.ARG_ADMIN_DOMAINBLOCK, adminDomainBlockResult);
|
||||
LocalBroadcastManager.getInstance(AdminDomainBlockActivity.this).sendBroadcast(intent);
|
||||
finish();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_admin_domain, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == android.R.id.home) {
|
||||
finish();
|
||||
return true;
|
||||
} else if (itemId == R.id.action_delete) {
|
||||
if (adminDomainBlock.id != null) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(AdminDomainBlockActivity.this, Helper.dialogStyle());
|
||||
builder.setMessage(getString(R.string.unblock_domain_confirm, adminDomainBlock.domain));
|
||||
builder
|
||||
.setPositiveButton(R.string.unblock_domain, (dialog, which) -> {
|
||||
adminVM.deleteDomain(MainActivity.currentInstance, MainActivity.currentToken, adminDomainBlock.id)
|
||||
.observe(AdminDomainBlockActivity.this, adminDomainBlockResult -> {
|
||||
Intent intent = new Intent(BROADCAST_DATA).putExtra(Helper.ARG_ADMIN_DOMAINBLOCK_DELETE, adminDomainBlock);
|
||||
LocalBroadcastManager.getInstance(AdminDomainBlockActivity.this).sendBroadcast(intent);
|
||||
finish();
|
||||
}
|
||||
);
|
||||
dialog.dismiss();
|
||||
})
|
||||
.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss())
|
||||
.show();
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -239,6 +239,7 @@ public class Helper {
|
|||
public static final String ARG_ACCOUNT = "ARG_ACCOUNT";
|
||||
public static final String ARG_ACCOUNT_ID = "ARG_ACCOUNT_ID";
|
||||
public static final String ARG_ADMIN_DOMAINBLOCK = "ARG_ADMIN_DOMAINBLOCK";
|
||||
public static final String ARG_ADMIN_DOMAINBLOCK_DELETE = "ARG_ADMIN_DOMAINBLOCK_DELETE";
|
||||
|
||||
public static final String ARG_REPORT = "ARG_REPORT";
|
||||
public static final String ARG_ACCOUNT_MENTION = "ARG_ACCOUNT_MENTION";
|
||||
|
|
|
@ -62,6 +62,48 @@ public class FragmentAdminDomain extends Fragment {
|
|||
}
|
||||
}
|
||||
|
||||
public void delete(AdminDomainBlock adminDomainBlock) {
|
||||
int position = 0;
|
||||
for (AdminDomainBlock adminDomainBlockPresent : adminDomainBlocks) {
|
||||
if (adminDomainBlockPresent.id.equals(adminDomainBlock.id)) {
|
||||
adminDomainBlocks.remove(position);
|
||||
adminDomainAdapter.notifyItemRemoved(position);
|
||||
break;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
}
|
||||
|
||||
public void update(AdminDomainBlock adminDomainBlock) {
|
||||
if (adminDomainBlocks == null) {
|
||||
AdminDomainBlocks adminDomainBlocks = new AdminDomainBlocks();
|
||||
adminDomainBlocks.adminDomainBlocks = new ArrayList<>();
|
||||
adminDomainBlocks.adminDomainBlocks.add(adminDomainBlock);
|
||||
initializeStatusesCommonView(adminDomainBlocks);
|
||||
}
|
||||
int position = 0;
|
||||
boolean find = false;
|
||||
for (AdminDomainBlock adminDomainBlockPresent : adminDomainBlocks) {
|
||||
if (adminDomainBlockPresent.id.equals(adminDomainBlock.id)) {
|
||||
adminDomainBlocks.get(position).private_comment = adminDomainBlock.private_comment;
|
||||
adminDomainBlocks.get(position).public_comment = adminDomainBlock.public_comment;
|
||||
adminDomainBlocks.get(position).severity = adminDomainBlock.severity;
|
||||
adminDomainBlocks.get(position).reject_reports = adminDomainBlock.reject_reports;
|
||||
adminDomainBlocks.get(position).reject_media = adminDomainBlock.reject_media;
|
||||
adminDomainBlocks.get(position).obfuscate = adminDomainBlock.obfuscate;
|
||||
adminDomainAdapter.notifyItemChanged(position);
|
||||
find = true;
|
||||
break;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
if (!find) {
|
||||
adminDomainBlocks.add(0, adminDomainBlock);
|
||||
adminDomainAdapter.notifyItemInserted(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@ package app.fedilab.android.viewmodel.mastodon;
|
|||
import android.app.Application;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
|
@ -56,6 +55,7 @@ public class AdminVM extends AndroidViewModel {
|
|||
private MutableLiveData<AdminReports> adminReporstListMutableLiveData;
|
||||
private MutableLiveData<AdminDomainBlock> adminDomainBlockMutableLiveData;
|
||||
private MutableLiveData<AdminDomainBlocks> adminDomainBlockListMutableLiveData;
|
||||
private MutableLiveData<Boolean> booleanMutableLiveData;
|
||||
|
||||
|
||||
public AdminVM(@NonNull Application application) {
|
||||
|
@ -658,11 +658,8 @@ public class AdminVM extends AndroidViewModel {
|
|||
Response<AdminDomainBlock> getDomainBlocksResponse = getDomainBlock.execute();
|
||||
if (getDomainBlocksResponse.isSuccessful()) {
|
||||
admDomainBlock = getDomainBlocksResponse.body();
|
||||
} else {
|
||||
Log.v(Helper.TAG, "errr: " + getDomainBlocksResponse.errorBody().string());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.v(Helper.TAG, "e: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -709,6 +706,29 @@ public class AdminVM extends AndroidViewModel {
|
|||
return adminDomainBlockListMutableLiveData;
|
||||
}
|
||||
|
||||
public LiveData<Boolean> deleteDomain(@NonNull String instance,
|
||||
String token,
|
||||
String id) {
|
||||
MastodonAdminService mastodonAdminService = init(instance);
|
||||
booleanMutableLiveData = new MutableLiveData<>();
|
||||
final boolean[] successReply = {false};
|
||||
new Thread(() -> {
|
||||
Call<Void> getDomainBlock = mastodonAdminService.deleteBlockDomain(token, id);
|
||||
if (getDomainBlock != null) {
|
||||
try {
|
||||
Response<Void> response = getDomainBlock.execute();
|
||||
successReply[0] = response.isSuccessful();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
Runnable myRunnable = () -> booleanMutableLiveData.setValue(successReply[0]);
|
||||
mainHandler.post(myRunnable);
|
||||
}).start();
|
||||
return booleanMutableLiveData;
|
||||
}
|
||||
|
||||
/**
|
||||
* View a single allowed domain
|
||||
*
|
||||
|
|
9
app/src/main/res/menu/menu_admin_domain.xml
Normal file
9
app/src/main/res/menu/menu_admin_domain.xml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/action_delete"
|
||||
android:icon="@drawable/ic_baseline_delete_24"
|
||||
android:title="@string/delete"
|
||||
app:showAsAction="ifRoom" />
|
||||
</menu>
|
Loading…
Reference in a new issue