From d551969da3764f4f0d2b6a3564099bf6d6fb3069 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 11 Mar 2021 14:30:33 -0500 Subject: [PATCH] close #660 --- .../fragments/main/ProfileFragment.java | 88 ++++++++++++++++--- .../repositories/FriendshipRepository.java | 5 ++ .../webservices/FriendshipService.java | 32 +++++++ .../webservices/StoriesService.java | 1 + app/src/main/res/menu/profile_menu.xml | 14 +++ app/src/main/res/values/strings.xml | 4 + 6 files changed, 130 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index a90bac90..4cb6cc64 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -122,9 +122,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private boolean hasStories = false; private HighlightsAdapter highlightsAdapter; private HighlightsViewModel highlightsViewModel; - private MenuItem blockMenuItem; - private MenuItem restrictMenuItem; - private MenuItem chainingMenuItem; + private MenuItem blockMenuItem, restrictMenuItem, chainingMenuItem; + private MenuItem muteStoriesMenuItem, mutePostsMenuItem; private boolean highlightsFetching; private boolean postsSetupDone = false; private Set selectedFeedModels; @@ -381,6 +380,25 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe restrictMenuItem.setVisible(false); } } + muteStoriesMenuItem = menu.findItem(R.id.mute_stories); + if (muteStoriesMenuItem != null) { + if (profileModel != null) { + muteStoriesMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + muteStoriesMenuItem.setTitle(profileModel.getFriendshipStatus().isMutingReel() ? R.string.mute_stories : R.string.unmute_stories); + } else { + muteStoriesMenuItem.setVisible(false); + } + } + mutePostsMenuItem = menu.findItem(R.id.mute_posts); + if (mutePostsMenuItem != null) { + if (profileModel != null) { + mutePostsMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + mutePostsMenuItem.setTitle(profileModel.getFriendshipStatus().isMuting() ? R.string.mute_posts : R.string.unmute_posts); + } + else { + mutePostsMenuItem.setVisible(false); + } + } chainingMenuItem = menu.findItem(R.id.chaining); if (chainingMenuItem != null) { if (profileModel != null) { @@ -459,6 +477,48 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe NavHostFragment.findNavController(this).navigate(navDirections); return true; } + if (item.getItemId() == R.id.mute_stories) { + if (!isLoggedIn) return false; + final String action = profileModel.getFriendshipStatus().isMutingReel() ? "Unmute stories" : "Mute stories"; + friendshipService.changeMute( + profileModel.getFriendshipStatus().isMutingReel(), + profileModel.getPk(), + true, + new ServiceCallback() { + @Override + public void onSuccess(final FriendshipChangeResponse result) { + Log.d(TAG, action + " success: " + result); + fetchProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error while performing " + action, t); + } + }); + return true; + } + if (item.getItemId() == R.id.mute_posts) { + if (!isLoggedIn) return false; + final String action = profileModel.getFriendshipStatus().isMuting() ? "Unmute stories" : "Mute stories"; + friendshipService.changeMute( + profileModel.getFriendshipStatus().isMuting(), + profileModel.getPk(), + false, + new ServiceCallback() { + @Override + public void onSuccess(final FriendshipChangeResponse result) { + Log.d(TAG, action + " success: " + result); + fetchProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error while performing " + action, t); + } + }); + return true; + } return super.onOptionsItemSelected(item); } @@ -896,21 +956,21 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } if (restrictMenuItem != null) { restrictMenuItem.setVisible(true); - if (profileModel.getFriendshipStatus().isRestricted()) { - restrictMenuItem.setTitle(R.string.unrestrict); - } else { - restrictMenuItem.setTitle(R.string.restrict); - } + restrictMenuItem.setTitle(profileModel.getFriendshipStatus().isRestricted() ? R.string.unrestrict : R.string.restrict); } if (blockMenuItem != null) { blockMenuItem.setVisible(true); - if (profileModel.getFriendshipStatus().isBlocking()) { - blockMenuItem.setTitle(R.string.unblock); - } else { - blockMenuItem.setTitle(R.string.block); - } + blockMenuItem.setTitle(profileModel.getFriendshipStatus().isBlocking() ? R.string.unblock : R.string.block); } - if (chainingMenuItem != null && !Objects.equals(profileId, myId)) { + if (muteStoriesMenuItem != null) { + muteStoriesMenuItem.setVisible(true); + muteStoriesMenuItem.setTitle(profileModel.getFriendshipStatus().isMutingReel() ? R.string.unmute_stories : R.string.mute_stories); + } + if (mutePostsMenuItem != null) { + mutePostsMenuItem.setVisible(true); + mutePostsMenuItem.setTitle(profileModel.getFriendshipStatus().isMuting() ? R.string.unmute_posts : R.string.mute_posts); + } + if (chainingMenuItem != null) { chainingMenuItem.setVisible(true); } return; diff --git a/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java b/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java index 123a7a4d..b3da4772 100644 --- a/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java +++ b/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java @@ -29,4 +29,9 @@ public interface FriendshipRepository { Call getList(@Path("userId") long userId, @Path("type") String type, // following or followers @QueryMap(encoded = true) Map queryParams); + + @FormUrlEncoded + @POST("/api/v1/friendships/{action}/") + Call changeMute(@Path("action") String action, + @FieldMap Map form); } diff --git a/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java b/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java index ec69ff54..7c5da8cf 100644 --- a/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java +++ b/app/src/main/java/awais/instagrabber/webservices/FriendshipService.java @@ -158,6 +158,38 @@ public class FriendshipService extends BaseService { }); } + public void changeMute(final boolean unmute, + final long targetUserId, + final boolean story, // true for story, false for posts + final ServiceCallback callback) { + final Map form = new HashMap<>(4); + form.put("_csrftoken", csrfToken); + form.put("_uid", String.valueOf(userId)); + form.put("_uuid", deviceUuid); + form.put(story ? "target_reel_author_id" : "target_posts_author_id", String.valueOf(targetUserId)); + final Call request = repository.changeMute(unmute ? + "unmute_posts_or_story_from_follow" : + "mute_posts_or_story_from_follow", + form); + request.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, + @NonNull final Response response) { + if (callback != null) { + callback.onSuccess(response.body()); + } + } + + @Override + public void onFailure(@NonNull final Call call, + @NonNull final Throwable t) { + if (callback != null) { + callback.onFailure(t); + } + } + }); + } + public void getList(final boolean follower, final long targetUserId, final String maxId, diff --git a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java index 96e6866f..adfd27c6 100644 --- a/app/src/main/java/awais/instagrabber/webservices/StoriesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/StoriesService.java @@ -137,6 +137,7 @@ public class StoriesService extends BaseService { final JSONArray feedStoriesReel = new JSONObject(body).getJSONArray("tray"); for (int i = 0; i < feedStoriesReel.length(); ++i) { final JSONObject node = feedStoriesReel.getJSONObject(i); + if (node.optBoolean("hide_from_feed_unit")) continue; final JSONObject userJson = node.getJSONObject(node.has("user") ? "user" : "owner"); try { final User user = new User(userJson.getLong("pk"), diff --git a/app/src/main/res/menu/profile_menu.xml b/app/src/main/res/menu/profile_menu.xml index cb30d82c..d8950667 100644 --- a/app/src/main/res/menu/profile_menu.xml +++ b/app/src/main/res/menu/profile_menu.xml @@ -26,4 +26,18 @@ android:title="@string/action_ayml" android:visible="false" app:showAsAction="never" /> + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3ae6ec58..7e814f5e 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,6 +123,10 @@ Unblock Restrict Unrestrict + Mute stories + Mute posts + Unmute stories + Unmute posts Copy bio Translate bio Mutual