From 569b131de0b27e29f80b6aa786ba48f08813a388 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 26 Apr 2023 21:15:16 +0000 Subject: [PATCH] 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 @@ + +

+ {{.locale.Tr "settings.timestamps"}} +

+
+
+ {{.CsrfTokenHtml}} +
+
+ + +
+
+
+ +
+
+
+

{{.locale.Tr "settings.hidden_comment_types"}}