mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 14:47:29 +00:00
simplify sharing media to dm
This commit is contained in:
parent
64f421ad4f
commit
079ce76bc3
@ -8,7 +8,7 @@ import awais.instagrabber.models.Resource
|
|||||||
import awais.instagrabber.models.Resource.Companion.error
|
import awais.instagrabber.models.Resource.Companion.error
|
||||||
import awais.instagrabber.models.Resource.Companion.loading
|
import awais.instagrabber.models.Resource.Companion.loading
|
||||||
import awais.instagrabber.models.Resource.Companion.success
|
import awais.instagrabber.models.Resource.Companion.success
|
||||||
import awais.instagrabber.repositories.requests.directmessages.ThreadIdOrUserIds.Companion.of
|
import awais.instagrabber.repositories.requests.directmessages.ThreadIdsOrUserIds
|
||||||
import awais.instagrabber.repositories.responses.User
|
import awais.instagrabber.repositories.responses.User
|
||||||
import awais.instagrabber.repositories.responses.directmessages.DirectThread
|
import awais.instagrabber.repositories.responses.directmessages.DirectThread
|
||||||
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient
|
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient
|
||||||
@ -67,62 +67,26 @@ object DirectMessagesManager {
|
|||||||
|
|
||||||
suspend fun createThread(userPk: Long): DirectThread = DirectMessagesService.createThread(csrfToken, viewerId, deviceUuid, listOf(userPk), null)
|
suspend fun createThread(userPk: Long): DirectThread = DirectMessagesService.createThread(csrfToken, viewerId, deviceUuid, listOf(userPk), null)
|
||||||
|
|
||||||
fun sendMedia(recipients: Set<RankedRecipient>, mediaId: String, scope: CoroutineScope) {
|
|
||||||
val resultsCount = intArrayOf(0)
|
|
||||||
val callback: () -> Unit = {
|
|
||||||
resultsCount[0]++
|
|
||||||
if (resultsCount[0] == recipients.size) {
|
|
||||||
inboxManager.refresh(scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (recipient in recipients) {
|
|
||||||
sendMedia(recipient, mediaId, false, callback, scope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sendMedia(recipient: RankedRecipient, mediaId: String, scope: CoroutineScope) {
|
fun sendMedia(recipient: RankedRecipient, mediaId: String, scope: CoroutineScope) {
|
||||||
sendMedia(recipient, mediaId, true, null, scope)
|
sendMedia(setOf(recipient), mediaId, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendMedia(
|
fun sendMedia(
|
||||||
recipient: RankedRecipient,
|
recipients: Set<RankedRecipient>,
|
||||||
mediaId: String,
|
mediaId: String,
|
||||||
refreshInbox: Boolean,
|
|
||||||
callback: (() -> Unit)?,
|
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
) {
|
) {
|
||||||
if (recipient.thread == null && recipient.user != null) {
|
val threadIds = recipients.mapNotNull{ it.thread?.threadId }
|
||||||
// create thread and forward
|
val userIdsTemp = recipients.mapNotNull{ it.user?.pk }
|
||||||
scope.launch(Dispatchers.IO) {
|
val userIds = userIdsTemp.map{ listOf(it.toString(10)) }
|
||||||
try {
|
sendMedia(threadIds, userIds, mediaId, scope) {
|
||||||
val (threadId) = createThread(recipient.user.pk)
|
|
||||||
val threadIdTemp = threadId ?: return@launch
|
|
||||||
sendMedia(threadIdTemp, mediaId, scope) {
|
|
||||||
if (refreshInbox) {
|
|
||||||
inboxManager.refresh(scope)
|
inboxManager.refresh(scope)
|
||||||
}
|
}
|
||||||
callback?.invoke()
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "sendMedia: ", e)
|
|
||||||
callback?.invoke()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (recipient.thread == null) return
|
|
||||||
// just forward
|
|
||||||
val thread = recipient.thread
|
|
||||||
val threadId = thread.threadId ?: return
|
|
||||||
sendMedia(threadId, mediaId, scope) {
|
|
||||||
if (refreshInbox) {
|
|
||||||
inboxManager.refresh(scope)
|
|
||||||
}
|
|
||||||
callback?.invoke()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendMedia(
|
private fun sendMedia(
|
||||||
threadId: String,
|
threadIds: List<String>,
|
||||||
|
userIds: List<List<String>>,
|
||||||
mediaId: String,
|
mediaId: String,
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
callback: (() -> Unit)?,
|
callback: (() -> Unit)?,
|
||||||
@ -136,7 +100,7 @@ object DirectMessagesManager {
|
|||||||
viewerId,
|
viewerId,
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
UUID.randomUUID().toString(),
|
UUID.randomUUID().toString(),
|
||||||
of(threadId),
|
ThreadIdsOrUserIds(threadIds, userIds),
|
||||||
mediaId
|
mediaId
|
||||||
)
|
)
|
||||||
data.postValue(success(Any()))
|
data.postValue(success(Any()))
|
||||||
|
Loading…
Reference in New Issue
Block a user