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: move reading and parsing body to their own functions

Kacaii 970a6460 07de37f1

+28 -20
+28 -20
src/app/web/socket.gleam
··· 14 14 import gleam/json 15 15 import gleam/list 16 16 import gleam/option 17 - import gleam/result 17 + import gleam/result.{try} 18 18 import gleam/time/timestamp 19 19 import group_registry 20 20 import mist ··· 254 254 |> process.select(group_subject) 255 255 |> process.select(user_subject) 256 256 257 - let body = { 258 - let max_length = 1024 * 1024 * 10 257 + // 󰘦 Parse body 258 + let body = read_body(req) 259 + let subscribed_result = parse_body(result.unwrap(body, "")) 259 260 260 - use req <- result.try( 261 - mist.read_body(req, max_length) 262 - |> result.replace_error(Nil), 263 - ) 261 + #( 262 + State(user_uuid:, subscribed: result.unwrap(subscribed_result, [])), 263 + option.Some(selector), 264 + ) 265 + } 264 266 265 - use body <- result.map( 266 - bit_array.to_string(req.body) 267 - |> result.replace_error(Nil), 268 - ) 269 - 270 - body 271 - } 272 - 267 + fn parse_body(body: String) -> Result(List(category.Category), json.DecodeError) { 273 268 let parse_result = 274 - json.parse(result.unwrap(body, ""), { 269 + json.parse(body, { 275 270 use subscribe_to <- decode.field( 276 271 "subscribe", 277 272 decode.list(category.decoder_pt_br()), ··· 279 274 280 275 decode.success(subscribe_to) 281 276 }) 277 + parse_result 278 + } 282 279 283 - #( 284 - State(user_uuid:, subscribed: result.unwrap(parse_result, [])), 285 - option.Some(selector), 280 + fn read_body(req: request.Request(mist.Connection)) -> Result(String, Nil) { 281 + use req <- try( 282 + mist.read_body(req, 1024 * 1024 * 10) 283 + |> result.replace_error(Nil), 284 + ) 285 + 286 + use body <- result.map( 287 + bit_array.to_string(req.body) 288 + |> result.replace_error(Nil), 286 289 ) 290 + 291 + body 287 292 } 288 293 289 294 // ON CLOSE -------------------------------------------------------------------- 290 295 291 296 fn ws_on_close( 292 - state _state: State, 297 + state state: State, 293 298 ctx _ctx: Context, 294 299 registry registry: group_registry.GroupRegistry(msg.Msg), 295 300 ) -> Nil { 296 301 group_registry.leave(registry, topic, [process.self()]) 302 + group_registry.leave(registry, uuid.to_string(state.user_uuid), [ 303 + process.self(), 304 + ]) 297 305 } 298 306 299 307 // HELPERS ---------------------------------------------------------------------