From e32b3bf6da78ceca439e3f77b5d64bd8cd687122 Mon Sep 17 00:00:00 2001
From: Thomas <tschneider.ac@gmail.com>
Date: Thu, 24 Nov 2022 10:35:50 +0100
Subject: [PATCH] Fix admin with api/v2

---
 .../activities/AccountReportActivity.java     | 19 ++---
 .../client/entities/api/AdminAccount.java     | 78 +++++++++++++++----
 .../ui/drawer/AdminAccountAdapter.java        |  3 +-
 .../android/viewmodel/mastodon/AdminVM.java   | 15 ++--
 4 files changed, 78 insertions(+), 37 deletions(-)

diff --git a/app/src/main/java/app/fedilab/android/activities/AccountReportActivity.java b/app/src/main/java/app/fedilab/android/activities/AccountReportActivity.java
index 6cbbe4fd..e8a701cd 100644
--- a/app/src/main/java/app/fedilab/android/activities/AccountReportActivity.java
+++ b/app/src/main/java/app/fedilab/android/activities/AccountReportActivity.java
@@ -221,7 +221,7 @@ public class AccountReportActivity extends BaseActivity {
             binding.email.setVisibility(View.GONE);
             binding.emailLabel.setVisibility(View.GONE);
         }
-        if (accountAdmin.ip == null || accountAdmin.ip.ip.trim().equals("")) {
+        if (accountAdmin.ip == null || accountAdmin.ip.trim().equals("")) {
             binding.recentIp.setVisibility(View.GONE);
             binding.recentIpLabel.setVisibility(View.GONE);
         }
@@ -243,7 +243,7 @@ public class AccountReportActivity extends BaseActivity {
             binding.emailUser.setVisibility(View.VISIBLE);
             binding.commentLabel.setVisibility(View.VISIBLE);
             binding.comment.setVisibility(View.VISIBLE);
-            binding.recentIp.setText(accountAdmin.ip != null ? accountAdmin.ip.ip : "");
+            binding.recentIp.setText(accountAdmin.ip != null ? accountAdmin.ip : "");
             binding.disable.setVisibility(View.VISIBLE);
             binding.suspend.setVisibility(View.VISIBLE);
         } else {
@@ -260,18 +260,9 @@ public class AccountReportActivity extends BaseActivity {
         }
 
         if (accountAdmin.role != null) {
-            switch (accountAdmin.role) {
-                case "user":
-                    binding.permissions.setText(getString(R.string.user));
-                    break;
-                case "moderator":
-                    binding.permissions.setText(getString(R.string.moderator));
-                    break;
-                case "admin":
-                    binding.permissions.setText(getString(R.string.administrator));
-                    break;
-            }
-            if (accountAdmin.role.equals("admin") || accountAdmin.role.equals("moderator")) {
+            binding.permissions.setText(AdminAccount.permissions.get(accountAdmin.role.permissions));
+            binding.permissions.setText(getString(R.string.user));
+            if (accountAdmin.role.permissions == 1 || accountAdmin.role.permissions == 400) {
                 binding.warn.setVisibility(View.GONE);
                 binding.suspend.setVisibility(View.GONE);
                 binding.silence.setVisibility(View.GONE);
diff --git a/app/src/main/java/app/fedilab/android/client/entities/api/AdminAccount.java b/app/src/main/java/app/fedilab/android/client/entities/api/AdminAccount.java
index 0dcaf923..99da1e3b 100644
--- a/app/src/main/java/app/fedilab/android/client/entities/api/AdminAccount.java
+++ b/app/src/main/java/app/fedilab/android/client/entities/api/AdminAccount.java
@@ -18,6 +18,7 @@ import com.google.gson.annotations.SerializedName;
 
 import java.io.Serializable;
 import java.util.Date;
+import java.util.LinkedHashMap;
 import java.util.List;
 
 public class AdminAccount implements Serializable {
@@ -32,26 +33,48 @@ public class AdminAccount implements Serializable {
     public Date created_at;
     @SerializedName("email")
     public String email;
+    public static LinkedHashMap<Integer, String> permissions;
+
+    static {
+        permissions = new LinkedHashMap<>();
+        permissions.put(1, "Administrator");
+        permissions.put(2, "Devops");
+        permissions.put(4, "View Audit Log");
+        permissions.put(8, "View Dashboard");
+        permissions.put(10, "Manage Reports");
+        permissions.put(20, "Manage Federation");
+        permissions.put(40, "Manage Settings");
+        permissions.put(80, "Manage Blocks");
+        permissions.put(100, "Manage Taxonomies");
+        permissions.put(200, "Manage Appeals");
+        permissions.put(400, "Manage Users");
+        permissions.put(800, "Manage Invites");
+        permissions.put(1000, "Manage Rules");
+        permissions.put(2000, "Manage Announcements");
+        permissions.put(4000, "Manage Custom Emojis");
+        permissions.put(8000, "Manage Webhooks");
+        permissions.put(10000, "Invite Users");
+        permissions.put(20000, "Manage Roles");
+        permissions.put(40000, "Manage User Access");
+        permissions.put(80000, "Delete User Data");
+    }
+
     @SerializedName("ip")
-    public IP ip;
-    @SerializedName("ips")
-    public List<IP> ips;
-    @SerializedName("locale")
-    public String locale;
-    @SerializedName("invite_request")
-    public String invite_request;
+    public String ip;
     @SerializedName("role")
-    public String role;
+    public Role role;
     @SerializedName("confirmed")
     public boolean confirmed;
-    @SerializedName("approved")
-    public boolean approved;
-    @SerializedName("disabled")
-    public boolean disabled;
-    @SerializedName("silenced")
-    public boolean silenced;
     @SerializedName("suspended")
     public boolean suspended;
+    @SerializedName("silenced")
+    public boolean silenced;
+    @SerializedName("disabled")
+    public boolean disabled;
+    @SerializedName("approved")
+    public boolean approved;
+    @SerializedName("ips")
+    public List<IP> ips;
     @SerializedName("account")
     public Account account;
     @SerializedName("created_by_application_id")
@@ -65,7 +88,30 @@ public class AdminAccount implements Serializable {
         public String ip;
         @SerializedName("used_at")
         public Date used_at;
-        @SerializedName("user_id")
-        public String user_id;
     }
+
+    @SerializedName("locale")
+    public String locale;
+    @SerializedName("invite_request")
+    public String invite_request;
+
+    public static class Role implements Serializable {
+        @SerializedName("ip")
+        public String ip;
+        @SerializedName("name")
+        public String name;
+        @SerializedName("color")
+        public String color;
+        @SerializedName("position")
+        public long position;
+        @SerializedName("permissions")
+        public int permissions;
+        @SerializedName("highlighted")
+        public boolean highlighted;
+        @SerializedName("created_at")
+        public Date created_at;
+        @SerializedName("updated_at")
+        public Date updated_at;
+    }
+
 }
diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/AdminAccountAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/AdminAccountAdapter.java
index 5d35e711..d4556fe4 100644
--- a/app/src/main/java/app/fedilab/android/ui/drawer/AdminAccountAdapter.java
+++ b/app/src/main/java/app/fedilab/android/ui/drawer/AdminAccountAdapter.java
@@ -78,8 +78,7 @@ public class AdminAccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
         holder.binding.followersCount.setText(String.valueOf(adminAccount.account.followers_count));
         holder.binding.email.setText(adminAccount.email);
         if (adminAccount.ip != null) {
-            holder.binding.lastActive.setText(Helper.shortDateToString(adminAccount.ip.used_at));
-            holder.binding.ip.setText(adminAccount.ip.ip);
+            holder.binding.ip.setText(adminAccount.ip);
         } else if (adminAccount.ips != null && adminAccount.ips.size() > 0) {
             holder.binding.lastActive.setText(Helper.shortDateToString(adminAccount.ips.get(0).used_at));
             holder.binding.ip.setText(adminAccount.ips.get(0).ip);
diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java
index cb764d01..88bd007e 100644
--- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java
+++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/AdminVM.java
@@ -23,9 +23,6 @@ import androidx.lifecycle.AndroidViewModel;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -60,7 +57,6 @@ public class AdminVM extends AndroidViewModel {
     }
 
     private MastodonAdminService init(@NonNull String instance) {
-        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
         Retrofit retrofit = new Retrofit.Builder()
                 .baseUrl("https://" + instance + "/api/v1/")
                 .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder()))
@@ -69,6 +65,15 @@ public class AdminVM extends AndroidViewModel {
         return retrofit.create(MastodonAdminService.class);
     }
 
+    private MastodonAdminService initv2(@NonNull String instance) {
+        Retrofit retrofit = new Retrofit.Builder()
+                .baseUrl("https://" + instance + "/api/v2/")
+                .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder()))
+                .client(okHttpClient)
+                .build();
+        return retrofit.create(MastodonAdminService.class);
+    }
+
     /**
      * View accounts matching certain criteria for filtering, up to 100 at a time.
      *
@@ -107,7 +112,7 @@ public class AdminVM extends AndroidViewModel {
                                                String maxId,
                                                String sinceId,
                                                Integer limit) {
-        MastodonAdminService mastodonAdminService = init(instance);
+        MastodonAdminService mastodonAdminService = initv2(instance);
         adminAccountsListMutableLiveData = new MutableLiveData<>();
         new Thread(() -> {
             Call<List<AdminAccount>> getAccountsCall = mastodonAdminService.getAccounts(