Skip to content

Commit

Permalink
add context menu to group peer items
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Jan 19, 2025
1 parent f7ec95f commit 1ee5b69
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 4 deletions.
51 changes: 51 additions & 0 deletions src/main/java/com/zoffcc/applications/trifa/ToxVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,57 @@ else if (value == TOX_GROUP_PRIVACY_STATE_PRIVATE.value)
}
}

public static enum Tox_Group_Mod_Event
{
/**
* A peer has been kicked from the group.
*/
TOX_GROUP_MOD_EVENT_KICK(0),

/**
* A peer as been given the observer role.
*/
TOX_GROUP_MOD_EVENT_OBSERVER(1),

/**
* A peer has been given the user role.
*/
TOX_GROUP_MOD_EVENT_USER(2),

/**
* A peer has been given the moderator role.
*/
TOX_GROUP_MOD_EVENT_MODERATOR(3);

public int value;

private Tox_Group_Mod_Event(int value)
{
this.value = value;
}

public static String value_str(int value)
{
if (value == TOX_GROUP_MOD_EVENT_KICK.value)
{
return "TOX_GROUP_MOD_EVENT_KICK";
}
else if (value == TOX_GROUP_MOD_EVENT_OBSERVER.value)
{
return "TOX_GROUP_MOD_EVENT_OBSERVER";
}
else if (value == TOX_GROUP_MOD_EVENT_USER.value)
{
return "TOX_GROUP_MOD_EVENT_USER";
}
else if (value == TOX_GROUP_MOD_EVENT_MODERATOR.value)
{
return "TOX_GROUP_MOD_EVENT_MODERATOR";
}
return "UNKNOWN";
}
}

public static enum Tox_Group_Role
{
/**
Expand Down
14 changes: 11 additions & 3 deletions src/main/kotlin/com/zoffcc/applications/trifa/GroupPeerStore.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("FunctionName", "SpellCheckingInspection", "LocalVariableName")

package com.zoffcc.applications.trifa

import androidx.compose.ui.text.toLowerCase
Expand Down Expand Up @@ -38,6 +40,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun add(item: GroupPeerItem)
{
//launch {
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var found = false
state.grouppeers.forEach {
Expand Down Expand Up @@ -69,6 +72,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun remove(item: GroupPeerItem)
{
//launch {
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var sel_pubkey = state.selectedGrouppeerPubkey
var sel_item = state.selectedGrouppeer
Expand Down Expand Up @@ -107,8 +111,9 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun select(pubkey: String?)
{
//launch {
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var wanted_contact_item: GroupPeerItem? = null
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var wanted_contact_item: GroupPeerItem? = null
state.grouppeers.forEach {
if (pubkey == it.pubkey)
{
Expand All @@ -129,6 +134,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun update(item: GroupPeerItem)
{
//launch {
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var update_item: GroupPeerItem? = null
state.grouppeers.forEach {
Expand Down Expand Up @@ -172,6 +178,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun update_ipaddr(groupID: String, pubkey: String, ipaddr: String)
{
//launch {
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
var update_item: GroupPeerItem? = null
state.grouppeers.forEach {
Expand All @@ -185,7 +192,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
var need_update = false
var new_peers: ArrayList<GroupPeerItem> = ArrayList()
new_peers.addAll(state.grouppeers)
var to_remove_item: GroupPeerItem? = null
// var to_remove_item: GroupPeerItem? = null
new_peers.forEach { item2 ->
if (item2.pubkey == update_item!!.pubkey)
{
Expand Down Expand Up @@ -213,6 +220,7 @@ fun CoroutineScope.createGroupPeerStore(): GroupPeerStore
override fun clear()
{
//launch {
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
global_semaphore_grouppeerlist_ui.acquire((Throwable().stackTrace[0].fileName + ":" + Throwable().stackTrace[0].lineNumber))
mutableStateFlow.value = state.copy(grouppeers = emptyList(),
selectedGrouppeerPubkey = null, selectedGrouppeer = null)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/zoffcc/applications/trifa/GroupStore.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("FunctionName", "LiftReturnOrAssignment", "SpellCheckingInspection")

package com.zoffcc.applications.trifa

import global_semaphore_grouplist_ui
Expand Down
69 changes: 69 additions & 0 deletions src/main/kotlin/com/zoffcc/applications/trifa/HelperGeneric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.zoffcc.applications.trifa.MainActivity.Companion.tox_friend_get_capab
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_friend_get_connection_status
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_group_leave
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_group_peer_get_public_key
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_group_self_get_public_key
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_messagev3_friend_send_message
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_self_set_nospam
import com.zoffcc.applications.trifa.MainActivity.Companion.toxav_ngc_audio_decode
Expand All @@ -69,10 +70,13 @@ import com.zoffcc.applications.trifa.ToxVars.TOX_MSGV3_MAX_MESSAGE_LENGTH
import com.zoffcc.applications.trifa.TrifaToxService.Companion.orma
import com.zoffcc.applications.trifa_material.trifa_material.BuildConfig
import globalstore
import grouppeerstore
import groupstore
import kotlinx.coroutines.withContext
import messagestore
import myUser
import org.briarproject.briar.desktop.contact.GroupItem
import org.briarproject.briar.desktop.contact.GroupPeerItem
import org.jetbrains.skia.Bitmap
import org.xml.sax.InputSource
import java.io.File
Expand Down Expand Up @@ -1054,6 +1058,7 @@ object HelperGeneric {
}
}

// !! unused now !!
fun replace_emojis_in_text(input: String): String
{
var output = input
Expand Down Expand Up @@ -1154,6 +1159,70 @@ object HelperGeneric {
return build_str
}

fun get_self_group_role(group_num: Long): Int
{
var peer_role = ToxVars.Tox_Group_Role.TOX_GROUP_ROLE_OBSERVER.value
try
{
val self_peer_role = MainActivity.tox_group_self_get_role(group_num)
if (self_peer_role >= 0)
{
peer_role = self_peer_role
}
} catch (_: Exception)
{
}

return peer_role
}

fun get_self_group_role(group_id: String): Int
{
val group_num_temp: Long = tox_group_by_groupid__wrapper(group_id)
if (group_num_temp == -1L)
{
return ToxVars.Tox_Group_Role.TOX_GROUP_ROLE_OBSERVER.value
}

return get_self_group_role(group_num_temp)
}

fun is_self_group_role_admin(group_role: Int): Boolean
{
if (group_role == ToxVars.Tox_Group_Role.TOX_GROUP_ROLE_FOUNDER.value)
{
return true
}
else if (group_role == ToxVars.Tox_Group_Role.TOX_GROUP_ROLE_MODERATOR.value)
{
return true
}
return false
}

fun is_peer_self(group_id: String, peer_pubkey: String): Boolean
{
try
{
val self_group_pubkey = tox_group_self_get_public_key(tox_group_by_groupid__wrapper(group_id))
return (self_group_pubkey == peer_pubkey)
}
catch(_: Exception)
{
}
return false
}

fun force_update_group_peerlist_ui(group_id: String)
{
try
{
// HINT: this is not pretty but it does the job for now :-(
grouppeerstore.update(GroupPeerItem(ip_addr = "127.0.0.1", groupID = group_id, name = "x", connectionStatus = 0, pubkey = "x", peerRole = 0))
} catch (_: Exception)
{
}
}
}

/*
Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3390,11 +3390,44 @@ class MainActivity
SnackBarToast("You joined a Group")
}

@OptIn(DelicateCoroutinesApi::class)
@JvmStatic
@Suppress("unused")
fun android_tox_callback_group_moderation_cb_method(group_number: Long, source_peer_id: Long, target_peer_id: Long, a_Tox_Group_Mod_Event: Int)
{
// ** this happens non stop, so don't save here ** // update_savedata_file_wrapper()
try
{
// Log.i(TAG, "android_tox_callback_group_moderation_cb_method:1: " + tox_group_get_name(group_number) + " s_peer:" + source_peer_id + " t_peer:" + target_peer_id + " " + ToxVars.Tox_Group_Mod_Event.value_str(a_Tox_Group_Mod_Event))
// val peername_src = tox_group_peer_get_name(group_number, source_peer_id)
// val peername_dst = tox_group_peer_get_name(group_number, target_peer_id)
// Log.i(TAG, "android_tox_callback_group_moderation_cb_method:1: peername_src=" + peername_src + " peername_dst=" + peername_dst)
}
catch(_: Exception)
{
}
if (target_peer_id != UINT32_MAX_JAVA)
{
GlobalScope.launch(Dispatchers.IO) {
try
{
try
{
Log.i(TAG, "android_tox_callback_group_moderation_cb_method:2: " + tox_group_get_name(group_number) + " " + ToxVars.Tox_Group_Mod_Event.value_str(a_Tox_Group_Mod_Event))
}
catch(_: Exception)
{
}
val group_id = tox_group_by_groupnum__wrapper(group_number)
if (group_id != null)
{
HelperGeneric.force_update_group_peerlist_ui(group_id)
}
} catch (_: Exception)
{
}
}
}
}

@JvmStatic
Expand Down
Loading

0 comments on commit 1ee5b69

Please sign in to comment.