From a6d152c46819337179cee05fa0d5625222bfdb27 Mon Sep 17 00:00:00 2001 From: ramdos0207 Date: Tue, 18 Jun 2024 18:29:24 +0900 Subject: [PATCH 1/2] impl ETag to /me --- router/v3/responses.go | 37 +++++++++++++++++++++++++++++++++++++ router/v3/users.go | 21 +++------------------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/router/v3/responses.go b/router/v3/responses.go index 7e09f1375..3cb122bde 100644 --- a/router/v3/responses.go +++ b/router/v3/responses.go @@ -5,6 +5,7 @@ import ( "time" "github.com/traPtitech/traQ/model" + "github.com/traPtitech/traQ/service/rbac/permission" "github.com/traPtitech/traQ/utils/optional" "github.com/gofrs/uuid" @@ -623,3 +624,39 @@ func formatStampPalettes(cfs []*model.StampPalette) []*StampPalette { sort.Slice(res, func(i, j int) bool { return res[i].ID.String() < res[j].ID.String() }) return res } + +type UserInfo struct { + ID uuid.UUID `json:"id"` + Bio string `json:"bio"` + Groups []uuid.UUID `json:"groups"` + Tags []UserTag `json:"tags"` + UpdatedAt time.Time `json:"updatedAt"` + LastOnline optional.Of[time.Time] `json:"lastOnline"` + TwitterID string `json:"twitterId"` + Name string `json:"name"` + DisplayName string `json:"displayName"` + IconFileID uuid.UUID `json:"iconFileId"` + Bot bool `json:"bot"` + State int `json:"state"` + Permissions []permission.Permission `json:"permissions"` + HomeChannel optional.Of[uuid.UUID] `json:"homeChannel"` +} + +func FormatUserInfo(ui model.UserInfo, tags []model.UserTag, groups []uuid.UUID, permissions []permission.Permission) *UserInfo { + return &UserInfo{ + ID: ui.GetID(), + Bio: ui.GetBio(), + Groups: groups, + Tags: formatUserTags(tags), + UpdatedAt: ui.GetUpdatedAt(), + LastOnline: ui.GetLastOnline(), + TwitterID: ui.GetTwitterID(), + Name: ui.GetName(), + DisplayName: ui.GetResponseDisplayName(), + IconFileID: ui.GetIconFileID(), + Bot: ui.IsBot(), + State: ui.GetState().Int(), + Permissions: permissions, + HomeChannel: ui.GetHomeChannel(), + } +} diff --git a/router/v3/users.go b/router/v3/users.go index 999e671b6..0f2033d7a 100644 --- a/router/v3/users.go +++ b/router/v3/users.go @@ -2,11 +2,12 @@ package v3 import ( "context" - "github.com/samber/lo" "net/http" "sort" "time" + "github.com/samber/lo" + vd "github.com/go-ozzo/ozzo-validation/v4" "github.com/gofrs/uuid" "github.com/golang-jwt/jwt/v5" @@ -100,23 +101,7 @@ func (h *Handlers) GetMe(c echo.Context) error { if err != nil { return herror.InternalServerError(err) } - - return c.JSON(http.StatusOK, echo.Map{ - "id": me.GetID(), - "bio": me.GetBio(), - "groups": groups, - "tags": formatUserTags(tags), - "updatedAt": me.GetUpdatedAt(), - "lastOnline": me.GetLastOnline(), - "twitterId": me.GetTwitterID(), - "name": me.GetName(), - "displayName": me.GetResponseDisplayName(), - "iconFileId": me.GetIconFileID(), - "bot": me.IsBot(), - "state": me.GetState().Int(), - "permissions": h.RBAC.GetGrantedPermissions(me.GetRole()), - "homeChannel": me.GetHomeChannel(), - }) + return extension.ServeJSONWithETag(c, FormatUserInfo(me, tags, groups, h.RBAC.GetGrantedPermissions(me.GetRole()))) } type userAccessScopes struct{} From bdabc5a9cd0a78274a5a4d932b7de861110b2850 Mon Sep 17 00:00:00 2001 From: ramdos0207 Date: Sat, 26 Oct 2024 21:11:45 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E6=A7=8B=E9=80=A0=E4=BD=93=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/v3/responses.go | 37 ------------------------------------- router/v3/users.go | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/router/v3/responses.go b/router/v3/responses.go index 3cb122bde..7e09f1375 100644 --- a/router/v3/responses.go +++ b/router/v3/responses.go @@ -5,7 +5,6 @@ import ( "time" "github.com/traPtitech/traQ/model" - "github.com/traPtitech/traQ/service/rbac/permission" "github.com/traPtitech/traQ/utils/optional" "github.com/gofrs/uuid" @@ -624,39 +623,3 @@ func formatStampPalettes(cfs []*model.StampPalette) []*StampPalette { sort.Slice(res, func(i, j int) bool { return res[i].ID.String() < res[j].ID.String() }) return res } - -type UserInfo struct { - ID uuid.UUID `json:"id"` - Bio string `json:"bio"` - Groups []uuid.UUID `json:"groups"` - Tags []UserTag `json:"tags"` - UpdatedAt time.Time `json:"updatedAt"` - LastOnline optional.Of[time.Time] `json:"lastOnline"` - TwitterID string `json:"twitterId"` - Name string `json:"name"` - DisplayName string `json:"displayName"` - IconFileID uuid.UUID `json:"iconFileId"` - Bot bool `json:"bot"` - State int `json:"state"` - Permissions []permission.Permission `json:"permissions"` - HomeChannel optional.Of[uuid.UUID] `json:"homeChannel"` -} - -func FormatUserInfo(ui model.UserInfo, tags []model.UserTag, groups []uuid.UUID, permissions []permission.Permission) *UserInfo { - return &UserInfo{ - ID: ui.GetID(), - Bio: ui.GetBio(), - Groups: groups, - Tags: formatUserTags(tags), - UpdatedAt: ui.GetUpdatedAt(), - LastOnline: ui.GetLastOnline(), - TwitterID: ui.GetTwitterID(), - Name: ui.GetName(), - DisplayName: ui.GetResponseDisplayName(), - IconFileID: ui.GetIconFileID(), - Bot: ui.IsBot(), - State: ui.GetState().Int(), - Permissions: permissions, - HomeChannel: ui.GetHomeChannel(), - } -} diff --git a/router/v3/users.go b/router/v3/users.go index 0f2033d7a..8696be697 100644 --- a/router/v3/users.go +++ b/router/v3/users.go @@ -6,12 +6,11 @@ import ( "sort" "time" - "github.com/samber/lo" - vd "github.com/go-ozzo/ozzo-validation/v4" "github.com/gofrs/uuid" "github.com/golang-jwt/jwt/v5" "github.com/labstack/echo/v4" + "github.com/samber/lo" "github.com/skip2/go-qrcode" "github.com/traPtitech/traQ/model" @@ -101,7 +100,22 @@ func (h *Handlers) GetMe(c echo.Context) error { if err != nil { return herror.InternalServerError(err) } - return extension.ServeJSONWithETag(c, FormatUserInfo(me, tags, groups, h.RBAC.GetGrantedPermissions(me.GetRole()))) + return extension.ServeJSONWithETag(c, echo.Map{ + "id": me.GetID(), + "bio": me.GetBio(), + "groups": groups, + "tags": formatUserTags(tags), + "updatedAt": me.GetUpdatedAt(), + "lastOnline": me.GetLastOnline(), + "twitterId": me.GetTwitterID(), + "name": me.GetName(), + "displayName": me.GetResponseDisplayName(), + "iconFileId": me.GetIconFileID(), + "bot": me.IsBot(), + "state": me.GetState().Int(), + "permissions": h.RBAC.GetGrantedPermissions(me.GetRole()), + "homeChannel": me.GetHomeChannel(), + }) } type userAccessScopes struct{}