Skip to content

Commit

Permalink
Change to "Force absolute timestamps" and add to the user's appearanc…
Browse files Browse the repository at this point in the history
…e setting

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
  • Loading branch information
yardenshoham committed Apr 26, 2023
1 parent 8f57aa0 commit 569b131
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/user/setting_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
3 changes: 3 additions & 0 deletions modules/timeutil/since.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions routers/web/user/setting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions services/forms/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions services/forms/user_form_timestamps.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions templates/user/settings/appearance.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@
</form>
</div>

<!-- Timestamps -->
<h4 class="ui top attached header">
{{.locale.Tr "settings.timestamps"}}
</h4>
<div class="ui attached segment">
<form class="ui form" action="{{.Link}}/timestamps" method="post">
{{.CsrfTokenHtml}}
<div class="field">
<div class="ui checkbox" id="force-absolute-timestamps">
<label data-tooltip-content="{{.locale.Tr "settings.force_absolute_timestamps_popup"}}"><strong>{{.locale.Tr "settings.force_absolute_timestamps"}}</strong></label>
<input name="force_absolute_timestamps" type="checkbox" {{if .ForceAbsoluteTimestamps}}checked{{end}}>
</div>
</div>
<div class="field">
<button class="ui green button">{{$.locale.Tr "save"}}</button>
</div>
</form>
</div>

<!-- Shown comment event types -->
<h4 class="ui top attached header">
{{.locale.Tr "settings.hidden_comment_types"}}
Expand Down

0 comments on commit 569b131

Please sign in to comment.