1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Convert to kotlin

This commit is contained in:
Ammar Githam 2021-05-23 16:48:27 +09:00
parent 333e0faed5
commit d7462109db
16 changed files with 271 additions and 400 deletions

View File

@ -58,7 +58,6 @@ import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -86,7 +85,7 @@ import awais.instagrabber.models.stickers.SliderModel;
import awais.instagrabber.models.stickers.SwipeUpModel; import awais.instagrabber.models.stickers.SwipeUpModel;
import awais.instagrabber.repositories.requests.StoryViewerOptions; import awais.instagrabber.repositories.requests.StoryViewerOptions;
import awais.instagrabber.repositories.requests.StoryViewerOptions.Type; 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.Media;
import awais.instagrabber.repositories.responses.StoryStickerResponse; import awais.instagrabber.repositories.responses.StoryStickerResponse;
import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThread;
@ -233,12 +232,12 @@ public class StoryViewerFragment extends Fragment {
return; return;
} }
final DirectThread thread = response.body(); final DirectThread thread = response.body();
try { final Call<DirectThreadBroadcastResponse> request = directMessagesService.broadcastStoryReply(
final Call<DirectThreadBroadcastResponse> request = directMessagesService ThreadIdOrUserIds.of(thread.getThreadId()),
.broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(thread.getThreadId()),
input.getText().toString(), input.getText().toString(),
currentStory.getStoryMediaId(), currentStory.getStoryMediaId(),
String.valueOf(currentStory.getUserId())); String.valueOf(currentStory.getUserId())
);
request.enqueue(new Callback<DirectThreadBroadcastResponse>() { request.enqueue(new Callback<DirectThreadBroadcastResponse>() {
@Override @Override
public void onResponse(@NonNull final Call<DirectThreadBroadcastResponse> call, public void onResponse(@NonNull final Call<DirectThreadBroadcastResponse> call,
@ -259,9 +258,6 @@ public class StoryViewerFragment extends Fragment {
} }
} }
}); });
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Error", e);
}
} }
@Override @Override

View File

@ -266,7 +266,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
@Override @Override
public void onReaction(final DirectItem item, final Emoji emoji) { public void onReaction(final DirectItem item, final Emoji emoji) {
if (item == null) return; if (item == null || emoji == null) return;
final LiveData<Resource<Object>> resourceLiveData = viewModel.sendReaction(item, emoji); final LiveData<Resource<Object>> resourceLiveData = viewModel.sendReaction(item, emoji);
if (resourceLiveData != null) { if (resourceLiveData != null) {
resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData)); resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData));
@ -1487,7 +1487,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
if (reactionDialogFragment != null) { if (reactionDialogFragment != null) {
reactionDialogFragment.dismiss(); reactionDialogFragment.dismiss();
} }
if (reaction == null) return; if (itemId == null || reaction == null) return;
if (reaction.getSenderId() == viewModel.getViewerId()) { if (reaction.getSenderId() == viewModel.getViewerId()) {
final LiveData<Resource<Object>> resourceLiveData = viewModel.sendDeleteReaction(itemId); final LiveData<Resource<Object>> resourceLiveData = viewModel.sendDeleteReaction(itemId);
if (resourceLiveData != null) { if (resourceLiveData != null) {
@ -1509,7 +1509,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
@Override @Override
public void onClick(final View view, final Emoji emoji) { public void onClick(final View view, final Emoji emoji) {
if (addReactionItem == null) return; if (addReactionItem == null || emoji == null) return;
final LiveData<Resource<Object>> resourceLiveData = viewModel.sendReaction(addReactionItem, emoji); final LiveData<Resource<Object>> resourceLiveData = viewModel.sendReaction(addReactionItem, emoji);
if (resourceLiveData != null) { if (resourceLiveData != null) {
resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData)); resourceLiveData.observe(getViewLifecycleOwner(), directItemResource -> handleSentMessage(resourceLiveData));

View File

@ -19,7 +19,7 @@ import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
import awais.instagrabber.models.Resource; 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.User;
import awais.instagrabber.repositories.responses.directmessages.DirectItem; import awais.instagrabber.repositories.responses.directmessages.DirectItem;
import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThread;
@ -220,7 +220,7 @@ public final class DirectMessagesManager {
data.postValue(Resource.loading(null)); data.postValue(Resource.loading(null));
final Call<DirectThreadBroadcastResponse> request = service.broadcastMediaShare( final Call<DirectThreadBroadcastResponse> request = service.broadcastMediaShare(
UUID.randomUUID().toString(), UUID.randomUUID().toString(),
BroadcastOptions.ThreadIdOrUserIds.of(threadId), ThreadIdOrUserIds.of(threadId),
mediaId mediaId
); );
request.enqueue(new Callback<DirectThreadBroadcastResponse>() { request.enqueue(new Callback<DirectThreadBroadcastResponse>() {

View File

@ -40,7 +40,7 @@ import awais.instagrabber.models.Resource.Status;
import awais.instagrabber.models.UploadVideoOptions; import awais.instagrabber.models.UploadVideoOptions;
import awais.instagrabber.models.enums.DirectItemType; import awais.instagrabber.models.enums.DirectItemType;
import awais.instagrabber.repositories.requests.UploadFinishOptions; 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.FriendshipChangeResponse;
import awais.instagrabber.repositories.responses.FriendshipRestrictResponse; import awais.instagrabber.repositories.responses.FriendshipRestrictResponse;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
@ -485,8 +485,8 @@ public final class ThreadManager {
inboxManager.addItemsToThread(threadId, index, items); inboxManager.addItemsToThread(threadId, index, items);
} }
private void addReaction(final DirectItem item, final Emoji emoji) { private void addReaction(@NonNull final DirectItem item, @NonNull final Emoji emoji) {
if (item == null || emoji == null || currentUser == null) return; if (currentUser == null) return;
final boolean isLike = emoji.getUnicode().equals("❤️"); final boolean isLike = emoji.getUnicode().equals("❤️");
DirectItemReactions reactions = item.getReactions(); DirectItemReactions reactions = item.getReactions();
if (reactions == null) { if (reactions == null) {
@ -737,7 +737,8 @@ public final class ThreadManager {
} }
@NonNull @NonNull
public LiveData<Resource<Object>> sendReaction(final DirectItem item, final Emoji emoji) { public LiveData<Resource<Object>> sendReaction(@NonNull final DirectItem item,
@NonNull final Emoji emoji) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
final Long userId = getCurrentUserId(data); final Long userId = getCurrentUserId(data);
if (userId == null) { if (userId == null) {
@ -752,13 +753,23 @@ public final class ThreadManager {
if (!emoji.getUnicode().equals("❤️")) { if (!emoji.getUnicode().equals("❤️")) {
emojiUnicode = emoji.getUnicode(); emojiUnicode = emoji.getUnicode();
} }
final String itemId = item.getItemId();
if (itemId == null) {
data.postValue(Resource.error("itemId is null", null));
return data;
}
final Call<DirectThreadBroadcastResponse> request = service.broadcastReaction( final Call<DirectThreadBroadcastResponse> request = service.broadcastReaction(
clientContext, threadIdOrUserIds, item.getItemId(), emojiUnicode, false); clientContext,
threadIdOrUserIds,
itemId,
emojiUnicode,
false
);
handleBroadcastReactionRequest(data, item, request); handleBroadcastReactionRequest(data, item, request);
return data; return data;
} }
public LiveData<Resource<Object>> sendDeleteReaction(final String itemId) { public LiveData<Resource<Object>> sendDeleteReaction(@NonNull final String itemId) {
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
final DirectItem item = getItem(itemId); final DirectItem item = getItem(itemId);
if (item == null) { if (item == null) {
@ -773,7 +784,12 @@ public final class ThreadManager {
} }
removeReaction(item); removeReaction(item);
final String clientContext = UUID.randomUUID().toString(); final String clientContext = UUID.randomUUID().toString();
final Call<DirectThreadBroadcastResponse> 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<DirectThreadBroadcastResponse> request = service.broadcastReaction(clientContext, threadIdOrUserIds, itemId1, null, true);
handleBroadcastReactionRequest(data, item, request); handleBroadcastReactionRequest(data, item, request);
return data; return data;
} }

View File

@ -1,27 +1,20 @@
package awais.instagrabber.repositories.requests.directmessages; package awais.instagrabber.repositories.requests.directmessages
import java.util.HashMap; import awais.instagrabber.models.enums.BroadcastItemType
import java.util.Map; import awais.instagrabber.repositories.responses.giphy.GiphyGif
import awais.instagrabber.models.enums.BroadcastItemType; class AnimatedMediaBroadcastOptions(
import awais.instagrabber.repositories.responses.giphy.GiphyGif; clientContext: String,
threadIdOrUserIds: ThreadIdOrUserIds,
public class AnimatedMediaBroadcastOptions extends BroadcastOptions { val giphyGif: GiphyGif
) : BroadcastOptions(
private final GiphyGif giphyGif; clientContext,
threadIdOrUserIds,
public AnimatedMediaBroadcastOptions(final String clientContext, BroadcastItemType.ANIMATED_MEDIA
final ThreadIdOrUserIds threadIdOrUserIds, ) {
final GiphyGif giphyGif) { override val formMap: Map<String, String>
super(clientContext, threadIdOrUserIds, BroadcastItemType.ANIMATED_MEDIA); get() = mapOf(
this.giphyGif = giphyGif; "is_sticker" to giphyGif.isSticker.toString(),
} "id" to giphyGif.id
)
@Override
public Map<String, String> getFormMap() {
final Map<String, String> form = new HashMap<>();
form.put("is_sticker", String.valueOf(giphyGif.isSticker()));
form.put("id", giphyGif.getId());
return form;
}
} }

View File

@ -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; sealed class BroadcastOptions(
import java.util.Map; 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<String>?
get() = threadIdOrUserIds.userIds
import awais.instagrabber.models.enums.BroadcastItemType; abstract val formMap: Map<String, String>
}
public abstract class BroadcastOptions { // TODO convert to data class once usages are migrated to kotlin
class ThreadIdOrUserIds(val threadId: String? = null, val userIds: List<String>? = null) {
private final String clientContext; companion object {
private final ThreadIdOrUserIds threadIdOrUserIds; @JvmStatic
private final BroadcastItemType itemType; fun of(threadId: String?): ThreadIdOrUserIds {
return ThreadIdOrUserIds(threadId, null)
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() { @JvmStatic
return clientContext; fun of(userIds: List<String>?): ThreadIdOrUserIds {
} return ThreadIdOrUserIds(null, userIds)
public String getThreadId() {
return threadIdOrUserIds.getThreadId();
}
public List<String> getUserIds() {
return threadIdOrUserIds.getUserIds();
}
public BroadcastItemType getItemType() {
return itemType;
}
public abstract Map<String, String> 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<String> userIds;
private ThreadIdOrUserIds(final String threadId, final List<String> 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<String> userIds) {
return new ThreadIdOrUserIds(null, userIds);
}
public String getThreadId() {
return threadId;
}
public List<String> getUserIds() {
return userIds;
} }
} }
} }

View File

@ -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; class LinkBroadcastOptions(
clientContext: String,
import java.util.List; threadIdOrUserIds: ThreadIdOrUserIds,
import java.util.Map; val linkText: String,
val urls: List<String>
import awais.instagrabber.models.enums.BroadcastItemType; ) : BroadcastOptions(
clientContext,
public class LinkBroadcastOptions extends BroadcastOptions { threadIdOrUserIds,
private final String linkText; BroadcastItemType.LINK
private final List<String> urls; ) {
override val formMap: Map<String, String>
public LinkBroadcastOptions(final String clientContext, get() = mapOf(
final ThreadIdOrUserIds threadIdOrUserIds, "link_text" to linkText,
final String linkText, "link_urls" to JSONArray(urls).toString()
final List<String> urls) { )
super(clientContext, threadIdOrUserIds, BroadcastItemType.LINK);
this.linkText = linkText;
this.urls = urls;
}
@Override
public Map<String, String> getFormMap() {
return ImmutableMap.of(
"link_text", linkText,
"link_urls", new JSONArray(urls).toString()
);
}
} }

View File

@ -3,7 +3,7 @@ package awais.instagrabber.repositories.requests.directmessages
import awais.instagrabber.models.enums.BroadcastItemType import awais.instagrabber.models.enums.BroadcastItemType
class MediaShareBroadcastOptions( class MediaShareBroadcastOptions(
clientContext: String?, clientContext: String,
threadIdOrUserIds: ThreadIdOrUserIds, threadIdOrUserIds: ThreadIdOrUserIds,
val mediaId: String val mediaId: String
) : BroadcastOptions( ) : BroadcastOptions(
@ -11,8 +11,6 @@ class MediaShareBroadcastOptions(
threadIdOrUserIds, threadIdOrUserIds,
BroadcastItemType.MEDIA_SHARE BroadcastItemType.MEDIA_SHARE
) { ) {
override val formMap: Map<String, String>
override fun getFormMap(): Map<String, String> { get() = mapOf("media_id" to mediaId)
return mapOf("media_id" to mediaId)
}
} }

View File

@ -1,28 +1,20 @@
package awais.instagrabber.repositories.requests.directmessages; package awais.instagrabber.repositories.requests.directmessages
import java.util.HashMap; import awais.instagrabber.models.enums.BroadcastItemType
import java.util.Map;
import awais.instagrabber.models.enums.BroadcastItemType; class PhotoBroadcastOptions(
clientContext: String,
public class PhotoBroadcastOptions extends BroadcastOptions { threadIdOrUserIds: ThreadIdOrUserIds,
final boolean allowFullAspectRatio; val allowFullAspectRatio: Boolean,
final String uploadId; val uploadId: String
) : BroadcastOptions(
public PhotoBroadcastOptions(final String clientContext, clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, threadIdOrUserIds,
final boolean allowFullAspectRatio, BroadcastItemType.IMAGE
final String uploadId) { ) {
super(clientContext, threadIdOrUserIds, BroadcastItemType.IMAGE); override val formMap: Map<String, String>
this.allowFullAspectRatio = allowFullAspectRatio; get() = mapOf(
this.uploadId = uploadId; "allow_full_aspect_ratio" to allowFullAspectRatio.toString(),
} "upload_id" to uploadId
)
@Override
public Map<String, String> getFormMap() {
final Map<String, String> form = new HashMap<>();
form.put("allow_full_aspect_ratio", String.valueOf(allowFullAspectRatio));
form.put("upload_id", uploadId);
return form;
}
} }

View File

@ -1,36 +1,19 @@
package awais.instagrabber.repositories.requests.directmessages; package awais.instagrabber.repositories.requests.directmessages
import java.util.HashMap; import awais.instagrabber.models.enums.BroadcastItemType
import java.util.Map;
import awais.instagrabber.models.enums.BroadcastItemType; class ReactionBroadcastOptions(
import awais.instagrabber.utils.TextUtils; clientContext: String,
threadIdOrUserIds: ThreadIdOrUserIds,
public class ReactionBroadcastOptions extends BroadcastOptions { val itemId: String,
private final String itemId; val emoji: String?,
private final String emoji; val delete: Boolean
private final boolean delete; ) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.REACTION) {
override val formMap: Map<String, String>
public ReactionBroadcastOptions(final String clientContext, get() = listOfNotNull(
final ThreadIdOrUserIds threadIdOrUserIds, "item_id" to itemId,
final String itemId, "reaction_status" to if (delete) "deleted" else "created",
final String emoji, "reaction_type" to "like",
final boolean delete) { if (!emoji.isNullOrBlank()) "emoji" to emoji else null,
super(clientContext, threadIdOrUserIds, BroadcastItemType.REACTION); ).toMap()
this.itemId = itemId;
this.emoji = emoji;
this.delete = delete;
}
@Override
public Map<String, String> getFormMap() {
final Map<String, String> 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;
}
} }

View File

@ -1,34 +1,19 @@
package awais.instagrabber.repositories.requests.directmessages; package awais.instagrabber.repositories.requests.directmessages
import java.io.UnsupportedEncodingException; import awais.instagrabber.models.enums.BroadcastItemType
import java.util.HashMap;
import java.util.Map;
import awais.instagrabber.models.enums.BroadcastItemType; class StoryReplyBroadcastOptions(
import awais.instagrabber.utils.TextUtils; clientContext: String,
threadIdOrUserIds: ThreadIdOrUserIds,
public class StoryReplyBroadcastOptions extends BroadcastOptions { val text: String,
private final String text, mediaId, reelId; val mediaId: String,
val reelId: String // or user id, usually same
public StoryReplyBroadcastOptions(final String clientContext, ) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.REELSHARE) {
final ThreadIdOrUserIds threadIdOrUserIds, override val formMap: Map<String, String>
final String text, get() = mapOf(
final String mediaId, "text" to text,
final String reelId) "media_id" to mediaId,
throws UnsupportedEncodingException { "reel_id" to reelId,
super(clientContext, threadIdOrUserIds, BroadcastItemType.REELSHARE); "entry" to "reel",
this.text = text; )
this.mediaId = mediaId;
this.reelId = reelId; // or user id, usually same
}
@Override
public Map<String, String> getFormMap() {
final Map<String, String> form = new HashMap<>();
form.put("text", text);
form.put("media_id", mediaId);
form.put("reel_id", reelId);
form.put("entry", "reel");
return form;
}
} }

View File

@ -1,22 +1,16 @@
package awais.instagrabber.repositories.requests.directmessages; package awais.instagrabber.repositories.requests.directmessages
import java.util.Collections; import awais.instagrabber.models.enums.BroadcastItemType
import java.util.Map;
import awais.instagrabber.models.enums.BroadcastItemType; class TextBroadcastOptions(
clientContext: String,
public class TextBroadcastOptions extends BroadcastOptions { threadIdOrUserIds: ThreadIdOrUserIds,
private final String text; val text: String
) : BroadcastOptions(
public TextBroadcastOptions(final String clientContext, clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, threadIdOrUserIds,
final String text) { BroadcastItemType.TEXT
super(clientContext, threadIdOrUserIds, BroadcastItemType.TEXT); ) {
this.text = text; override val formMap: Map<String, String>
} get() = mapOf("text" to text)
@Override
public Map<String, String> getFormMap() {
return Collections.singletonMap("text", text);
}
} }

View File

@ -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; class VideoBroadcastOptions(
clientContext: String,
import awais.instagrabber.models.enums.BroadcastItemType; threadIdOrUserIds: ThreadIdOrUserIds,
val videoResult: String,
public class VideoBroadcastOptions extends BroadcastOptions { val uploadId: String,
private final String videoResult; val sampled: Boolean
private final String uploadId; ) : BroadcastOptions(
private final boolean sampled; clientContext,
threadIdOrUserIds,
public VideoBroadcastOptions(final String clientContext, BroadcastItemType.VIDEO
final ThreadIdOrUserIds threadIdOrUserIds, ) {
final String videoResult, override val formMap: Map<String, String>
final String uploadId, get() = mapOf(
final boolean sampled) { "video_result" to videoResult,
super(clientContext, threadIdOrUserIds, BroadcastItemType.VIDEO); "upload_id" to uploadId,
this.videoResult = videoResult; "sampled" to sampled.toString()
this.uploadId = uploadId; )
this.sampled = sampled;
}
@Override
public Map<String, String> getFormMap() {
return ImmutableMap.<String, String>of(
"video_result", videoResult,
"upload_id", uploadId,
"sampled", String.valueOf(sampled)
);
}
} }

View File

@ -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; class VoiceBroadcastOptions(
clientContext: String,
import java.util.List; threadIdOrUserIds: ThreadIdOrUserIds,
import java.util.Map; val uploadId: String,
val waveform: List<Float>,
import awais.instagrabber.models.enums.BroadcastItemType; val waveformSamplingFrequencyHz: Int
) : BroadcastOptions(clientContext, threadIdOrUserIds, BroadcastItemType.VOICE) {
public class VoiceBroadcastOptions extends BroadcastOptions { override val formMap: Map<String, String>
private final List<Float> waveform; get() = mapOf(
private final String uploadId; "waveform" to JSONArray(waveform).toString(),
private final int waveformSamplingFrequencyHz; "upload_id" to uploadId,
"waveform_sampling_frequency_hz" to waveformSamplingFrequencyHz.toString()
public VoiceBroadcastOptions(final String clientContext, )
final ThreadIdOrUserIds threadIdOrUserIds,
final String uploadId,
final List<Float> waveform,
final int waveformSamplingFrequencyHz) {
super(clientContext, threadIdOrUserIds, BroadcastItemType.VOICE);
this.waveform = waveform;
this.uploadId = uploadId;
this.waveformSamplingFrequencyHz = waveformSamplingFrequencyHz;
}
@Override
public Map<String, String> getFormMap() {
return ImmutableMap.<String, String>of(
"waveform", new JSONArray(waveform).toString(),
"upload_id", uploadId,
"waveform_sampling_frequency_hz", String.valueOf(waveformSamplingFrequencyHz)
);
}
} }

View File

@ -214,11 +214,11 @@ public class DirectThreadViewModel extends AndroidViewModel {
voiceRecorder = null; voiceRecorder = null;
} }
public LiveData<Resource<Object>> sendReaction(final DirectItem item, final Emoji emoji) { public LiveData<Resource<Object>> sendReaction(@NonNull final DirectItem item, @NonNull final Emoji emoji) {
return threadManager.sendReaction(item, emoji); return threadManager.sendReaction(item, emoji);
} }
public LiveData<Resource<Object>> sendDeleteReaction(final String itemId) { public LiveData<Resource<Object>> sendDeleteReaction(@NonNull final String itemId) {
return threadManager.sendDeleteReaction(itemId); return threadManager.sendDeleteReaction(itemId);
} }

View File

@ -7,7 +7,6 @@ import com.google.common.collect.ImmutableMap;
import org.json.JSONArray; import org.json.JSONArray;
import java.io.UnsupportedEncodingException;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -19,13 +18,13 @@ import java.util.stream.Collectors;
import awais.instagrabber.repositories.DirectMessagesRepository; import awais.instagrabber.repositories.DirectMessagesRepository;
import awais.instagrabber.repositories.requests.directmessages.AnimatedMediaBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.AnimatedMediaBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions; 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.LinkBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.MediaShareBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.MediaShareBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.PhotoBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.PhotoBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.ReactionBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.ReactionBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.StoryReplyBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.StoryReplyBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.TextBroadcastOptions; 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.VideoBroadcastOptions;
import awais.instagrabber.repositories.requests.directmessages.VoiceBroadcastOptions; import awais.instagrabber.repositories.requests.directmessages.VoiceBroadcastOptions;
import awais.instagrabber.repositories.responses.directmessages.DirectBadgeCount; import awais.instagrabber.repositories.responses.directmessages.DirectBadgeCount;
@ -121,73 +120,77 @@ public class DirectMessagesService extends BaseService {
return repository.fetchUnseenCount(); return repository.fetchUnseenCount();
} }
public Call<DirectThreadBroadcastResponse> broadcastText(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastText(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String text, @NonNull final String text,
final String repliedToItemId, @Nullable final String repliedToItemId,
final String repliedToClientContext) { @Nullable final String repliedToClientContext) {
final List<String> urls = TextUtils.extractUrls(text); final List<String> urls = TextUtils.extractUrls(text);
if (!urls.isEmpty()) { if (!urls.isEmpty()) {
return broadcastLink(clientContext, threadIdOrUserIds, text, urls, repliedToItemId, repliedToClientContext); return broadcastLink(clientContext, threadIdOrUserIds, text, urls, repliedToItemId, repliedToClientContext);
} }
final TextBroadcastOptions broadcastOptions = new TextBroadcastOptions(clientContext, threadIdOrUserIds, text); final TextBroadcastOptions broadcastOptions = new TextBroadcastOptions(clientContext, threadIdOrUserIds, text);
if (repliedToItemId != null && repliedToClientContext != null) {
broadcastOptions.setRepliedToItemId(repliedToItemId); broadcastOptions.setRepliedToItemId(repliedToItemId);
broadcastOptions.setRepliedToClientContext(repliedToClientContext); broadcastOptions.setRepliedToClientContext(repliedToClientContext);
}
return broadcast(broadcastOptions); return broadcast(broadcastOptions);
} }
public Call<DirectThreadBroadcastResponse> broadcastLink(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastLink(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String linkText, @NonNull final String linkText,
final List<String> urls, @NonNull final List<String> urls,
final String repliedToItemId, @Nullable final String repliedToItemId,
final String repliedToClientContext) { @Nullable final String repliedToClientContext) {
final LinkBroadcastOptions broadcastOptions = new LinkBroadcastOptions(clientContext, threadIdOrUserIds, linkText, urls); final LinkBroadcastOptions broadcastOptions = new LinkBroadcastOptions(clientContext, threadIdOrUserIds, linkText, urls);
if (repliedToItemId != null && repliedToClientContext != null) {
broadcastOptions.setRepliedToItemId(repliedToItemId); broadcastOptions.setRepliedToItemId(repliedToItemId);
broadcastOptions.setRepliedToClientContext(repliedToClientContext); broadcastOptions.setRepliedToClientContext(repliedToClientContext);
}
return broadcast(broadcastOptions); return broadcast(broadcastOptions);
} }
public Call<DirectThreadBroadcastResponse> broadcastPhoto(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastPhoto(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String uploadId) { @NonNull final String uploadId) {
return broadcast(new PhotoBroadcastOptions(clientContext, threadIdOrUserIds, true, uploadId)); return broadcast(new PhotoBroadcastOptions(clientContext, threadIdOrUserIds, true, uploadId));
} }
public Call<DirectThreadBroadcastResponse> broadcastVideo(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastVideo(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String uploadId, @NonNull final String uploadId,
final String videoResult, @NonNull final String videoResult,
final boolean sampled) { final boolean sampled) {
return broadcast(new VideoBroadcastOptions(clientContext, threadIdOrUserIds, videoResult, uploadId, sampled)); return broadcast(new VideoBroadcastOptions(clientContext, threadIdOrUserIds, videoResult, uploadId, sampled));
} }
public Call<DirectThreadBroadcastResponse> broadcastVoice(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastVoice(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String uploadId, @NonNull final String uploadId,
final List<Float> waveform, @NonNull final List<Float> waveform,
final int samplingFreq) { final int samplingFreq) {
return broadcast(new VoiceBroadcastOptions(clientContext, threadIdOrUserIds, uploadId, waveform, samplingFreq)); return broadcast(new VoiceBroadcastOptions(clientContext, threadIdOrUserIds, uploadId, waveform, samplingFreq));
} }
public Call<DirectThreadBroadcastResponse> broadcastStoryReply(final ThreadIdOrUserIds threadIdOrUserIds, public Call<DirectThreadBroadcastResponse> broadcastStoryReply(@NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String text, @NonNull final String text,
final String mediaId, @NonNull final String mediaId,
final String reelId) throws UnsupportedEncodingException { @NonNull final String reelId) {
return broadcast(new StoryReplyBroadcastOptions(UUID.randomUUID().toString(), threadIdOrUserIds, text, mediaId, reelId)); return broadcast(new StoryReplyBroadcastOptions(UUID.randomUUID().toString(), threadIdOrUserIds, text, mediaId, reelId));
} }
public Call<DirectThreadBroadcastResponse> broadcastReaction(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastReaction(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final String itemId, @NonNull final String itemId,
final String emoji, @Nullable final String emoji,
final boolean delete) { final boolean delete) {
return broadcast(new ReactionBroadcastOptions(clientContext, threadIdOrUserIds, itemId, emoji, delete)); return broadcast(new ReactionBroadcastOptions(clientContext, threadIdOrUserIds, itemId, emoji, delete));
} }
public Call<DirectThreadBroadcastResponse> broadcastAnimatedMedia(final String clientContext, public Call<DirectThreadBroadcastResponse> broadcastAnimatedMedia(@NonNull final String clientContext,
final ThreadIdOrUserIds threadIdOrUserIds, @NonNull final ThreadIdOrUserIds threadIdOrUserIds,
final GiphyGif giphyGif) { @NonNull final GiphyGif giphyGif) {
return broadcast(new AnimatedMediaBroadcastOptions(clientContext, threadIdOrUserIds, giphyGif)); return broadcast(new AnimatedMediaBroadcastOptions(clientContext, threadIdOrUserIds, giphyGif));
} }
@ -456,8 +459,10 @@ public class DirectMessagesService extends BaseService {
return repository.declineRequest(threadId, form); return repository.declineRequest(threadId, form);
} }
@Nullable
public Call<DirectItemSeenResponse> markAsSeen(@NonNull final String threadId, public Call<DirectItemSeenResponse> markAsSeen(@NonNull final String threadId,
@NonNull final DirectItem directItem) { @NonNull final DirectItem directItem) {
if (directItem.getItemId() == null) return null;
final ImmutableMap<String, String> form = ImmutableMap.<String, String>builder() final ImmutableMap<String, String> form = ImmutableMap.<String, String>builder()
.put("_csrftoken", csrfToken) .put("_csrftoken", csrfToken)
.put("_uuid", deviceUuid) .put("_uuid", deviceUuid)