Skip to content

Commit

Permalink
tweak iteration interval,
Browse files Browse the repository at this point in the history
add friends to db.
  • Loading branch information
zoff99 committed Dec 22, 2023
1 parent aa06e65 commit fde5653
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 32 deletions.
36 changes: 18 additions & 18 deletions src/main/java/com/zoffcc/applications/sorm/FriendList.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,65 +40,65 @@ public class FriendList

@Column
@Nullable
String name;
public String name;

@Column(indexed = true, defaultExpr = "", helpers = Column.Helpers.ALL)
@Nullable
String alias_name;
public String alias_name;

@Column
@Nullable
String status_message;
public String status_message;

@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)
public int TOX_CONNECTION_real; // 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 = "0", helpers = Column.Helpers.ALL)
int TOX_CONNECTION_on_off_real; // 0 --> offline, 1 --> online
public int TOX_CONNECTION_on_off_real; // 0 --> offline, 1 --> online

@Column(indexed = true, defaultExpr = "0", helpers = Column.Helpers.ALL)
int TOX_USER_STATUS; // 0 --> NONE, 1 --> online AWAY, 2 --> online BUSY
public int TOX_USER_STATUS; // 0 --> NONE, 1 --> online AWAY, 2 --> online BUSY

@Column
@Nullable
String avatar_pathname = null;
public String avatar_pathname = null;

@Column
@Nullable
String avatar_filename = null;
public String avatar_filename = null;

@Column(indexed = true, defaultExpr = "false", helpers = Column.Helpers.ALL)
@Nullable
boolean avatar_update = false; // has avatar changed for this friend?
public boolean avatar_update = false; // has avatar changed for this friend?

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

@Column(indexed = true, defaultExpr = "false", helpers = Column.Helpers.ALL)
@Nullable
boolean notification_silent = false; // show notifications for this friend?
public boolean notification_silent = false; // show notifications for this friend?

@Column(indexed = true, defaultExpr = "0", helpers = Column.Helpers.ALL)
int sort = 0;
public int sort = 0;

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

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

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

@Column(indexed = true, defaultExpr = "false", helpers = Column.Helpers.ALL)
@Nullable
boolean is_relay = false;
public boolean is_relay = false;

@Column(indexed = true, defaultExpr = "", helpers = Column.Helpers.ALL)
@Nullable
Expand All @@ -108,7 +108,7 @@ public class FriendList
public long capabilities = 0;

@Column(indexed = true, defaultExpr = "0", helpers = Column.Helpers.ALL)
long msgv3_capability = 0;
public long msgv3_capability = 0;

static FriendList deep_copy(FriendList in)
{
Expand Down
125 changes: 124 additions & 1 deletion src/main/java/com/zoffcc/applications/trifa/HelperFriend.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zoffcc.applications.sorm.TRIFADatabaseGlobalsNew;
import org.sqlite.SQLiteException;

import javax.swing.*;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -16,7 +17,7 @@
import static com.zoffcc.applications.trifa.HelperMessage.update_message_in_db_sent_push_set;
import static com.zoffcc.applications.trifa.HelperRelay.get_pushurl_for_friend;
import static com.zoffcc.applications.trifa.HelperRelay.is_valid_pushurl_for_friend_with_whitelist;
import static com.zoffcc.applications.trifa.MainActivity.tox_friend_get_public_key;
import static com.zoffcc.applications.trifa.MainActivity.*;
import static com.zoffcc.applications.trifa.TRIFAGlobals.*;
import static java.time.temporal.ChronoUnit.SECONDS;

Expand Down Expand Up @@ -231,4 +232,126 @@ static boolean friend_do_actual_weburl_call(final String friend_pubkey, final St

return false;
}

public static void add_friend_real(final String friend_tox_id)
{
// nospam=8 chars, checksum=4 chars
String friend_public_key = friend_tox_id.substring(0, friend_tox_id.length() - 12);
// Log.i(TAG, "add_friend_real:add friend PK:" + friend_public_key);
FriendList f = new FriendList();
f.tox_public_key_string = friend_public_key.toUpperCase();

try
{
// set name as the last 5 char of TOXID (until we get a name sent from friend)
f.name = friend_public_key.substring(friend_public_key.length() - 5,
friend_public_key.length());
}
catch (Exception e)
{
e.printStackTrace();
f.name = "Unknown";
}

f.TOX_USER_STATUS = 0;
f.TOX_CONNECTION = 0;
f.TOX_CONNECTION_on_off = 0;
f.avatar_filename = null;
f.avatar_pathname = null;

try
{
insert_into_friendlist_db(f);
}
catch (Exception e)
{
// e.printStackTrace();
}
}

static void add_friend_to_system(final String friend_public_key, final boolean as_friends_relay, final String owner_public_key)
{
final FriendList f = new FriendList();
f.tox_public_key_string = friend_public_key;
f.TOX_USER_STATUS = 0;
f.TOX_CONNECTION = 0;
f.TOX_CONNECTION_on_off = 0;
// set name as the last 5 char of the publickey (until we get a proper name)
f.name = friend_public_key.substring(friend_public_key.length() - 5, friend_public_key.length());
f.avatar_pathname = null;
f.avatar_filename = null;
f.capabilities = 0;

try
{
f.added_timestamp = System.currentTimeMillis();
insert_into_friendlist_db(f);
}
catch (Exception e)
{
e.printStackTrace();
Log.i(TAG, "friend_request:insert:EE2:" + e.getMessage());
return;
}

if (as_friends_relay)
{
// add relay for friend to DB
// TODO // HelperRelay.add_or_update_friend_relay(friend_public_key, owner_public_key);
}

if (MainActivity.getDB_PREF__U_keep_nospam() == false)
{
// ---- set new random nospam value after each added friend ----
// ---- set new random nospam value after each added friend ----
// ---- set new random nospam value after each added friend ----
HelperGeneric.set_new_random_nospam_value();
final String my_tox_id_local = get_my_toxid();
global_my_toxid = my_tox_id_local;
}
}

synchronized static void insert_into_friendlist_db(final FriendList f)
{
try
{
if (TrifaToxService.Companion.getOrma().selectFromFriendList().
tox_public_key_stringEq(f.tox_public_key_string).count() == 0)
{
f.added_timestamp = System.currentTimeMillis();
TrifaToxService.Companion.getOrma().insertIntoFriendList(f);
Log.i(TAG, "friend added to DB: " + f.tox_public_key_string.substring(0, 5));
}
else
{
// friend already in DB
Log.i(TAG, "friend already in DB: " + f.tox_public_key_string.substring(0, 5));
}
}
catch (Exception e)
{
e.printStackTrace();
Log.i(TAG, "friend add to DB:EE:" + e.getMessage());
}

// }
// };
// t.start();
}

static void update_friend_in_db_capabilities(FriendList f)
{
TrifaToxService.Companion.getOrma().updateFriendList().
tox_public_key_stringEq(f.tox_public_key_string).
capabilities(f.capabilities).
execute();
}

static void update_friend_in_db_msgv3_capability(FriendList f)
{
TrifaToxService.Companion.getOrma().updateFriendList().
tox_public_key_stringEq(f.tox_public_key_string).
msgv3_capability(f.msgv3_capability).
execute();
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/zoffcc/applications/trifa/HelperGeneric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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_messagev3_friend_send_message
import com.zoffcc.applications.trifa.MainActivity.Companion.tox_self_set_nospam
import com.zoffcc.applications.trifa.MainActivity.Companion.update_savedata_file
import com.zoffcc.applications.trifa.TRIFAGlobals.VFS_FILE_DIR
import com.zoffcc.applications.trifa.ToxVars.TOX_HASH_LENGTH
Expand Down Expand Up @@ -556,7 +557,7 @@ object HelperGeneric {
var ret: Long = 0
return try {
val fcap = tox_friend_get_capabilities(tox_friend_by_public_key(friend_public_key_string))
if ((fcap and ToxVars.TOX_CAPABILITY_MSGV2) != 0.toLong())
if ((fcap and ToxVars.TOX_CAPABILITY_MSGV3) != 0.toLong())
{
1
}
Expand Down Expand Up @@ -594,6 +595,15 @@ object HelperGeneric {
// TODO: // update_single_message(m, true)
return true
}

@JvmStatic
fun set_new_random_nospam_value()
{
val random = Random()
val new_nospam = random.nextInt().toLong() + (1L shl 31)
tox_self_set_nospam(new_nospam)
update_savedata_file_wrapper()
}
}

/*
Expand Down
56 changes: 48 additions & 8 deletions src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SnackBarToast
import UIGroupMessage
import UIMessage
import User
import androidx.compose.ui.text.toUpperCase
import avstatestore
import avstatestorecallstate
import avstatestorevcapfpsstate
Expand All @@ -29,7 +30,10 @@ import com.zoffcc.applications.trifa.HelperFiletransfer.insert_into_filetransfer
import com.zoffcc.applications.trifa.HelperFiletransfer.move_tmp_file_to_real_file
import com.zoffcc.applications.trifa.HelperFiletransfer.set_message_accepted_from_id
import com.zoffcc.applications.trifa.HelperFiletransfer.update_filetransfer_db_full
import com.zoffcc.applications.trifa.HelperFriend.main_get_friend
import com.zoffcc.applications.trifa.HelperFriend.send_friend_msg_receipt_v2_wrapper
import com.zoffcc.applications.trifa.HelperFriend.update_friend_in_db_capabilities
import com.zoffcc.applications.trifa.HelperFriend.update_friend_in_db_msgv3_capability
import com.zoffcc.applications.trifa.HelperGeneric.PubkeyShort
import com.zoffcc.applications.trifa.HelperGeneric.bytesToHex
import com.zoffcc.applications.trifa.HelperGeneric.get_friend_msgv3_capability
Expand Down Expand Up @@ -68,6 +72,7 @@ import com.zoffcc.applications.trifa.TRIFAGlobals.VFS_TMP_FILE_DIR
import com.zoffcc.applications.trifa.TRIFAGlobals.global_last_activity_outgoung_ft_ts
import com.zoffcc.applications.trifa.TRIFAGlobals.global_self_connection_status
import com.zoffcc.applications.trifa.TRIFAGlobals.global_self_last_went_offline_timestamp
import com.zoffcc.applications.trifa.ToxVars.TOX_CAPABILITY_DECODE
import com.zoffcc.applications.trifa.ToxVars.TOX_CONNECTION
import com.zoffcc.applications.trifa.ToxVars.TOX_FILE_ID_LENGTH
import com.zoffcc.applications.trifa.ToxVars.TOX_HASH_LENGTH
Expand Down Expand Up @@ -105,6 +110,7 @@ import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import kotlin.concurrent.fixedRateTimer

@Suppress("UNUSED_PARAMETER")
class MainActivity
Expand Down Expand Up @@ -140,6 +146,7 @@ class MainActivity
var PREF__force_udp_only = 0
@JvmStatic var DB_PREF__open_files_directly = false
@JvmStatic var DB_PREF__notifications_active = true
@JvmStatic var DB_PREF__U_keep_nospam = true;
var incoming_messages_queue: BlockingQueue<String> = LinkedBlockingQueue()
@JvmStatic var video_play_count_frames: Long = 0
@JvmStatic var video_play_last_timestamp: Long = 0
Expand Down Expand Up @@ -1453,11 +1460,26 @@ class MainActivity
{
// ******** friend just came online ********
Log.i(TAG, "android_tox_callback_friend_connection_status_cb_method:friend just came online: friend number: " + friend_number)
// resend latest msgV3 message that was not "read"
try
{
val fpubkey = tox_friend_get_public_key(friend_number)
// Log.i(TAG, "friend_connection_status_cb:friend just came online:" + fpubkey)
var f = main_get_friend(friend_number)
if (f != null)
{
val friend_capabilities = tox_friend_get_capabilities(friend_number)
f.capabilities = friend_capabilities
update_friend_in_db_capabilities(f)
if (TOX_CAPABILITY_DECODE(f.capabilities).msgv3)
{
f.msgv3_capability = 1
}
else
{
f.msgv3_capability = 0
}
update_friend_in_db_msgv3_capability(f)
}

if (get_friend_msgv3_capability(fpubkey) == 1L)
{
resend_v3_messages(fpubkey)
Expand Down Expand Up @@ -1487,14 +1509,32 @@ class MainActivity
{
Log.i(TAG, "android_tox_callback_friend_request_cb_method: friend_public_key=" + friend_public_key)
val new_friendnumber = tox_friend_add_norequest(friend_public_key)
try
{
contactstore.add(item = ContactItem(name = "new Friend #" + new_friendnumber, isConnected = 0, pubkey = friend_public_key!!))
} catch (_: Exception)
if (new_friendnumber > -1)
{
if (new_friendnumber != UINT32_MAX_JAVA)
{
update_savedata_file_wrapper()
try
{
if (friend_public_key != null)
{
HelperFriend.add_friend_to_system(friend_public_key.toUpperCase(),
false, null);
}
}
catch(_: Exception)
{
}

try
{
contactstore.add(item = ContactItem(name = "new Friend #" + new_friendnumber, isConnected = 0, pubkey = friend_public_key!!))
} catch (_: Exception)
{
}
SnackBarToast("Invited by a new Friend")
}
}
update_savedata_file_wrapper()
SnackBarToast("Invited by a new Friend")
}

@JvmStatic
Expand Down
Loading

0 comments on commit fde5653

Please sign in to comment.