Skip to content

Commit

Permalink
chore: update id type to int32 (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack authored Aug 4, 2023
1 parent cbe2792 commit 7c5296c
Show file tree
Hide file tree
Showing 36 changed files with 208 additions and 198 deletions.
10 changes: 5 additions & 5 deletions api/v1/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (l ActivityLevel) String() string {
}

type ActivityUserCreatePayload struct {
UserID int `json:"userId"`
UserID int32 `json:"userId"`
Username string `json:"username"`
Role Role `json:"role"`
}

type ActivityUserAuthSignInPayload struct {
UserID int `json:"userId"`
UserID int32 `json:"userId"`
IP string `json:"ip"`
}

Expand Down Expand Up @@ -107,10 +107,10 @@ type ActivityServerStartPayload struct {
}

type Activity struct {
ID int `json:"id"`
ID int32 `json:"id"`

// Standard fields
CreatorID int `json:"creatorId"`
CreatorID int32 `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`

// Domain specific fields
Expand All @@ -122,7 +122,7 @@ type Activity struct {
// ActivityCreate is the API message for creating an activity.
type ActivityCreate struct {
// Standard fields
CreatorID int
CreatorID int32

// Domain specific fields
Type ActivityType `json:"type"`
Expand Down
2 changes: 1 addition & 1 deletion api/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type SignIn struct {
}

type SSOSignIn struct {
IdentityProviderID int `json:"identityProviderId"`
IdentityProviderID int32 `json:"identityProviderId"`
Code string `json:"code"`
RedirectURI string `json:"redirectUri"`
}
Expand Down
22 changes: 11 additions & 11 deletions api/v1/idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/usememos/memos/api/auth"
"github.com/usememos/memos/common/util"
"github.com/usememos/memos/store"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ type FieldMapping struct {
}

type IdentityProvider struct {
ID int `json:"id"`
ID int32 `json:"id"`
Name string `json:"name"`
Type IdentityProviderType `json:"type"`
IdentifierFilter string `json:"identifierFilter"`
Expand All @@ -57,7 +57,7 @@ type CreateIdentityProviderRequest struct {
}

type UpdateIdentityProviderRequest struct {
ID int `json:"-"`
ID int32 `json:"-"`
Type IdentityProviderType `json:"type"`
Name *string `json:"name"`
IdentifierFilter *string `json:"identifierFilter"`
Expand All @@ -67,7 +67,7 @@ type UpdateIdentityProviderRequest struct {
func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
g.POST("/idp", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {

g.PATCH("/idp/:idpId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
Expand All @@ -116,7 +116,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
}

identityProviderID, err := strconv.Atoi(c.Param("idpId"))
identityProviderID, err := util.ConvertStringToInt32(c.Param("idpId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("idpId"))).SetInternal(err)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find identity provider list").SetInternal(err)
}

userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
isHostUser := false
if ok {
user, err := s.Store.GetUser(ctx, &store.FindUser{
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {

g.GET("/idp/:idpId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
Expand All @@ -191,7 +191,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
}

identityProviderID, err := strconv.Atoi(c.Param("idpId"))
identityProviderID, err := util.ConvertStringToInt32(c.Param("idpId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("idpId"))).SetInternal(err)
}
Expand All @@ -210,7 +210,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {

g.DELETE("/idp/:idpId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
Expand All @@ -225,7 +225,7 @@ func (s *APIV1Service) registerIdentityProviderRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
}

identityProviderID, err := strconv.Atoi(c.Param("idpId"))
identityProviderID, err := util.ConvertStringToInt32(c.Param("idpId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("idpId"))).SetInternal(err)
}
Expand Down
9 changes: 4 additions & 5 deletions api/v1/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -21,7 +20,7 @@ type claimsMessage struct {
}

// GenerateAccessToken generates an access token for web.
func GenerateAccessToken(username string, userID int, secret string) (string, error) {
func GenerateAccessToken(username string, userID int32, secret string) (string, error) {
expirationTime := time.Now().Add(auth.AccessTokenDuration)
return generateToken(username, userID, auth.AccessTokenAudienceName, expirationTime, []byte(secret))
}
Expand Down Expand Up @@ -58,7 +57,7 @@ func setTokenCookie(c echo.Context, name, token string, expiration time.Time) {
}

// generateToken generates a jwt token.
func generateToken(username string, userID int, aud string, expirationTime time.Time, secret []byte) (string, error) {
func generateToken(username string, userID int32, aud string, expirationTime time.Time, secret []byte) (string, error) {
// Create the JWT claims, which includes the username and expiry time.
claims := &claimsMessage{
Name: username,
Expand All @@ -68,7 +67,7 @@ func generateToken(username string, userID int, aud string, expirationTime time.
ExpiresAt: jwt.NewNumericDate(expirationTime),
IssuedAt: jwt.NewNumericDate(time.Now()),
Issuer: auth.Issuer,
Subject: strconv.Itoa(userID),
Subject: fmt.Sprintf("%d", userID),
},
}

Expand Down Expand Up @@ -174,7 +173,7 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e
}

// We either have a valid access token or we will attempt to generate new access token and refresh token
userID, err := strconv.Atoi(claims.Subject)
userID, err := util.ConvertStringToInt32(claims.Subject)
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Malformed ID in the token.")
}
Expand Down
45 changes: 23 additions & 22 deletions api/v1/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/usememos/memos/api/auth"
"github.com/usememos/memos/common/util"
"github.com/usememos/memos/store"
)

Expand Down Expand Up @@ -39,11 +40,11 @@ func (v Visibility) String() string {
}

type Memo struct {
ID int `json:"id"`
ID int32 `json:"id"`

// Standard fields
RowStatus RowStatus `json:"rowStatus"`
CreatorID int `json:"creatorId"`
CreatorID int32 `json:"creatorId"`
CreatedTs int64 `json:"createdTs"`
UpdatedTs int64 `json:"updatedTs"`

Expand All @@ -62,20 +63,20 @@ type Memo struct {

type CreateMemoRequest struct {
// Standard fields
CreatorID int `json:"-"`
CreatorID int32 `json:"-"`
CreatedTs *int64 `json:"createdTs"`

// Domain specific fields
Visibility Visibility `json:"visibility"`
Content string `json:"content"`

// Related fields
ResourceIDList []int `json:"resourceIdList"`
ResourceIDList []int32 `json:"resourceIdList"`
RelationList []*UpsertMemoRelationRequest `json:"relationList"`
}

type PatchMemoRequest struct {
ID int `json:"-"`
ID int32 `json:"-"`

// Standard fields
CreatedTs *int64 `json:"createdTs"`
Expand All @@ -87,16 +88,16 @@ type PatchMemoRequest struct {
Visibility *Visibility `json:"visibility"`

// Related fields
ResourceIDList []int `json:"resourceIdList"`
ResourceIDList []int32 `json:"resourceIdList"`
RelationList []*UpsertMemoRelationRequest `json:"relationList"`
}

type FindMemoRequest struct {
ID *int
ID *int32

// Standard fields
RowStatus *RowStatus
CreatorID *int
CreatorID *int32

// Domain specific fields
Pinned *bool
Expand All @@ -114,7 +115,7 @@ const maxContentLength = 1 << 30
func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
g.POST("/memo", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
Expand Down Expand Up @@ -225,12 +226,12 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {

g.PATCH("/memo/:memoId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}

memoID, err := strconv.Atoi(c.Param("memoId"))
memoID, err := util.ConvertStringToInt32(c.Param("memoId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
}
Expand Down Expand Up @@ -352,7 +353,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
g.GET("/memo", func(c echo.Context) error {
ctx := c.Request().Context()
findMemoMessage := &store.FindMemo{}
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
if userID, err := util.ConvertStringToInt32(c.QueryParam("creatorId")); err == nil {
findMemoMessage.CreatorID = &userID
}

Expand All @@ -363,7 +364,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
}
}

currentUserID, ok := c.Get(auth.UserIDContextKey).(int)
currentUserID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
// Anonymous use should only fetch PUBLIC memos with specified user
if findMemoMessage.CreatorID == nil {
Expand Down Expand Up @@ -435,7 +436,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {

g.GET("/memo/:memoId", func(c echo.Context) error {
ctx := c.Request().Context()
memoID, err := strconv.Atoi(c.Param("memoId"))
memoID, err := util.ConvertStringToInt32(c.Param("memoId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
}
Expand All @@ -450,7 +451,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo not found: %d", memoID))
}

userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if memo.Visibility == store.Private {
if !ok || memo.CreatorID != userID {
return echo.NewHTTPError(http.StatusForbidden, "this memo is private only")
Expand All @@ -473,7 +474,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
findMemoMessage := &store.FindMemo{
RowStatus: &normalStatus,
}
if creatorID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
if creatorID, err := util.ConvertStringToInt32(c.QueryParam("creatorId")); err == nil {
findMemoMessage.CreatorID = &creatorID
}

Expand All @@ -488,7 +489,7 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
}

currentUserID, ok := c.Get(auth.UserIDContextKey).(int)
currentUserID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
findMemoMessage.VisibilityList = []store.Visibility{store.Public}
} else {
Expand Down Expand Up @@ -590,11 +591,11 @@ func (s *APIV1Service) registerMemoRoutes(g *echo.Group) {

g.DELETE("/memo/:memoId", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(auth.UserIDContextKey).(int)
userID, ok := c.Get(auth.UserIDContextKey).(int32)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
memoID, err := strconv.Atoi(c.Param("memoId"))
memoID, err := util.ConvertStringToInt32(c.Param("memoId"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
}
Expand Down Expand Up @@ -757,12 +758,12 @@ func getMemoRelationListDiff(oldList, newList []*store.MemoRelation) (addedList,
return addedList, removedList
}

func getIDListDiff(oldList, newList []int) (addedList, removedList []int) {
oldMap := map[int]bool{}
func getIDListDiff(oldList, newList []int32) (addedList, removedList []int32) {
oldMap := map[int32]bool{}
for _, id := range oldList {
oldMap[id] = true
}
newMap := map[int]bool{}
newMap := map[int32]bool{}
for _, id := range newList {
newMap[id] = true
}
Expand Down
Loading

0 comments on commit 7c5296c

Please sign in to comment.