Skip to content

Commit 569b131

Browse files
committed
Change to "Force absolute timestamps" and add to the user's appearance setting
Signed-off-by: Yarden Shoham <git@yardenshoham.com>
1 parent 8f57aa0 commit 569b131

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

models/user/setting_keys.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ const (
1212
UserActivityPubPrivPem = "activitypub.priv_pem"
1313
// UserActivityPubPubPem is user's public key
1414
UserActivityPubPubPem = "activitypub.pub_pem"
15+
// SettingsForceAbsoluteTimestamps is the setting key for hidden comment types
16+
SettingsForceAbsoluteTimestamps = "timestamps.force_absolute"
1517
)

modules/timeutil/since.go

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
132132

133133
// TimeSince renders relative time HTML given a time.Time
134134
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
135+
// if user forces absolute timestamps, use the full time
136+
// (how can I get the context here? I want to run `user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps)`)
137+
// return DateTime("full", then)
135138
return timeSinceUnix(then, time.Now(), lang)
136139
}
137140

options/locale/locale_en-US.ini

+3
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ saved_successfully = Your settings were saved successfully.
640640
privacy = Privacy
641641
keep_activity_private = Hide the activity from the profile page
642642
keep_activity_private_popup = Makes the activity visible only for you and the admins
643+
timestamps = Timestamps
644+
force_absolute_timestamps = Force absolute timestamps
645+
force_absolute_timestamps_popup = Always render all timestamps as dates, do not allow relative form (e.g. 5 days ago)
643646

644647
lookup_avatar_by_mail = Look Up Avatar by Email Address
645648
federated_avatar_lookup = Federated Avatar Lookup

routers/web/user/setting/profile.go

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"path/filepath"
15+
"strconv"
1516
"strings"
1617

1718
"code.gitea.io/gitea/models/db"
@@ -350,6 +351,14 @@ func Appearance(ctx *context.Context) {
350351
return forms.IsUserHiddenCommentTypeGroupChecked(commentTypeGroup, hiddenCommentTypes)
351352
}
352353

354+
val, err = user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps)
355+
if err != nil {
356+
ctx.ServerError("GetUserSetting", err)
357+
return
358+
}
359+
forceAbsoluteTimestamps, _ := strconv.ParseBool(val) // we can safely ignore the failed conversion here
360+
ctx.Data["ForceAbsoluteTimestamps"] = forceAbsoluteTimestamps
361+
353362
ctx.HTML(http.StatusOK, tplSettingsAppearance)
354363
}
355364

@@ -421,3 +430,16 @@ func UpdateUserHiddenComments(ctx *context.Context) {
421430
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
422431
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
423432
}
433+
434+
// UpdateUserTimestamps update a user's timestamp preferences
435+
func UpdateUserTimestamps(ctx *context.Context) {
436+
err := user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsForceAbsoluteTimestamps, strconv.FormatBool(forms.UserTimestampsFromRequest(ctx).ForceAbsoluteTimestamps))
437+
if err != nil {
438+
ctx.ServerError("SetUserSetting", err)
439+
return
440+
}
441+
442+
log.Trace("User settings updated: %s", ctx.Doer.Name)
443+
ctx.Flash.Success(ctx.Tr("settings.saved_successfully"))
444+
ctx.Redirect(setting.AppSubURL + "/user/settings/appearance")
445+
}

routers/web/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ func RegisterRoutes(m *web.Route) {
446446
m.Get("", user_setting.Appearance)
447447
m.Post("/language", web.Bind(forms.UpdateLanguageForm{}), user_setting.UpdateUserLang)
448448
m.Post("/hidden_comments", user_setting.UpdateUserHiddenComments)
449+
m.Post("/timestamps", user_setting.UpdateUserTimestamps)
449450
m.Post("/theme", web.Bind(forms.UpdateThemeForm{}), user_setting.UpdateUIThemePost)
450451
})
451452
m.Group("/security", func() {

services/forms/user_form.go

+5
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ type UpdateLanguageForm struct {
261261
Language string
262262
}
263263

264+
// UpdateTimestampsForm form for updating profile
265+
type UpdateTimestampsForm struct {
266+
ForceAbsoluteTimestamps bool
267+
}
268+
264269
// Validate validates the fields
265270
func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
266271
ctx := context.GetContext(req)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package forms
5+
6+
import (
7+
"code.gitea.io/gitea/modules/context"
8+
)
9+
10+
// UserTimestampsFromRequest parse the form to hidden comment types bitset
11+
func UserTimestampsFromRequest(ctx *context.Context) *UpdateTimestampsForm {
12+
timestampsForm := &UpdateTimestampsForm{ForceAbsoluteTimestamps: ctx.FormBool("force_absolute_timestamps")}
13+
return timestampsForm
14+
}

templates/user/settings/appearance.tmpl

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@
6666
</form>
6767
</div>
6868

69+
<!-- Timestamps -->
70+
<h4 class="ui top attached header">
71+
{{.locale.Tr "settings.timestamps"}}
72+
</h4>
73+
<div class="ui attached segment">
74+
<form class="ui form" action="{{.Link}}/timestamps" method="post">
75+
{{.CsrfTokenHtml}}
76+
<div class="field">
77+
<div class="ui checkbox" id="force-absolute-timestamps">
78+
<label data-tooltip-content="{{.locale.Tr "settings.force_absolute_timestamps_popup"}}"><strong>{{.locale.Tr "settings.force_absolute_timestamps"}}</strong></label>
79+
<input name="force_absolute_timestamps" type="checkbox" {{if .ForceAbsoluteTimestamps}}checked{{end}}>
80+
</div>
81+
</div>
82+
<div class="field">
83+
<button class="ui green button">{{$.locale.Tr "save"}}</button>
84+
</div>
85+
</form>
86+
</div>
87+
6988
<!-- Shown comment event types -->
7089
<h4 class="ui top attached header">
7190
{{.locale.Tr "settings.hidden_comment_types"}}

0 commit comments

Comments
 (0)