Gleam Lustre Fullstack Atproto Demo App w/Slices.Network GraphQL API
1import api/graphql
2import gleam/io
3import gleam/json
4import gleam/option.{None, Some}
5import gleam/time/duration
6import gleam/time/timestamp
7import shared/api/graphql/create_profile as create_profile_gql
8
9/// Initialize user profile by:
10/// 1. Checking if profile already exists
11/// 2. Syncing user collections (Bluesky data)
12/// 3. Fetching Bluesky profile
13/// 4. Creating org.atmosphereconf.profile with Bluesky data
14pub fn initialize_user_profile(
15 config: graphql.Config,
16 user_did: String,
17 user_handle: String,
18) -> Result(Nil, String) {
19 // 1. Check if profile already exists
20 case graphql.check_profile_exists(config, user_did) {
21 Ok(True) -> {
22 // User already has a profile, nothing to do
23 io.println("Profile already exists for " <> user_did)
24 Ok(Nil)
25 }
26 Ok(False) -> {
27 io.println("Initializing profile for " <> user_did)
28
29 // 2. Sync user collections (to get Bluesky data)
30 let sync_result = graphql.sync_user_collections(config, user_did)
31 case sync_result {
32 Error(err) -> {
33 io.println("Warning: Failed to sync collections: " <> err)
34 // Continue anyway, we can still create a basic profile
35 Nil
36 }
37 Ok(_) -> {
38 io.println("Successfully synced user collections")
39 Nil
40 }
41 }
42
43 // 3. Fetch Bluesky profile data
44 let bsky_profile_result = graphql.get_bluesky_profile(config, user_did)
45
46 // 4. Create org.atmosphereconf.profile with Bluesky data
47 let display_name = case bsky_profile_result {
48 Ok(Some(profile)) ->
49 case profile.display_name {
50 Some(name) -> name
51 None -> user_handle
52 }
53 _ -> user_handle
54 }
55
56 let description = case bsky_profile_result {
57 Ok(Some(profile)) -> profile.description
58 _ -> None
59 }
60
61 let avatar = case bsky_profile_result {
62 Ok(Some(profile)) ->
63 case profile.avatar {
64 Some(avatar_blob) ->
65 Some(
66 json.object([
67 #("ref", json.string(avatar_blob.ref)),
68 #("mimeType", json.string(avatar_blob.mime_type)),
69 #("size", json.int(avatar_blob.size)),
70 ]),
71 )
72 None -> None
73 }
74 _ -> None
75 }
76
77 let now = timestamp.system_time()
78 let created_at = timestamp.to_rfc3339(now, duration.seconds(0))
79
80 let profile_input =
81 create_profile_gql.OrgAtmosphereconfProfileInput(
82 display_name: Some(display_name),
83 description: description,
84 avatar: avatar,
85 created_at: Some(created_at),
86 home_town: None,
87 interests: None,
88 )
89
90 case graphql.create_profile(config, profile_input) {
91 Ok(_) -> {
92 io.println("Successfully initialized profile for " <> user_did)
93 Ok(Nil)
94 }
95 Error(err) -> {
96 io.println("Failed to create profile: " <> err)
97 Error(err)
98 }
99 }
100 }
101 Error(err) -> {
102 io.println("Failed to check if profile exists: " <> err)
103 Error(err)
104 }
105 }
106}