This is an automated email from the ASF dual-hosted git repository.
linkinstar pushed a commit to branch feat/1.6.0/user
in repository https://gitbox.apache.org/repos/asf/answer.git
The following commit(s) were added to refs/heads/feat/1.6.0/user by this push:
new 3b8b5871 feat(user): add user status messages and support localization
3b8b5871 is described below
commit 3b8b5871a6191163901287383bcc7b764944bf6f
Author: LinkinStars <[email protected]>
AuthorDate: Thu Jun 26 16:10:09 2025 +0800
feat(user): add user status messages and support localization
---
i18n/en_US.yaml | 8 +++++
i18n/zh_CN.yaml | 8 +++++
internal/base/reason/reason.go | 4 +++
internal/schema/user_schema.go | 50 +++++++++++++++++++-------------
internal/service/content/user_service.go | 2 +-
5 files changed, 51 insertions(+), 21 deletions(-)
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index 1791d85c..67d3e3a9 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -307,6 +307,14 @@ backend:
other: "Error {{.Field}} format near '{{.Content}}' at line {{.Line}}.
{{.ExtraMessage}}"
add_bulk_users_amount_error:
other: "The number of users you add at once should be in the range of
1-{{.MaxAmount}}."
+ status_suspended_forever:
+ other: "<strong>This user was suspended forever.</strong> This user
doesn't meet a community guideline."
+ status_suspended_until:
+ other: "<strong>This user was suspended until
{{.SuspendedUntil}}.</strong> This user doesn't meet a community guideline."
+ status_deleted:
+ other: "This user was deleted."
+ status_inactive:
+ other: "This user is inactive."
config:
read_config_failed:
other: Read config failed
diff --git a/i18n/zh_CN.yaml b/i18n/zh_CN.yaml
index 6b153e0a..7a94840e 100644
--- a/i18n/zh_CN.yaml
+++ b/i18n/zh_CN.yaml
@@ -306,6 +306,14 @@ backend:
other: "发生错误,{{.Field}} 格式错误,在 '{{.Content}}' 行数 {{.Line}}.
{{.ExtraMessage}}"
add_bulk_users_amount_error:
other: "一次性添加的用户数量应在 1-{{.MaxAmount}} 之间。"
+ status_suspended_forever:
+ other: "<strong>该用户已被永久封禁。</strong>该用户不符合社区准则。"
+ status_suspended_until:
+ other: "<strong>该用户已被封禁至 {{.SuspendedUntil}}。</strong>该用户不符合社区准则。"
+ status_deleted:
+ other: "该用户已被删除。"
+ status_inactive:
+ other: "该用户未激活。"
config:
read_config_failed:
other: 读取配置失败
diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go
index f318e335..953c316e 100644
--- a/internal/base/reason/reason.go
+++ b/internal/base/reason/reason.go
@@ -111,6 +111,10 @@ const (
MetaObjectNotFound = "error.meta.object_not_found"
BadgeObjectNotFound = "error.badge.object_not_found"
StatusInvalid = "error.common.status_invalid"
+ UserStatusInactive = "error.user.status_inactive"
+ UserStatusSuspendedForever = "error.user.status_suspended_forever"
+ UserStatusSuspendedUntil = "error.user.status_suspended_until"
+ UserStatusDeleted = "error.user.status_deleted"
)
// user external login reasons
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index dd65cba9..a320ca9b 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -20,10 +20,13 @@
package schema
import (
+ "context"
"encoding/json"
+ "github.com/apache/answer/internal/base/handler"
"github.com/apache/answer/internal/base/reason"
"github.com/apache/answer/internal/base/translator"
+ "github.com/apache/answer/pkg/day"
"github.com/segmentfault/pacman/errors"
"github.com/apache/answer/internal/base/constant"
@@ -176,29 +179,36 @@ func (r *GetOtherUserInfoByUsernameResp)
ConvertFromUserEntity(userInfo *entity.
if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
r.SuspendedUntil = userInfo.SuspendedUntil.Unix()
}
- if userInfo.MailStatus == entity.EmailStatusToBeVerified {
- statusMsgShow, ok := UserStatusShowMsg[11]
- if ok {
- r.StatusMsg = statusMsgShow
- }
- } else {
- statusMsgShow, ok := UserStatusShowMsg[userInfo.Status]
- if ok {
- r.StatusMsg = statusMsgShow
- }
- }
+ r.StatusMsg = ""
}
-const (
- NoticeStatusOn = 1
- NoticeStatusOff = 2
-)
+func (r *GetOtherUserInfoByUsernameResp) ConvertFromUserEntityWithLang(ctx
context.Context, userInfo *entity.User) {
+ _ = copier.Copy(r, userInfo)
+ r.CreatedAt = userInfo.CreatedAt.Unix()
+ r.LastLoginDate = userInfo.LastLoginDate.Unix()
+ r.Status = constant.ConvertUserStatus(userInfo.Status,
userInfo.MailStatus)
+ if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ r.SuspendedUntil = userInfo.SuspendedUntil.Unix()
+ }
-var UserStatusShowMsg = map[int]string{
- 1: "",
- 9: "<strong>This user was suspended forever.</strong> This user
doesn't meet a community guideline.",
- 10: "This user was deleted.",
- 11: "This user is inactive.",
+ lang := handler.GetLangByCtx(ctx)
+ if userInfo.MailStatus == entity.EmailStatusToBeVerified {
+ r.StatusMsg = translator.Tr(lang, reason.UserStatusInactive)
+ }
+ switch userInfo.Status {
+ case entity.UserStatusSuspended:
+ if userInfo.SuspendedUntil.IsZero() || userInfo.SuspendedUntil
== entity.PermanentSuspensionTime {
+ r.StatusMsg = translator.Tr(lang,
reason.UserStatusSuspendedForever)
+ } else {
+ trans := translator.GlobalTrans.Tr(lang,
"ui.dates.long_date_with_year")
+ suspendedUntilFormatted :=
day.Format(userInfo.SuspendedUntil.Unix(), trans, "UTC")
+ r.StatusMsg = translator.TrWithData(lang,
reason.UserStatusSuspendedUntil, map[string]interface{}{
+ "SuspendedUntil": suspendedUntilFormatted,
+ })
+ }
+ case entity.UserStatusDeleted:
+ r.StatusMsg = translator.Tr(lang, reason.UserStatusDeleted)
+ }
}
// UserEmailLoginReq user email login request
diff --git a/internal/service/content/user_service.go
b/internal/service/content/user_service.go
index 1c94d22d..81f7ac82 100644
--- a/internal/service/content/user_service.go
+++ b/internal/service/content/user_service.go
@@ -141,7 +141,7 @@ func (us *UserService) GetOtherUserInfoByUsername(ctx
context.Context, req *sche
return nil, errors.NotFound(reason.UserNotFound)
}
resp = &schema.GetOtherUserInfoByUsernameResp{}
- resp.ConvertFromUserEntity(userInfo)
+ resp.ConvertFromUserEntityWithLang(ctx, userInfo)
resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar,
userInfo.EMail, userInfo.Status).GetURL()
// Only the user himself and the administrator can see the hidden
questions