wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
0
fork

Configure Feed

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

:recycle: qualify `result.try`

Kacaii 54a1d97a 05fc7cc1

+26 -35
+1 -3
src/app/routes/occurrence.gleam
··· 44 44 registry registry: group_registry.GroupRegistry(msg.Msg), 45 45 ) -> Nil { 46 46 let members = group_registry.members(registry, uuid.to_string(user_id)) 47 - 48 47 use subject <- list.each(members) 49 48 process.send(subject, msg.UserAssignedToOccurrence(user_id:, occurrence_id:)) 50 49 } ··· 55 54 of occ_type: category.Category, 56 55 registry registry: group_registry.GroupRegistry(msg.Msg), 57 56 ) -> Nil { 58 - let members = group_registry.members(registry, socket.topic) 59 - 57 + let members = group_registry.members(registry, socket.ws_topic) 60 58 use subject <- list.each(members) 61 59 process.send(subject, msg.NewOccurrence(occ_id:, occ_type:)) 62 60 }
+25 -32
src/app/web/socket.gleam
··· 15 15 import gleam/json 16 16 import gleam/list 17 17 import gleam/option 18 - import gleam/result.{try} 18 + import gleam/result 19 19 import gleam/time/timestamp 20 20 import group_registry 21 21 import mist 22 22 import youid/uuid 23 23 24 - pub const topic = "active_users" 24 + pub const ws_topic = "active_users" 25 25 26 26 /// Connecting to a websocket can fail 27 27 pub opaque type WebSocketError { ··· 186 186 data_type data_type: String, 187 187 data data: json.Json, 188 188 ) -> mist.Next(State, msg.Msg) { 189 - //  Build metadata 190 189 let meta = 191 190 envelope.MetaData( 192 191 timestamp: timestamp.system_time(), 193 192 request_id: state.user_uuid, 194 193 ) 195 194 196 - // 󰛮 Wrap envelope 197 - let envelope_json = 198 - envelope.to_json(envelope.Envelope(data_type:, data:, meta:)) 195 + let frame_result = 196 + envelope.Envelope(data_type:, data:, meta:) 197 + |> envelope.to_json 198 + |> json.to_string 199 + |> mist.send_text_frame(conn, _) 199 200 200 - // 󱅡 Send data 201 - let msg_result = mist.send_text_frame(conn, json.to_string(envelope_json)) 202 - case msg_result { 203 - Error(_) -> mist.stop_abnormal("Failed to send message to User") 201 + case frame_result { 202 + Error(_) -> mist.stop_abnormal("Failed to send text frame") 204 203 Ok(_) -> mist.continue(state) 205 204 } 206 205 } ··· 212 211 let cookies = request.get_cookies(req) 213 212 let salt = <<ctx.secret_key_base:utf8>> 214 213 215 - use hashed_uuid <- try( 214 + use hashed_uuid <- result.try( 216 215 list.key_find(cookies, user.uuid_cookie_name) 217 216 |> result.replace_error(MissingCookie), 218 217 ) 219 218 220 - use decrypted <- try( 219 + use decrypted <- result.try( 221 220 crypto.verify_signed_message(hashed_uuid, salt) 222 221 |> result.replace_error(InvalidSignature), 223 222 ) 224 223 225 - use maybe_uuid_str <- try( 224 + use maybe_uuid_str <- result.try( 226 225 bit_array.to_string(decrypted) 227 226 |> result.replace_error(InvalidUtf8), 228 227 ) 229 228 230 - use user_uuid <- try( 229 + use user_uuid <- result.try( 231 230 uuid.from_string(maybe_uuid_str) 232 231 |> result.replace_error(InvalidUuid(maybe_uuid_str)), 233 232 ) ··· 245 244 registry registry: group_registry.GroupRegistry(msg.Msg), 246 245 ) -> #(State, option.Option(process.Selector(msg.Msg))) { 247 246 let self = process.self() 248 - let group_subject = group_registry.join(registry, topic, self) 247 + let group_subject = group_registry.join(registry, ws_topic, self) 249 248 250 - let user_subject = 251 - group_registry.join(registry, uuid.to_string(user_uuid), self) 249 + let user_topic = uuid.to_string(user_uuid) 250 + let user_subject = group_registry.join(registry, user_topic, self) 252 251 253 252 let selector = 254 253 process.new_selector() ··· 265 264 } 266 265 267 266 fn parse_body(body: String) -> Result(List(category.Category), json.DecodeError) { 268 - let parse_result = 269 - json.parse(body, { 270 - use subscribe_to <- decode.field( 271 - "subscribe", 272 - decode.list(category.decoder_pt_br()), 273 - ) 274 - 275 - decode.success(subscribe_to) 276 - }) 277 - 278 - parse_result 267 + json.parse(body, { 268 + let category_list_decoder = decode.list(category.decoder_pt_br()) 269 + use subscribe_to <- decode.field("subscribe", category_list_decoder) 270 + decode.success(subscribe_to) 271 + }) 279 272 } 280 273 281 274 fn read_body( 282 275 req: request.Request(mist.Connection), 283 276 ) -> Result(String, mist.ReadError) { 284 - let max_body_limit = 277 + let req_result = 285 278 request.get_header(req, "content-length") 286 279 |> result.try(int.parse) 287 280 |> result.unwrap(0) 288 - 289 - use req <- result.map(mist.read_body(req, max_body_limit:)) 281 + |> mist.read_body(req, _) 290 282 283 + use req <- result.map(req_result) 291 284 req.body 292 285 |> bit_array.to_string 293 286 |> result.unwrap("") ··· 301 294 registry registry: group_registry.GroupRegistry(msg.Msg), 302 295 ) -> Nil { 303 296 let self = process.self() 304 - group_registry.leave(registry, topic, [self]) 297 + group_registry.leave(registry, ws_topic, [self]) 305 298 group_registry.leave(registry, uuid.to_string(state.user_uuid), [self]) 306 299 } 307 300