Skip to content

Commit

Permalink
WIP: add and show friends relays
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Feb 24, 2024
1 parent 8613725 commit 45bcd86
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/zoffcc/applications/sorm/RelayListDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ public class RelayListDB

// pubkey is always saved as UPPER CASE hex string!! -----------------
@PrimaryKey
String tox_public_key_string = "";
public String tox_public_key_string = "";
// pubkey is always saved as UPPER CASE hex string!! -----------------

@Column(indexed = true, defaultExpr = "0", helpers = Column.Helpers.ALL)
int TOX_CONNECTION; // 0 --> NONE (offline), 1 --> TCP (online), 2 --> UDP (online)
public int TOX_CONNECTION; // 0 --> NONE (offline), 1 --> TCP (online), 2 --> UDP (online)

@Column(indexed = true, defaultExpr = "0", helpers = Column.Helpers.ALL)
int TOX_CONNECTION_on_off; // 0 --> offline, 1 --> online
public int TOX_CONNECTION_on_off; // 0 --> offline, 1 --> online

@Column(indexed = true, defaultExpr = "false", helpers = Column.Helpers.ALL)
boolean own_relay = false; // false --> friends relay, true --> my relay
public boolean own_relay = false; // false --> friends relay, true --> my relay

@Column(indexed = true, defaultExpr = "-1", helpers = Column.Helpers.ALL)
long last_online_timestamp = -1L;
public long last_online_timestamp = -1L;

// pubkey is always saved as UPPER CASE hex string!! -----------------
@Column(indexed = true, defaultExpr = "", helpers = Column.Helpers.ALL)
@Nullable
String tox_public_key_string_of_owner = "";
public String tox_public_key_string_of_owner = "";
// pubkey is always saved as UPPER CASE hex string!! -----------------

static RelayListDB deep_copy(RelayListDB in)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ static void add_friend_to_system(final String friend_public_key, final boolean a
if (as_friends_relay)
{
// add relay for friend to DB
// TODO // HelperRelay.add_or_update_friend_relay(friend_public_key, owner_public_key);
HelperRelay.add_or_update_friend_relay(friend_public_key, owner_public_key);
}

if (MainActivity.getDB_PREF__U_keep_nospam() == false)
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/com/zoffcc/applications/trifa/HelperRelay.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

package com.zoffcc.applications.trifa;

import com.zoffcc.applications.sorm.FriendList;
import com.zoffcc.applications.sorm.OrmaDatabase;
import com.zoffcc.applications.sorm.RelayListDB;

import java.sql.ResultSet;
import java.sql.Statement;

import static com.zoffcc.applications.sorm.OrmaDatabase.s;
import static com.zoffcc.applications.trifa.MainActivity.tox_friend_by_public_key;
import static com.zoffcc.applications.trifa.TRIFAGlobals.*;
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_NTFY_PUSH_URL_PREFIX;

Expand Down Expand Up @@ -122,4 +125,143 @@ static boolean is_valid_pushurl_for_friend_with_whitelist(String push_url)
// anything else is not allowed at this time!
return false;
}

static boolean is_any_relay(String friend_pubkey)
{
try
{
Statement statement = OrmaDatabase.getSqldb().createStatement();
ResultSet rs = statement.executeQuery(
"select count(*) as count from FriendList where tox_public_key_string='" +
s(friend_pubkey.toUpperCase()) +
"' and is_relay='1'");
if (rs.next())
{
int count = rs.getInt("count");
if (count > 0)
{
try
{
statement.close();
}
catch (Exception ignored)
{
}
return true;
}
}
else
{
try
{
statement.close();
}
catch (Exception ignored)
{
}
return false;
}
}
catch (Exception e)
{
return false;
}

return false;
}

static void delete_friend_current_relay(String friend_pubkey)
{
try
{
//**// TODO:
}
catch (Exception e)
{
e.printStackTrace();
}
}

static void add_or_update_friend_relay(String relay_public_key_string, String friend_pubkey)
{
Log.i(TAG, "add_or_update_friend_relay:001");
if (relay_public_key_string == null)
{
Log.i(TAG, "add_or_update_friend_relay:ret01");
return;
}

if (friend_pubkey == null)
{
Log.i(TAG, "add_or_update_friend_relay:ret02");
return;
}

try
{
if (!is_any_relay(friend_pubkey))
{
String friend_old_relay_pubkey = get_relay_for_friend(friend_pubkey);

if (friend_old_relay_pubkey != null)
{
// delete old relay
delete_friend_current_relay(friend_pubkey);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}

try
{
if (!is_any_relay(friend_pubkey))
{
FriendList fl = HelperFriend.main_get_friend(
tox_friend_by_public_key(friend_pubkey));

if (fl != null)
{
// add relay to DB table
RelayListDB new_relay = new RelayListDB();
new_relay.own_relay = false;
new_relay.TOX_CONNECTION = fl.TOX_CONNECTION;
new_relay.TOX_CONNECTION_on_off = fl.TOX_CONNECTION_on_off;
new_relay.last_online_timestamp = fl.last_online_timestamp;
new_relay.tox_public_key_string = relay_public_key_string.toUpperCase();
new_relay.tox_public_key_string_of_owner = friend_pubkey;

//
try
{
TrifaToxService.Companion.getOrma().insertIntoRelayListDB(new_relay);
Log.i(TAG, "add_or_update_friend_relay:+ADD friend relay+ owner pubkey=" + friend_pubkey);
}
catch (Exception e2)
{
// e2.printStackTrace();
}

// friend exists -> update
try
{
TrifaToxService.Companion.getOrma().updateFriendList().
tox_public_key_stringEq(relay_public_key_string).
is_relay(true).
execute();
}
catch (Exception e2)
{
// e2.printStackTrace();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ import com.zoffcc.applications.trifa.FriendSettingDetails
import com.zoffcc.applications.trifa.HelperGeneric.ngc_video_frame_last_incoming_ts
import com.zoffcc.applications.trifa.MainActivity.Companion.DEBUG_COMPOSE_UI_UPDATES
import com.zoffcc.applications.trifa.MainActivity.Companion.PREF__do_not_sync_av
import org.briarproject.briar.desktop.ui.ExplainerInfoIsRelay
import org.briarproject.briar.desktop.ui.Tooltip

private const val TAG = "trifa.Main.kt"
Expand Down Expand Up @@ -239,6 +240,7 @@ const val MAX_EMOJI_POP_RESULT = 15
const val MAX_ONE_ON_ONE_MESSAGES_TO_SHOW = 20000
const val MAX_GROUP_MESSAGES_TO_SHOW = 20000
const val SNACKBAR_TOAST_MS_DURATION: Long = 1200
val BG_COLOR_RELAY_CONTACT_ITEM = 0x448ABEB9
val MESSAGE_CHECKMARKS_ICON_SIZE = 12.dp
val MESSAGE_CHECKMARKS_CONTAINER_SIZE = 12.dp
var emojis_cat_all_gropued: ArrayList<ArrayList<ArrayList<EmojiStrAndName>>> = ArrayList()
Expand Down Expand Up @@ -1127,6 +1129,10 @@ fun App()
if (contacts.selectedContactPubkey == null)
{
ExplainerChat()
}
if ((contacts.selectedContact != null) && (contacts.selectedContact!!.is_relay))
{
ExplainerInfoIsRelay(contacts.selectedContact)
} else
{
Log.i(TAG, "CONTACTS -> draw")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ fun getFriendListWithGroupingAndSorting(friendlist: ArrayList<ContactItem>)
: ArrayList<ContactItem>
{
return ArrayList(friendlist.sortedWith(
compareBy<ContactItem> { friendsRolesOrder[it.isConnected] }.
compareBy<ContactItem> { it.is_relay }.
thenBy<ContactItem> { friendsRolesOrder[it.isConnected] }.
thenBy { it.name.toLowerCase() }
)
)
Expand Down
52 changes: 45 additions & 7 deletions src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.zoffcc.applications.trifa.HelperMessage.process_msgv3_high_level_ack
import com.zoffcc.applications.trifa.HelperMessage.update_message_in_db_read_rcvd_timestamp_rawmsgbytes
import com.zoffcc.applications.trifa.HelperMessage.update_single_message_from_ftid
import com.zoffcc.applications.trifa.HelperMessage.update_single_message_from_messge_id
import com.zoffcc.applications.trifa.HelperRelay.is_any_relay
import com.zoffcc.applications.trifa.TRIFAGlobals.ABSOLUTE_MINIMUM_GLOBAL_VIDEO_BITRATE
import com.zoffcc.applications.trifa.TRIFAGlobals.AVATAR_INCOMING_MAX_BYTE_SIZE
import com.zoffcc.applications.trifa.TRIFAGlobals.GLOBAL_AUDIO_BITRATE
Expand Down Expand Up @@ -1440,7 +1441,11 @@ class MainActivity
{
try
{
contactstore.update(item = ContactItem(name = friend_name!!, isConnected = tox_friend_get_connection_status(friend_number), pubkey = tox_friend_get_public_key(friend_number)!!))
val friend_pubkey = tox_friend_get_public_key(friend_number)!!
val is_relay = is_any_relay(friend_pubkey)
contactstore.update(item = ContactItem(name = friend_name!!,
isConnected = tox_friend_get_connection_status(friend_number), pubkey = friend_pubkey,
is_relay = is_relay))
} catch (_: Exception)
{
}
Expand All @@ -1457,7 +1462,12 @@ class MainActivity
{
fname = "Friend"
}
contactstore.update(item = ContactItem(name = fname, isConnected = tox_friend_get_connection_status(friend_number), pubkey = tox_friend_get_public_key(friend_number)!!))
val friend_pubkey = tox_friend_get_public_key(friend_number)!!
val is_relay = is_any_relay(friend_pubkey)
contactstore.update(item = ContactItem(name = fname,
isConnected = tox_friend_get_connection_status(friend_number),
pubkey = friend_pubkey,
is_relay = is_relay))
} catch (_: Exception)
{
}
Expand All @@ -1479,10 +1489,30 @@ class MainActivity
{
if (length == (TOX_PUBLIC_KEY_SIZE + 1).toLong())
{
Log.i(TAG, "friend_lossless_packet_cb:recevied CONTROL_PROXY_MESSAGE_TYPE_PROXY_PUBKEY_FOR_FRIEND")
val relay_pubkey: String = bytes_to_hex(data).substring(2)
Log.i(TAG, "friend_lossless_packet_cb:recevied pubkey:" + relay_pubkey.uppercase())
// TODO: add relays // HelperFriend.add_friend_to_system(relay_pubkey.uppercase(), true, fpubkey)
val new_friendnumber = tox_friend_add_norequest(relay_pubkey)
if (new_friendnumber > -1)
{
if (new_friendnumber != UINT32_MAX_JAVA)
{
update_savedata_file_wrapper()

Log.i(TAG, "friend_lossless_packet_cb:recevied CONTROL_PROXY_MESSAGE_TYPE_PROXY_PUBKEY_FOR_FRIEND")
Log.i(TAG, "friend_lossless_packet_cb:recevied pubkey:" + relay_pubkey.uppercase())
HelperFriend.add_friend_to_system(relay_pubkey.uppercase(), true, fpubkey)

try
{
contactstore.add(item = ContactItem(name = "Relay #" + relay_pubkey.uppercase().take(6),
isConnected = 0,
pubkey = relay_pubkey.uppercase(),
is_relay = true))
} catch (_: Exception)
{
}
SnackBarToast("Friend Relay updated or added")
}
}
}
} else if (data[0].toUByte().toInt() == TRIFAGlobals.CONTROL_PROXY_MESSAGE_TYPE.CONTROL_PROXY_MESSAGE_TYPE_PUSH_URL_FOR_FRIEND.value)
{
Expand Down Expand Up @@ -1526,7 +1556,12 @@ class MainActivity
{
fname = "Friend"
}
contactstore.update(item = ContactItem(name = fname, isConnected = tox_friend_get_connection_status(friend_number), pubkey = tox_friend_get_public_key(friend_number)!!))
val friend_pubkey = tox_friend_get_public_key(friend_number)!!
val is_relay = is_any_relay(friend_pubkey)
contactstore.update(item = ContactItem(name = fname,
isConnected = tox_friend_get_connection_status(friend_number),
pubkey = friend_pubkey,
is_relay = is_relay))
} catch (_: Exception)
{
}
Expand Down Expand Up @@ -1649,7 +1684,10 @@ class MainActivity

try
{
contactstore.add(item = ContactItem(name = "new Friend #" + new_friendnumber, isConnected = 0, pubkey = friend_public_key!!))
contactstore.add(item = ContactItem(name = "new Friend #" + new_friendnumber,
isConnected = 0,
pubkey = friend_public_key!!,
is_relay = false))
} catch (_: Exception)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,10 @@ class TrifaToxService

try
{
contactstore.add(item = ContactItem(name = fname, isConnected = 0, pubkey = tox_friend_get_public_key(it)!!))
contactstore.add(item = ContactItem(name = fname,
isConnected = 0,
pubkey = tox_friend_get_public_key(it)!!,
is_relay = f.is_relay))
} catch (_: Exception)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fun CoroutineScope.createUnreadMessages(): UnreadMessages {
}
catch (e: Exception)
{
e.printStackTrace()
// e.printStackTrace()
}
val tmp = HashMap(state.unread_per_friend_message_count)
tmp.remove(friend_pubkey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ package org.briarproject.briar.desktop.contact
data class ContactItem(
val name: String,
val isConnected: Int,
val pubkey: String
val pubkey: String,
val is_relay: Boolean
) {
fun updateName(n: String) =
copy(name = n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fun ContactItemView(
Box(Modifier.align(Top).padding(vertical = 0.dp)) {
ProfileCircle(45.dp, contactItem)
val current_friendtorerunreadmessagesstore by globalfrndstoreunreadmsgs.stateFlow.collectAsState()
val num_unread = current_friendtorerunreadmessagesstore.unread_per_friend_message_count.get(contactItem.pubkey)
var num_unread = current_friendtorerunreadmessagesstore.unread_per_friend_message_count.get(contactItem.pubkey)
if (contactItem.is_relay) num_unread = 0
NumberBadge(
num = if (num_unread == null) 0 else num_unread,
modifier = Modifier.align(TopEnd).offset(6.dp, (-6).dp)
Expand Down
Loading

0 comments on commit 45bcd86

Please sign in to comment.