mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-05-29 13:00:17 +02:00
feat(scripting): messaging module
This commit is contained in:
parent
2a8fcacd2f
commit
7aa05e996a
@ -108,8 +108,23 @@ enum class MediaReferenceType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum class MessageUpdate {
|
enum class MessageUpdate(
|
||||||
UNKNOWN, READ, RELEASE, SAVE, UNSAVE, ERASE, SCREENSHOT, SCREEN_RECORD, REPLAY, REACTION, REMOVEREACTION, REVOKETRANSCRIPTION, ALLOWTRANSCRIPTION, ERASESAVEDSTORYMEDIA
|
val key: String,
|
||||||
|
) {
|
||||||
|
UNKNOWN("unknown"),
|
||||||
|
READ("read"),
|
||||||
|
RELEASE("release"),
|
||||||
|
SAVE("save"),
|
||||||
|
UNSAVE("unsave"),
|
||||||
|
ERASE("erase"),
|
||||||
|
SCREENSHOT("screenshot"),
|
||||||
|
SCREEN_RECORD("screen_record"),
|
||||||
|
REPLAY("replay"),
|
||||||
|
REACTION("reaction"),
|
||||||
|
REMOVEREACTION("remove_reaction"),
|
||||||
|
REVOKETRANSCRIPTION("revoke_transcription"),
|
||||||
|
ALLOWTRANSCRIPTION("allow_transcription"),
|
||||||
|
ERASESAVEDSTORYMEDIA("erase_saved_story_media"),
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class FriendLinkType(val value: Int, val shortName: String) {
|
enum class FriendLinkType(val value: Int, val shortName: String) {
|
||||||
|
@ -421,7 +421,7 @@ class Notifications : Feature("Notifications", loadParams = FeatureLoadParams.IN
|
|||||||
|
|
||||||
context.coroutineScope.launch(coroutineDispatcher) {
|
context.coroutineScope.launch(coroutineDispatcher) {
|
||||||
suspendCoroutine { continuation ->
|
suspendCoroutine { continuation ->
|
||||||
conversationManager.fetchMessageByServerId(conversationId, serverMessageId, onSuccess = {
|
conversationManager.fetchMessageByServerId(conversationId, serverMessageId.toLong(), onSuccess = {
|
||||||
if (it.senderId.toString() == context.database.myUserId) {
|
if (it.senderId.toString() == context.database.myUserId) {
|
||||||
param.invokeOriginal()
|
param.invokeOriginal()
|
||||||
continuation.resumeWith(Result.success(Unit))
|
continuation.resumeWith(Result.success(Unit))
|
||||||
|
@ -65,7 +65,7 @@ class CoreMessagingBridge(
|
|||||||
suspendCancellableCoroutine { continuation ->
|
suspendCancellableCoroutine { continuation ->
|
||||||
conversationManager?.fetchMessageByServerId(
|
conversationManager?.fetchMessageByServerId(
|
||||||
conversationId,
|
conversationId,
|
||||||
serverMessageId,
|
serverMessageId.toLong(),
|
||||||
onSuccess = {
|
onSuccess = {
|
||||||
continuation.resumeWith(Result.success(it.toBridge()))
|
continuation.resumeWith(Result.success(it.toBridge()))
|
||||||
},
|
},
|
||||||
|
@ -7,6 +7,7 @@ import me.rhunk.snapenhance.common.scripting.ScriptRuntime
|
|||||||
import me.rhunk.snapenhance.common.scripting.bindings.BindingSide
|
import me.rhunk.snapenhance.common.scripting.bindings.BindingSide
|
||||||
import me.rhunk.snapenhance.core.ModContext
|
import me.rhunk.snapenhance.core.ModContext
|
||||||
import me.rhunk.snapenhance.core.scripting.impl.CoreIPC
|
import me.rhunk.snapenhance.core.scripting.impl.CoreIPC
|
||||||
|
import me.rhunk.snapenhance.core.scripting.impl.CoreMessaging
|
||||||
import me.rhunk.snapenhance.core.scripting.impl.CoreScriptConfig
|
import me.rhunk.snapenhance.core.scripting.impl.CoreScriptConfig
|
||||||
import me.rhunk.snapenhance.core.scripting.impl.CoreScriptHooker
|
import me.rhunk.snapenhance.core.scripting.impl.CoreScriptHooker
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ class CoreScriptRuntime(
|
|||||||
CoreScriptConfig(),
|
CoreScriptConfig(),
|
||||||
CoreIPC(),
|
CoreIPC(),
|
||||||
CoreScriptHooker(),
|
CoreScriptHooker(),
|
||||||
|
CoreMessaging(modContext)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,148 @@
|
|||||||
|
package me.rhunk.snapenhance.core.scripting.impl
|
||||||
|
|
||||||
|
import me.rhunk.snapenhance.common.data.MessageUpdate
|
||||||
|
import me.rhunk.snapenhance.common.scripting.bindings.AbstractBinding
|
||||||
|
import me.rhunk.snapenhance.common.scripting.bindings.BindingSide
|
||||||
|
import me.rhunk.snapenhance.common.scripting.ktx.scriptableObject
|
||||||
|
import me.rhunk.snapenhance.core.ModContext
|
||||||
|
import me.rhunk.snapenhance.core.features.impl.messaging.Messaging
|
||||||
|
import me.rhunk.snapenhance.core.wrapper.impl.Message
|
||||||
|
import me.rhunk.snapenhance.core.wrapper.impl.SnapUUID
|
||||||
|
import org.mozilla.javascript.Scriptable
|
||||||
|
import org.mozilla.javascript.annotations.JSFunction
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
|
class CoreMessaging(
|
||||||
|
private val modContext: ModContext
|
||||||
|
) : AbstractBinding("messaging", BindingSide.CORE) {
|
||||||
|
private val conversationManager get() = modContext.feature(Messaging::class).conversationManager
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun isPresent() = conversationManager != null
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun newSnapUUID(uuid: String) = SnapUUID.fromString(uuid)
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun updateMessage(
|
||||||
|
conversationId: String,
|
||||||
|
messageId: Number,
|
||||||
|
action: String,
|
||||||
|
callback: (error: String?) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.updateMessage(conversationId, messageId.toLong(), MessageUpdate.entries.find { it.key == action }
|
||||||
|
?: throw RuntimeException("Could not find message update $action"),
|
||||||
|
callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun fetchConversationWithMessagesPaginated(
|
||||||
|
conversationId: String,
|
||||||
|
lastMessageId: Long,
|
||||||
|
amount: Int,
|
||||||
|
callback: (error: String?, message: List<Message>) -> Unit,
|
||||||
|
) {
|
||||||
|
conversationManager?.fetchConversationWithMessagesPaginated(conversationId, lastMessageId, amount, onSuccess = {
|
||||||
|
callback(null, it)
|
||||||
|
}, onError = {
|
||||||
|
callback(it, emptyList())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun fetchConversationWithMessages(
|
||||||
|
conversationId: String,
|
||||||
|
callback: (error: String?, List<Message>) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.fetchConversationWithMessages(conversationId, onSuccess = {
|
||||||
|
callback(null, it)
|
||||||
|
}, onError = {
|
||||||
|
callback(it, emptyList())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun fetchMessageByServerId(
|
||||||
|
conversationId: String,
|
||||||
|
serverId: Long,
|
||||||
|
callback: (error: String?, message: Message?) -> Unit,
|
||||||
|
) {
|
||||||
|
conversationManager?.fetchMessageByServerId(conversationId, serverId, onSuccess = {
|
||||||
|
callback(null, it)
|
||||||
|
}, onError = {
|
||||||
|
callback(it, null)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun fetchMessagesByServerIds(
|
||||||
|
conversationId: String,
|
||||||
|
serverIds: List<Number>,
|
||||||
|
callback: (error: String?, List<Message>) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.fetchMessagesByServerIds(conversationId, serverIds.map {
|
||||||
|
it.toLong()
|
||||||
|
}, onSuccess = {
|
||||||
|
callback(null, it)
|
||||||
|
}, onError = {
|
||||||
|
callback(it, emptyList())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun displayedMessages(
|
||||||
|
conversationId: String,
|
||||||
|
lastMessageId: Number,
|
||||||
|
callback: (error: String?) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.displayedMessages(conversationId, lastMessageId.toLong(), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun fetchMessage(
|
||||||
|
conversationId: String,
|
||||||
|
messageId: Number,
|
||||||
|
callback: (error: String?, message: Message?) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.fetchMessage(conversationId, messageId.toLong(), onSuccess = {
|
||||||
|
callback(null, it)
|
||||||
|
}, onError = { callback(it, null) })
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun clearConversation(
|
||||||
|
conversationId: String,
|
||||||
|
callback: (error: String?) -> Unit
|
||||||
|
) {
|
||||||
|
conversationManager?.clearConversation(conversationId, onSuccess = {
|
||||||
|
callback(null)
|
||||||
|
}, onError = {
|
||||||
|
callback(it)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun getOneOnOneConversationIds(userIds: List<String>, callback: (error: String?, List<Scriptable>) -> Unit) {
|
||||||
|
conversationManager?.getOneOnOneConversationIds(userIds, onSuccess = {
|
||||||
|
callback(null, it.map { (userId, conversationId) ->
|
||||||
|
scriptableObject {
|
||||||
|
putConst("conversationId", this, conversationId)
|
||||||
|
putConst("userId", this, userId)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, onError = {
|
||||||
|
callback(it, emptyList())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunction
|
||||||
|
fun sendChatMessage(
|
||||||
|
conversationId: String,
|
||||||
|
message: String,
|
||||||
|
result: (error: String?) -> Unit
|
||||||
|
) {
|
||||||
|
modContext.messageSender.sendChatMessage(listOf(SnapUUID.fromString(conversationId)), message, onSuccess = { result(null) }, onError = { result(it.toString()) })
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getObject() = this
|
||||||
|
}
|
@ -91,10 +91,10 @@ class ConversationManager(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchMessageByServerId(conversationId: String, serverMessageId: String, onSuccess: (Message) -> Unit, onError: (error: String) -> Unit) {
|
fun fetchMessageByServerId(conversationId: String, serverMessageId: Long, onSuccess: (Message) -> Unit, onError: (error: String) -> Unit) {
|
||||||
val serverMessageIdentifier = CallbackBuilder.createEmptyObject(context.classCache.serverMessageIdentifier.constructors.first())?.apply {
|
val serverMessageIdentifier = CallbackBuilder.createEmptyObject(context.classCache.serverMessageIdentifier.constructors.first())?.apply {
|
||||||
setObjectField("mServerConversationId", conversationId.toSnapUUID().instanceNonNull())
|
setObjectField("mServerConversationId", conversationId.toSnapUUID().instanceNonNull())
|
||||||
setObjectField("mServerMessageId", serverMessageId.toLong())
|
setObjectField("mServerMessageId", serverMessageId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchMessageByServerId.invoke(
|
fetchMessageByServerId.invoke(
|
||||||
|
@ -2,7 +2,6 @@ package me.rhunk.snapenhance.core.wrapper.impl
|
|||||||
|
|
||||||
import me.rhunk.snapenhance.core.wrapper.AbstractWrapper
|
import me.rhunk.snapenhance.core.wrapper.AbstractWrapper
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
class MessageDestinations(obj: Any) : AbstractWrapper(obj){
|
class MessageDestinations(obj: Any) : AbstractWrapper(obj){
|
||||||
var conversations by field("mConversations", uuidArrayListMapper)
|
var conversations by field("mConversations", uuidArrayListMapper)
|
||||||
var stories by field<ArrayList<*>>("mStories")
|
var stories by field<ArrayList<*>>("mStories")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user