Added server ^•^ #8

Merged
maddie merged 51 commits from server into main 2026-12-01 09:49:53 +11:00
Collaborator
No description provided.
dawn requested changes 2026-11-23 15:14:55 +11:00
Dismissed
dawn left a comment

I can see why you didn't like writing tests, it's quite un-ergonomic.

One way you could make it better for yourself is to reduce the amount of raw sql you're writing, or move it out into seperate helper functions. For example the cleanup_db functions i think could be calls to the existing delete() function.

I can see why you didn't like writing tests, it's quite un-ergonomic. One way you could make it better for yourself is to reduce the amount of raw sql you're writing, or move it out into seperate helper functions. For example the cleanup_db functions i think could be calls to the existing `delete()` function.
@ -12,0 +171,4 @@
if !LOGGER_INITIALISED.swap(true, std::sync::atomic::Ordering::Relaxed) {
env_logger::init();
}
dotenvy::dotenv().expect("Couldn't load environment variables!");
Owner

shouldn't this be loaded before initializing the env_logger? Then the .env file could contain logging settings

shouldn't this be loaded before initializing the env_logger? Then the `.env` file could contain logging settings
dawn marked this conversation as resolved
@ -12,0 +282,4 @@
);
assert_eq!(
String::from("Message for test user 2").into_bytes(),
Owner

This string is somewhat magic. You should probably make the SendRequests::debug() a variable and grab the string out from that instead.

This string is somewhat magic. You should probably make the `SendRequests::debug()` a variable and grab the string out from that instead.
dawn marked this conversation as resolved
@ -12,0 +394,4 @@
.await
.expect("Failed to populate database with test user");
sqlx::query!(
"InSERT INTO messages VALUES ('test_user_5', $1, 1)",
Owner

typo :p

typo :p
dawn marked this conversation as resolved
@ -0,0 +76,4 @@
});
}
pub fn debug() -> Self {
Owner

You can make the "this function is unused" warning go away by adding #[cfg(test)] so it only exists when compiling for tests

You can make the "this function is unused" warning go away by adding `#[cfg(test)]` so it only exists when compiling for tests
dawn marked this conversation as resolved
maddie changed title from Added server ^•^ to WIP: Added server ^•^ 2026-11-24 10:04:30 +11:00
this way if either part fails the user can try again (previously, if the
message deletion failed, their record would already be gone so all
subsequent requests would be invalid
maddie changed title from WIP: Added server ^•^ to Added server ^•^ 2026-11-24 11:23:19 +11:00
maddie requested review from dawn 2026-11-24 11:23:26 +11:00
now the only uncovered stuff is internal server errors which we can't
predict or engineer
dawn approved these changes 2026-11-24 12:55:13 +11:00
dawn left a comment

lgtm!

lgtm!
Lycanea requested changes 2026-11-25 20:43:14 +11:00
Dismissed
@ -0,0 +2,4 @@
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS messages;
CREATE TABLE users (
uuid varchar NOT NULL,
Collaborator

this could use the postgres uuid type that adheres to RFC 9562

also, the uuid doesnt have much reason to be generated on the client and probably also doesnt need to be generated in the backend either, it isnt a thing the user would care about "customizing" and having it generated on the backend/database allows more control over rules for how its generated (incase these were to be changed later), when creating a new user, the client could just provide the token and the server writes to the database that token and either writes a uuid alongside it or lets the database generate one and the database query will return with the user object and the uuid can be sent back to the client

this could use the [postgres uuid type](https://www.postgresql.org/docs/current/datatype-uuid.html) that adheres to RFC 9562 also, the uuid doesnt have much reason to be generated on the client and probably also doesnt need to be generated in the backend either, it isnt a thing the user would care about "customizing" and having it generated on the backend/database allows more control over rules for how its generated (incase these were to be changed later), when creating a new user, the client could just provide the token and the server writes to the database that token and either writes a uuid alongside it or lets the database generate one and the database query will return with the user object and the uuid can be sent back to the client
Author
Collaborator

as discussed in vc, keeping as fun easter egg, yes? :3

as discussed in vc, keeping as fun easter egg, yes? :3
maddie marked this conversation as resolved
@ -0,0 +3,4 @@
DROP TABLE IF EXISTS messages;
CREATE TABLE users (
uuid varchar NOT NULL,
token varchar NOT NULL,
Collaborator

maybe renaming this to be "password" or something would make more sense? a token implies something like a jwt generated on the server but this moreso functions as just a users password/key and also doesnt have any restrictions for what form it takes (whether or not such restrictions should be added is kind of unimportant, users should just have a secure password lmao)

maybe renaming this to be "password" or something would make more sense? a token implies something like a jwt generated on the server but this moreso functions as just a users password/key and also doesnt have any restrictions for what form it takes (whether or not such restrictions should be added is kind of unimportant, users should just have a secure password lmao)
maddie marked this conversation as resolved
@ -0,0 +8,4 @@
);
CREATE TABLE messages (
recipient varchar NOT NULL,
Collaborator

should probably have an index for recipient just since thats going to be queried for a lot, also making it a foreign key to uuid in users is "more correct" id say and would allow adding cascade on delete so all a users messages are automatically removed when the user is removed

would use something like:

ALTER TABLE messages ADD CONSTRAINT messages_users_recipient_fk FOREIGN KEY (recipient) REFERENCES users(uuid) ON DELETE CASCADE;
should probably have an index for recipient just since thats going to be queried for a lot, also making it a foreign key to uuid in users is "more correct" id say and would allow adding cascade on delete so all a users messages are automatically removed when the user is removed would use something like: ```sql ALTER TABLE messages ADD CONSTRAINT messages_users_recipient_fk FOREIGN KEY (recipient) REFERENCES users(uuid) ON DELETE CASCADE; ```
Collaborator

removing the foreign key (doesnt delete the column, just reverts it back to being not "attached" to the users table column) can be done like so:

ALTER TABLE messages DROP CONSTRAINT messages_users_recipient_fk;
removing the foreign key (doesnt delete the column, just reverts it back to being not "attached" to the users table column) can be done like so: ```sql ALTER TABLE messages DROP CONSTRAINT messages_users_recipient_fk; ```
Author
Collaborator

awesome yay tyyy ❤️❤️❤️

awesome yay tyyy ❤️❤️❤️
maddie marked this conversation as resolved
@ -0,0 +10,4 @@
CREATE TABLE messages (
recipient varchar NOT NULL,
message bytea NOT NULL,
message_receive_time int8 NOT NULL
Collaborator

this could use the timestamp data type which is internally stored as a int8 aswell but has standardized precision and is just a more fitting data type (could be more useful if future operations want to do comparisons with time and such)

it would also skip the need for the complicated conversion on write on line 108 (instead just storing NOW()) and would only need the inverse extract(epoch FROM message_receive_time) at read if it being read as an integer is required

this could use the [timestamp data type](https://www.postgresql.org/docs/current/datatype-datetime.html) which is internally stored as a int8 aswell but has standardized precision and is just a more fitting data type (could be more useful if future operations want to do comparisons with time and such) it would also skip the need for the complicated conversion on write on line 108 (instead just storing `NOW()`) and would only need the inverse `extract(epoch FROM message_receive_time)` at read if it being read as an integer is required
Author
Collaborator

What's the time complexity on ordering according to receive time? That's the concern by which dawn made the case for using int8 instead of timestamp...

What's the time complexity on ordering according to receive time? That's the concern by which dawn made the case for using int8 instead of timestamp...
Collaborator

the time complexity of both timestamp and int8 would be O(n log n) if there is no index created as the query requires doing a sort (which defaults to b-tree), both types could have a b-tree index added if a single recipient ends up having more than like... a thousand messages processed at once

the time complexity of both timestamp and int8 would be O(n log n) if there is no index created as the query requires doing a sort (which defaults to b-tree), both types could have a b-tree index added if a single recipient ends up having more than like... a thousand messages processed at once
maddie marked this conversation as resolved
@ -7,0 +45,4 @@
let insertion_result = sqlx::query!(
"INSERT INTO users VALUES ( $1, $2 );",
auth_info.uuid,
auth_info.token
Collaborator

if token is arbitrary user input the data type should atleast specify n as like 512 or something. without specifying a length, varchar can store up to 1 GB of data, as specified on the Character Types documentation

if token is arbitrary user input the data type should atleast specify n as like 512 or something. without specifying a length, varchar can store up to 1 GB of data, as specified on the [Character Types documentation](https://www.postgresql.org/docs/current/datatype-character.html)
Author
Collaborator

I can actually set an exact fixed length as all passkeys will eventually be hashed. UUID can also just have the uuid length as max length. Still deciding whether to set a max size on requests and what it should be. would like to discuss over call once other things r resolved

I can actually set an exact fixed length as [all passkeys will eventually be hashed](https://git.pawswap.space/dawn/BigSister/issues/9). UUID can also just have the uuid length as max length. Still deciding whether to set a max size on requests and what it should be. would like to discuss over call once other things r resolved
maddie marked this conversation as resolved
@ -7,0 +88,4 @@
let mut invalid_recipients: Vec<String> = vec![];
let mut valid_recipients: Vec<SendRequest> = vec![];
for message in send_request.0 {
Collaborator

just going off vibes but i feel like this could be instead replaced with one sql query, maybe define a sql function on the database? dunno if this is worth looking into but im just saying anything that comes to mind

just going off vibes but i feel like this could be instead replaced with one sql query, maybe define a sql function on the database? dunno if this is worth looking into but im just saying anything that comes to mind
Author
Collaborator

wait no ur so true I gotta hop on that

wait no ur so true I gotta hop on that
Author
Collaborator

So turns out sqlx query! macro literally can't handle WHERE / IN patterns ?? found a blog post whinging about this so it's not just me. They had a solution but it was messy and came with enough caveats that I ditched it.

anyways tossing up using the non-statically-checked query() function. Think I will implement it as such rn but want to discuss further before merging.

So turns out sqlx query! macro literally can't handle WHERE / IN patterns ?? found a blog post whinging about this so it's not just me. They had a solution but it was messy and came with enough caveats that I ditched it. anyways tossing up using the non-statically-checked query() function. Think I will implement it as such rn but want to discuss further before merging.
Owner

imo, not having static checking is fine since the sql will be run by the tests.

imo, not having static checking is fine since the sql will be run by the tests.
Author
Collaborator

ough trying to implement this is hell
without the macros u dont get the compile-time anonymous structure so u have to manually parse it and coerce it into strings and handle the result for every single thing and genuinely it's almost more painful...

ough trying to implement this is hell without the macros u dont get the compile-time anonymous structure so u have to manually parse it and coerce it into strings and handle the result for every single thing and genuinely it's almost more painful...
Author
Collaborator

turns out it doesn't work as a runtime query either. fuck

turns out it doesn't work as a runtime query either. fuck
Author
Collaborator

still unresolved; genuinely not sure what to do with this

still unresolved; genuinely not sure what to do with this
Collaborator

I think it's fine to ignore this for now but if there was somehow a bottleneck here in terms of like performance or something then it could be good to optimise later

it sounds like the implementation is made difficult by SQLX and not SQL itself so not much can be done by lycanea I think....

I think it's fine to ignore this for now but if there was somehow a bottleneck here in terms of like performance or something then it could be good to optimise later it sounds like the implementation is made difficult by SQLX and not SQL itself so not much can be done by lycanea I think....
Author
Collaborator

yea this one isn't a lycanea solveable, alas. If optimisation becomes a requirement we can have another stab, but much like the uuid thing i dont think that's necessary yet

yea this one isn't a lycanea solveable, alas. If optimisation becomes a requirement we can have another stab, but much like the uuid thing i dont think that's necessary yet
Lycanea marked this conversation as resolved
@ -7,0 +104,4 @@
// Actually make the request
let insertion_result = sqlx::query!(
"INSERT INTO messages(recipient, message, message_receive_time) \
Collaborator

this is silly but you could just make the message_receive_time a default field in the schema and then itll be filled automatically when new rows are made

this is silly but you could just make the message_receive_time a default field in the schema and then itll be filled automatically when new rows are made
maddie marked this conversation as resolved
@ -7,0 +150,4 @@
};
// Order by receive order
messages.sort_by_key(|message| message.message_receive_time);
Collaborator

this sorting could happen in the query with something like ORDER BY message_receive_time both if int8 is used and if timestamp is used and imo thatd be simpler (i think stylistically its better to have finished as much simple data processing as possible before it reaches the actual backend code)

this sorting could happen in the query with something like `ORDER BY message_receive_time` both if int8 is used and if timestamp is used and imo thatd be simpler (i think stylistically its better to have finished as much simple data processing as possible before it reaches the actual backend code)
maddie marked this conversation as resolved
@ -7,0 +156,4 @@
.map(|message| message.message)
.collect::<PollResponse>();
// Only try deleting messages if there were messages TO delete (to prevent unnecessary requests)
Collaborator

i believe it could actually be possible to make the above query a delete query and itd return all deleted rows and thus wouldnt need a separate delete query, dunno if the library being used would like... prevent this for some reason?

i believe it could actually be possible to make the above query a delete query and itd return all deleted rows and thus wouldnt need a separate delete query, dunno if the library being used would like... prevent this for some reason?
Author
Collaborator

struggling to make this work. if madam SQL cares to help me craft a query i'll happily do it but I can't seem to get it functional

struggling to make this work. if madam SQL cares to help me craft a query i'll happily do it but I can't seem to get it functional
Collaborator
WITH deleted AS (
	DELETE FROM messages
	WHERE recipient = $1
	RETURNING recipient, message, message_receive_time
)
SELECT recipient, message, message_receive_time
FROM deleted
ORDER BY message_receive_time;

this query replaces the above SELECT query and will return exactly the same while also deleting everything along the way, 'recipient' could be removed from both the RETURNING and SELECT lines because we already know the recipient for every row but thats up to you

```sql WITH deleted AS ( DELETE FROM messages WHERE recipient = $1 RETURNING recipient, message, message_receive_time ) SELECT recipient, message, message_receive_time FROM deleted ORDER BY message_receive_time; ``` this query replaces the above SELECT query and will return exactly the same while also deleting everything along the way, 'recipient' could be removed from both the RETURNING and SELECT lines because we already know the recipient for every row but thats up to you
Author
Collaborator

they call me the girl who implements lycanea's awesome sql

!8 (commit 942613f441)

they call me the girl who implements lycanea's awesome sql https://git.pawswap.space/dawn/BigSister/pulls/8/commits/942613f441ec19efe9e61f3d8a5ef06999ce80103b68ad31a58f0dbd30cc590f
@ -7,0 +188,4 @@
// Delete pending messages for that user
let message_deletion_result =
sqlx::query!("DELETE FROM messages WHERE recipient = $1", auth_info.uuid)
Collaborator

if the messages schema has recipient as a foreign key it can be set to "cascade on delete" and once the user is deleted below the messages the user is set to receive will also be deleted

if the messages schema has recipient as a foreign key it can be set to "cascade on delete" and once the user is deleted below the messages the user is set to receive will also be deleted
Author
Collaborator

Done. Dawn says u should press resolve on ur comments if u think it's resolved rather than me resolving them so if ur satisfied do that again

[Done.](https://git.pawswap.space/dawn/BigSister/pulls/8/commits/d619b9570447ace832a543f89ca48d6ce7f0e31ea78cc2e1b0200433bee469df) Dawn says u should press resolve on ur comments if u think it's resolved rather than me resolving them so if ur satisfied do that again
Lycanea marked this conversation as resolved
Collaborator

rewrite in Javascript

rewrite in Javascript
Lycanea self-assigned this 2026-11-25 21:36:56 +11:00
Lycanea removed their assignment 2026-11-25 21:37:08 +11:00
Owner

I since since there is no data to migrate yet, it would make sense to merge all the migration files into one. That will make it much easier to work out the current schema. That's what I'm doing in my client code.

I since since there is no data to migrate yet, it would make sense to merge all the migration files into one. That will make it much easier to work out the current schema. That's what I'm doing in my client code.
circular approved these changes 2026-11-27 09:23:44 +11:00
@ -0,0 +2,4 @@
# Dev setup
1. Install postgresql
2. Create a db for bigsister with `createdb bigsister`
Collaborator

could this all be put into a script?

could this all be put into a script?
Author
Collaborator

idk all this is handled for me on macos. dawn's the one who had to do this; ask her @dawn

idk all this is handled for me on macos. dawn's the one who had to do this; ask her @dawn
Collaborator

I think the initial database setup can't really/shouldn't be a SQL script, you wouldn't have a database to run the script on and its best that scripts are ran as the bigsister user instead of the database admins user, this is all the dev setup instructions though and not instructions for setting up a "production instance" so I guess more "rules" can be broken for simplicity but still 🤷

I think the initial database setup can't really/shouldn't be a SQL script, you wouldn't have a database to run the script on and its best that scripts are ran as the bigsister user instead of the database admins user, this is all the dev setup instructions though and not instructions for setting up a "production instance" so I guess more "rules" can be broken for simplicity but still 🤷
@ -4,3 +34,3 @@
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
"Goodbye, world!"
Collaborator
No description provided.
Author
Collaborator

cat with legs

cat with legs
@ -7,0 +55,4 @@
return Status::InternalServerError; // Insertion failed
}
Status::Ok // Yay Yippee welcome to Big Sister !!!
Collaborator

going on with what lycanea said in her comment, are we sure we should let users specify uuids? it seems kinda unnecessary to me

also if the endpoint isn't going to return any actual data wouldn't it be more appropriate to respond with a status code 204 No Content?

going on with what lycanea said in her comment, are we sure we should let users specify uuids? it seems kinda unnecessary to me also if the endpoint isn't going to return any actual data wouldn't it be more appropriate to respond with a status code 204 No Content?
Author
Collaborator

it's not necessary, but the characters are limited so it's not a risk and it's funny to let us set our own uuids

it's not necessary, but the characters are limited so it's not a risk and it's funny to let us set our own uuids
Collaborator

in terms of the non technical part of "should users be allowed to specify uuids" it doesnt really matter apart from just being a bit less consistent but maddie finds it to be a fun idea and i dont really see the harm in it (apart from what i specify below) so im all for fun and games

the only actual downside is that its a bit less efficient/performant and uses more storage, using the varchar requires 36 bytes to store each uuid while an actual uuid type would only require 16, its really not that massive of a difference and i doubt the impact in terms of performance on a larger scale would be very noticeable but it would result in the uuids and any indexes containing them being over double the size

in terms of the non technical part of "should users be allowed to specify uuids" it doesnt really matter apart from just being a bit less consistent but maddie finds it to be a fun idea and i dont really see the harm in it (apart from what i specify below) so im all for fun and games the only actual downside is that its a bit less efficient/performant and uses more storage, using the varchar requires 36 bytes to store each uuid while an actual uuid type would only require 16, its really not that massive of a difference and i doubt the impact in terms of performance on a larger scale would be very noticeable but it would result in the uuids and any indexes containing them being over double the size
Author
Collaborator

yeah we can handle this if userbase explodes

yeah we can handle this if userbase explodes
maddie requested review from Lycanea 2026-11-30 10:55:03 +11:00
Lycanea approved these changes 2026-12-01 09:47:35 +11:00
Lycanea left a comment

dont have any more sql-y database stuff to comment on and i am not knowledgeable about rust soo.... looks... good to me !

dont have any more sql-y database stuff to comment on and i am not knowledgeable about rust soo.... looks... good to me !
maddie merged commit b01388481d into main 2026-12-01 09:49:53 +11:00
maddie referenced this pull request from a commit 2026-12-01 09:49:55 +11:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
Reference
dawn/BigSister!8
No description provided.