Skip to content

Commit

Permalink
feat: activity preference intro
Browse files Browse the repository at this point in the history
  • Loading branch information
112batuhan committed Nov 17, 2024
1 parent 085f7fd commit 391086a
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 4 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ serde = { git = "https://github.com/frederik-uni/serde" }

[dev-dependencies]
axum-test = { version = "16.4.0", features = ["ws"] }
base64 = "0.22.1"
flate2 = "1.0"
testcontainers-modules = { version = "0.11.4", features = ["surrealdb"] }
uuid = "1.11"
2 changes: 1 addition & 1 deletion migrations/migrations/definitions/_initial.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"schemas":"DEFINE FUNCTION OVERWRITE fn::id_or_null($value: any) -> any {\n IF $value.is_none() {\n RETURN NONE;\n }\n ELSE {\n RETURN meta::id($value);\n };\n} PERMISSIONS FULL;\n\nDEFINE FUNCTION OVERWRITE fn::add_possible_nulls($var1: any, $var2: any) -> any {\n IF type::is::none($var1) AND type::is::none($var2) {\n RETURN NONE;\n }\n ELSE {\n RETURN $var1 + $var2;\n };\n} PERMISSIONS FULL;\n\nDEFINE TABLE OVERWRITE influenced_by SCHEMAFULL TYPE RELATION IN user OUT user ENFORCED;\n\nDEFINE FIELD OVERWRITE influence_type on influenced_by TYPE int DEFAULT 1;\nDEFINE FIELD OVERWRITE description ON influenced_by TYPE string DEFAULT \"\";\nDEFINE FIELD OVERWRITE beatmaps ON influenced_by TYPE set<int> DEFAULT [];\nDEFINE FIELD OVERWRITE updated_at ON influenced_by type datetime VALUE time::now();\nDEFINE FIELD OVERWRITE created_at ON influenced_by type datetime VALUE time::now() READONLY;\n\n// COUNTLESS HOURS LOST BECAUSE I USED VALUE INSTEAD OF DEFAULT\nDEFINE FIELD OVERWRITE order on influenced_by TYPE int \nDEFAULT (UPSERT increment:order SET increment+=1 RETURN increment).at(0).values().at(0);\n\nDEFINE INDEX OVERWRITE unique_in_out ON TABLE influenced_by COLUMNS in, out UNIQUE;\n\nDEFINE TABLE OVERWRITE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD OVERWRITE script_name ON script_migration TYPE string;\nDEFINE FIELD OVERWRITE executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;\nDEFINE TABLE OVERWRITE user SCHEMAFULL;\n\nDEFINE FIELD OVERWRITE username ON user TYPE string;\nDEFINE FIELD OVERWRITE avatar_url ON user TYPE string;\nDEFINE FIELD OVERWRITE bio ON user TYPE string DEFAULT \"\";\nDEFINE FIELD OVERWRITE ranked_mapper ON user TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE authenticated ON user TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE beatmaps ON user TYPE set<int> DEFAULT [];\nDEFINE FIELD OVERWRITE updated_at ON user type datetime VALUE time::now();\nDEFINE FIELD OVERWRITE created_at ON user type datetime VALUE time::now() READONLY;\nDEFINE FIELD OVERWRITE country_name ON user TYPE string;\nDEFINE FIELD OVERWRITE country_code ON user TYPE string;\nDEFINE FIELD OVERWRITE groups ON user FLEXIBLE TYPE array<object>;\nDEFINE FIELD OVERWRITE previous_usernames ON user TYPE array<string>;\nDEFINE FIELD OVERWRITE ranked_and_approved_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE ranked_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE nominated_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE guest_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE loved_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE graveyard_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE pending_beatmapset_count ON user TYPE int;\n\nDEFINE INDEX OVERWRITE country_name_index ON TABLE user COLUMNS country_name;\n","events":"DEFINE EVENT OVERWRITE add_influence ON TABLE influenced_by\nWHEN \n $session.tk.ID == \"backend\" AND $event == \"CREATE\"\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(),\n event_type = \"ADD_INFLUENCE\", \n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE add_influence_beatmap ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($after.beatmaps) > array::len($before.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.in,\n created_at = time::now(),\n event_type = \"ADD_INFLUENCE_BEATMAP\", \n beatmap = array::complement($after.beatmaps, $before.beatmaps).at(0),\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE add_user_beatmap ON TABLE user \nWHEN \n $session.tk.ID == \"backend\" \n AND $event == \"UPDATE\" \n AND array::len($after.beatmaps) > array::len($before.beatmaps) \nTHEN (\n CREATE activity \n SET user = $after.id,\n created_at = time::now(),\n event_type = \"ADD_USER_BEATMAP\", \n beatmap = array::complement($after.beatmaps, $before.beatmaps).at(0)\n);\n\n// edit_bio logs when creating user, so added before != null\nDEFINE EVENT OVERWRITE edit_bio ON TABLE user \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.bio != $after.bio\nTHEN (\n CREATE activity \n SET user = $after.id, \n created_at = time::now(), \n event_type = \"EDIT_BIO\", \n bio = $after.bio\n);\n\nDEFINE EVENT OVERWRITE edit_influence_description ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.description != $after.description\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(), \n event_type = \"EDIT_INFLUENCE_DESC\", \n description = $after.description,\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE edit_influence_type ON TABLE influenced_by \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.influence_type != $after.influence_type \nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(), \n event_type = \"EDIT_INFLUENCE_TYPE\", \n influence_type= $after.influence_type,\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE remove_influence ON TABLE influenced_by\nWHEN \n $session.tk.ID == \"backend\" AND $event == \"DELETE\"\nTHEN (\n CREATE activity \n SET user = $before.in, \n created_at = time::now(),\n event_type = \"REMOVE_INFLUENCE\", \n influence = $before\n);\n\nDEFINE EVENT OVERWRITE remove_influence_beatmap ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($before.beatmaps) > array::len($after.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(),\n event_type = \"REMOVE_INFLUENCE_BEATMAP\", \n beatmap = array::complement($before.beatmaps, $after.beatmaps).at(0),\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE remove_user_beatmap ON TABLE user \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($before.beatmaps) > array::len($after.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.id, \n created_at = time::now(),\n event_type = \"REMOVE_USER_BEATMAP\", \n beatmap = array::complement($before.beatmaps, $after.beatmaps).at(0)\n);\n"}
{"schemas":"DEFINE TABLE OVERWRITE activity_preference SCHEMAFULL;\n\nDEFINE FIELD OVERWRITE login ON activity_preference TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE add_influence ON activity_preference TYPE bool DEFAULT true;\nDEFINE FIELD OVERWRITE remove_influence ON activity_preference TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE add_user_beatmap ON activity_preference TYPE bool DEFAULT true;\nDEFINE FIELD OVERWRITE remove_user_beatmap ON activity_preference TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE add_influence_beatmap ON activity_preference TYPE bool DEFAULT true;\nDEFINE FIELD OVERWRITE remove_influence_beatmap ON activity_preference TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE edit_influence_description ON activity_preference TYPE bool DEFAULT true;\nDEFINE FIELD OVERWRITE edit_influence_type ON activity_preference TYPE bool DEFAULT true;\nDEFINE FIELD OVERWRITE edit_bio ON activity_preference TYPE bool DEFAULT true;\n\nDEFINE FUNCTION OVERWRITE fn::id_or_null($value: any) -> any {\n IF $value.is_none() {\n RETURN NONE;\n }\n ELSE {\n RETURN meta::id($value);\n };\n} PERMISSIONS FULL;\n\nDEFINE FUNCTION OVERWRITE fn::add_possible_nulls($var1: any, $var2: any) -> any {\n IF type::is::none($var1) AND type::is::none($var2) {\n RETURN NONE;\n }\n ELSE {\n RETURN $var1 + $var2;\n };\n} PERMISSIONS FULL;\n\nDEFINE TABLE OVERWRITE influenced_by SCHEMAFULL TYPE RELATION IN user OUT user ENFORCED;\n\nDEFINE FIELD OVERWRITE influence_type on influenced_by TYPE int DEFAULT 1;\nDEFINE FIELD OVERWRITE description ON influenced_by TYPE string DEFAULT \"\";\nDEFINE FIELD OVERWRITE beatmaps ON influenced_by TYPE set<int> DEFAULT [];\nDEFINE FIELD OVERWRITE updated_at ON influenced_by type datetime VALUE time::now();\nDEFINE FIELD OVERWRITE created_at ON influenced_by type datetime VALUE time::now() READONLY;\n\n// COUNTLESS HOURS LOST BECAUSE I USED VALUE INSTEAD OF DEFAULT\nDEFINE FIELD OVERWRITE order on influenced_by TYPE int \nDEFAULT (UPSERT increment:order SET increment+=1 RETURN increment).at(0).values().at(0);\n\nDEFINE INDEX OVERWRITE unique_in_out ON TABLE influenced_by COLUMNS in, out UNIQUE;\n\nDEFINE TABLE OVERWRITE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD OVERWRITE script_name ON script_migration TYPE string;\nDEFINE FIELD OVERWRITE executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;\nDEFINE TABLE OVERWRITE user SCHEMAFULL;\n\nDEFINE FIELD OVERWRITE username ON user TYPE string;\nDEFINE FIELD OVERWRITE avatar_url ON user TYPE string;\nDEFINE FIELD OVERWRITE bio ON user TYPE string DEFAULT \"\";\nDEFINE FIELD OVERWRITE ranked_mapper ON user TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE authenticated ON user TYPE bool DEFAULT false;\nDEFINE FIELD OVERWRITE beatmaps ON user TYPE set<int> DEFAULT [];\nDEFINE FIELD OVERWRITE updated_at ON user type datetime VALUE time::now();\nDEFINE FIELD OVERWRITE created_at ON user type datetime VALUE time::now() READONLY;\nDEFINE FIELD OVERWRITE country_name ON user TYPE string;\nDEFINE FIELD OVERWRITE country_code ON user TYPE string;\nDEFINE FIELD OVERWRITE groups ON user FLEXIBLE TYPE array<object>;\nDEFINE FIELD OVERWRITE previous_usernames ON user TYPE array<string>;\nDEFINE FIELD OVERWRITE ranked_and_approved_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE ranked_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE nominated_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE guest_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE loved_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE graveyard_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE pending_beatmapset_count ON user TYPE int;\nDEFINE FIELD OVERWRITE activity_preference ON user TYPE option<record<activity_preference>>;\n\nDEFINE INDEX OVERWRITE country_name_index ON TABLE user COLUMNS country_name;\n","events":"DEFINE EVENT OVERWRITE add_influence ON TABLE influenced_by\nWHEN \n $session.tk.ID == \"backend\" AND $event == \"CREATE\"\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(),\n event_type = \"ADD_INFLUENCE\", \n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE add_influence_beatmap ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($after.beatmaps) > array::len($before.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.in,\n created_at = time::now(),\n event_type = \"ADD_INFLUENCE_BEATMAP\", \n beatmap = array::complement($after.beatmaps, $before.beatmaps).at(0),\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE add_user_beatmap ON TABLE user \nWHEN \n $session.tk.ID == \"backend\" \n AND $event == \"UPDATE\" \n AND array::len($after.beatmaps) > array::len($before.beatmaps) \nTHEN (\n CREATE activity \n SET user = $after.id,\n created_at = time::now(),\n event_type = \"ADD_USER_BEATMAP\", \n beatmap = array::complement($after.beatmaps, $before.beatmaps).at(0)\n);\n\n// edit_bio logs when creating user, so added before != null\nDEFINE EVENT OVERWRITE edit_bio ON TABLE user \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.bio != $after.bio\nTHEN (\n CREATE activity \n SET user = $after.id, \n created_at = time::now(), \n event_type = \"EDIT_BIO\", \n bio = $after.bio\n);\n\nDEFINE EVENT OVERWRITE edit_influence_description ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.description != $after.description\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(), \n event_type = \"EDIT_INFLUENCE_DESC\", \n description = $after.description,\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE edit_influence_type ON TABLE influenced_by \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND $before.influence_type != $after.influence_type \nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(), \n event_type = \"EDIT_INFLUENCE_TYPE\", \n influence_type= $after.influence_type,\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE remove_influence ON TABLE influenced_by\nWHEN \n $session.tk.ID == \"backend\" AND $event == \"DELETE\"\nTHEN (\n CREATE activity \n SET user = $before.in, \n created_at = time::now(),\n event_type = \"REMOVE_INFLUENCE\", \n influence = $before\n);\n\nDEFINE EVENT OVERWRITE remove_influence_beatmap ON TABLE influenced_by\nWHEN\n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($before.beatmaps) > array::len($after.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.in, \n created_at = time::now(),\n event_type = \"REMOVE_INFLUENCE_BEATMAP\", \n beatmap = array::complement($before.beatmaps, $after.beatmaps).at(0),\n influence = {\n id: $after.id,\n out: $after.out,\n }\n);\n\nDEFINE EVENT OVERWRITE remove_user_beatmap ON TABLE user \nWHEN \n $session.tk.ID == \"backend\"\n AND $event == \"UPDATE\"\n AND array::len($before.beatmaps) > array::len($after.beatmaps)\nTHEN (\n CREATE activity \n SET user = $after.id, \n created_at = time::now(),\n event_type = \"REMOVE_USER_BEATMAP\", \n beatmap = array::complement($before.beatmaps, $after.beatmaps).at(0)\n);\n"}
12 changes: 12 additions & 0 deletions migrations/schemas/activity_preference.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DEFINE TABLE OVERWRITE activity_preference SCHEMAFULL;

DEFINE FIELD OVERWRITE login ON activity_preference TYPE bool DEFAULT false;
DEFINE FIELD OVERWRITE add_influence ON activity_preference TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE remove_influence ON activity_preference TYPE bool DEFAULT false;
DEFINE FIELD OVERWRITE add_user_beatmap ON activity_preference TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE remove_user_beatmap ON activity_preference TYPE bool DEFAULT false;
DEFINE FIELD OVERWRITE add_influence_beatmap ON activity_preference TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE remove_influence_beatmap ON activity_preference TYPE bool DEFAULT false;
DEFINE FIELD OVERWRITE edit_influence_description ON activity_preference TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE edit_influence_type ON activity_preference TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE edit_bio ON activity_preference TYPE bool DEFAULT true;
1 change: 1 addition & 0 deletions migrations/schemas/user.surql
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ DEFINE FIELD OVERWRITE guest_beatmapset_count ON user TYPE int;
DEFINE FIELD OVERWRITE loved_beatmapset_count ON user TYPE int;
DEFINE FIELD OVERWRITE graveyard_beatmapset_count ON user TYPE int;
DEFINE FIELD OVERWRITE pending_beatmapset_count ON user TYPE int;
DEFINE FIELD OVERWRITE activity_preference ON user TYPE option<record<activity_preference>>;

DEFINE INDEX OVERWRITE country_name_index ON TABLE user COLUMNS country_name;
3 changes: 2 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async fn main() {
//db.get_inner_ref().query("delete activity").await.unwrap();

let mut handlers = Vec::new();
let arc_db = Arc::new(db);
let arc_db = db.clone();
for user in full_users {
let order_vec: Vec<u32> = user
.user
Expand All @@ -223,5 +223,6 @@ async fn main() {
}
join_all(handlers).await;
println!("custom order insertion done");

println!("done");
}
Loading

0 comments on commit 391086a

Please sign in to comment.