enforce _uuid consistency

This commit is contained in:
Austin Huang 2021-01-11 15:08:23 -05:00
parent 2e58bab635
commit c9f891e4e1
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
12 changed files with 129 additions and 133 deletions

View File

@ -29,8 +29,9 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService {
final List<Media> feedModels = new ArrayList<>(); final List<Media> feedModels = new ArrayList<>();
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID);
feedModels.clear(); feedModels.clear();
feedService.fetch(csrfToken, nextCursor, new ServiceCallback<PostsFetchResponse>() { feedService.fetch(csrfToken, deviceUuid, nextCursor, new ServiceCallback<PostsFetchResponse>() {
@Override @Override
public void onSuccess(final PostsFetchResponse result) { public void onSuccess(final PostsFetchResponse result) {
if (result == null && feedModels.size() > 0) { if (result == null && feedModels.size() > 0) {

View File

@ -64,7 +64,7 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
private LinearLayoutManager layoutManager; private LinearLayoutManager layoutManager;
private RecyclerLazyLoader lazyLoader; private RecyclerLazyLoader lazyLoader;
private String shortCode; private String shortCode;
private long userId; private long authorUserId, userIdFromCookie;
private String endCursor = null; private String endCursor = null;
private Resources resources; private Resources resources;
private InputMethodManager imm; private InputMethodManager imm;
@ -140,14 +140,13 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
Toast.makeText(context, R.string.comment_send_empty_comment, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.comment_send_empty_comment, Toast.LENGTH_SHORT).show();
return; return;
} }
final long userId = CookieUtils.getUserIdFromCookie(cookie); if (userIdFromCookie == 0) return;
if (userId == 0) return;
String replyToId = null; String replyToId = null;
final CommentModel commentModel = commentsAdapter.getSelected(); final CommentModel commentModel = commentsAdapter.getSelected();
if (commentModel != null) { if (commentModel != null) {
replyToId = commentModel.getId(); replyToId = commentModel.getId();
} }
mediaService.comment(postId, text.toString(), userId, replyToId, CookieUtils.getCsrfTokenFromCookie(cookie), new ServiceCallback<Boolean>() { mediaService.comment(postId, text.toString(), replyToId, new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
commentsAdapter.clearSelection(); commentsAdapter.clearSelection();
@ -171,7 +170,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
fragmentActivity = (AppCompatActivity) getActivity(); fragmentActivity = (AppCompatActivity) getActivity();
mediaService = MediaService.getInstance(); final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
userIdFromCookie = CookieUtils.getUserIdFromCookie(cookie);
mediaService = MediaService.getInstance(deviceUuid, csrfToken, userIdFromCookie);
// setHasOptionsMenu(true); // setHasOptionsMenu(true);
} }
@ -231,7 +233,7 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
final CommentsViewerFragmentArgs fragmentArgs = CommentsViewerFragmentArgs.fromBundle(getArguments()); final CommentsViewerFragmentArgs fragmentArgs = CommentsViewerFragmentArgs.fromBundle(getArguments());
shortCode = fragmentArgs.getShortCode(); shortCode = fragmentArgs.getShortCode();
postId = fragmentArgs.getPostId(); postId = fragmentArgs.getPostId();
userId = fragmentArgs.getPostUserId(); authorUserId = fragmentArgs.getPostUserId();
// setTitle(); // setTitle();
binding.swipeRefreshLayout.setOnRefreshListener(this); binding.swipeRefreshLayout.setOnRefreshListener(this);
binding.swipeRefreshLayout.setRefreshing(true); binding.swipeRefreshLayout.setRefreshing(true);
@ -289,10 +291,9 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
String[] commentDialogList; String[] commentDialogList;
final long userIdFromCookie = CookieUtils.getUserIdFromCookie(cookie);
if (!TextUtils.isEmpty(cookie) if (!TextUtils.isEmpty(cookie)
&& userIdFromCookie != 0 && userIdFromCookie != 0
&& (userIdFromCookie == commentModel.getProfileModel().getPk() || userIdFromCookie == userId)) { && (userIdFromCookie == commentModel.getProfileModel().getPk() || userIdFromCookie == authorUserId)) {
commentDialogList = new String[]{ commentDialogList = new String[]{
resources.getString(R.string.open_profile), resources.getString(R.string.open_profile),
resources.getString(R.string.comment_viewer_copy_comment), resources.getString(R.string.comment_viewer_copy_comment),
@ -324,7 +325,6 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
if (context == null) return; if (context == null) return;
final DialogInterface.OnClickListener profileDialogListener = (dialog, which) -> { final DialogInterface.OnClickListener profileDialogListener = (dialog, which) -> {
final User profileModel = commentModel.getProfileModel(); final User profileModel = commentModel.getProfileModel();
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
switch (which) { switch (which) {
case 0: // open profile case 0: // open profile
openProfile("@" + profileModel.getUsername()); openProfile("@" + profileModel.getUsername());
@ -354,11 +354,8 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
}, 200); }, 200);
break; break;
case 4: // like/unlike comment case 4: // like/unlike comment
if (csrfToken == null) {
return;
}
if (!commentModel.getLiked()) { if (!commentModel.getLiked()) {
mediaService.commentLike(commentModel.getId(), csrfToken, new ServiceCallback<Boolean>() { mediaService.commentLike(commentModel.getId(), new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
if (!result) { if (!result) {
@ -376,7 +373,7 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
}); });
return; return;
} }
mediaService.commentUnlike(commentModel.getId(), csrfToken, new ServiceCallback<Boolean>() { mediaService.commentUnlike(commentModel.getId(), new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
if (!result) { if (!result) {
@ -416,10 +413,9 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment impl
}); });
break; break;
case 6: // delete comment case 6: // delete comment
final long userId = CookieUtils.getUserIdFromCookie(cookie); if (userIdFromCookie == 0) return;
if (userId == 0) return;
mediaService.deleteComment( mediaService.deleteComment(
postId, userId, commentModel.getId(), csrfToken, postId, commentModel.getId(),
new ServiceCallback<Boolean>() { new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {

View File

@ -118,7 +118,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
friendshipService = FriendshipService.getInstance(); friendshipService = FriendshipService.getInstance(null, null, 0);
fragmentActivity = (AppCompatActivity) getActivity(); fragmentActivity = (AppCompatActivity) getActivity();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -104,7 +104,7 @@ public final class LikesViewerFragment extends BottomSheetDialogFragment impleme
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
// final AppCompatActivity fragmentActivity = (AppCompatActivity) getActivity(); // final AppCompatActivity fragmentActivity = (AppCompatActivity) getActivity();
mediaService = isLoggedIn ? MediaService.getInstance() : null; mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null;
graphQLService = isLoggedIn ? null : GraphQLService.getInstance(); graphQLService = isLoggedIn ? null : GraphQLService.getInstance();
// setHasOptionsMenu(true); // setHasOptionsMenu(true);
} }

View File

@ -60,7 +60,6 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe
private NotificationViewModel notificationViewModel; private NotificationViewModel notificationViewModel;
private FriendshipService friendshipService; private FriendshipService friendshipService;
private MediaService mediaService; private MediaService mediaService;
private long userId;
private String csrfToken; private String csrfToken;
private String type; private String type;
private Context context; private Context context;
@ -133,7 +132,7 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe
break; break;
case 1: case 1:
if (model.getType() == NotificationType.REQUEST) { if (model.getType() == NotificationType.REQUEST) {
friendshipService.approve(userId, model.getUserId(), csrfToken, new ServiceCallback<FriendshipChangeResponse>() { friendshipService.approve(model.getUserId(), new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
onRefresh(); onRefresh();
@ -175,7 +174,7 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe
}); });
break; break;
case 2: case 2:
friendshipService.ignore(userId, model.getUserId(), csrfToken, new ServiceCallback<FriendshipChangeResponse>() { friendshipService.ignore(model.getUserId(), new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
onRefresh(); onRefresh();
@ -218,10 +217,11 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe
if (TextUtils.isEmpty(cookie)) { if (TextUtils.isEmpty(cookie)) {
Toast.makeText(context, R.string.activity_notloggedin, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.activity_notloggedin, Toast.LENGTH_SHORT).show();
} }
friendshipService = FriendshipService.getInstance(); mediaService = MediaService.getInstance(null, null, 0);
mediaService = MediaService.getInstance(); final long userId = CookieUtils.getUserIdFromCookie(cookie);
userId = CookieUtils.getUserIdFromCookie(cookie); final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, userId);
} }
@NonNull @NonNull

View File

@ -99,7 +99,6 @@ import awais.instagrabber.webservices.StoriesService;
import static androidx.core.content.PermissionChecker.checkSelfPermission; import static androidx.core.content.PermissionChecker.checkSelfPermission;
import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG; import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG;
import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION; import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION;
import static awais.instagrabber.utils.Utils.settingsHelper;
public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = "ProfileFragment"; private static final String TAG = "ProfileFragment";
@ -300,10 +299,15 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
cookie = Utils.settingsHelper.getString(Constants.COOKIE);
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
final long userId = CookieUtils.getUserIdFromCookie(cookie);
final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
fragmentActivity = (MainActivity) requireActivity(); fragmentActivity = (MainActivity) requireActivity();
friendshipService = FriendshipService.getInstance(); friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, userId);
storiesService = StoriesService.getInstance(); storiesService = StoriesService.getInstance();
mediaService = MediaService.getInstance(); mediaService = MediaService.getInstance(null, null, 0);
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext())); accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext()));
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext()));
setHasOptionsMenu(true); setHasOptionsMenu(true);
@ -313,8 +317,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
public View onCreateView(@NonNull final LayoutInflater inflater, public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, final ViewGroup container,
final Bundle savedInstanceState) { final Bundle savedInstanceState) {
cookie = settingsHelper.getString(Constants.COOKIE);
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
if (root != null) { if (root != null) {
if (getArguments() != null) { if (getArguments() != null) {
final ProfileFragmentArgs fragmentArgs = ProfileFragmentArgs.fromBundle(getArguments()); final ProfileFragmentArgs fragmentArgs = ProfileFragmentArgs.fromBundle(getArguments());
@ -380,7 +382,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
friendshipService.toggleRestrict( friendshipService.toggleRestrict(
profileModel.getPk(), profileModel.getPk(),
!profileModel.getFriendshipStatus().isRestricted(), !profileModel.getFriendshipStatus().isRestricted(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipRestrictResponse>() { new ServiceCallback<FriendshipRestrictResponse>() {
@Override @Override
public void onSuccess(final FriendshipRestrictResponse result) { public void onSuccess(final FriendshipRestrictResponse result) {
@ -396,13 +397,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
return true; return true;
} }
if (item.getItemId() == R.id.block) { if (item.getItemId() == R.id.block) {
final long userIdFromCookie = CookieUtils.getUserIdFromCookie(cookie);
if (!isLoggedIn) return false; if (!isLoggedIn) return false;
if (profileModel.getFriendshipStatus().isBlocking()) { if (profileModel.getFriendshipStatus().isBlocking()) {
friendshipService.unblock( friendshipService.unblock(
userIdFromCookie,
profileModel.getPk(), profileModel.getPk(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipChangeResponse>() { new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
@ -418,9 +416,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
return true; return true;
} }
friendshipService.block( friendshipService.block(
userIdFromCookie,
profileModel.getPk(), profileModel.getPk(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipChangeResponse>() { new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
@ -894,7 +890,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
private void setupCommonListeners() { private void setupCommonListeners() {
final Context context = getContext(); final Context context = getContext();
final long userIdFromCookie = CookieUtils.getUserIdFromCookie(cookie);
profileDetailsBinding.btnFollow.setOnClickListener(v -> { profileDetailsBinding.btnFollow.setOnClickListener(v -> {
if (profileModel.getFriendshipStatus().isFollowing() && profileModel.isPrivate()) { if (profileModel.getFriendshipStatus().isFollowing() && profileModel.isPrivate()) {
new AlertDialog.Builder(context) new AlertDialog.Builder(context)
@ -902,9 +897,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
.setMessage(R.string.priv_acc_confirm) .setMessage(R.string.priv_acc_confirm)
.setPositiveButton(R.string.confirm, (d, w) -> .setPositiveButton(R.string.confirm, (d, w) ->
friendshipService.unfollow( friendshipService.unfollow(
userIdFromCookie,
profileModel.getPk(), profileModel.getPk(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipChangeResponse>() { new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
@ -921,9 +914,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
.show(); .show();
} else if (profileModel.getFriendshipStatus().isFollowing() || profileModel.getFriendshipStatus().isOutgoingRequest()) { } else if (profileModel.getFriendshipStatus().isFollowing() || profileModel.getFriendshipStatus().isOutgoingRequest()) {
friendshipService.unfollow( friendshipService.unfollow(
userIdFromCookie,
profileModel.getPk(), profileModel.getPk(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipChangeResponse>() { new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
@ -938,9 +929,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
}); });
} else { } else {
friendshipService.follow( friendshipService.follow(
userIdFromCookie,
profileModel.getPk(), profileModel.getPk(),
CookieUtils.getCsrfTokenFromCookie(cookie),
new ServiceCallback<FriendshipChangeResponse>() { new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {

View File

@ -79,7 +79,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
throw new IllegalArgumentException("User is not logged in!"); throw new IllegalArgumentException("User is not logged in!");
} }
directMessagesService = DirectMessagesService.getInstance(csrfToken, userId, deviceUuid); directMessagesService = DirectMessagesService.getInstance(csrfToken, userId, deviceUuid);
friendshipService = FriendshipService.getInstance(); friendshipService = FriendshipService.getInstance(deviceUuid, csrfToken, userId);
resources = getApplication().getResources(); resources = getApplication().getResources();
} }
@ -264,7 +264,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
private LiveData<Resource<Object>> blockUser(final User user) { private LiveData<Resource<Object>> blockUser(final User user) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
friendshipService.block(userId, user.getPk(), csrfToken, new ServiceCallback<FriendshipChangeResponse>() { friendshipService.block(user.getPk(), new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
// refresh thread // refresh thread
@ -281,7 +281,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
private LiveData<Resource<Object>> unblockUser(final User user) { private LiveData<Resource<Object>> unblockUser(final User user) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
friendshipService.unblock(userId, user.getPk(), csrfToken, new ServiceCallback<FriendshipChangeResponse>() { friendshipService.unblock(user.getPk(), new ServiceCallback<FriendshipChangeResponse>() {
@Override @Override
public void onSuccess(final FriendshipChangeResponse result) { public void onSuccess(final FriendshipChangeResponse result) {
// refresh thread // refresh thread
@ -298,7 +298,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
private LiveData<Resource<Object>> restrictUser(final User user) { private LiveData<Resource<Object>> restrictUser(final User user) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
friendshipService.toggleRestrict(user.getPk(), true, csrfToken, new ServiceCallback<FriendshipRestrictResponse>() { friendshipService.toggleRestrict(user.getPk(), true, new ServiceCallback<FriendshipRestrictResponse>() {
@Override @Override
public void onSuccess(final FriendshipRestrictResponse result) { public void onSuccess(final FriendshipRestrictResponse result) {
// refresh thread // refresh thread
@ -315,7 +315,7 @@ public class DirectSettingsViewModel extends AndroidViewModel {
private LiveData<Resource<Object>> unRestrictUser(final User user) { private LiveData<Resource<Object>> unRestrictUser(final User user) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
friendshipService.toggleRestrict(user.getPk(), false, csrfToken, new ServiceCallback<FriendshipRestrictResponse>() { friendshipService.toggleRestrict(user.getPk(), false, new ServiceCallback<FriendshipRestrictResponse>() {
@Override @Override
public void onSuccess(final FriendshipRestrictResponse result) { public void onSuccess(final FriendshipRestrictResponse result) {
// refresh thread // refresh thread

View File

@ -99,7 +99,7 @@ public class DirectThreadViewModel extends AndroidViewModel {
throw new IllegalArgumentException("User is not logged in!"); throw new IllegalArgumentException("User is not logged in!");
} }
service = DirectMessagesService.getInstance(csrfToken, userId, deviceUuid); service = DirectMessagesService.getInstance(csrfToken, userId, deviceUuid);
mediaService = MediaService.getInstance(); mediaService = MediaService.getInstance(deviceUuid, csrfToken, userId);
contentResolver = application.getContentResolver(); contentResolver = application.getContentResolver();
recordingsDir = DirectoryUtils.getOutputMediaDirectory(application, "Recordings"); recordingsDir = DirectoryUtils.getOutputMediaDirectory(application, "Recordings");
this.application = application; this.application = application;
@ -395,7 +395,7 @@ public class DirectThreadViewModel extends AndroidViewModel {
.setUploadId(uploadDmVideoOptions.getUploadId()) .setUploadId(uploadDmVideoOptions.getUploadId())
.setSourceType("2") .setSourceType("2")
.setVideoOptions(new UploadFinishOptions.VideoOptions().setLength(duration / 1000f)); .setVideoOptions(new UploadFinishOptions.VideoOptions().setLength(duration / 1000f));
final Call<String> uploadFinishRequest = mediaService.uploadFinish(userId, csrfToken, uploadFinishOptions); final Call<String> uploadFinishRequest = mediaService.uploadFinish(uploadFinishOptions);
uploadFinishRequest.enqueue(new Callback<String>() { uploadFinishRequest.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) {
@ -512,7 +512,7 @@ public class DirectThreadViewModel extends AndroidViewModel {
final UploadFinishOptions uploadFinishOptions = new UploadFinishOptions() final UploadFinishOptions uploadFinishOptions = new UploadFinishOptions()
.setUploadId(uploadDmVoiceOptions.getUploadId()) .setUploadId(uploadDmVoiceOptions.getUploadId())
.setSourceType("4"); .setSourceType("4");
final Call<String> uploadFinishRequest = mediaService.uploadFinish(userId, csrfToken, uploadFinishOptions); final Call<String> uploadFinishRequest = mediaService.uploadFinish(uploadFinishOptions);
uploadFinishRequest.enqueue(new Callback<String>() { uploadFinishRequest.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) { public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) {

View File

@ -43,16 +43,16 @@ public class PostViewV2ViewModel extends ViewModel {
private final MutableLiveData<List<Integer>> options = new MutableLiveData<>(new ArrayList<>()); private final MutableLiveData<List<Integer>> options = new MutableLiveData<>(new ArrayList<>());
private final MediaService mediaService; private final MediaService mediaService;
private final long viewerId; private final long viewerId;
private final String csrfToken;
private final boolean isLoggedIn; private final boolean isLoggedIn;
private Media media; private Media media;
public PostViewV2ViewModel() { public PostViewV2ViewModel() {
mediaService = MediaService.getInstance();
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
viewerId = CookieUtils.getUserIdFromCookie(cookie); viewerId = CookieUtils.getUserIdFromCookie(cookie);
csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId);
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
} }
@ -142,14 +142,14 @@ public class PostViewV2ViewModel extends ViewModel {
public LiveData<Resource<Object>> like() { public LiveData<Resource<Object>> like() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
mediaService.like(media.getPk(), viewerId, csrfToken, getLikeUnlikeCallback(data)); mediaService.like(media.getPk(), getLikeUnlikeCallback(data));
return data; return data;
} }
public LiveData<Resource<Object>> unlike() { public LiveData<Resource<Object>> unlike() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
mediaService.unlike(media.getPk(), viewerId, csrfToken, getLikeUnlikeCallback(data)); mediaService.unlike(media.getPk(), getLikeUnlikeCallback(data));
return data; return data;
} }
@ -196,14 +196,14 @@ public class PostViewV2ViewModel extends ViewModel {
public LiveData<Resource<Object>> save() { public LiveData<Resource<Object>> save() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
mediaService.save(media.getPk(), viewerId, csrfToken, getSaveUnsaveCallback(data)); mediaService.save(media.getPk(), getSaveUnsaveCallback(data));
return data; return data;
} }
public LiveData<Resource<Object>> unsave() { public LiveData<Resource<Object>> unsave() {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
mediaService.unsave(media.getPk(), viewerId, csrfToken, getSaveUnsaveCallback(data)); mediaService.unsave(media.getPk(), getSaveUnsaveCallback(data));
return data; return data;
} }
@ -232,7 +232,7 @@ public class PostViewV2ViewModel extends ViewModel {
public LiveData<Resource<Object>> updateCaption(final String caption) { public LiveData<Resource<Object>> updateCaption(final String caption) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
mediaService.editCaption(media.getPk(), viewerId, caption, csrfToken, new ServiceCallback<Boolean>() { mediaService.editCaption(media.getPk(), caption, new ServiceCallback<Boolean>() {
@Override @Override
public void onSuccess(final Boolean result) { public void onSuccess(final Boolean result) {
if (result) { if (result) {

View File

@ -47,10 +47,11 @@ public class FeedService extends BaseService {
} }
public void fetch(final String csrfToken, public void fetch(final String csrfToken,
final String deviceUuid,
final String cursor, final String cursor,
final ServiceCallback<PostsFetchResponse> callback) { final ServiceCallback<PostsFetchResponse> callback) {
final Map<String, String> form = new HashMap<>(); final Map<String, String> form = new HashMap<>();
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("phone_id", UUID.randomUUID().toString()); form.put("phone_id", UUID.randomUUID().toString());
form.put("device_id", UUID.randomUUID().toString()); form.put("device_id", UUID.randomUUID().toString());

View File

@ -13,6 +13,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import awais.instagrabber.models.FollowModel; import awais.instagrabber.models.FollowModel;
@ -32,58 +33,71 @@ public class FriendshipService extends BaseService {
private static final String TAG = "FriendshipService"; private static final String TAG = "FriendshipService";
private final FriendshipRepository repository; private final FriendshipRepository repository;
private final String deviceUuid, csrfToken;
private final long userId;
private static FriendshipService instance; private static FriendshipService instance;
private FriendshipService() { private FriendshipService(final String deviceUuid,
final String csrfToken,
final long userId) {
this.deviceUuid = deviceUuid;
this.csrfToken = csrfToken;
this.userId = userId;
final Retrofit retrofit = getRetrofitBuilder() final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com") .baseUrl("https://i.instagram.com")
.build(); .build();
repository = retrofit.create(FriendshipRepository.class); repository = retrofit.create(FriendshipRepository.class);
} }
public static FriendshipService getInstance() { public String getCsrfToken() {
if (instance == null) { return csrfToken;
instance = new FriendshipService(); }
public String getDeviceUuid() {
return deviceUuid;
}
public long getUserId() {
return userId;
}
public static FriendshipService getInstance(final String deviceUuid, final String csrfToken, final long userId) {
if (instance == null
|| !Objects.equals(instance.getCsrfToken(), csrfToken)
|| !Objects.equals(instance.getDeviceUuid(), deviceUuid)
|| !Objects.equals(instance.getUserId(), userId)) {
instance = new FriendshipService(deviceUuid, csrfToken, userId);
} }
return instance; return instance;
} }
public void follow(final long userId, public void follow(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("create", userId, targetUserId, csrfToken, callback); change("create", targetUserId, callback);
} }
public void unfollow(final long userId, public void unfollow(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("destroy", userId, targetUserId, csrfToken, callback); change("destroy", targetUserId, callback);
} }
public void block(final long userId, public void block(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("block", userId, targetUserId, csrfToken, callback); change("block", targetUserId, callback);
} }
public void unblock(final long userId, public void unblock(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("unblock", userId, targetUserId, csrfToken, callback); change("unblock", targetUserId, callback);
} }
public void toggleRestrict(final long targetUserId, public void toggleRestrict(final long targetUserId,
final boolean restrict, final boolean restrict,
final String csrfToken,
final ServiceCallback<FriendshipRestrictResponse> callback) { final ServiceCallback<FriendshipRestrictResponse> callback) {
final Map<String, String> form = new HashMap<>(3); final Map<String, String> form = new HashMap<>(3);
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
form.put("target_user_id", String.valueOf(targetUserId)); form.put("target_user_id", String.valueOf(targetUserId));
final String action = restrict ? "restrict" : "unrestrict"; final String action = restrict ? "restrict" : "unrestrict";
final Call<FriendshipRestrictResponse> request = repository.toggleRestrict(Constants.I_USER_AGENT, action, form); final Call<FriendshipRestrictResponse> request = repository.toggleRestrict(Constants.I_USER_AGENT, action, form);
@ -106,29 +120,23 @@ public class FriendshipService extends BaseService {
}); });
} }
public void approve(final long userId, public void approve(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("approve", userId, targetUserId, csrfToken, callback); change("approve", targetUserId, callback);
} }
public void ignore(final long userId, public void ignore(final long targetUserId,
final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
change("ignore", userId, targetUserId, csrfToken, callback); change("ignore", targetUserId, callback);
} }
private void change(final String action, private void change(final String action,
final long userId,
final long targetUserId, final long targetUserId,
final String csrfToken,
final ServiceCallback<FriendshipChangeResponse> callback) { final ServiceCallback<FriendshipChangeResponse> callback) {
final Map<String, Object> form = new HashMap<>(5); final Map<String, Object> form = new HashMap<>(5);
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uid", userId); form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
form.put("radio_type", "wifi-none"); form.put("radio_type", "wifi-none");
form.put("user_id", targetUserId); form.put("user_id", targetUserId);
final Map<String, String> signedForm = Utils.sign(form); final Map<String, String> signedForm = Utils.sign(form);

View File

@ -14,6 +14,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import awais.instagrabber.repositories.MediaRepository; import awais.instagrabber.repositories.MediaRepository;
@ -34,19 +35,41 @@ public class MediaService extends BaseService {
private static final String TAG = "MediaService"; private static final String TAG = "MediaService";
private final MediaRepository repository; private final MediaRepository repository;
private final String deviceUuid, csrfToken;
private final long userId;
private static MediaService instance; private static MediaService instance;
private MediaService() { private MediaService(final String deviceUuid,
final String csrfToken,
final long userId) {
this.deviceUuid = deviceUuid;
this.csrfToken = csrfToken;
this.userId = userId;
final Retrofit retrofit = getRetrofitBuilder() final Retrofit retrofit = getRetrofitBuilder()
.baseUrl("https://i.instagram.com") .baseUrl("https://i.instagram.com")
.build(); .build();
repository = retrofit.create(MediaRepository.class); repository = retrofit.create(MediaRepository.class);
} }
public static MediaService getInstance() { public String getCsrfToken() {
if (instance == null) { return csrfToken;
instance = new MediaService(); }
public String getDeviceUuid() {
return deviceUuid;
}
public long getUserId() {
return userId;
}
public static MediaService getInstance(final String deviceUuid, final String csrfToken, final long userId) {
if (instance == null
|| !Objects.equals(instance.getCsrfToken(), csrfToken)
|| !Objects.equals(instance.getDeviceUuid(), deviceUuid)
|| !Objects.equals(instance.getUserId(), userId)) {
instance = new MediaService(deviceUuid, csrfToken, userId);
} }
return instance; return instance;
} }
@ -78,43 +101,33 @@ public class MediaService extends BaseService {
} }
public void like(final String mediaId, public void like(final String mediaId,
final long userId,
final String csrfToken,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
action(mediaId, userId, "like", csrfToken, callback); action(mediaId, "like", callback);
} }
public void unlike(final String mediaId, public void unlike(final String mediaId,
final long userId,
final String csrfToken,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
action(mediaId, userId, "unlike", csrfToken, callback); action(mediaId, "unlike", callback);
} }
public void save(final String mediaId, public void save(final String mediaId,
final long userId,
final String csrfToken,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
action(mediaId, userId, "save", csrfToken, callback); action(mediaId, "save", callback);
} }
public void unsave(final String mediaId, public void unsave(final String mediaId,
final long userId,
final String csrfToken,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
action(mediaId, userId, "unsave", csrfToken, callback); action(mediaId, "unsave", callback);
} }
private void action(final String mediaId, private void action(final String mediaId,
final long userId,
final String action, final String action,
final String csrfToken,
final ServiceCallback<Boolean> callback) { final ServiceCallback<Boolean> callback) {
final Map<String, Object> form = new HashMap<>(4); final Map<String, Object> form = new HashMap<>(4);
form.put("media_id", mediaId); form.put("media_id", mediaId);
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uid", userId); form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
// form.put("radio_type", "wifi-none"); // form.put("radio_type", "wifi-none");
final Map<String, String> signedForm = Utils.sign(form); final Map<String, String> signedForm = Utils.sign(form);
final Call<String> request = repository.action(action, mediaId, signedForm); final Call<String> request = repository.action(action, mediaId, signedForm);
@ -149,9 +162,7 @@ public class MediaService extends BaseService {
public void comment(@NonNull final String mediaId, public void comment(@NonNull final String mediaId,
@NonNull final String comment, @NonNull final String comment,
final long userId,
final String replyToCommentId, final String replyToCommentId,
final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
final String module = "self_comments_v2"; final String module = "self_comments_v2";
final Map<String, Object> form = new HashMap<>(); final Map<String, Object> form = new HashMap<>();
@ -159,7 +170,7 @@ public class MediaService extends BaseService {
form.put("idempotence_token", UUID.randomUUID().toString()); form.put("idempotence_token", UUID.randomUUID().toString());
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uid", userId); form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
form.put("comment_text", comment); form.put("comment_text", comment);
form.put("containermodule", module); form.put("containermodule", module);
if (!TextUtils.isEmpty(replyToCommentId)) { if (!TextUtils.isEmpty(replyToCommentId)) {
@ -194,23 +205,19 @@ public class MediaService extends BaseService {
} }
public void deleteComment(final String mediaId, public void deleteComment(final String mediaId,
final long userId,
final String commentId, final String commentId,
final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
deleteComments(mediaId, userId, Collections.singletonList(commentId), csrfToken, callback); deleteComments(mediaId, Collections.singletonList(commentId), callback);
} }
public void deleteComments(final String mediaId, public void deleteComments(final String mediaId,
final long userId,
final List<String> commentIds, final List<String> commentIds,
final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
final Map<String, Object> form = new HashMap<>(); final Map<String, Object> form = new HashMap<>();
form.put("comment_ids_to_delete", TextUtils.join(",", commentIds)); form.put("comment_ids_to_delete", TextUtils.join(",", commentIds));
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uid", userId); form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
final Map<String, String> signedForm = Utils.sign(form); final Map<String, String> signedForm = Utils.sign(form);
final Call<String> bulkDeleteRequest = repository.commentsBulkDelete(mediaId, signedForm); final Call<String> bulkDeleteRequest = repository.commentsBulkDelete(mediaId, signedForm);
bulkDeleteRequest.enqueue(new Callback<String>() { bulkDeleteRequest.enqueue(new Callback<String>() {
@ -241,12 +248,11 @@ public class MediaService extends BaseService {
} }
public void commentLike(@NonNull final String commentId, public void commentLike(@NonNull final String commentId,
@NonNull final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
final Map<String, Object> form = new HashMap<>(); final Map<String, Object> form = new HashMap<>();
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
// form.put("_uid", userId); // form.put("_uid", userId);
// form.put("_uuid", UUID.randomUUID().toString()); // form.put("_uuid", deviceUuid);
final Map<String, String> signedForm = Utils.sign(form); final Map<String, String> signedForm = Utils.sign(form);
final Call<String> commentLikeRequest = repository.commentLike(commentId, signedForm); final Call<String> commentLikeRequest = repository.commentLike(commentId, signedForm);
commentLikeRequest.enqueue(new Callback<String>() { commentLikeRequest.enqueue(new Callback<String>() {
@ -277,12 +283,11 @@ public class MediaService extends BaseService {
} }
public void commentUnlike(final String commentId, public void commentUnlike(final String commentId,
@NonNull final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
final Map<String, Object> form = new HashMap<>(); final Map<String, Object> form = new HashMap<>();
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
// form.put("_uid", userId); // form.put("_uid", userId);
// form.put("_uuid", UUID.randomUUID().toString()); // form.put("_uuid", deviceUuid);
final Map<String, String> signedForm = Utils.sign(form); final Map<String, String> signedForm = Utils.sign(form);
final Call<String> commentUnlikeRequest = repository.commentUnlike(commentId, signedForm); final Call<String> commentUnlikeRequest = repository.commentUnlike(commentId, signedForm);
commentUnlikeRequest.enqueue(new Callback<String>() { commentUnlikeRequest.enqueue(new Callback<String>() {
@ -313,14 +318,12 @@ public class MediaService extends BaseService {
} }
public void editCaption(final String postId, public void editCaption(final String postId,
final long userId,
final String newCaption, final String newCaption,
@NonNull final String csrfToken,
@NonNull final ServiceCallback<Boolean> callback) { @NonNull final ServiceCallback<Boolean> callback) {
final Map<String, Object> form = new HashMap<>(); final Map<String, Object> form = new HashMap<>();
form.put("_csrftoken", csrfToken); form.put("_csrftoken", csrfToken);
form.put("_uid", userId); form.put("_uid", userId);
form.put("_uuid", UUID.randomUUID().toString()); form.put("_uuid", deviceUuid);
form.put("igtv_feed_preview", "false"); form.put("igtv_feed_preview", "false");
form.put("media_id", postId); form.put("media_id", postId);
form.put("caption_text", newCaption); form.put("caption_text", newCaption);
@ -411,9 +414,7 @@ public class MediaService extends BaseService {
}); });
} }
public Call<String> uploadFinish(final long userId, public Call<String> uploadFinish(@NonNull final UploadFinishOptions options) {
@NonNull final String csrfToken,
@NonNull final UploadFinishOptions options) {
if (options.getVideoOptions() != null) { if (options.getVideoOptions() != null) {
final UploadFinishOptions.VideoOptions videoOptions = options.getVideoOptions(); final UploadFinishOptions.VideoOptions videoOptions = options.getVideoOptions();
if (videoOptions.getClips() == null) { if (videoOptions.getClips() == null) {
@ -430,7 +431,7 @@ public class MediaService extends BaseService {
.put("_csrftoken", csrfToken) .put("_csrftoken", csrfToken)
.put("source_type", options.getSourceType()) .put("source_type", options.getSourceType())
.put("_uid", String.valueOf(userId)) .put("_uid", String.valueOf(userId))
.put("_uuid", UUID.randomUUID().toString()) .put("_uuid", deviceUuid)
.put("upload_id", options.getUploadId()); .put("upload_id", options.getUploadId());
if (options.getVideoOptions() != null) { if (options.getVideoOptions() != null) {
formBuilder.putAll(options.getVideoOptions().getMap()); formBuilder.putAll(options.getVideoOptions().getMap());