diff --git a/handlers/dashboard.go b/handlers/dashboard.go index e75a537dd..48c6323b7 100644 --- a/handlers/dashboard.go +++ b/handlers/dashboard.go @@ -178,38 +178,15 @@ func RecentActivity(w http.ResponseWriter, r *http.Request) { c.HandleError(w, r, err) return } + for _, p := range pHits.Hits { prevP, err := c.Repo.GetPublicationSnapshotBefore(p.ID, *p.DateFrom) if err != nil && !errors.Is(err, models.ErrNotFound) { c.HandleError(w, r, err) return } - act := views.Activity{ - Object: views.PublicationObject, - User: p.User, - Datestamp: *p.DateUpdated, - URL: c.PathTo("publication", "id", p.ID).String(), - Status: p.Status, - Title: p.Title, - } - if prevP == nil { - act.Event = views.CreateEvent - } else if p.Status == "public" && prevP.Status == "returned" { - act.Event = views.RepublishEvent - } else if p.Status == "public" && prevP.Status != "public" { - act.Event = views.PublishEvent - } else if p.Status == "returned" && prevP.Status != "returned" { - act.Event = views.WithdrawEvent - } else if p.Locked && !prevP.Locked { - act.Event = views.LockEvent - } else if !p.Locked && prevP.Locked { - act.Event = views.UnlockEvent - } else if p.Message != "" && p.Message != prevP.Message { - act.Event = views.MessageEvent - } else { - act.Event = views.UpdateEvent - } - acts = append(acts, act) + + acts = append(acts, GetPublicationActivity(c, p, prevP)) } dHits, err := c.DatasetSearchIndex.Search(models.NewSearchArgs(). @@ -260,3 +237,35 @@ func RecentActivity(w http.ResponseWriter, r *http.Request) { views.RecentActivity(c, acts).Render(r.Context(), w) } + +func GetPublicationActivity(c *ctx.Ctx, p *models.Publication, prevP *models.Publication) views.Activity { + act := views.Activity{ + Object: views.PublicationObject, + User: p.User, + Datestamp: *p.DateUpdated, + URL: c.PathTo("publication", "id", p.ID).String(), + Status: p.Status, + RecordID: p.ID, + Title: p.Title, + } + + if prevP == nil { + act.Event = views.CreateEvent + } else if p.Status == "public" && prevP.Status == "returned" { + act.Event = views.RepublishEvent + } else if p.Status == "public" && prevP.Status != "public" { + act.Event = views.PublishEvent + } else if p.Status == "returned" && prevP.Status != "returned" { + act.Event = views.WithdrawEvent + } else if p.Locked && !prevP.Locked { + act.Event = views.LockEvent + } else if !p.Locked && prevP.Locked { + act.Event = views.UnlockEvent + } else if p.Message != "" && p.Message != prevP.Message { + act.Event = views.MessageEvent + } else { + act.Event = views.UpdateEvent + } + + return act +} diff --git a/handlers/publicationviewing/show.go b/handlers/publicationviewing/show.go index 8595747c5..d0c6760f6 100644 --- a/handlers/publicationviewing/show.go +++ b/handlers/publicationviewing/show.go @@ -4,6 +4,9 @@ import ( "net/http" "github.com/ugent-library/biblio-backoffice/ctx" + "github.com/ugent-library/biblio-backoffice/handlers" + "github.com/ugent-library/biblio-backoffice/models" + "github.com/ugent-library/biblio-backoffice/views" publicationviews "github.com/ugent-library/biblio-backoffice/views/publication" ) @@ -85,3 +88,31 @@ func ShowActivity(w http.ResponseWriter, r *http.Request) { publicationviews.Activity(c, p, redirectURL).Render(r.Context(), w) } + +func RecentActivity(w http.ResponseWriter, r *http.Request) { + c := ctx.Get(r) + p := ctx.GetPublication(r) + + var ( + acts []views.Activity + nextSnapshot *models.Publication + ) + + err := c.Repo.PublicationHistory(p.ID, func(snapshot *models.Publication) bool { + if nextSnapshot != nil { + acts = append(acts, handlers.GetPublicationActivity(c, nextSnapshot, snapshot)) + } + + nextSnapshot = snapshot + + return true + }) + if err != nil { + c.HandleError(w, r, err) + return + } + + acts = append(acts, handlers.GetPublicationActivity(c, nextSnapshot, nil)) + + publicationviews.RecentActivity(c, acts, p).Render(r.Context(), w) +} diff --git a/routes/register.go b/routes/register.go index b20d1f85b..4cd1c56ef 100644 --- a/routes/register.go +++ b/routes/register.go @@ -315,6 +315,7 @@ func Register(c Config) { r.With(ctx.SetSubNav("datasets")).Get("/datasets", publicationviewing.ShowDatasets).Name("publication_datasets") r.With(ctx.SetSubNav("activity")).Get("/activity", publicationviewing.ShowActivity).Name("publication_activity") r.Get("/files/{file_id}", publicationviewing.DownloadFile).Name("publication_download_file") + r.Get("/details/recent-activity", publicationviewing.RecentActivity).Name("publication_details_recent_activity") }) // edit only diff --git a/views/publication/show.templ b/views/publication/show.templ index f6f3ef7db..7436f1125 100644 --- a/views/publication/show.templ +++ b/views/publication/show.templ @@ -275,7 +275,7 @@ templ Show(c *ctx.Ctx, p *models.Publication, redirectURL string) { ID: "history", Icon: "if-time", Title: "Activity history", - Component: historyPane(), + Component: historyPane(c, p), }, shared.DetailsSidebarStepArgs{ ID: "info", @@ -327,41 +327,14 @@ templ messagesPane() { } -templ historyPane() { - +templ historyPane(c *ctx.Ctx, p *models.Publication) { +
+ Loading recent activity… +
+} + +templ RecentActivity(c *ctx.Ctx, acts []views.Activity, p *models.Publication) { + @views.ActivityList(c, acts, lo.ToPtr(p.ID)) } templ infoPane(c *ctx.Ctx, p *models.Publication) { diff --git a/views/publication/show_templ.go b/views/publication/show_templ.go index 2808c9f54..489b78696 100644 --- a/views/publication/show_templ.go +++ b/views/publication/show_templ.go @@ -760,7 +760,7 @@ func Show(c *ctx.Ctx, p *models.Publication, redirectURL string) templ.Component ID: "history", Icon: "if-time", Title: "Activity history", - Component: historyPane(), + Component: historyPane(c, p), }, shared.DetailsSidebarStepArgs{ ID: "info", @@ -817,7 +817,7 @@ func messagesPane() templ.Component { }) } -func historyPane() templ.Component { +func historyPane(c *ctx.Ctx, p *models.Publication) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -835,7 +835,46 @@ func historyPane() templ.Component { templ_7745c5c3_Var42 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Loading recent activity…
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +func RecentActivity(c *ctx.Ctx, acts []views.Activity, p *models.Publication) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var44 := templ.GetChildren(ctx) + if templ_7745c5c3_Var44 == nil { + templ_7745c5c3_Var44 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Err = views.ActivityList(c, acts, lo.ToPtr(p.ID)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -856,9 +895,9 @@ func infoPane(c *ctx.Ctx, p *models.Publication) templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var43 := templ.GetChildren(ctx) - if templ_7745c5c3_Var43 == nil { - templ_7745c5c3_Var43 = templ.NopComponent + templ_7745c5c3_Var45 := templ.GetChildren(ctx) + if templ_7745c5c3_Var45 == nil { + templ_7745c5c3_Var45 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("