From 569b131de0b27e29f80b6aa786ba48f08813a388 Mon Sep 17 00:00:00 2001
From: Yarden Shoham
Date: Wed, 26 Apr 2023 21:15:16 +0000
Subject: [PATCH 1/9] Change to "Force absolute timestamps" and add to the
user's appearance setting
Signed-off-by: Yarden Shoham
---
models/user/setting_keys.go | 2 ++
modules/timeutil/since.go | 3 +++
options/locale/locale_en-US.ini | 3 +++
routers/web/user/setting/profile.go | 22 ++++++++++++++++++++++
routers/web/web.go | 1 +
services/forms/user_form.go | 5 +++++
services/forms/user_form_timestamps.go | 14 ++++++++++++++
templates/user/settings/appearance.tmpl | 19 +++++++++++++++++++
8 files changed, 69 insertions(+)
create mode 100644 services/forms/user_form_timestamps.go
diff --git a/models/user/setting_keys.go b/models/user/setting_keys.go
index 10255735b3177..40f815a51cd3f 100644
--- a/models/user/setting_keys.go
+++ b/models/user/setting_keys.go
@@ -12,4 +12,6 @@ const (
UserActivityPubPrivPem = "activitypub.priv_pem"
// UserActivityPubPubPem is user's public key
UserActivityPubPubPem = "activitypub.pub_pem"
+ // SettingsForceAbsoluteTimestamps is the setting key for hidden comment types
+ SettingsForceAbsoluteTimestamps = "timestamps.force_absolute"
)
diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go
index 04fcff54a3384..8c835d87f8a3a 100644
--- a/modules/timeutil/since.go
+++ b/modules/timeutil/since.go
@@ -132,6 +132,9 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
// TimeSince renders relative time HTML given a time.Time
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
+ // if user forces absolute timestamps, use the full time
+ // (how can I get the context here? I want to run `user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps)`)
+ // return DateTime("full", then)
return timeSinceUnix(then, time.Now(), lang)
}
diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini
index 54e041d7854a8..0be572ff9b0fb 100644
--- a/options/locale/locale_en-US.ini
+++ b/options/locale/locale_en-US.ini
@@ -640,6 +640,9 @@ saved_successfully = Your settings were saved successfully.
privacy = Privacy
keep_activity_private = Hide the activity from the profile page
keep_activity_private_popup = Makes the activity visible only for you and the admins
+timestamps = Timestamps
+force_absolute_timestamps = Force absolute timestamps
+force_absolute_timestamps_popup = Always render all timestamps as dates, do not allow relative form (e.g. 5 days ago)
lookup_avatar_by_mail = Look Up Avatar by Email Address
federated_avatar_lookup = Federated Avatar Lookup
diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go
index 0a8a5e6280c46..8bc25883b33a0 100644
--- a/routers/web/user/setting/profile.go
+++ b/routers/web/user/setting/profile.go
@@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path/filepath"
+ "strconv"
"strings"
"code.gitea.io/gitea/models/db"
@@ -350,6 +351,14 @@ func Appearance(ctx *context.Context) {
return forms.IsUserHiddenCommentTypeGroupChecked(commentTypeGroup, hiddenCommentTypes)
}
+ val, err = user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps)
+ if err != nil {
+ ctx.ServerError("GetUserSetting", err)
+ return
+ }
+ forceAbsoluteTimestamps, _ := strconv.ParseBool(val) // we can safely ignore the failed conversion here
+ ctx.Data["ForceAbsoluteTimestamps"] = forceAbsoluteTimestamps
+
ctx.HTML(http.StatusOK, tplSettingsAppearance)
}
@@ -421,3 +430,16 @@ func UpdateUserHiddenComments(ctx *context.Context) {
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
}
+
+// UpdateUserTimestamps update a user's timestamp preferences
+func UpdateUserTimestamps(ctx *context.Context) {
+ err := user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps, strconv.FormatBool(forms.UserTimestampsFromRequest(ctx).ForceAbsoluteTimestamps))
+ if err != nil {
+ ctx.ServerError("SetUserSetting", err)
+ return
+ }
+
+ log.Trace("User settings updated: %s", ctx.Doer.Name)
+ ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
+ ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
+}
diff --git a/routers/web/web.go b/routers/web/web.go
index 779499889fb05..4fcae7efe80d2 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -446,6 +446,7 @@ func RegisterRoutes(m *web.Route) {
m.Get("", user_setting.Appearance)
m.Post("/language", web.Bind(forms.UpdateLanguageForm{}), user_setting.UpdateUserLang)
m.Post("/hidden_comments", user_setting.UpdateUserHiddenComments)
+ m.Post("/timestamps", user_setting.UpdateUserTimestamps)
m.Post("/theme", web.Bind(forms.UpdateThemeForm{}), user_setting.UpdateUIThemePost)
})
m.Group("/security", func() {
diff --git a/services/forms/user_form.go b/services/forms/user_form.go
index 285bc398b26c5..6b01218b5f99f 100644
--- a/services/forms/user_form.go
+++ b/services/forms/user_form.go
@@ -261,6 +261,11 @@ type UpdateLanguageForm struct {
Language string
}
+// UpdateTimestampsForm form for updating profile
+type UpdateTimestampsForm struct {
+ ForceAbsoluteTimestamps bool
+}
+
// Validate validates the fields
func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
diff --git a/services/forms/user_form_timestamps.go b/services/forms/user_form_timestamps.go
new file mode 100644
index 0000000000000..f86189f8cded0
--- /dev/null
+++ b/services/forms/user_form_timestamps.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package forms
+
+import (
+ "code.gitea.io/gitea/modules/context"
+)
+
+// UserTimestampsFromRequest parse the form to hidden comment types bitset
+func UserTimestampsFromRequest(ctx *context.Context) *UpdateTimestampsForm {
+ timestampsForm := &UpdateTimestampsForm{ForceAbsoluteTimestamps: ctx.FormBool("force_absolute_timestamps")}
+ return timestampsForm
+}
diff --git a/templates/user/settings/appearance.tmpl b/templates/user/settings/appearance.tmpl
index 129fca26577f9..6ca9502bed67f 100644
--- a/templates/user/settings/appearance.tmpl
+++ b/templates/user/settings/appearance.tmpl
@@ -66,6 +66,25 @@
+
+
+
+
{{end}}
diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl
index 596d9ae78bd67..0dd5c565cd2ba 100644
--- a/templates/repo/branch/list.tmpl
+++ b/templates/repo/branch/list.tmpl
@@ -18,7 +18,7 @@
{{svg "octicon-shield-lock"}}
{{end}}
{{.DefaultBranch}}
- {{svg "octicon-git-commit" 16 "gt-mr-2"}}{{ShortSha .DefaultBranchBranch.Commit.ID.String}} · {{RenderCommitMessage $.Context .DefaultBranchBranch.Commit.CommitMessage .RepoLink .Repository.ComposeMetas}} · {{.locale.Tr "org.repo_updated"}} {{TimeSince .DefaultBranchBranch.Commit.Committer.When .locale}}
+ {{svg "octicon-git-commit" 16 "gt-mr-2"}}{{ShortSha .DefaultBranchBranch.Commit.ID.String}} · {{RenderCommitMessage $.Context .DefaultBranchBranch.Commit.CommitMessage .RepoLink .Repository.ComposeMetas}} · {{.locale.Tr "org.repo_updated"}} {{TimeSince .Context .DefaultBranchBranch.Commit.Committer.When .locale}}
{{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}}
@@ -60,13 +60,13 @@
|
{{if .IsDeleted}}
{{.Name}}
- {{$.locale.Tr "repo.branch.deleted_by" .DeletedBranch.DeletedBy.Name}} {{TimeSinceUnix .DeletedBranch.DeletedUnix $.locale}}
+ {{$.locale.Tr "repo.branch.deleted_by" .DeletedBranch.DeletedBy.Name}} {{TimeSinceUnix $.Context .DeletedBranch.DeletedUnix $.locale}}
{{else}}
{{if .IsProtected}}
{{svg "octicon-shield-lock"}}
{{end}}
{{.Name}}
- {{svg "octicon-git-commit" 16 "gt-mr-2"}}{{ShortSha .Commit.ID.String}} · {{RenderCommitMessage $.Context .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}} · {{$.locale.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.locale}}
+ {{svg "octicon-git-commit" 16 "gt-mr-2"}}{{ShortSha .Commit.ID.String}} · {{RenderCommitMessage $.Context .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}} · {{$.locale.Tr "org.repo_updated"}} {{TimeSince $.Context .Commit.Committer.When $.locale}}
{{end}}
|
diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl
index 5e26c04fd879c..00f192245f2ea 100644
--- a/templates/repo/commit_page.tmpl
+++ b/templates/repo/commit_page.tmpl
@@ -156,7 +156,7 @@
{{avatarByEmail $.Context .Commit.Author.Email .Commit.Author.Email 28 "gt-mr-3"}}
{{.Commit.Author.Name}}
{{end}}
- {{TimeSince .Commit.Author.When $.locale}}
+ {{TimeSince $.Context .Commit.Author.When $.locale}}
{{if or (ne .Commit.Committer.Name .Commit.Author.Name) (ne .Commit.Committer.Email .Commit.Author.Email)}}
{{.locale.Tr "repo.diff.committed_by"}}
{{if ne .Verification.CommittingUser.ID 0}}
@@ -277,7 +277,7 @@
{{else}}
{{.NoteCommit.Author.Name}}
{{end}}
- {{TimeSince .NoteCommit.Author.When $.locale}}
+ {{TimeSince $.Context .NoteCommit.Author.When $.locale}}
{{RenderNote $.Context .Note $.RepoLink $.Repository.ComposeMetas}}
diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl
index 36333c554079c..ae15fa5844495 100644
--- a/templates/repo/commits_list.tmpl
+++ b/templates/repo/commits_list.tmpl
@@ -76,9 +76,9 @@
{{end}}
|
{{if .Committer}}
- {{TimeSince .Committer.When $.locale}} |
+ {{TimeSince $.Context .Committer.When $.locale}} |
{{else}}
- {{TimeSince .Author.When $.locale}} |
+ {{TimeSince $.Context .Author.When $.locale}} |
{{end}}
{{end}}
diff --git a/templates/repo/diff/comments.tmpl b/templates/repo/diff/comments.tmpl
index f30126153307a..7c4067b9db9b7 100644
--- a/templates/repo/diff/comments.tmpl
+++ b/templates/repo/diff/comments.tmpl
@@ -1,6 +1,6 @@
{{range .comments}}
-{{$createdStr:= TimeSinceUnix .CreatedUnix $.root.locale}}
+{{$createdStr:= TimeSinceUnix $.root.Context .CreatedUnix $.root.locale}}
- {{$closedDate:= TimeSinceUnix .Milestone.ClosedDateUnix $.locale}}
+ {{$closedDate:= TimeSinceUnix $.Context .Milestone.ClosedDateUnix $.locale}}
{{if .IsClosed}}
{{svg "octicon-clock"}} {{$.locale.Tr "repo.milestones.closed" $closedDate | Safe}}
{{else}}
diff --git a/templates/repo/issue/milestones.tmpl b/templates/repo/issue/milestones.tmpl
index 3a23d225c4306..5487ff0b53eb6 100644
--- a/templates/repo/issue/milestones.tmpl
+++ b/templates/repo/issue/milestones.tmpl
@@ -71,7 +71,7 @@
- {{$closedDate:= TimeSinceUnix .ClosedDateUnix $.locale}}
+ {{$closedDate:= TimeSinceUnix $.Context .ClosedDateUnix $.locale}}
{{if .IsClosed}}
{{svg "octicon-clock"}} {{$.locale.Tr "repo.milestones.closed" $closedDate | Safe}}
{{else}}
@@ -88,7 +88,7 @@
{{svg "octicon-check" 16 "gt-mr-3"}}
{{$.locale.PrettyNumber .NumClosedIssues}} {{$.locale.Tr "repo.issues.closed_title"}}
{{if .TotalTrackedTime}}{{svg "octicon-clock"}} {{.TotalTrackedTime|Sec2Time}}{{end}}
- {{if .UpdatedUnix}}{{svg "octicon-clock"}} {{$.locale.Tr "repo.milestones.update_ago" (TimeSinceUnix .UpdatedUnix $.locale) | Safe}}{{end}}
+ {{if .UpdatedUnix}}{{svg "octicon-clock"}} {{$.locale.Tr "repo.milestones.update_ago" (TimeSinceUnix $.Context .UpdatedUnix $.locale) | Safe}}{{end}}
{{if and (or $.CanWriteIssues $.CanWritePulls) (not $.Repository.IsArchived)}}
diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl
index 7d3bce2b6a872..712c121500bd3 100644
--- a/templates/repo/issue/view_content.tmpl
+++ b/templates/repo/issue/view_content.tmpl
@@ -15,7 +15,7 @@
- {{$createdStr:= TimeSinceUnix .Issue.CreatedUnix $.locale}}
+ {{$createdStr:= TimeSinceUnix $.Context .Issue.CreatedUnix $.locale}}