Skip to content

Commit

Permalink
feat(scripting): messaging module
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunk committed Dec 27, 2023
1 parent 2a8fcac commit 7aa05e9
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,23 @@ enum class MediaReferenceType {
}


enum class MessageUpdate {
UNKNOWN, READ, RELEASE, SAVE, UNSAVE, ERASE, SCREENSHOT, SCREEN_RECORD, REPLAY, REACTION, REMOVEREACTION, REVOKETRANSCRIPTION, ALLOWTRANSCRIPTION, ERASESAVEDSTORYMEDIA
enum class MessageUpdate(
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class Notifications : Feature("Notifications", loadParams = FeatureLoadParams.IN

context.coroutineScope.launch(coroutineDispatcher) {
suspendCoroutine { continuation ->
conversationManager.fetchMessageByServerId(conversationId, serverMessageId, onSuccess = {
conversationManager.fetchMessageByServerId(conversationId, serverMessageId.toLong(), onSuccess = {
if (it.senderId.toString() == context.database.myUserId) {
param.invokeOriginal()
continuation.resumeWith(Result.success(Unit))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CoreMessagingBridge(
suspendCancellableCoroutine { continuation ->
conversationManager?.fetchMessageByServerId(
conversationId,
serverMessageId,
serverMessageId.toLong(),
onSuccess = {
continuation.resumeWith(Result.success(it.toBridge()))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import me.rhunk.snapenhance.common.scripting.ScriptRuntime
import me.rhunk.snapenhance.common.scripting.bindings.BindingSide
import me.rhunk.snapenhance.core.ModContext
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.CoreScriptHooker

Expand All @@ -23,6 +24,7 @@ class CoreScriptRuntime(
CoreScriptConfig(),
CoreIPC(),
CoreScriptHooker(),
CoreMessaging(modContext)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
setObjectField("mServerConversationId", conversationId.toSnapUUID().instanceNonNull())
setObjectField("mServerMessageId", serverMessageId.toLong())
setObjectField("mServerMessageId", serverMessageId)
}

fetchMessageByServerId.invoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.rhunk.snapenhance.core.wrapper.impl

import me.rhunk.snapenhance.core.wrapper.AbstractWrapper

@Suppress("UNCHECKED_CAST")
class MessageDestinations(obj: Any) : AbstractWrapper(obj){
var conversations by field("mConversations", uuidArrayListMapper)
var stories by field<ArrayList<*>>("mStories")
Expand Down

0 comments on commit 7aa05e9

Please sign in to comment.