From d7462109dbd07df0c2e8524d6a442a5b54a3f4b7 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 23 May 2021 16:48:27 +0900 Subject: [PATCH] Convert to kotlin --- .../fragments/StoryViewerFragment.java | 52 ++++---- .../DirectMessageThreadFragment.java | 6 +- .../managers/DirectMessagesManager.java | 4 +- .../instagrabber/managers/ThreadManager.java | 30 +++-- .../AnimatedMediaBroadcastOptions.kt | 43 +++---- .../directmessages/BroadcastOptions.kt | 112 +++++------------- .../directmessages/LinkBroadcastOptions.kt | 49 +++----- .../MediaShareBroadcastOptions.kt | 8 +- .../directmessages/PhotoBroadcastOptions.kt | 44 +++---- .../ReactionBroadcastOptions.kt | 51 +++----- .../StoryReplyBroadcastOptions.kt | 49 +++----- .../directmessages/TextBroadcastOptions.kt | 34 +++--- .../directmessages/VideoBroadcastOptions.kt | 51 ++++---- .../directmessages/VoiceBroadcastOptions.kt | 51 +++----- .../viewmodels/DirectThreadViewModel.java | 4 +- .../webservices/DirectMessagesService.java | 83 +++++++------ 16 files changed, 271 insertions(+), 400 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java index 5394c868..8e64b335 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java @@ -58,7 +58,6 @@ import com.google.android.exoplayer2.source.dash.DashMediaSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collections; @@ -86,7 +85,7 @@ import awais.instagrabber.models.stickers.SliderModel; import awais.instagrabber.models.stickers.SwipeUpModel; import awais.instagrabber.repositories.requests.StoryViewerOptions; import awais.instagrabber.repositories.requests.StoryViewerOptions.Type; -import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions; +import awais.instagrabber.repositories.requests.directmessages.ThreadIdOrUserIds; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.StoryStickerResponse; import awais.instagrabber.repositories.responses.directmessages.DirectThread; @@ -233,35 +232,32 @@ public class StoryViewerFragment extends Fragment { return; } final DirectThread thread = response.body(); - try { - final Call request = directMessagesService - .broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(thread.getThreadId()), - input.getText().toString(), - currentStory.getStoryMediaId(), - String.valueOf(currentStory.getUserId())); - request.enqueue(new Callback() { - @Override - public void onResponse(@NonNull final Call call, - @NonNull final Response response) { - if (!response.isSuccessful()) { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - return; - } - Toast.makeText(context, R.string.answered_story, Toast.LENGTH_SHORT).show(); + final Call request = directMessagesService.broadcastStoryReply( + ThreadIdOrUserIds.of(thread.getThreadId()), + input.getText().toString(), + currentStory.getStoryMediaId(), + String.valueOf(currentStory.getUserId()) + ); + request.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, + @NonNull final Response response) { + if (!response.isSuccessful()) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + return; } + Toast.makeText(context, R.string.answered_story, Toast.LENGTH_SHORT).show(); + } - @Override - public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { - try { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - Log.e(TAG, "onFailure: ", t); - } catch (Throwable ignored) { - } + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + try { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + Log.e(TAG, "onFailure: ", t); + } catch (Throwable ignored) { } - }); - } catch (UnsupportedEncodingException e) { - Log.e(TAG, "Error", e); - } + } + }); } @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 28068735..4815859d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -266,7 +266,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact @Override public void onReaction(final DirectItem item, final Emoji emoji) { - if (item == null) return; + if (item == null || emoji == null) return; final LiveData> resourceLiveData = viewModel.sendReaction(item, emoji); if (resourceLiveData != null) { resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData)); @@ -1487,7 +1487,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact if (reactionDialogFragment != null) { reactionDialogFragment.dismiss(); } - if (reaction == null) return; + if (itemId == null || reaction == null) return; if (reaction.getSenderId() == viewModel.getViewerId()) { final LiveData> resourceLiveData = viewModel.sendDeleteReaction(itemId); if (resourceLiveData != null) { @@ -1509,7 +1509,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact @Override public void onClick(final View view, final Emoji emoji) { - if (addReactionItem == null) return; + if (addReactionItem == null || emoji == null) return; final LiveData> resourceLiveData = viewModel.sendReaction(addReactionItem, emoji); if (resourceLiveData != null) { resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData)); diff --git a/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java b/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java index 10f700bf..412038b2 100644 --- a/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java +++ b/app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.java @@ -19,7 +19,7 @@ import java.util.UUID; import java.util.function.Function; import awais.instagrabber.models.Resource; -import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions; +import awais.instagrabber.repositories.requests.directmessages.ThreadIdOrUserIds; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectThread; @@ -220,7 +220,7 @@ public final class DirectMessagesManager { data.postValue(Resource.loading(null)); final Call request = service.broadcastMediaShare( UUID.randomUUID().toString(), - BroadcastOptions.ThreadIdOrUserIds.of(threadId), + ThreadIdOrUserIds.of(threadId), mediaId ); request.enqueue(new Callback() { diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java index f5089ce1..1a463d2d 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.java +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.java @@ -40,7 +40,7 @@ import awais.instagrabber.models.Resource.Status; import awais.instagrabber.models.UploadVideoOptions; import awais.instagrabber.models.enums.DirectItemType; import awais.instagrabber.repositories.requests.UploadFinishOptions; -import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions.ThreadIdOrUserIds; +import awais.instagrabber.repositories.requests.directmessages.ThreadIdOrUserIds; import awais.instagrabber.repositories.responses.FriendshipChangeResponse; import awais.instagrabber.repositories.responses.FriendshipRestrictResponse; import awais.instagrabber.repositories.responses.User; @@ -485,8 +485,8 @@ public final class ThreadManager { inboxManager.addItemsToThread(threadId, index, items); } - private void addReaction(final DirectItem item, final Emoji emoji) { - if (item == null || emoji == null || currentUser == null) return; + private void addReaction(@NonNull final DirectItem item, @NonNull final Emoji emoji) { + if (currentUser == null) return; final boolean isLike = emoji.getUnicode().equals("❤️"); DirectItemReactions reactions = item.getReactions(); if (reactions == null) { @@ -737,7 +737,8 @@ public final class ThreadManager { } @NonNull - public LiveData> sendReaction(final DirectItem item, final Emoji emoji) { + public LiveData> sendReaction(@NonNull final DirectItem item, + @NonNull final Emoji emoji) { final MutableLiveData> data = new MutableLiveData<>(); final Long userId = getCurrentUserId(data); if (userId == null) { @@ -752,13 +753,23 @@ public final class ThreadManager { if (!emoji.getUnicode().equals("❤️")) { emojiUnicode = emoji.getUnicode(); } + final String itemId = item.getItemId(); + if (itemId == null) { + data.postValue(Resource.error("itemId is null", null)); + return data; + } final Call request = service.broadcastReaction( - clientContext, threadIdOrUserIds, item.getItemId(), emojiUnicode, false); + clientContext, + threadIdOrUserIds, + itemId, + emojiUnicode, + false + ); handleBroadcastReactionRequest(data, item, request); return data; } - public LiveData> sendDeleteReaction(final String itemId) { + public LiveData> sendDeleteReaction(@NonNull final String itemId) { final MutableLiveData> data = new MutableLiveData<>(); final DirectItem item = getItem(itemId); if (item == null) { @@ -773,7 +784,12 @@ public final class ThreadManager { } removeReaction(item); final String clientContext = UUID.randomUUID().toString(); - final Call request = service.broadcastReaction(clientContext, threadIdOrUserIds, item.getItemId(), null, true); + final String itemId1 = item.getItemId(); + if (itemId1 == null) { + data.postValue(Resource.error("itemId is null", null)); + return data; + } + final Call request = service.broadcastReaction(clientContext, threadIdOrUserIds, itemId1, null, true); handleBroadcastReactionRequest(data, item, request); return data; } diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/AnimatedMediaBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/AnimatedMediaBroadcastOptions.kt index 8b71f194..0c30d278 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/AnimatedMediaBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/AnimatedMediaBroadcastOptions.kt @@ -1,27 +1,20 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import java.util.HashMap; -import java.util.Map; +import awais.instagrabber.models.enums.BroadcastItemType +import awais.instagrabber.repositories.responses.giphy.GiphyGif -import awais.instagrabber.models.enums.BroadcastItemType; -import awais.instagrabber.repositories.responses.giphy.GiphyGif; - -public class AnimatedMediaBroadcastOptions extends BroadcastOptions { - - private final GiphyGif giphyGif; - - public AnimatedMediaBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final GiphyGif giphyGif) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.ANIMATED_MEDIA); - this.giphyGif = giphyGif; - } - - @Override - public Map getFormMap() { - final Map form = new HashMap<>(); - form.put("is_sticker", String.valueOf(giphyGif.isSticker())); - form.put("id", giphyGif.getId()); - return form; - } -} +class AnimatedMediaBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val giphyGif: GiphyGif +) : BroadcastOptions( + clientContext, + threadIdOrUserIds, + BroadcastItemType.ANIMATED_MEDIA +) { + override val formMap: Map + get() = mapOf( + "is_sticker" to giphyGif.isSticker.toString(), + "id" to giphyGif.id + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/BroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/BroadcastOptions.kt index af4fa00f..8c772074 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/BroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/BroadcastOptions.kt @@ -1,86 +1,34 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import androidx.annotation.NonNull; +import awais.instagrabber.models.enums.BroadcastItemType -import java.util.List; -import java.util.Map; +sealed class BroadcastOptions( + val clientContext: String, + private val threadIdOrUserIds: ThreadIdOrUserIds, + val itemType: BroadcastItemType +) { + var repliedToItemId: String? = null + var repliedToClientContext: String? = null + val threadId: String? + get() = threadIdOrUserIds.threadId + val userIds: List? + get() = threadIdOrUserIds.userIds -import awais.instagrabber.models.enums.BroadcastItemType; - -public abstract class BroadcastOptions { - - private final String clientContext; - private final ThreadIdOrUserIds threadIdOrUserIds; - private final BroadcastItemType itemType; - - private String repliedToItemId; - private String repliedToClientContext; - - public BroadcastOptions(final String clientContext, - @NonNull final ThreadIdOrUserIds threadIdOrUserIds, - @NonNull final BroadcastItemType itemType) { - this.clientContext = clientContext; - this.threadIdOrUserIds = threadIdOrUserIds; - this.itemType = itemType; - } - - public String getClientContext() { - return clientContext; - } - - public String getThreadId() { - return threadIdOrUserIds.getThreadId(); - } - - public List getUserIds() { - return threadIdOrUserIds.getUserIds(); - } - - public BroadcastItemType getItemType() { - return itemType; - } - - public abstract Map getFormMap(); - - public String getRepliedToItemId() { - return repliedToItemId; - } - - public void setRepliedToItemId(final String repliedToItemId) { - this.repliedToItemId = repliedToItemId; - } - - public String getRepliedToClientContext() { - return repliedToClientContext; - } - - public void setRepliedToClientContext(final String repliedToClientContext) { - this.repliedToClientContext = repliedToClientContext; - } - - public static final class ThreadIdOrUserIds { - private final String threadId; - private final List userIds; - - private ThreadIdOrUserIds(final String threadId, final List userIds) { - this.threadId = threadId; - this.userIds = userIds; - } - - public static ThreadIdOrUserIds of(final String threadId) { - return new ThreadIdOrUserIds(threadId, null); - } - - public static ThreadIdOrUserIds of(final List userIds) { - return new ThreadIdOrUserIds(null, userIds); - } - - public String getThreadId() { - return threadId; - } - - public List getUserIds() { - return userIds; - } - } + abstract val formMap: Map } + +// TODO convert to data class once usages are migrated to kotlin +class ThreadIdOrUserIds(val threadId: String? = null, val userIds: List? = null) { + + companion object { + @JvmStatic + fun of(threadId: String?): ThreadIdOrUserIds { + return ThreadIdOrUserIds(threadId, null) + } + + @JvmStatic + fun of(userIds: List?): ThreadIdOrUserIds { + return ThreadIdOrUserIds(null, userIds) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/LinkBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/LinkBroadcastOptions.kt index d159e0e8..5cbf4155 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/LinkBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/LinkBroadcastOptions.kt @@ -1,32 +1,21 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import com.google.common.collect.ImmutableMap; +import awais.instagrabber.models.enums.BroadcastItemType +import org.json.JSONArray -import org.json.JSONArray; - -import java.util.List; -import java.util.Map; - -import awais.instagrabber.models.enums.BroadcastItemType; - -public class LinkBroadcastOptions extends BroadcastOptions { - private final String linkText; - private final List urls; - - public LinkBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String linkText, - final List urls) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.LINK); - this.linkText = linkText; - this.urls = urls; - } - - @Override - public Map getFormMap() { - return ImmutableMap.of( - "link_text", linkText, - "link_urls", new JSONArray(urls).toString() - ); - } -} +class LinkBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val linkText: String, + val urls: List +) : BroadcastOptions( + clientContext, + threadIdOrUserIds, + BroadcastItemType.LINK +) { + override val formMap: Map + get() = mapOf( + "link_text" to linkText, + "link_urls" to JSONArray(urls).toString() + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/MediaShareBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/MediaShareBroadcastOptions.kt index 70d34d6b..00f122b8 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/MediaShareBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/MediaShareBroadcastOptions.kt @@ -3,7 +3,7 @@ package awais.instagrabber.repositories.requests.directmessages import awais.instagrabber.models.enums.BroadcastItemType class MediaShareBroadcastOptions( - clientContext: String?, + clientContext: String, threadIdOrUserIds: ThreadIdOrUserIds, val mediaId: String ) : BroadcastOptions( @@ -11,8 +11,6 @@ class MediaShareBroadcastOptions( threadIdOrUserIds, BroadcastItemType.MEDIA_SHARE ) { - - override fun getFormMap(): Map { - return mapOf("media_id" to mediaId) - } + override val formMap: Map + get() = mapOf("media_id" to mediaId) } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/PhotoBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/PhotoBroadcastOptions.kt index 500de92c..b09b673d 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/PhotoBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/PhotoBroadcastOptions.kt @@ -1,28 +1,20 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import java.util.HashMap; -import java.util.Map; +import awais.instagrabber.models.enums.BroadcastItemType -import awais.instagrabber.models.enums.BroadcastItemType; - -public class PhotoBroadcastOptions extends BroadcastOptions { - final boolean allowFullAspectRatio; - final String uploadId; - - public PhotoBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final boolean allowFullAspectRatio, - final String uploadId) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.IMAGE); - this.allowFullAspectRatio = allowFullAspectRatio; - this.uploadId = uploadId; - } - - @Override - public Map getFormMap() { - final Map form = new HashMap<>(); - form.put("allow_full_aspect_ratio", String.valueOf(allowFullAspectRatio)); - form.put("upload_id", uploadId); - return form; - } -} +class PhotoBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val allowFullAspectRatio: Boolean, + val uploadId: String +) : BroadcastOptions( + clientContext, + threadIdOrUserIds, + BroadcastItemType.IMAGE +) { + override val formMap: Map + get() = mapOf( + "allow_full_aspect_ratio" to allowFullAspectRatio.toString(), + "upload_id" to uploadId + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/ReactionBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/ReactionBroadcastOptions.kt index ccfc9795..7aacdc5f 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/ReactionBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/ReactionBroadcastOptions.kt @@ -1,36 +1,19 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import java.util.HashMap; -import java.util.Map; +import awais.instagrabber.models.enums.BroadcastItemType -import awais.instagrabber.models.enums.BroadcastItemType; -import awais.instagrabber.utils.TextUtils; - -public class ReactionBroadcastOptions extends BroadcastOptions { - private final String itemId; - private final String emoji; - private final boolean delete; - - public ReactionBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String itemId, - final String emoji, - final boolean delete) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.REACTION); - this.itemId = itemId; - this.emoji = emoji; - this.delete = delete; - } - - @Override - public Map getFormMap() { - final Map form = new HashMap<>(); - form.put("item_id", itemId); - form.put("reaction_status", delete ? "deleted" : "created"); - form.put("reaction_type", "like"); - if (!TextUtils.isEmpty(emoji)) { - form.put("emoji", emoji); - } - return form; - } -} +class ReactionBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val itemId: String, + val emoji: String?, + val delete: Boolean +) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.REACTION) { + override val formMap: Map + get() = listOfNotNull( + "item_id" to itemId, + "reaction_status" to if (delete) "deleted" else "created", + "reaction_type" to "like", + if (!emoji.isNullOrBlank()) "emoji" to emoji else null, + ).toMap() +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/StoryReplyBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/StoryReplyBroadcastOptions.kt index 7fe3f8d0..2ac32eb2 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/StoryReplyBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/StoryReplyBroadcastOptions.kt @@ -1,34 +1,19 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import java.io.UnsupportedEncodingException; -import java.util.HashMap; -import java.util.Map; +import awais.instagrabber.models.enums.BroadcastItemType -import awais.instagrabber.models.enums.BroadcastItemType; -import awais.instagrabber.utils.TextUtils; - -public class StoryReplyBroadcastOptions extends BroadcastOptions { - private final String text, mediaId, reelId; - - public StoryReplyBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String text, - final String mediaId, - final String reelId) - throws UnsupportedEncodingException { - super(clientContext, threadIdOrUserIds, BroadcastItemType.REELSHARE); - this.text = text; - this.mediaId = mediaId; - this.reelId = reelId; // or user id, usually same - } - - @Override - public Map getFormMap() { - final Map form = new HashMap<>(); - form.put("text", text); - form.put("media_id", mediaId); - form.put("reel_id", reelId); - form.put("entry", "reel"); - return form; - } -} +class StoryReplyBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val text: String, + val mediaId: String, + val reelId: String // or user id, usually same +) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.REELSHARE) { + override val formMap: Map + get() = mapOf( + "text" to text, + "media_id" to mediaId, + "reel_id" to reelId, + "entry" to "reel", + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/TextBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/TextBroadcastOptions.kt index 56cc568d..2273b68f 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/TextBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/TextBroadcastOptions.kt @@ -1,22 +1,16 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import java.util.Collections; -import java.util.Map; +import awais.instagrabber.models.enums.BroadcastItemType -import awais.instagrabber.models.enums.BroadcastItemType; - -public class TextBroadcastOptions extends BroadcastOptions { - private final String text; - - public TextBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String text) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.TEXT); - this.text = text; - } - - @Override - public Map getFormMap() { - return Collections.singletonMap("text", text); - } -} +class TextBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val text: String +) : BroadcastOptions( + clientContext, + threadIdOrUserIds, + BroadcastItemType.TEXT +) { + override val formMap: Map + get() = mapOf("text" to text) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VideoBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VideoBroadcastOptions.kt index d594045d..ab614431 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VideoBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VideoBroadcastOptions.kt @@ -1,33 +1,22 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import com.google.common.collect.ImmutableMap; +import awais.instagrabber.models.enums.BroadcastItemType -import java.util.Map; - -import awais.instagrabber.models.enums.BroadcastItemType; - -public class VideoBroadcastOptions extends BroadcastOptions { - private final String videoResult; - private final String uploadId; - private final boolean sampled; - - public VideoBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String videoResult, - final String uploadId, - final boolean sampled) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.VIDEO); - this.videoResult = videoResult; - this.uploadId = uploadId; - this.sampled = sampled; - } - - @Override - public Map getFormMap() { - return ImmutableMap.of( - "video_result", videoResult, - "upload_id", uploadId, - "sampled", String.valueOf(sampled) - ); - } -} +class VideoBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val videoResult: String, + val uploadId: String, + val sampled: Boolean +) : BroadcastOptions( + clientContext, + threadIdOrUserIds, + BroadcastItemType.VIDEO +) { + override val formMap: Map + get() = mapOf( + "video_result" to videoResult, + "upload_id" to uploadId, + "sampled" to sampled.toString() + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VoiceBroadcastOptions.kt b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VoiceBroadcastOptions.kt index 97aa85ed..bc8e806f 100644 --- a/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VoiceBroadcastOptions.kt +++ b/app/src/main/java/awais/instagrabber/repositories/requests/directmessages/VoiceBroadcastOptions.kt @@ -1,36 +1,19 @@ -package awais.instagrabber.repositories.requests.directmessages; +package awais.instagrabber.repositories.requests.directmessages -import com.google.common.collect.ImmutableMap; +import awais.instagrabber.models.enums.BroadcastItemType +import org.json.JSONArray -import org.json.JSONArray; - -import java.util.List; -import java.util.Map; - -import awais.instagrabber.models.enums.BroadcastItemType; - -public class VoiceBroadcastOptions extends BroadcastOptions { - private final List waveform; - private final String uploadId; - private final int waveformSamplingFrequencyHz; - - public VoiceBroadcastOptions(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String uploadId, - final List waveform, - final int waveformSamplingFrequencyHz) { - super(clientContext, threadIdOrUserIds, BroadcastItemType.VOICE); - this.waveform = waveform; - this.uploadId = uploadId; - this.waveformSamplingFrequencyHz = waveformSamplingFrequencyHz; - } - - @Override - public Map getFormMap() { - return ImmutableMap.of( - "waveform", new JSONArray(waveform).toString(), - "upload_id", uploadId, - "waveform_sampling_frequency_hz", String.valueOf(waveformSamplingFrequencyHz) - ); - } -} +class VoiceBroadcastOptions( + clientContext: String, + threadIdOrUserIds: ThreadIdOrUserIds, + val uploadId: String, + val waveform: List, + val waveformSamplingFrequencyHz: Int +) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.VOICE) { + override val formMap: Map + get() = mapOf( + "waveform" to JSONArray(waveform).toString(), + "upload_id" to uploadId, + "waveform_sampling_frequency_hz" to waveformSamplingFrequencyHz.toString() + ) +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java index 3329d6c8..a74c68a0 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.java @@ -214,11 +214,11 @@ public class DirectThreadViewModel extends AndroidViewModel { voiceRecorder = null; } - public LiveData> sendReaction(final DirectItem item, final Emoji emoji) { + public LiveData> sendReaction(@NonNull final DirectItem item, @NonNull final Emoji emoji) { return threadManager.sendReaction(item, emoji); } - public LiveData> sendDeleteReaction(final String itemId) { + public LiveData> sendDeleteReaction(@NonNull final String itemId) { return threadManager.sendDeleteReaction(itemId); } diff --git a/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java b/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java index 68e58b99..c5ec9f8b 100644 --- a/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java +++ b/app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.java @@ -7,7 +7,6 @@ import com.google.common.collect.ImmutableMap; import org.json.JSONArray; -import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -19,13 +18,13 @@ import java.util.stream.Collectors; import awais.instagrabber.repositories.DirectMessagesRepository; import awais.instagrabber.repositories.requests.directmessages.AnimatedMediaBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions; -import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions.ThreadIdOrUserIds; import awais.instagrabber.repositories.requests.directmessages.LinkBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.MediaShareBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.PhotoBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.ReactionBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.StoryReplyBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.TextBroadcastOptions; +import awais.instagrabber.repositories.requests.directmessages.ThreadIdOrUserIds; import awais.instagrabber.repositories.requests.directmessages.VideoBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.VoiceBroadcastOptions; import awais.instagrabber.repositories.responses.directmessages.DirectBadgeCount; @@ -121,73 +120,77 @@ public class DirectMessagesService extends BaseService { return repository.fetchUnseenCount(); } - public Call broadcastText(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String text, - final String repliedToItemId, - final String repliedToClientContext) { + public Call broadcastText(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String text, + @Nullable final String repliedToItemId, + @Nullable final String repliedToClientContext) { final List urls = TextUtils.extractUrls(text); if (!urls.isEmpty()) { return broadcastLink(clientContext, threadIdOrUserIds, text, urls, repliedToItemId, repliedToClientContext); } final TextBroadcastOptions broadcastOptions = new TextBroadcastOptions(clientContext, threadIdOrUserIds, text); - broadcastOptions.setRepliedToItemId(repliedToItemId); - broadcastOptions.setRepliedToClientContext(repliedToClientContext); + if (repliedToItemId != null && repliedToClientContext != null) { + broadcastOptions.setRepliedToItemId(repliedToItemId); + broadcastOptions.setRepliedToClientContext(repliedToClientContext); + } return broadcast(broadcastOptions); } - public Call broadcastLink(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String linkText, - final List urls, - final String repliedToItemId, - final String repliedToClientContext) { + public Call broadcastLink(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String linkText, + @NonNull final List urls, + @Nullable final String repliedToItemId, + @Nullable final String repliedToClientContext) { final LinkBroadcastOptions broadcastOptions = new LinkBroadcastOptions(clientContext, threadIdOrUserIds, linkText, urls); - broadcastOptions.setRepliedToItemId(repliedToItemId); - broadcastOptions.setRepliedToClientContext(repliedToClientContext); + if (repliedToItemId != null && repliedToClientContext != null) { + broadcastOptions.setRepliedToItemId(repliedToItemId); + broadcastOptions.setRepliedToClientContext(repliedToClientContext); + } return broadcast(broadcastOptions); } - public Call broadcastPhoto(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String uploadId) { + public Call broadcastPhoto(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String uploadId) { return broadcast(new PhotoBroadcastOptions(clientContext, threadIdOrUserIds, true, uploadId)); } - public Call broadcastVideo(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String uploadId, - final String videoResult, + public Call broadcastVideo(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String uploadId, + @NonNull final String videoResult, final boolean sampled) { return broadcast(new VideoBroadcastOptions(clientContext, threadIdOrUserIds, videoResult, uploadId, sampled)); } - public Call broadcastVoice(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String uploadId, - final List waveform, + public Call broadcastVoice(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String uploadId, + @NonNull final List waveform, final int samplingFreq) { return broadcast(new VoiceBroadcastOptions(clientContext, threadIdOrUserIds, uploadId, waveform, samplingFreq)); } - public Call broadcastStoryReply(final ThreadIdOrUserIds threadIdOrUserIds, - final String text, - final String mediaId, - final String reelId) throws UnsupportedEncodingException { + public Call broadcastStoryReply(@NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String text, + @NonNull final String mediaId, + @NonNull final String reelId) { return broadcast(new StoryReplyBroadcastOptions(UUID.randomUUID().toString(), threadIdOrUserIds, text, mediaId, reelId)); } - public Call broadcastReaction(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final String itemId, - final String emoji, + public Call broadcastReaction(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final String itemId, + @Nullable final String emoji, final boolean delete) { return broadcast(new ReactionBroadcastOptions(clientContext, threadIdOrUserIds, itemId, emoji, delete)); } - public Call broadcastAnimatedMedia(final String clientContext, - final ThreadIdOrUserIds threadIdOrUserIds, - final GiphyGif giphyGif) { + public Call broadcastAnimatedMedia(@NonNull final String clientContext, + @NonNull final ThreadIdOrUserIds threadIdOrUserIds, + @NonNull final GiphyGif giphyGif) { return broadcast(new AnimatedMediaBroadcastOptions(clientContext, threadIdOrUserIds, giphyGif)); } @@ -456,8 +459,10 @@ public class DirectMessagesService extends BaseService { return repository.declineRequest(threadId, form); } + @Nullable public Call markAsSeen(@NonNull final String threadId, @NonNull final DirectItem directItem) { + if (directItem.getItemId() == null) return null; final ImmutableMap form = ImmutableMap.builder() .put("_csrftoken", csrfToken) .put("_uuid", deviceUuid)