Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/server/src/fbi/handlers/settings.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fbi/run/image_gc
import gleam/dynamic/decode
import gleam/http
import gleam/json
import gleam/option.{None}
import gleam/option.{type Option, None}
import wisp.{type Request, type Response}

pub fn handle(req: Request, ctx: Context) -> Response {
Expand Down Expand Up @@ -61,15 +61,15 @@ fn update(req: Request, ctx: Context) -> Response {
None,
decode.optional(decode.bool),
)
use global_marketplaces_json <- decode.optional_field(
"global_marketplaces_json",
use global_marketplaces <- decode.optional_field(
"global_marketplaces",
None,
decode.optional(decode.string),
decode.optional(decode.list(decode.string)),
)
use global_plugins_json <- decode.optional_field(
"global_plugins_json",
use global_plugins <- decode.optional_field(
"global_plugins",
None,
decode.optional(decode.string),
decode.optional(decode.list(decode.string)),
)
use usage_notifications_enabled <- decode.optional_field(
"usage_notifications_enabled",
Expand All @@ -83,8 +83,8 @@ fn update(req: Request, ctx: Context) -> Response {
auto_resume_max_attempts,
concurrency_warn_at,
image_gc_enabled,
global_marketplaces_json,
global_plugins_json,
global_marketplaces,
global_plugins,
usage_notifications_enabled,
))
}
Expand All @@ -97,11 +97,16 @@ fn update(req: Request, ctx: Context) -> Response {
auto_resume_max_attempts,
concurrency_warn_at,
image_gc_enabled,
global_marketplaces_json,
global_plugins_json,
global_marketplaces,
global_plugins,
usage_notifications_enabled,
)) -> {
let now = now_ms()
let encode_list = fn(ms: Option(List(String))) {
option.map(ms, fn(xs) {
json.array(xs, json.string) |> json.to_string()
})
}
case
settings.patch(
ctx.db,
Expand All @@ -111,8 +116,8 @@ fn update(req: Request, ctx: Context) -> Response {
auto_resume_max_attempts,
concurrency_warn_at,
image_gc_enabled,
global_marketplaces_json,
global_plugins_json,
encode_list(global_marketplaces),
encode_list(global_plugins),
usage_notifications_enabled,
now,
)
Expand Down
17 changes: 15 additions & 2 deletions src/server/src/fbi/json/settings.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fbi/db/settings.{type Settings}
import gleam/dynamic/decode
import gleam/json
import gleam/result

fn parse_string_list(raw: String) -> List(String) {
json.parse(raw, decode.list(decode.string))
|> result.unwrap([])
}

pub fn encode(s: Settings) -> json.Json {
json.object([
Expand All @@ -11,8 +18,14 @@ pub fn encode(s: Settings) -> json.Json {
#("last_gc_at", json.nullable(s.last_gc_at, json.int)),
#("last_gc_count", json.nullable(s.last_gc_count, json.int)),
#("last_gc_bytes", json.nullable(s.last_gc_bytes, json.int)),
#("global_marketplaces_json", json.string(s.global_marketplaces_json)),
#("global_plugins_json", json.string(s.global_plugins_json)),
#(
"global_marketplaces",
json.array(parse_string_list(s.global_marketplaces_json), json.string),
),
#(
"global_plugins",
json.array(parse_string_list(s.global_plugins_json), json.string),
),
#("auto_resume_enabled", json.bool(s.auto_resume_enabled)),
#("auto_resume_max_attempts", json.int(s.auto_resume_max_attempts)),
#("usage_notifications_enabled", json.bool(s.usage_notifications_enabled)),
Expand Down
Loading