Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed Dec 10, 2024
1 parent 20b781f commit 0c437ec
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 64 deletions.
4 changes: 0 additions & 4 deletions src/db.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ struct SdbUserStats

class Sdb
{
// Attributes

sqlite3* db;
sqlite3_stmt* stmt;

Expand All @@ -36,8 +34,6 @@ class Sdb
const config_table = "config";


// Constructor

this(string file, bool update = false)
{
open_db(file);
Expand Down
4 changes: 0 additions & 4 deletions src/server/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import std.string : strip;

class Server
{
// Attributes

Sdb db;
GlobalRoom global_room;

Expand All @@ -46,8 +44,6 @@ class Server
private User[string] user_list;


// Constructor

this(string db_file)
{
started_at = MonoTime.currTime;
Expand Down
116 changes: 60 additions & 56 deletions src/server/user.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import std.stdio : writefln;

class User
{
// Attributes

string username;
uint major_version;
uint minor_version;
Expand All @@ -44,23 +42,20 @@ class User

private uint ip_address;
private ushort port;

private long priv_expiration;

private string[string] liked_things;
private string[string] hated_things;

private string[string] watch_list;

private Room[string] joined_rooms;

private string[string] watch_list;

private ubyte[] in_buf;
private long in_msg_size = -1;
private ubyte[] out_buf;


// Constructor

this(Server serv, Socket sock, uint ip_address)
{
this.server = serv;
Expand All @@ -70,7 +65,7 @@ class User
}


// Misc
// Client

string h_client_version()
{
Expand All @@ -82,15 +77,19 @@ class User
return "%s:%d".format(InternetAddress.addrToString(ip_address), port);
}

void send_pm(PM pm, bool new_message)

// Status

private void set_status(uint new_status)
{
scope msg = new SMessageUser(
pm.id, pm.timestamp, pm.from, pm.content,
new_message
);
send_message(msg);
status = new_status;
scope msg = new SGetUserStatus(username, new_status, privileged);
send_to_watching(msg);
}


// Stats

private void calc_speed(uint new_speed)
{
if (upload_number == 0) {
Expand Down Expand Up @@ -179,6 +178,47 @@ class User
}


// Watchlist

private void watch(string peer_username)
{
if (peer_username != server_user)
watch_list[peer_username] = peer_username;
}

private void unwatch(string peer_username)
{
if (peer_username == username)
// Always watch our own username for updates
return;

if (peer_username in watch_list)
watch_list.remove(peer_username);
}

private bool is_watching(string peer_username)
{
if (peer_username in watch_list)
return true;

foreach (room ; joined_rooms)
if (room.is_joined(peer_username))
return true;

return false;
}

private void send_to_watching(scope SMessage msg)
{
debug (msg) writefln(
"Transmit=> %s (code %d) to users watching user %s...",
blue ~ msg.name ~ norm, msg.code, blue ~ username ~ norm
);
foreach (user ; server.users)
if (user.is_watching(username)) user.send_message(msg);
}


// Interests

private void add_thing_he_likes(string thing)
Expand Down Expand Up @@ -291,51 +331,15 @@ class User
}


// Watchlist

private void watch(string peer_username)
{
if (peer_username != server_user)
watch_list[peer_username] = peer_username;
}

private void unwatch(string peer_username)
{
if (peer_username == username)
// Always watch our own username for updates
return;

if (peer_username in watch_list)
watch_list.remove(peer_username);
}

private bool is_watching(string peer_username)
{
if (peer_username in watch_list)
return true;

foreach (room ; joined_rooms)
if (room.is_joined(peer_username))
return true;

return false;
}
// Private Messages

private void send_to_watching(scope SMessage msg)
void send_pm(PM pm, bool new_message)
{
debug (msg) writefln(
"Transmit=> %s (code %d) to users watching user %s...",
blue ~ msg.name ~ norm, msg.code, blue ~ username ~ norm
scope msg = new SMessageUser(
pm.id, pm.timestamp, pm.from, pm.content,
new_message
);
foreach (user ; server.users)
if (user.is_watching(username)) user.send_message(msg);
}

private void set_status(uint new_status)
{
status = new_status;
scope msg = new SGetUserStatus(username, new_status, privileged);
send_to_watching(msg);
send_message(msg);
}


Expand Down

0 comments on commit 0c437ec

Please sign in to comment.