diff --git a/app/src/main/java/awais/instagrabber/activities/SavedViewer.java b/app/src/main/java/awais/instagrabber/activities/SavedViewer.java index a02a1b72..a650a8d9 100755 --- a/app/src/main/java/awais/instagrabber/activities/SavedViewer.java +++ b/app/src/main/java/awais/instagrabber/activities/SavedViewer.java @@ -114,7 +114,7 @@ public final class SavedViewer extends BaseLanguageActivity implements SwipeRefr savedBinding.mainPosts.setVisibility(View.VISIBLE); }); - final PostModel model = result[result.length - 1]; + final PostModel model = result.length > 0 ? result[result.length - 1] : null; if (model != null) { endCursor = model.getEndCursor(); @@ -130,13 +130,12 @@ public final class SavedViewer extends BaseLanguageActivity implements SwipeRefr } model.setPageCursor(false, null); } - } else { - savedBinding.swipeRefreshLayout.setRefreshing(false); - // if (oldSize == 0) { - // Toast.makeText(getApplicationContext(), R.string.empty_list, Toast.LENGTH_SHORT).show(); - // finish(); - // } } + savedBinding.swipeRefreshLayout.setRefreshing(false); + // if (oldSize == 0) { + // Toast.makeText(getApplicationContext(), R.string.empty_list, Toast.LENGTH_SHORT).show(); + // finish(); + // } } }; diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java index 0eb70d79..e7765492 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java @@ -103,7 +103,7 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh NavHostFragment.findNavController(this).navigate(action); }); inboxList.setAdapter(inboxAdapter); - listViewModel = new ViewModelProvider(fragmentActivity).get(InboxThreadModelListViewModel.class); + listViewModel = new ViewModelProvider(this).get(InboxThreadModelListViewModel.class); listViewModel.getList().observe(fragmentActivity, inboxAdapter::submitList); initData(); return root; @@ -127,6 +127,12 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh } } + @Override + public void onDestroy() { + super.onDestroy(); + listViewModel.getList().postValue(Collections.emptyList()); + } + private void initData() { lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> { if (!Utils.isEmpty(endCursor)) 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 6908da18..cc075b85 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -59,7 +59,8 @@ import awais.instagrabber.models.ProfileModel; import awais.instagrabber.models.StoryModel; import awais.instagrabber.models.enums.DownloadMethod; import awais.instagrabber.models.enums.ItemGetType; -import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject; +import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse; +import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse; import awais.instagrabber.services.FriendshipService; import awais.instagrabber.services.ServiceCallback; import awais.instagrabber.utils.Constants; @@ -222,10 +223,10 @@ public class ProfileFragment extends Fragment { } setupPosts(); setupCommonListeners(); - fetchProfile(); + fetchUsername(); } - private void fetchProfile() { + private void fetchUsername() { final String uid = Utils.getUserIdFromCookie(cookie); if (username == null && uid != null) { final FetchListener fetchListener = username -> { @@ -339,36 +340,18 @@ public class ProfileFragment extends Fragment { binding.btnRestrict, ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_orange_background))); } - if (profileModel.isReallyPrivate()) { - binding.btnBlock.setVisibility(View.VISIBLE); - binding.btnTagged.setVisibility(View.GONE); - if (profileModel.getBlocked()) { - binding.btnBlock.setText(R.string.unblock); - ViewCompat.setBackgroundTintList( - binding.btnBlock, - ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background))); - } else { - binding.btnBlock.setText(R.string.block); - ViewCompat.setBackgroundTintList( - binding.btnBlock, - ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background))); - } + binding.btnBlock.setVisibility(View.VISIBLE); + binding.btnTagged.setVisibility(View.VISIBLE); + if (profileModel.getBlocked()) { + binding.btnBlock.setText(R.string.unblock); + ViewCompat.setBackgroundTintList( + binding.btnBlock, + ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background))); } else { - binding.btnBlock.setVisibility(View.GONE); - binding.btnSaved.setVisibility(View.VISIBLE); - binding.btnTagged.setVisibility(View.VISIBLE); - if (profileModel.getBlocked()) { - binding.btnSaved.setText(R.string.unblock); - ViewCompat.setBackgroundTintList( - binding.btnSaved, - ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_green_background))); - } else { - binding.btnSaved.setText(R.string.block); - ViewCompat.setBackgroundTintList( - binding.btnSaved, - ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background)) - ); - } + binding.btnBlock.setText(R.string.block); + ViewCompat.setBackgroundTintList( + binding.btnBlock, + ColorStateList.valueOf(ContextCompat.getColor(requireContext(), R.color.btn_red_background))); } } } else { @@ -511,10 +494,11 @@ public class ProfileFragment extends Fragment { userIdFromCookie, profileModel.getId(), Utils.getCsrfTokenFromCookie(cookie), - new ServiceCallback() { + new ServiceCallback() { @Override - public void onSuccess(final FriendshipRepositoryChangeResponseRootObject result) { + public void onSuccess(final FriendshipRepoChangeRootResponse result) { Log.d(TAG, "Unfollow success: " + result); + fetchProfileDetails(); } @Override @@ -527,10 +511,11 @@ public class ProfileFragment extends Fragment { userIdFromCookie, profileModel.getId(), Utils.getCsrfTokenFromCookie(cookie), - new ServiceCallback() { + new ServiceCallback() { @Override - public void onSuccess(final FriendshipRepositoryChangeResponseRootObject result) { + public void onSuccess(final FriendshipRepoChangeRootResponse result) { Log.d(TAG, "Follow success: " + result); + fetchProfileDetails(); } @Override @@ -543,12 +528,61 @@ public class ProfileFragment extends Fragment { binding.btnRestrict.setOnClickListener(v -> { if (!isLoggedIn) return; - // restrict - // new ProfileAction().execute("restrict"); + final String action = profileModel.getRestricted() ? "Unrestrict" : "Restrict"; + friendshipService.toggleRestrict( + profileModel.getId(), + !profileModel.getRestricted(), + Utils.getCsrfTokenFromCookie(cookie), + new ServiceCallback() { + @Override + public void onSuccess(final FriendshipRepoRestrictRootResponse result) { + Log.d(TAG, action + " success: " + result); + fetchProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error while performing " + action, t); + } + }); }); binding.btnBlock.setOnClickListener(v -> { if (!isLoggedIn) return; - // new MainHelper.ProfileAction().execute("block"); + if (profileModel.getBlocked()) { + friendshipService.unblock( + userIdFromCookie, + profileModel.getId(), + Utils.getCsrfTokenFromCookie(cookie), + new ServiceCallback() { + @Override + public void onSuccess(final FriendshipRepoChangeRootResponse result) { + Log.d(TAG, "Unblock success: " + result); + fetchProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error unblocking", t); + } + }); + return; + } + friendshipService.block( + userIdFromCookie, + profileModel.getId(), + Utils.getCsrfTokenFromCookie(cookie), + new ServiceCallback() { + @Override + public void onSuccess(final FriendshipRepoChangeRootResponse result) { + Log.d(TAG, "Block success: " + result); + fetchProfileDetails(); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error blocking", t); + } + }); }); binding.btnSaved.setOnClickListener(v -> startActivity(new Intent(requireContext(), SavedViewer.class) .putExtra(Constants.EXTRAS_INDEX, "$" + profileModel.getId()) diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/helpers/AutoSummaryDropDownPreference.java b/app/src/main/java/awais/instagrabber/fragments/settings/helpers/AutoSummaryDropDownPreference.java deleted file mode 100644 index 1ab0142e..00000000 --- a/app/src/main/java/awais/instagrabber/fragments/settings/helpers/AutoSummaryDropDownPreference.java +++ /dev/null @@ -1,4 +0,0 @@ -package awais.instagrabber.fragments.settings.helpers; - -public class AutoSummaryDropDownPreference { -} diff --git a/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java b/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java index 96935721..699874d7 100644 --- a/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java +++ b/app/src/main/java/awais/instagrabber/repositories/FriendshipRepository.java @@ -2,10 +2,12 @@ package awais.instagrabber.repositories; import java.util.Map; -import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject; +import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse; +import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse; import retrofit2.Call; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; +import retrofit2.http.Header; import retrofit2.http.POST; import retrofit2.http.Path; @@ -13,7 +15,14 @@ public interface FriendshipRepository { @FormUrlEncoded @POST("/api/v1/friendships/{action}/{id}/") - Call change(@Path("action") String action, - @Path("id") String id, - @FieldMap Map form); + Call change(@Header("User-Agent") String userAgent, + @Path("action") String action, + @Path("id") String id, + @FieldMap Map form); + + @FormUrlEncoded + @POST("/api/v1/restrict_action/{action}/") + Call toggleRestrict(@Header("User-Agent") String userAgent, + @Path("action") String action, + @FieldMap Map form); } diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseFriendshipStatus.java b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeResponseFriendshipStatus.java similarity index 68% rename from app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseFriendshipStatus.java rename to app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeResponseFriendshipStatus.java index b34e914f..cfaa8d17 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseFriendshipStatus.java +++ b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeResponseFriendshipStatus.java @@ -1,6 +1,8 @@ package awais.instagrabber.repositories.responses; -public class FriendshipRepositoryChangeResponseFriendshipStatus { +import androidx.annotation.NonNull; + +public class FriendshipRepoChangeResponseFriendshipStatus { private boolean following; private boolean followedBy; private boolean blocking; @@ -10,14 +12,14 @@ public class FriendshipRepositoryChangeResponseFriendshipStatus { private boolean outgoingRequest; private boolean isBestie; - public FriendshipRepositoryChangeResponseFriendshipStatus(final boolean following, - final boolean followedBy, - final boolean blocking, - final boolean muting, - final boolean isPrivate, - final boolean incomingRequest, - final boolean outgoingRequest, - final boolean isBestie) { + public FriendshipRepoChangeResponseFriendshipStatus(final boolean following, + final boolean followedBy, + final boolean blocking, + final boolean muting, + final boolean isPrivate, + final boolean incomingRequest, + final boolean outgoingRequest, + final boolean isBestie) { this.following = following; this.followedBy = followedBy; this.blocking = blocking; @@ -60,6 +62,7 @@ public class FriendshipRepositoryChangeResponseFriendshipStatus { return isBestie; } + @NonNull @Override public String toString() { return "FriendshipRepositoryChangeResponseFriendshipStatus{" + diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseRootObject.java b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeRootResponse.java similarity index 57% rename from app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseRootObject.java rename to app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeRootResponse.java index ebfdc39e..0d0de816 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepositoryChangeResponseRootObject.java +++ b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoChangeRootResponse.java @@ -1,15 +1,16 @@ package awais.instagrabber.repositories.responses; -public class FriendshipRepositoryChangeResponseRootObject { - private FriendshipRepositoryChangeResponseFriendshipStatus friendshipStatus; +public class FriendshipRepoChangeRootResponse { + private FriendshipRepoChangeResponseFriendshipStatus friendshipStatus; private String status; - public FriendshipRepositoryChangeResponseRootObject(final FriendshipRepositoryChangeResponseFriendshipStatus friendshipStatus, final String status) { + public FriendshipRepoChangeRootResponse(final FriendshipRepoChangeResponseFriendshipStatus friendshipStatus, + final String status) { this.friendshipStatus = friendshipStatus; this.status = status; } - public FriendshipRepositoryChangeResponseFriendshipStatus getFriendshipStatus() { + public FriendshipRepoChangeResponseFriendshipStatus getFriendshipStatus() { return friendshipStatus; } diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseFriendshipStatus.java b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseFriendshipStatus.java new file mode 100644 index 00000000..2a84eed0 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseFriendshipStatus.java @@ -0,0 +1,40 @@ +package awais.instagrabber.repositories.responses; + +import androidx.annotation.NonNull; + +public class FriendshipRepoRestrictResponseFriendshipStatus extends FriendshipRepoChangeResponseFriendshipStatus { + private boolean isRestricted; + + public FriendshipRepoRestrictResponseFriendshipStatus(final boolean following, + final boolean followedBy, + final boolean blocking, + final boolean muting, + final boolean isPrivate, + final boolean incomingRequest, + final boolean outgoingRequest, + final boolean isBestie, + final boolean isRestricted) { + super(following, followedBy, blocking, muting, isPrivate, incomingRequest, outgoingRequest, isBestie); + this.isRestricted = isRestricted; + } + + public boolean isRestricted() { + return isRestricted; + } + + @NonNull + @Override + public String toString() { + return "FriendshipRepoRestrictResponseFriendshipStatus{" + + "following=" + isFollowing() + + ", followedBy=" + isFollowedBy() + + ", blocking=" + isBlocking() + + ", muting=" + isMuting() + + ", isPrivate=" + isPrivate() + + ", incomingRequest=" + isIncomingRequest() + + ", outgoingRequest=" + isOutgoingRequest() + + ", isBestie=" + isBestie() + + ", isRestricted=" + isRestricted() + + '}'; + } +} diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseUsersItem.java b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseUsersItem.java new file mode 100644 index 00000000..3bb65d17 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictResponseUsersItem.java @@ -0,0 +1,65 @@ +package awais.instagrabber.repositories.responses; + +import androidx.annotation.NonNull; + +public class FriendshipRepoRestrictResponseUsersItem { + private long pk; + private String username; + private String fullName; + private boolean isPrivate; + private String profilePicUrl; + private FriendshipRepoRestrictResponseFriendshipStatus friendshipStatus; + private boolean isVerified; + + public FriendshipRepoRestrictResponseUsersItem(final long pk, final String username, final String fullName, final boolean isPrivate, final String profilePicUrl, final FriendshipRepoRestrictResponseFriendshipStatus friendshipStatus, final boolean isVerified) { + this.pk = pk; + this.username = username; + this.fullName = fullName; + this.isPrivate = isPrivate; + this.profilePicUrl = profilePicUrl; + this.friendshipStatus = friendshipStatus; + this.isVerified = isVerified; + } + + public long getPk() { + return pk; + } + + public String getUsername() { + return username; + } + + public String getFullName() { + return fullName; + } + + public boolean isPrivate() { + return isPrivate; + } + + public String getProfilePicUrl() { + return profilePicUrl; + } + + public FriendshipRepoRestrictResponseFriendshipStatus getFriendshipStatus() { + return friendshipStatus; + } + + public boolean isVerified() { + return isVerified; + } + + @NonNull + @Override + public String toString() { + return "FriendshipRepoRestrictResponseUsersItem{" + + "pk=" + pk + + ", username='" + username + '\'' + + ", fullName='" + fullName + '\'' + + ", isPrivate=" + isPrivate + + ", profilePicUrl='" + profilePicUrl + '\'' + + ", friendshipStatus=" + friendshipStatus + + ", isVerified=" + isVerified + + '}'; + } +} diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictRootResponse.java b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictRootResponse.java new file mode 100644 index 00000000..51955f7a --- /dev/null +++ b/app/src/main/java/awais/instagrabber/repositories/responses/FriendshipRepoRestrictRootResponse.java @@ -0,0 +1,32 @@ +package awais.instagrabber.repositories.responses; + +import androidx.annotation.NonNull; + +import java.util.List; + +public class FriendshipRepoRestrictRootResponse { + private List users; + private String status; + + public FriendshipRepoRestrictRootResponse(final List users, final String status) { + this.users = users; + this.status = status; + } + + public List getUsers() { + return users; + } + + public String getStatus() { + return status; + } + + @NonNull + @Override + public String toString() { + return "FriendshipRepoRestrictRootResponse{" + + "users=" + users + + ", status='" + status + '\'' + + '}'; + } +} diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/LoginRequiredResponse.java b/app/src/main/java/awais/instagrabber/repositories/responses/LoginRequiredResponse.java new file mode 100644 index 00000000..b34e0d65 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/repositories/responses/LoginRequiredResponse.java @@ -0,0 +1,25 @@ +package awais.instagrabber.repositories.responses; + +public class LoginRequiredResponse { + private String message = "login_required"; + private int logoutReason; + private String status = "fail"; + + public LoginRequiredResponse(final String message, final int logoutReason, final String status) { + this.message = message; + this.logoutReason = logoutReason; + this.status = status; + } + + public String getMessage() { + return message; + } + + public int getLogoutReason() { + return logoutReason; + } + + public String getStatus() { + return status; + } +} diff --git a/app/src/main/java/awais/instagrabber/services/BaseService.java b/app/src/main/java/awais/instagrabber/services/BaseService.java index dd1a822e..99022370 100644 --- a/app/src/main/java/awais/instagrabber/services/BaseService.java +++ b/app/src/main/java/awais/instagrabber/services/BaseService.java @@ -1,5 +1,10 @@ package awais.instagrabber.services; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import awais.instagrabber.BuildConfig; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -11,15 +16,20 @@ public abstract class BaseService { Retrofit.Builder getRetrofitBuilder() { if (builder == null) { - final OkHttpClient client = new OkHttpClient.Builder() + final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .addInterceptor(new AddCookiesInterceptor()) - .followRedirects(false) - .followSslRedirects(false) - .build(); + .followRedirects(true) + .followSslRedirects(true); + if (BuildConfig.DEBUG) { + // clientBuilder.addInterceptor(new LoggingInterceptor()); + } + final Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .create(); builder = new Retrofit.Builder() .addConverterFactory(ScalarsConverterFactory.create()) - .addConverterFactory(GsonConverterFactory.create()) - .client(client); + .addConverterFactory(GsonConverterFactory.create(gson)) + .client(clientBuilder.build()); } return builder; } diff --git a/app/src/main/java/awais/instagrabber/services/FriendshipService.java b/app/src/main/java/awais/instagrabber/services/FriendshipService.java index 8eb8aaaa..d1fac3dd 100644 --- a/app/src/main/java/awais/instagrabber/services/FriendshipService.java +++ b/app/src/main/java/awais/instagrabber/services/FriendshipService.java @@ -7,7 +7,9 @@ import java.util.Map; import java.util.UUID; import awais.instagrabber.repositories.FriendshipRepository; -import awais.instagrabber.repositories.responses.FriendshipRepositoryChangeResponseRootObject; +import awais.instagrabber.repositories.responses.FriendshipRepoChangeRootResponse; +import awais.instagrabber.repositories.responses.FriendshipRepoRestrictRootResponse; +import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Utils; import retrofit2.Call; import retrofit2.Callback; @@ -38,40 +40,89 @@ public class FriendshipService extends BaseService { public void follow(final String userId, final String targetUserId, final String crsfToken, - final ServiceCallback callback) { + final ServiceCallback callback) { change("create", userId, targetUserId, crsfToken, callback); } public void unfollow(final String userId, final String targetUserId, final String crsfToken, - final ServiceCallback callback) { + final ServiceCallback callback) { change("destroy", userId, targetUserId, crsfToken, callback); } - private void change(final String action, - final String userId, + public void block(final String userId, + final String targetUserId, + final String crsfToken, + final ServiceCallback callback) { + change("block", userId, targetUserId, crsfToken, callback); + } + + public void unblock(final String userId, final String targetUserId, final String crsfToken, - final ServiceCallback callback) { - final Map form = new HashMap<>(5); - form.put("_csrftoken", crsfToken); - form.put("_uid", userId); + final ServiceCallback callback) { + change("unblock", userId, targetUserId, crsfToken, callback); + } + + public void restrict(final String targetUserId, + final String csrfToken, + final ServiceCallback callback) { + } + + public void toggleRestrict(final String targetUserId, + final boolean restrict, + final String csrfToken, + final ServiceCallback callback) { + final Map form = new HashMap<>(3); + form.put("_csrftoken", csrfToken); form.put("_uuid", UUID.randomUUID().toString()); - form.put("user_id", targetUserId); - final Map signedForm = Utils.sign(form); - final Call request = repository.change(action, targetUserId, signedForm); - request.enqueue(new Callback() { + form.put("target_user_id", targetUserId); + final String action = restrict ? "restrict" : "unrestrict"; + final Call request = repository.toggleRestrict(Constants.I_USER_AGENT, action, form); + request.enqueue(new Callback() { @Override - public void onResponse(@NonNull final Call call, - @NonNull final Response response) { + 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, + public void onFailure(@NonNull final Call call, + @NonNull final Throwable t) { + if (callback != null) { + callback.onFailure(t); + } + } + }); + } + + private void change(final String action, + final String userId, + final String targetUserId, + final String csrfToken, + final ServiceCallback callback) { + final Map form = new HashMap<>(5); + form.put("_csrftoken", csrfToken); + form.put("_uid", userId); + form.put("_uuid", UUID.randomUUID().toString()); + form.put("radio_type", "wifi-none"); + form.put("user_id", targetUserId); + final Map signedForm = Utils.sign(form); + final Call request = repository.change(Constants.I_USER_AGENT, action, targetUserId, signedForm); + 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); diff --git a/app/src/main/java/awais/instagrabber/services/LoggingInterceptor.java b/app/src/main/java/awais/instagrabber/services/LoggingInterceptor.java new file mode 100644 index 00000000..0f008a07 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/services/LoggingInterceptor.java @@ -0,0 +1,29 @@ +package awais.instagrabber.services; + +import android.util.Log; + +import androidx.annotation.NonNull; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +class LoggingInterceptor implements Interceptor { + private static final String TAG = "LoggingInterceptor"; + + @NonNull + @Override + public Response intercept(Interceptor.Chain chain) throws IOException { + Request request = chain.request(); + long t1 = System.nanoTime(); + Log.i(TAG, String.format("Sending request %s on %s%n%s", + request.url(), chain.connection(), request.headers())); + Response response = chain.proceed(request); + long t2 = System.nanoTime(); + Log.i(TAG, String.format("Received response for %s in %.1fms%n%s\nbody: %s", + response.request().url(), (t2 - t1) / 1e6d, response.headers(), response.body())); + return response; + } +}