Diffusion: a simple federated social site, built in Gleam
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

some more testing against my sloppy JS impl

+72 -8
+2
gleam.toml
··· 18 18 wisp = ">= 2.1.0 and < 3.0.0" 19 19 mist = ">= 5.0.3 and < 6.0.0" 20 20 gleam_time = ">= 1.4.0 and < 2.0.0" 21 + gleam_http = ">= 4.3.0 and < 5.0.0" 22 + rsa_keys = ">= 2.1.0 and < 3.0.0" 21 23 22 24 [dev-dependencies] 23 25 gleeunit = ">= 1.0.0 and < 2.0.0"
+3
manifest.toml
··· 24 24 { name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" }, 25 25 { name = "mist", version = "5.0.3", build_tools = ["gleam"], requirements = ["exception", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gleam_yielder", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "7C4BE717A81305323C47C8A591E6B9BA4AC7F56354BF70B4D3DF08CC01192668" }, 26 26 { name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" }, 27 + { name = "rsa_keys", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rsa_keys", source = "hex", outer_checksum = "9C5C7EA035904EED0C187AD8D56F506BD2C792D8DC17AE6024CE55216F9BD94B" }, 27 28 { name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" }, 28 29 { name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" }, 29 30 { name = "wisp", version = "2.1.0", build_tools = ["gleam"], requirements = ["directories", "exception", "filepath", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "houdini", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "362BDDD11BF48EB38CDE51A73BC7D1B89581B395CA998E3F23F11EC026151C54" }, 30 31 ] 31 32 32 33 [requirements] 34 + gleam_http = { version = ">= 4.3.0 and < 5.0.0" } 33 35 gleam_httpc = { version = ">= 5.0.0 and < 6.0.0" } 34 36 gleam_json = { version = ">= 3.0.2 and < 4.0.0" } 35 37 gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } 36 38 gleam_time = { version = ">= 1.4.0 and < 2.0.0" } 37 39 gleeunit = { version = ">= 1.0.0 and < 2.0.0" } 38 40 mist = { version = ">= 5.0.3 and < 6.0.0" } 41 + rsa_keys = { version = ">= 2.1.0 and < 3.0.0" } 39 42 wisp = { version = ">= 2.1.0 and < 3.0.0" }
+6 -1
src/diffusion.gleam
··· 19 19 // } 20 20 21 21 pub type User { 22 - User(internal_id: Int, username: String, public_key: String) 22 + User( 23 + internal_id: Int, 24 + username: String, 25 + public_key: String, 26 + private_key: String, 27 + ) 23 28 } 24 29 25 30 pub fn user_to_actor_json(domain: String, user: User) -> json.Json {
+61 -7
test/diffusion_test.gleam
··· 1 1 import diffusion 2 + import gleam/http 3 + import gleam/http/request 4 + import gleam/httpc 2 5 import gleam/json 6 + import gleam/string 3 7 import gleeunit 4 - 5 - const user = diffusion.User(1, "isaac", "abcd") 8 + import rsa_keys 6 9 7 10 pub fn main() -> Nil { 8 11 gleeunit.main() 9 12 } 10 13 11 - // gleeunit test functions end in `_test` 14 + // Utils 15 + const user = diffusion.User(1, "isaac", "abc", "def") 16 + 17 + fn base_req() { 18 + request.new() 19 + |> request.set_body("{\"username\":\"isaac\"}") 20 + |> request.set_header("Content-Type", "application/json") 21 + |> request.set_scheme(http.Http) 22 + |> request.set_host("localhost:3000") 23 + } 24 + 25 + pub fn create_rsa_keypair_test() { 26 + let #(public_key, private_key) = rsa_keys.generate_rsa_keys() 27 + 28 + assert string.length(public_key.pem) > 0 29 + assert string.length(private_key.pem) > 0 30 + } 31 + 12 32 pub fn encode_actor_test() { 13 - diffusion.user_to_actor_json("gleam.social", user) 14 - |> echo 15 - |> json.to_string 16 - |> echo 33 + let encoded = 34 + diffusion.user_to_actor_json("gleam.social", user) 35 + |> json.to_string 36 + 37 + assert encoded 38 + == "{\"id\":\"https://gleam.social/users/isaac\",\"type\":\"Person\",\"preferredUsername\":\"isaac\",\"inbox\":\"https://gleam.social/users/isaac/inbox\",\"outbox\":\"https://gleam.social/users/isaac/outbox\",\"followers\":\"https://gleam.social/users/isaac/followers\",\"following\":\"https://gleam.social/users/isaac/following\",\"publicKey\":{\"id\":\"https://gleam.social/users/isaac#main-key\",\"owner\":\"https://gleam.social/users/isaac\",\"publicKeyPem\":\"abc\"}}" 39 + } 40 + 41 + pub fn http_create_user_test() { 42 + let assert Ok(res) = 43 + base_req() 44 + |> request.set_method(http.Post) 45 + |> request.set_path("/admin/create-user") 46 + |> httpc.send 47 + 48 + assert { res.status >= 200 && res.status < 300 } 49 + || res.status == 400 50 + && res.body == "{\"error\":\"User already exists\"}" 51 + } 52 + 53 + pub fn http_post_on_profile_test() { 54 + let assert Ok(res) = 55 + base_req() 56 + |> request.set_method(http.Post) 57 + |> request.set_path("/users/isaac/outbox") 58 + |> request.set_body("{\"content\": \"Hello! This is my first post.\"}") 59 + |> httpc.send 60 + 61 + assert res.status >= 200 && res.status < 300 62 + } 63 + 64 + pub fn http_view_user_outbox_test() { 65 + let assert Ok(res) = 66 + base_req() 67 + |> request.set_path("/users/isaac/outbox") 68 + |> httpc.send 69 + 70 + assert res.status >= 200 && res.status < 300 17 71 }