Skip to content

Commit 2aebf79

Browse files
committed
getting diffs via View.diff for recent_activity page
1 parent 3343a63 commit 2aebf79

3 files changed

Lines changed: 40 additions & 25 deletions

File tree

canopy_article.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ let to_tyxml article =
4949
Tyxml.Html.article [Unsafe.data article.content]
5050
]]
5151

52-
let to_tyxml_history cache =
53-
let activity_list = KeyMap.fold (fun k v list -> v :: list) cache [] in
54-
let format_activity activity =
55-
li ~a:[] [pcdata activity]
52+
let to_tyxml_history history_cache =
53+
let format_activity history =
54+
li ~a:[] [pcdata history]
5655
in
57-
let activity_list_html = List.map format_activity activity_list in
56+
let activity_list_html = List.map format_activity history_cache in
5857
[div
5958
[ul ~a:[] activity_list_html]]
6059

canopy_content.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ type error_t =
88
| Error of string
99
| Ok of content_t
1010

11+
let of_c_history timestamp diffs =
12+
List.map (fun diff ->
13+
let date = match timestamp with
14+
| None -> ""
15+
| Some timestamp -> ptime_to_pretty_date timestamp
16+
in
17+
match diff with
18+
| _, `Added value -> Printf.sprintf "+ : %s %s" value date
19+
| _, `Removed value -> Printf.sprintf "- : %s %s" value date
20+
| _, `Updated (removed, added) -> Printf.sprintf "* : %s and %s %s" removed added date
21+
) diffs
22+
1123
let meta_assoc str =
1224
Re_str.split (Re_str.regexp "\n") str |>
1325
List.map (fun meta ->

canopy_store.ml

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ open Lwt.Infix
22
open Canopy_config
33
open Canopy_utils
44

5+
56
module Store (CTX: Irmin_mirage.CONTEXT) (INFL: Git.Inflate.S) = struct
67

78
module Hash = Irmin.Hash.SHA1
89
module Mirage_git_memory = Irmin_mirage.Irmin_git.Memory(CTX)(INFL)
910
module Store = Mirage_git_memory(Irmin.Contents.String)(Irmin.Ref.String)(Hash)
1011
module Sync = Irmin.Sync(Store)
1112
module Topological = Graph.Topological.Make(Store.History)
13+
module View = Irmin.View(Store)
1214

1315
let src = Logs.Src.create "canopy-store" ~doc:"Canopy store logger"
1416
module Log = (val Logs.src_log src : Logs.LOG)
@@ -77,33 +79,35 @@ module Store (CTX: Irmin_mirage.CONTEXT) (INFL: Git.Inflate.S) = struct
7779
in
7880
Topological.fold aux history (Lwt.return (commit, commit, None))
7981

82+
let set_diffs repo c1 c2 =
83+
let view_of_commit repo commit_id =
84+
Store.of_commit_id task commit_id repo >>= fun t ->
85+
View.of_path (t "view") []
86+
in
87+
view_of_commit repo c1 >>= fun v1 ->
88+
view_of_commit repo c2 >>= fun v2 ->
89+
View.diff v1 v2 >|= fun diffs ->
90+
diffs
91+
8092
let fill_history_cache () =
8193
new_task () >>= fun t ->
8294
repo () >>= fun repo ->
8395
Store.history (t "Reading history") >>= fun history ->
84-
let fn key value cache =
85-
value () >>= fun value ->
86-
match key_type key with
87-
| `Article -> (
88-
let uri = String.concat "/" key in
89-
match KeyMap.find_opt cache key with
90-
| None ->
91-
let create_event = Printf.sprintf "Article added: %s" uri in
92-
KeyMap.add key create_event cache |> Lwt.return
93-
| Some old_value ->
94-
if old_value = value then Lwt.return cache
95-
else
96-
let update_event = Printf.sprintf "Article modified: %s" uri in
97-
KeyMap.add key update_event cache |> Lwt.return
98-
)
99-
| `Static | `Config -> Lwt.return cache
100-
in
10196
let aux commit_id acc =
102-
Store.of_commit_id (Irmin.Task.none) commit_id repo >>= fun store ->
10397
acc >>= fun acc ->
104-
fold (store ()) fn acc
98+
match acc with
99+
| None, acc_list ->
100+
(Some commit_id, acc_list) |> Lwt.return
101+
| Some prev_commit_id, acc_list ->
102+
Store.Repo.task_of_commit_id repo commit_id >>= fun task ->
103+
let timestamp = Irmin.Task.date task |> Int64.to_float |> Ptime.of_float_s in
104+
set_diffs repo prev_commit_id commit_id >|= fun diffs ->
105+
let c_history = Canopy_content.of_c_history timestamp diffs in
106+
let acc_list = List.append acc_list c_history in
107+
(Some commit_id, acc_list)
105108
in
106-
Topological.fold aux history (Lwt.return KeyMap.empty)
109+
Topological.fold aux history (Lwt.return (None, [])) >|= fun (_, diffs) ->
110+
diffs
107111

108112
let date_updated_created key =
109113
new_task () >>= fun t ->

0 commit comments

Comments
 (0)