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 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 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;
title = json |> member "title" |> 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 rec fold_fn (accum: (Datatypes.reminder list)) = function
let rec fold_fn (accum: (Datatypes.forgejo_issue_data list)) = function
| [] -> Ok accum
| (m_room_string, {url; headers})::rest ->
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 ->
Httpclient.make_get_request http_actor
|> Result.map Pam.Datatypes.forgejo_issues
|> Result.map_error Pam.Datatypes.forgejo_error
|> Result.fold ~ok:identity ~error:identity
|> Riot.send_by_name ~name:_MQ_CLIENT
|> Result.map_error Pam.Datatypes.internal_failure
|> Result.fold ~ok:identity ~error:(fun e -> [e])
|> List.map (Riot.send_by_name ~name:_MQ_CLIENT)
| m -> unhandled m
@ -45,13 +45,13 @@ let mq_client (mq_url, mq_user, mq_password) =
let _ =
try%lwt
match Riot.receive ~after:one_second () with
| ForgejoError err ->
| InternalFailure err ->
let _ = Pamlog.error [%string "Got error from Forgejo: %{err}"] in
Mq.mq_publish mq err
| ForgejoIssues reminders ->
let _ = [%string "Got reminders: %{Batteries.dump reminders}"] |> print_endline in
let rems = List.map pprint reminders in
Mq.mq_publish_all mq rems
| Reminder reminder ->
let _ = [%string "Got reminders: %{Batteries.dump reminder}"] |> print_endline in
let rems = pprint reminder in
Mq.mq_publish mq rems
| m -> unhandled m
with | Riot.Receive_timeout -> Lwt.return_unit
in

View file

@ -6,7 +6,7 @@ module MatrixRoom = struct
let make str = str
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;
title: string;
due_date: string option;
@ -18,9 +18,8 @@ type Riot.Message.t +=
| RegisterClient of (client_id * Riot.Pid.t)
| LookupClient of client_id
| ListIssues
| ForgejoIssues of reminder list
| ForgejoError of string
| Reminder of forgejo_issue_data
| InternalFailure of string
let forgejo_issues lst = ForgejoIssues lst
let forgejo_error reason = ForgejoError reason
let forgejo_issues issues = issues |> List.map (fun r -> Reminder r)
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_}
| _ -> 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=Some due_date; body=body; _} ->
let rec all acc = function [] -> Ok acc | Ok x::xs -> all (x::acc) xs | Error e::_ -> Error e in