mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Block User option
This commit adds implementation to the block/unblock user option
This commit is contained in:
parent
37fa76b79a
commit
6621748de5
@ -79,9 +79,9 @@ public class LinkResolverActivity extends AppCompatActivity {
|
||||
|
||||
private Uri getRedditUriByPath(String path) {
|
||||
if (path.charAt(0) != '/') {
|
||||
return Uri.parse("https://www.reddit.com/" + path);
|
||||
return Uri.parse(mRetrofit.getBaseURL() + path);
|
||||
} else {
|
||||
return Uri.parse("https://www.reddit.com" + path);
|
||||
return Uri.parse(mRetrofit.getBaseURL() + path);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,7 @@ import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@ -71,11 +72,14 @@ import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
|
||||
import eu.toldi.infinityforlemmy.RetrofitHolder;
|
||||
import eu.toldi.infinityforlemmy.SortType;
|
||||
import eu.toldi.infinityforlemmy.SortTypeSelectionCallback;
|
||||
import eu.toldi.infinityforlemmy.account.FetchBlockedThings;
|
||||
import eu.toldi.infinityforlemmy.adapters.SubredditAutocompleteRecyclerViewAdapter;
|
||||
import eu.toldi.infinityforlemmy.apis.RedditAPI;
|
||||
import eu.toldi.infinityforlemmy.asynctasks.AddSubredditOrUserToMultiReddit;
|
||||
import eu.toldi.infinityforlemmy.asynctasks.CheckIsFollowingUser;
|
||||
import eu.toldi.infinityforlemmy.asynctasks.SwitchAccount;
|
||||
import eu.toldi.infinityforlemmy.blockedcommunity.BlockedCommunityData;
|
||||
import eu.toldi.infinityforlemmy.blockeduser.BlockedUserData;
|
||||
import eu.toldi.infinityforlemmy.bottomsheetfragments.CopyTextBottomSheetFragment;
|
||||
import eu.toldi.infinityforlemmy.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
|
||||
import eu.toldi.infinityforlemmy.bottomsheetfragments.PostLayoutBottomSheetFragment;
|
||||
@ -237,9 +241,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
||||
private int mMessageId;
|
||||
private String mNewAccountName;
|
||||
|
||||
|
||||
private UserData mUserData;
|
||||
|
||||
private boolean isBlocked;
|
||||
|
||||
//private MaterialAlertDialogBuilder nsfwWarningBuilder;
|
||||
|
||||
@Override
|
||||
@ -1091,6 +1096,25 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
||||
setupVisibleElements();
|
||||
new ViewUserDetailActivity.InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData,
|
||||
() -> mFetchUserInfoSuccess = true).execute();
|
||||
FetchBlockedThings.fetchBlockedThings(mRetrofit.getRetrofit(), mAccessToken, mAccountQualifiedName, new FetchBlockedThings.FetchBlockedThingsListener() {
|
||||
|
||||
@Override
|
||||
public void onFetchBlockedThingsSuccess(List<BlockedUserData> blockedUsers, List<BlockedCommunityData> blockedCommunities) {
|
||||
for (BlockedUserData blockedUserData : blockedUsers) {
|
||||
if (blockedUserData.getQualifiedName().equals(qualifiedName)) {
|
||||
isBlocked = true;
|
||||
invalidateOptionsMenu();
|
||||
return;
|
||||
}
|
||||
}
|
||||
isBlocked = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFetchBlockedThingsFailure() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -1131,6 +1155,12 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
||||
menu.findItem(R.id.action_block_user_view_user_detail_activity).setVisible(false);
|
||||
} else {
|
||||
menu.findItem(R.id.action_edit_profile_view_user_detail_activity).setVisible(false);
|
||||
if (isBlocked) {
|
||||
menu.findItem(R.id.action_block_user_view_user_detail_activity).setVisible(false);
|
||||
menu.findItem(R.id.action_unblock_user_view_user_detail_activity).setVisible(true);
|
||||
} else {
|
||||
menu.findItem(R.id.action_unblock_user_view_user_detail_activity).setVisible(false);
|
||||
}
|
||||
}
|
||||
applyMenuItemTheme(menu);
|
||||
return true;
|
||||
@ -1202,10 +1232,13 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
||||
.setTitle(R.string.block_user)
|
||||
.setMessage(R.string.are_you_sure)
|
||||
.setPositiveButton(R.string.yes, (dialogInterface, i)
|
||||
-> BlockUser.blockUser(mOauthRetrofit, mAccessToken, username, new BlockUser.BlockUserListener() {
|
||||
-> BlockUser.blockUser(mRetrofit.getRetrofit(), mAccessToken, mUserData.getId(), true, new BlockUser.BlockUserListener() {
|
||||
@Override
|
||||
public void success() {
|
||||
Toast.makeText(ViewUserDetailActivity.this, R.string.block_user_success, Toast.LENGTH_SHORT).show();
|
||||
isBlocked = true;
|
||||
sectionsPagerAdapter.refresh();
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -1219,6 +1252,25 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
|
||||
} else if (itemId == R.id.action_edit_profile_view_user_detail_activity) {
|
||||
startActivity(new Intent(this, EditProfileActivity.class));
|
||||
return true;
|
||||
} else if (itemId == R.id.action_unblock_user_view_user_detail_activity) {
|
||||
if (mAccessToken == null) {
|
||||
Toast.makeText(this, R.string.login_first, Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
BlockUser.blockUser(mRetrofit.getRetrofit(), mAccessToken, mUserData.getId(), false, new BlockUser.BlockUserListener() {
|
||||
@Override
|
||||
public void success() {
|
||||
isBlocked = false;
|
||||
Toast.makeText(ViewUserDetailActivity.this, R.string.unblock_user_success, Toast.LENGTH_SHORT).show();
|
||||
sectionsPagerAdapter.refresh();
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed() {
|
||||
Toast.makeText(ViewUserDetailActivity.this, R.string.unblock_user_failed, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import eu.toldi.infinityforlemmy.dto.ReadPostDTO;
|
||||
import eu.toldi.infinityforlemmy.dto.SaveCommentDTO;
|
||||
import eu.toldi.infinityforlemmy.dto.SavePostDTO;
|
||||
import eu.toldi.infinityforlemmy.dto.SubmitPostDTO;
|
||||
import eu.toldi.infinityforlemmy.dto.UserBlockDTO;
|
||||
import eu.toldi.infinityforlemmy.message.MessageCount;
|
||||
import okhttp3.MultipartBody;
|
||||
import retrofit2.Call;
|
||||
@ -43,6 +44,10 @@ public interface LemmyAPI {
|
||||
@GET("api/v3/user")
|
||||
Call<String> userInfo(@Query("username") String username, @Query("auth") String access_token);
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("api/v3/user/block")
|
||||
Call<String> userBlock(@Body UserBlockDTO params);
|
||||
|
||||
@GET("api/v3/user/mention")
|
||||
Call<String> userMentions(@Query("sort") String sort, @Query("page") Integer page, @Query("limit") Integer limit, @Query("unread_only") boolean unread_only, @Query("auth") String access_token);
|
||||
|
||||
|
@ -2,11 +2,8 @@ package eu.toldi.infinityforlemmy.user;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import eu.toldi.infinityforlemmy.apis.RedditAPI;
|
||||
import eu.toldi.infinityforlemmy.utils.APIUtils;
|
||||
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
|
||||
import eu.toldi.infinityforlemmy.dto.UserBlockDTO;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
@ -15,13 +12,13 @@ import retrofit2.Retrofit;
|
||||
public class BlockUser {
|
||||
public interface BlockUserListener {
|
||||
void success();
|
||||
|
||||
void failed();
|
||||
}
|
||||
|
||||
public static void blockUser(Retrofit oauthRetrofit, String accessToken, String username, BlockUserListener blockUserListener) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put(APIUtils.NAME_KEY, username);
|
||||
oauthRetrofit.create(RedditAPI.class).blockUser(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
|
||||
public static void blockUser(Retrofit retrofit, String accessToken, int userID, boolean block, BlockUserListener blockUserListener) {
|
||||
LemmyAPI api = retrofit.create(LemmyAPI.class);
|
||||
api.userBlock(new UserBlockDTO(userID, block, accessToken)).enqueue(new Callback<>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
|
@ -0,0 +1,4 @@
|
||||
package eu.toldi.infinityforlemmy.dto
|
||||
|
||||
data class UserBlockDTO(val person_id: Int, val block: Boolean, val auth: String)
|
||||
|
@ -54,6 +54,12 @@
|
||||
android:orderInCategory="10"
|
||||
android:title="@string/action_block_user" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_unblock_user_view_user_detail_activity"
|
||||
android:visible="false"
|
||||
android:orderInCategory="10"
|
||||
android:title="@string/action_unblock_user" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_edit_profile_view_user_detail_activity"
|
||||
android:orderInCategory="11"
|
||||
|
@ -1362,4 +1362,7 @@
|
||||
<string name="blocked_communities">Blocked Communities</string>
|
||||
<string name="blocked_users">Blocked users</string>
|
||||
<string name="blocks">Blocks</string>
|
||||
<string name="action_unblock_user">Unblock User</string>
|
||||
<string name="unblock_user_success">Unblocked</string>
|
||||
<string name="unblock_user_failed">Failed to unblock user</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user