refactor datatypes

This commit is contained in:
Benedetta 2024-04-24 09:45:20 +02:00
parent 8b695ac466
commit a1679df6a6
4 changed files with 16 additions and 17 deletions

View file

@ -6,10 +6,10 @@ open Config
open Utils open Utils
open Pam open Pam
let issue_of_json (m_room: Datatypes.MatrixRoom.t) (json): Datatypes.reminder = let issue_of_json (m_room: Datatypes.MatrixRoom.t) (json): Datatypes.forgejo_issue_data =
let open Yojson.Safe.Util in let open Yojson.Safe.Util in
let due_date = json |> member "due_date" |> to_option to_string in let due_date = json |> member "due_date" |> to_option to_string in
let record: Datatypes.reminder = { let record: Datatypes.forgejo_issue_data = {
url = json |> member "url" |> to_string; url = json |> member "url" |> to_string;
title = json |> member "title" |> to_string; title = json |> member "title" |> to_string;
body = json |> member "body" |> to_string; body = json |> member "body" |> to_string;
@ -73,7 +73,7 @@ let make_get_request {requests; repos} =
let get = Py.Module.get_function_with_keywords requests "get" in let get = Py.Module.get_function_with_keywords requests "get" in
let rec fold_fn (accum: (Datatypes.reminder list)) = function let rec fold_fn (accum: (Datatypes.forgejo_issue_data list)) = function
| [] -> Ok accum | [] -> Ok accum
| (m_room_string, {url; headers})::rest -> | (m_room_string, {url; headers})::rest ->
let pyurl = url |> ForgejoUrl.to_string |> Py.String.of_string in let pyurl = url |> ForgejoUrl.to_string |> Py.String.of_string in

View file

@ -19,9 +19,9 @@ let http_client (repos: Config.repo_data list) =
| ListIssues -> | ListIssues ->
Httpclient.make_get_request http_actor Httpclient.make_get_request http_actor
|> Result.map Pam.Datatypes.forgejo_issues |> Result.map Pam.Datatypes.forgejo_issues
|> Result.map_error Pam.Datatypes.forgejo_error |> Result.map_error Pam.Datatypes.internal_failure
|> Result.fold ~ok:identity ~error:identity |> Result.fold ~ok:identity ~error:(fun e -> [e])
|> Riot.send_by_name ~name:_MQ_CLIENT |> List.map (Riot.send_by_name ~name:_MQ_CLIENT)
| m -> unhandled m | m -> unhandled m
@ -45,13 +45,13 @@ let mq_client (mq_url, mq_user, mq_password) =
let _ = let _ =
try%lwt try%lwt
match Riot.receive ~after:one_second () with match Riot.receive ~after:one_second () with
| ForgejoError err -> | InternalFailure err ->
let _ = Pamlog.error [%string "Got error from Forgejo: %{err}"] in let _ = Pamlog.error [%string "Got error from Forgejo: %{err}"] in
Mq.mq_publish mq err Mq.mq_publish mq err
| ForgejoIssues reminders -> | Reminder reminder ->
let _ = [%string "Got reminders: %{Batteries.dump reminders}"] |> print_endline in let _ = [%string "Got reminders: %{Batteries.dump reminder}"] |> print_endline in
let rems = List.map pprint reminders in let rems = pprint reminder in
Mq.mq_publish_all mq rems Mq.mq_publish mq rems
| m -> unhandled m | m -> unhandled m
with | Riot.Receive_timeout -> Lwt.return_unit with | Riot.Receive_timeout -> Lwt.return_unit
in in

View file

@ -6,7 +6,7 @@ module MatrixRoom = struct
let make str = str let make str = str
end end
type reminder = { (* from an issue in forgejo get a reminder *) type forgejo_issue_data = { (* from an issue in forgejo get a reminder *)
url: string; url: string;
title: string; title: string;
due_date: string option; due_date: string option;
@ -18,9 +18,8 @@ type Riot.Message.t +=
| RegisterClient of (client_id * Riot.Pid.t) | RegisterClient of (client_id * Riot.Pid.t)
| LookupClient of client_id | LookupClient of client_id
| ListIssues | ListIssues
| ForgejoIssues of reminder list | Reminder of forgejo_issue_data
| ForgejoError of string
| InternalFailure of string | InternalFailure of string
let forgejo_issues lst = ForgejoIssues lst let forgejo_issues issues = issues |> List.map (fun r -> Reminder r)
let forgejo_error reason = ForgejoError reason let internal_failure reason = InternalFailure reason

View file

@ -69,7 +69,7 @@ let to_alert_request str =
| h::m::[] when is_number h && is_number m -> Ok {date_offset=offset; exact_time=(int_of_string h, int_of_string m); when_=when_} | h::m::[] when is_number h && is_number m -> Ok {date_offset=offset; exact_time=(int_of_string h, int_of_string m); when_=when_}
| _ -> Error [%string "Malformed time: %{str}"]) | _ -> Error [%string "Malformed time: %{str}"])
let to_datetime: Datatypes.reminder -> (Ptime.t list option, string) result = function let to_datetime: Datatypes.forgejo_issue_data -> (Ptime.t list option, string) result = function
| {due_date=None; _} -> Ok None | {due_date=None; _} -> Ok None
| {due_date=Some due_date; body=body; _} -> | {due_date=Some due_date; body=body; _} ->
let rec all acc = function [] -> Ok acc | Ok x::xs -> all (x::acc) xs | Error e::_ -> Error e in let rec all acc = function [] -> Ok acc | Ok x::xs -> all (x::acc) xs | Error e::_ -> Error e in