diff --git a/api/v1/activity.go b/api/v1/activity.go index a5d1bc5b..ff90bbf0 100644 --- a/api/v1/activity.go +++ b/api/v1/activity.go @@ -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"` } @@ -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 @@ -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"` diff --git a/api/v1/auth.go b/api/v1/auth.go index cc2030a9..da9c803e 100644 --- a/api/v1/auth.go +++ b/api/v1/auth.go @@ -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"` } diff --git a/api/v1/idp.go b/api/v1/idp.go index 2580c2e6..0b63f749 100644 --- a/api/v1/idp.go +++ b/api/v1/idp.go @@ -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" ) @@ -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"` @@ -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"` @@ -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") } @@ -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") } @@ -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) } @@ -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{ @@ -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") } @@ -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) } @@ -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") } @@ -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) } diff --git a/api/v1/jwt.go b/api/v1/jwt.go index a3e1696c..af07f124 100644 --- a/api/v1/jwt.go +++ b/api/v1/jwt.go @@ -3,7 +3,6 @@ package v1 import ( "fmt" "net/http" - "strconv" "strings" "time" @@ -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)) } @@ -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, @@ -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), }, } @@ -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.") } diff --git a/api/v1/memo.go b/api/v1/memo.go index 1ce53cb2..ff01c894 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -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" ) @@ -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"` @@ -62,7 +63,7 @@ type Memo struct { type CreateMemoRequest struct { // Standard fields - CreatorID int `json:"-"` + CreatorID int32 `json:"-"` CreatedTs *int64 `json:"createdTs"` // Domain specific fields @@ -70,12 +71,12 @@ type CreateMemoRequest struct { 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"` @@ -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 @@ -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") } @@ -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) } @@ -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 } @@ -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 { @@ -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) } @@ -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") @@ -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 } @@ -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 { @@ -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) } @@ -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 } diff --git a/api/v1/memo_organizer.go b/api/v1/memo_organizer.go index 1e02912c..a55b7ffd 100644 --- a/api/v1/memo_organizer.go +++ b/api/v1/memo_organizer.go @@ -4,17 +4,17 @@ 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" ) type MemoOrganizer struct { - MemoID int `json:"memoId"` - UserID int `json:"userId"` - Pinned bool `json:"pinned"` + MemoID int32 `json:"memoId"` + UserID int32 `json:"userId"` + Pinned bool `json:"pinned"` } type UpsertMemoOrganizerRequest struct { @@ -24,12 +24,12 @@ type UpsertMemoOrganizerRequest struct { func (s *APIV1Service) registerMemoOrganizerRoutes(g *echo.Group) { g.POST("/memo/:memoId/organizer", 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) } - 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") } diff --git a/api/v1/memo_relation.go b/api/v1/memo_relation.go index 9ab8079a..63e94d56 100644 --- a/api/v1/memo_relation.go +++ b/api/v1/memo_relation.go @@ -4,9 +4,9 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "github.com/labstack/echo/v4" + "github.com/usememos/memos/common/util" "github.com/usememos/memos/store" ) @@ -18,20 +18,20 @@ const ( ) type MemoRelation struct { - MemoID int `json:"memoId"` - RelatedMemoID int `json:"relatedMemoId"` + MemoID int32 `json:"memoId"` + RelatedMemoID int32 `json:"relatedMemoId"` Type MemoRelationType `json:"type"` } type UpsertMemoRelationRequest struct { - RelatedMemoID int `json:"relatedMemoId"` + RelatedMemoID int32 `json:"relatedMemoId"` Type MemoRelationType `json:"type"` } func (s *APIV1Service) registerMemoRelationRoutes(g *echo.Group) { g.POST("/memo/:memoId/relation", 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) } @@ -54,7 +54,7 @@ func (s *APIV1Service) registerMemoRelationRoutes(g *echo.Group) { g.GET("/memo/:memoId/relation", 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) } @@ -70,11 +70,11 @@ func (s *APIV1Service) registerMemoRelationRoutes(g *echo.Group) { g.DELETE("/memo/:memoId/relation/:relatedMemoId/type/:relationType", 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("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err) } - relatedMemoID, err := strconv.Atoi(c.Param("relatedMemoId")) + relatedMemoID, err := util.ConvertStringToInt32(c.Param("relatedMemoId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Related memo ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) } diff --git a/api/v1/memo_resource.go b/api/v1/memo_resource.go index 197cacff..26fa5692 100644 --- a/api/v1/memo_resource.go +++ b/api/v1/memo_resource.go @@ -4,45 +4,45 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "time" "github.com/labstack/echo/v4" "github.com/usememos/memos/api/auth" + "github.com/usememos/memos/common/util" "github.com/usememos/memos/store" ) type MemoResource struct { - MemoID int `json:"memoId"` - ResourceID int `json:"resourceId"` + MemoID int32 `json:"memoId"` + ResourceID int32 `json:"resourceId"` CreatedTs int64 `json:"createdTs"` UpdatedTs int64 `json:"updatedTs"` } type UpsertMemoResourceRequest struct { - ResourceID int `json:"resourceId"` + ResourceID int32 `json:"resourceId"` UpdatedTs *int64 `json:"updatedTs"` } type MemoResourceFind struct { - MemoID *int - ResourceID *int + MemoID *int32 + ResourceID *int32 } type MemoResourceDelete struct { - MemoID *int - ResourceID *int + MemoID *int32 + ResourceID *int32 } func (s *APIV1Service) registerMemoResourceRoutes(g *echo.Group) { g.POST("/memo/:memoId/resource", 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) } - 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") } @@ -78,7 +78,7 @@ func (s *APIV1Service) registerMemoResourceRoutes(g *echo.Group) { g.GET("/memo/:memoId/resource", 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) } @@ -98,15 +98,15 @@ func (s *APIV1Service) registerMemoResourceRoutes(g *echo.Group) { g.DELETE("/memo/:memoId/resource/:resourceId", 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("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err) } - resourceID, err := strconv.Atoi(c.Param("resourceId")) + resourceID, err := util.ConvertStringToInt32(c.Param("resourceId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Resource ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) } diff --git a/api/v1/resource.go b/api/v1/resource.go index b65fb286..9edb7031 100644 --- a/api/v1/resource.go +++ b/api/v1/resource.go @@ -30,10 +30,10 @@ import ( ) type Resource struct { - ID int `json:"id"` + ID int32 `json:"id"` // Standard fields - CreatorID int `json:"creatorId"` + CreatorID int32 `json:"creatorId"` CreatedTs int64 `json:"createdTs"` UpdatedTs int64 `json:"updatedTs"` @@ -58,8 +58,8 @@ type CreateResourceRequest struct { } type FindResourceRequest struct { - ID *int `json:"id"` - CreatorID *int `json:"creatorId"` + ID *int32 `json:"id"` + CreatorID *int32 `json:"creatorId"` Filename *string `json:"filename"` } @@ -83,7 +83,7 @@ var fileKeyPattern = regexp.MustCompile(`\{[a-z]{1,9}\}`) func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { g.POST("/resource", 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") } @@ -157,7 +157,7 @@ func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { g.POST("/resource/blob", 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") } @@ -217,7 +217,7 @@ func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { g.GET("/resource", 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") } @@ -244,12 +244,12 @@ func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { g.PATCH("/resource/:resourceId", 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") } - resourceID, err := strconv.Atoi(c.Param("resourceId")) + resourceID, err := util.ConvertStringToInt32(c.Param("resourceId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) } @@ -290,12 +290,12 @@ func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { g.DELETE("/resource/:resourceId", 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") } - resourceID, err := strconv.Atoi(c.Param("resourceId")) + resourceID, err := util.ConvertStringToInt32(c.Param("resourceId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) } @@ -335,7 +335,7 @@ func (s *APIV1Service) registerResourceRoutes(g *echo.Group) { func (s *APIV1Service) registerResourcePublicRoutes(g *echo.Group) { f := func(c echo.Context) error { ctx := c.Request().Context() - resourceID, err := strconv.Atoi(c.Param("resourceId")) + resourceID, err := util.ConvertStringToInt32(c.Param("resourceId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err) } @@ -346,7 +346,7 @@ func (s *APIV1Service) registerResourcePublicRoutes(g *echo.Group) { } // Protected resource require a logined user - userID, ok := c.Get(auth.UserIDContextKey).(int) + userID, ok := c.Get(auth.UserIDContextKey).(int32) if resourceVisibility == store.Protected && (!ok || userID <= 0) { return echo.NewHTTPError(http.StatusUnauthorized, "Resource visibility not match").SetInternal(err) } @@ -501,7 +501,7 @@ func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error) return dstBlob, nil } -func checkResourceVisibility(ctx context.Context, s *store.Store, resourceID int) (store.Visibility, error) { +func checkResourceVisibility(ctx context.Context, s *store.Store, resourceID int32) (store.Visibility, error) { memoResources, err := s.ListMemoResources(ctx, &store.FindMemoResource{ ResourceID: &resourceID, }) @@ -514,7 +514,7 @@ func checkResourceVisibility(ctx context.Context, s *store.Store, resourceID int return store.Private, nil } - memoIDs := make([]int, 0, len(memoResources)) + memoIDs := make([]int32, 0, len(memoResources)) for _, memoResource := range memoResources { memoIDs = append(memoIDs, memoResource.MemoID) } diff --git a/api/v1/rss.go b/api/v1/rss.go index 7d130cf2..320d3938 100644 --- a/api/v1/rss.go +++ b/api/v1/rss.go @@ -49,7 +49,7 @@ func (s *APIV1Service) registerRSSRoutes(g *echo.Group) { g.GET("/u/:id/rss.xml", func(c echo.Context) error { ctx := c.Request().Context() - id, err := strconv.Atoi(c.Param("id")) + id, err := util.ConvertStringToInt32(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err) } @@ -94,10 +94,10 @@ func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []* memo := memoList[i] feed.Items[i] = &feeds.Item{ Title: getRSSItemTitle(memo.Content), - Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)}, + Link: &feeds.Link{Href: baseURL + "/m/" + fmt.Sprintf("%d", memo.ID)}, Description: getRSSItemDescription(memo.Content), Created: time.Unix(memo.CreatedTs, 0), - Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + strconv.Itoa(memo.ID) + "/image"}, + Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + fmt.Sprintf("%d", memo.ID) + "/image"}, } if len(memo.ResourceIDList) > 0 { resourceID := memo.ResourceIDList[0] @@ -114,7 +114,7 @@ func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []* if resource.ExternalLink != "" { enclosure.Url = resource.ExternalLink } else { - enclosure.Url = baseURL + "/o/r/" + strconv.Itoa(resource.ID) + enclosure.Url = baseURL + "/o/r/" + fmt.Sprintf("%d", resource.ID) } enclosure.Length = strconv.Itoa(int(resource.Size)) enclosure.Type = resource.Type diff --git a/api/v1/storage.go b/api/v1/storage.go index f5b181fc..e7beba7f 100644 --- a/api/v1/storage.go +++ b/api/v1/storage.go @@ -4,18 +4,18 @@ 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" ) const ( // LocalStorage means the storage service is local file system. - LocalStorage = -1 + LocalStorage int32 = -1 // DatabaseStorage means the storage service is database. - DatabaseStorage = 0 + DatabaseStorage int32 = 0 ) type StorageType string @@ -44,7 +44,7 @@ type StorageS3Config struct { } type Storage struct { - ID int `json:"id"` + ID int32 `json:"id"` Name string `json:"name"` Type StorageType `json:"type"` Config *StorageConfig `json:"config"` @@ -65,7 +65,7 @@ type UpdateStorageRequest struct { func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { g.POST("/storage", 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") } @@ -111,7 +111,7 @@ func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { g.PATCH("/storage/:storageId", 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") } @@ -126,7 +126,7 @@ func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") } - storageID, err := strconv.Atoi(c.Param("storageId")) + storageID, err := util.ConvertStringToInt32(c.Param("storageId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("storageId"))).SetInternal(err) } @@ -165,7 +165,7 @@ func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { g.GET("/storage", 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") } @@ -199,7 +199,7 @@ func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { g.DELETE("/storage/:storageId", 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") } @@ -214,7 +214,7 @@ func (s *APIV1Service) registerStorageRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") } - storageID, err := strconv.Atoi(c.Param("storageId")) + storageID, err := util.ConvertStringToInt32(c.Param("storageId")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("storageId"))).SetInternal(err) } diff --git a/api/v1/system.go b/api/v1/system.go index 1f48f1fb..3c8b2578 100644 --- a/api/v1/system.go +++ b/api/v1/system.go @@ -35,7 +35,7 @@ type SystemStatus struct { // Customized server profile, including server name and external url. CustomizedProfile CustomizedProfile `json:"customizedProfile"` // Storage service ID. - StorageServiceID int `json:"storageServiceId"` + StorageServiceID int32 `json:"storageServiceId"` // Local storage path. LocalStoragePath string `json:"localStoragePath"` // Memo display with updated timestamp. @@ -126,7 +126,7 @@ func (s *APIV1Service) registerSystemRoutes(g *echo.Group) { } systemStatus.CustomizedProfile = customizedProfile case SystemSettingStorageServiceIDName.String(): - systemStatus.StorageServiceID = int(baseValue.(float64)) + systemStatus.StorageServiceID = int32(baseValue.(float64)) case SystemSettingLocalStoragePathName.String(): systemStatus.LocalStoragePath = baseValue.(string) case SystemSettingMemoDisplayWithUpdatedTsName.String(): @@ -141,7 +141,7 @@ func (s *APIV1Service) registerSystemRoutes(g *echo.Group) { g.POST("/system/vacuum", 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") } diff --git a/api/v1/system_setting.go b/api/v1/system_setting.go index cb5f28fd..6aec4490 100644 --- a/api/v1/system_setting.go +++ b/api/v1/system_setting.go @@ -187,7 +187,7 @@ func (upsert UpsertSystemSettingRequest) Validate() error { func (s *APIV1Service) registerSystemSettingRoutes(g *echo.Group) { g.POST("/system/setting", 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") } @@ -237,7 +237,7 @@ func (s *APIV1Service) registerSystemSettingRoutes(g *echo.Group) { g.GET("/system/setting", 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") } diff --git a/api/v1/tag.go b/api/v1/tag.go index 1c4488a4..12d95f57 100644 --- a/api/v1/tag.go +++ b/api/v1/tag.go @@ -16,7 +16,7 @@ import ( type Tag struct { Name string - CreatorID int + CreatorID int32 } type UpsertTagRequest struct { @@ -30,7 +30,7 @@ type DeleteTagRequest struct { func (s *APIV1Service) registerTagRoutes(g *echo.Group) { g.POST("/tag", 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") } @@ -59,7 +59,7 @@ func (s *APIV1Service) registerTagRoutes(g *echo.Group) { g.GET("/tag", 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.StatusBadRequest, "Missing user id to find tag") } @@ -80,7 +80,7 @@ func (s *APIV1Service) registerTagRoutes(g *echo.Group) { g.GET("/tag/suggestion", 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.StatusBadRequest, "Missing user session") } @@ -125,7 +125,7 @@ func (s *APIV1Service) registerTagRoutes(g *echo.Group) { g.POST("/tag/delete", 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") } diff --git a/api/v1/user.go b/api/v1/user.go index 7fa672e3..51fc863d 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "time" "github.com/labstack/echo/v4" @@ -32,7 +31,7 @@ func (role Role) String() string { } type User struct { - ID int `json:"id"` + ID int32 `json:"id"` // Standard fields RowStatus RowStatus `json:"rowStatus"` @@ -133,7 +132,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // POST /user - Create a new user. g.POST("/user", 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 auth session") } @@ -208,7 +207,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // GET /user/me - Get current user. g.GET("/user/me", 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 auth session") } @@ -239,7 +238,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // GET /user/:id - Get user by id. g.GET("/user/:id", func(c echo.Context) error { ctx := c.Request().Context() - id, err := strconv.Atoi(c.Param("id")) + id, err := util.ConvertStringToInt32(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err) } @@ -282,12 +281,12 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // PUT /user/:id - Update user by id. g.PATCH("/user/:id", func(c echo.Context) error { ctx := c.Request().Context() - userID, err := strconv.Atoi(c.Param("id")) + userID, err := util.ConvertStringToInt32(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) } - currentUserID, ok := c.Get(auth.UserIDContextKey).(int) + currentUserID, ok := c.Get(auth.UserIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") } @@ -367,7 +366,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // DELETE /user/:id - Delete user by id. g.DELETE("/user/:id", func(c echo.Context) error { ctx := c.Request().Context() - currentUserID, ok := c.Get(auth.UserIDContextKey).(int) + currentUserID, ok := c.Get(auth.UserIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") } @@ -383,7 +382,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusForbidden, "Unauthorized to delete user").SetInternal(err) } - userID, err := strconv.Atoi(c.Param("id")) + userID, err := util.ConvertStringToInt32(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err) } diff --git a/api/v1/user_setting.go b/api/v1/user_setting.go index cab0f53b..4ef883f3 100644 --- a/api/v1/user_setting.go +++ b/api/v1/user_setting.go @@ -67,13 +67,13 @@ var ( ) type UserSetting struct { - UserID int `json:"userId"` + UserID int32 `json:"userId"` Key UserSettingKey `json:"key"` Value string `json:"value"` } type UpsertUserSettingRequest struct { - UserID int `json:"-"` + UserID int32 `json:"-"` Key UserSettingKey `json:"key"` Value string `json:"value"` } @@ -122,7 +122,7 @@ func (upsert UpsertUserSettingRequest) Validate() error { func (s *APIV1Service) registerUserSettingRoutes(g *echo.Group) { g.POST("/user/setting", 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 auth session") } diff --git a/api/v2/jwt.go b/api/v2/jwt.go index 7fdf3cad..f26eb1a5 100644 --- a/api/v2/jwt.go +++ b/api/v2/jwt.go @@ -10,6 +10,7 @@ import ( "github.com/golang-jwt/jwt/v4" "github.com/pkg/errors" "github.com/usememos/memos/api/auth" + "github.com/usememos/memos/common/util" "github.com/usememos/memos/store" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -76,7 +77,7 @@ func (in *GRPCAuthInterceptor) AuthenticationInterceptor(ctx context.Context, re return handler(childCtx, request) } -func (in *GRPCAuthInterceptor) authenticate(ctx context.Context, accessTokenStr string) (int, error) { +func (in *GRPCAuthInterceptor) authenticate(ctx context.Context, accessTokenStr string) (int32, error) { if accessTokenStr == "" { return 0, status.Errorf(codes.Unauthenticated, "access token not found") } @@ -103,7 +104,7 @@ func (in *GRPCAuthInterceptor) authenticate(ctx context.Context, accessTokenStr ) } - userID, err := strconv.Atoi(claims.Subject) + userID, err := util.ConvertStringToInt32(claims.Subject) if err != nil { return 0, status.Errorf(codes.Unauthenticated, "malformed ID %q in the access token", claims.Subject) } diff --git a/api/v2/tag_service.go b/api/v2/tag_service.go index 3f063fdf..1a5500d1 100644 --- a/api/v2/tag_service.go +++ b/api/v2/tag_service.go @@ -24,7 +24,7 @@ func NewTagService(store *store.Store) *TagService { func (s *TagService) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) { tags, err := s.Store.ListTags(ctx, &store.FindTag{ - CreatorID: int(request.CreatorId), + CreatorID: request.CreatorId, }) if err != nil { return nil, status.Errorf(codes.Internal, "failed to list tags: %v", err) diff --git a/api/v2/user_service.go b/api/v2/user_service.go index c9b51403..abd3d036 100644 --- a/api/v2/user_service.go +++ b/api/v2/user_service.go @@ -37,9 +37,8 @@ func (s *UserService) GetUser(ctx context.Context, request *apiv2pb.GetUserReque // Data desensitization. userMessage.OpenId = "" - userUID := int(userMessage.Id) userSettings, err := s.Store.ListUserSettings(ctx, &store.FindUserSetting{ - UserID: &userUID, + UserID: &userMessage.Id, }) if err != nil { return nil, status.Errorf(codes.Internal, "failed to list user settings: %v", err) diff --git a/common/util/util.go b/common/util/util.go index 4ae025d6..96d2fde8 100644 --- a/common/util/util.go +++ b/common/util/util.go @@ -4,11 +4,21 @@ import ( "crypto/rand" "math/big" "net/mail" + "strconv" "strings" "github.com/google/uuid" ) +// ConvertStringToInt32 converts a string to int32. +func ConvertStringToInt32(src string) (int32, error) { + i, err := strconv.Atoi(src) + if err != nil { + return 0, err + } + return int32(i), nil +} + // HasPrefixes returns true if the string s has any of the given prefixes. func HasPrefixes(src string, prefixes ...string) bool { for _, prefix := range prefixes { diff --git a/server/telegram.go b/server/telegram.go index b3b1da1b..4de2fe74 100644 --- a/server/telegram.go +++ b/server/telegram.go @@ -37,7 +37,7 @@ func (t *telegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot, return fmt.Errorf("fail to SendReplyMessage: %s", err) } - var creatorID int + var creatorID int32 userSettingList, err := t.store.ListUserSettings(ctx, &store.FindUserSetting{ Key: apiv1.UserSettingTelegramUserIDKey.String(), }) @@ -121,7 +121,7 @@ func (t *telegramHandler) MessageHandle(ctx context.Context, bot *telegram.Bot, } func (t *telegramHandler) CallbackQueryHandle(ctx context.Context, bot *telegram.Bot, callbackQuery telegram.CallbackQuery) error { - var memoID int + var memoID int32 var visibility store.Visibility n, err := fmt.Sscanf(callbackQuery.Data, "%s %d", &visibility, &memoID) if err != nil || n != 2 { @@ -146,7 +146,7 @@ func (t *telegramHandler) CallbackQueryHandle(ctx context.Context, bot *telegram return bot.AnswerCallbackQuery(ctx, callbackQuery.ID, fmt.Sprintf("Success change Memo %d to %s", memoID, visibility)) } -func generateKeyboardForMemoID(id int) [][]telegram.InlineKeyboardButton { +func generateKeyboardForMemoID(id int32) [][]telegram.InlineKeyboardButton { allVisibility := []store.Visibility{ store.Public, store.Protected, diff --git a/store/activity.go b/store/activity.go index 639bb43e..96c80c5f 100644 --- a/store/activity.go +++ b/store/activity.go @@ -5,10 +5,10 @@ import ( ) type Activity struct { - ID int + ID int32 // Standard fields - CreatorID int + CreatorID int32 CreatedTs int64 // Domain specific fields diff --git a/store/cache.go b/store/cache.go index 15cd713f..2a060f9b 100644 --- a/store/cache.go +++ b/store/cache.go @@ -4,6 +4,6 @@ import ( "fmt" ) -func getUserSettingCacheKey(userID int, key string) string { +func getUserSettingCacheKey(userID int32, key string) string { return fmt.Sprintf("%d-%s", userID, key) } diff --git a/store/idp.go b/store/idp.go index e94eab73..61a1df8a 100644 --- a/store/idp.go +++ b/store/idp.go @@ -38,7 +38,7 @@ type FieldMapping struct { } type IdentityProvider struct { - ID int + ID int32 Name string Type IdentityProviderType IdentifierFilter string @@ -46,11 +46,11 @@ type IdentityProvider struct { } type FindIdentityProvider struct { - ID *int + ID *int32 } type UpdateIdentityProvider struct { - ID int + ID int32 Type IdentityProviderType Name *string IdentifierFilter *string @@ -58,7 +58,7 @@ type UpdateIdentityProvider struct { } type DeleteIdentityProvider struct { - ID int + ID int32 } func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProvider) (*IdentityProvider, error) { diff --git a/store/memo.go b/store/memo.go index 83c7b0f9..af259b72 100644 --- a/store/memo.go +++ b/store/memo.go @@ -4,9 +4,10 @@ import ( "context" "database/sql" "fmt" - "strconv" "strings" "time" + + "github.com/usememos/memos/common/util" ) // Visibility is the type of a visibility. @@ -34,11 +35,11 @@ func (v Visibility) String() string { } type Memo struct { - ID int + ID int32 // Standard fields RowStatus RowStatus - CreatorID int + CreatorID int32 CreatedTs int64 UpdatedTs int64 @@ -48,16 +49,16 @@ type Memo struct { // Composed fields Pinned bool - ResourceIDList []int + ResourceIDList []int32 RelationList []*MemoRelation } type FindMemo struct { - ID *int + ID *int32 // Standard fields RowStatus *RowStatus - CreatorID *int + CreatorID *int32 // Domain specific fields Pinned *bool @@ -71,7 +72,7 @@ type FindMemo struct { } type UpdateMemo struct { - ID int + ID int32 CreatedTs *int64 UpdatedTs *int64 RowStatus *RowStatus @@ -80,7 +81,7 @@ type UpdateMemo struct { } type DeleteMemo struct { - ID int + ID int32 } func (s *Store) CreateMemo(ctx context.Context, create *Memo) (*Memo, error) { @@ -220,9 +221,9 @@ func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) if memoResourceIDList.Valid { idStringList := strings.Split(memoResourceIDList.String, ",") - memo.ResourceIDList = make([]int, 0, len(idStringList)) + memo.ResourceIDList = make([]int32, 0, len(idStringList)) for _, idString := range idStringList { - id, err := strconv.Atoi(idString) + id, err := util.ConvertStringToInt32(idString) if err != nil { return nil, err } @@ -237,7 +238,7 @@ func (s *Store) ListMemos(ctx context.Context, find *FindMemo) ([]*Memo, error) if len(relatedMemoTypeList) != 2 { return nil, fmt.Errorf("invalid relation format") } - relatedMemoID, err := strconv.Atoi(relatedMemoTypeList[0]) + relatedMemoID, err := util.ConvertStringToInt32(relatedMemoTypeList[0]) if err != nil { return nil, err } @@ -318,7 +319,7 @@ func (s *Store) DeleteMemo(ctx context.Context, delete *DeleteMemo) error { return nil } -func (s *Store) FindMemosVisibilityList(ctx context.Context, memoIDs []int) ([]Visibility, error) { +func (s *Store) FindMemosVisibilityList(ctx context.Context, memoIDs []int32) ([]Visibility, error) { args := make([]any, 0, len(memoIDs)) list := make([]string, 0, len(memoIDs)) for _, memoID := range memoIDs { diff --git a/store/memo_organizer.go b/store/memo_organizer.go index 910440a8..9737cffb 100644 --- a/store/memo_organizer.go +++ b/store/memo_organizer.go @@ -8,19 +8,19 @@ import ( ) type MemoOrganizer struct { - MemoID int - UserID int + MemoID int32 + UserID int32 Pinned bool } type FindMemoOrganizer struct { - MemoID int - UserID int + MemoID int32 + UserID int32 } type DeleteMemoOrganizer struct { - MemoID *int - UserID *int + MemoID *int32 + UserID *int32 } func (s *Store) UpsertMemoOrganizer(ctx context.Context, upsert *MemoOrganizer) (*MemoOrganizer, error) { diff --git a/store/memo_relation.go b/store/memo_relation.go index d32a7f35..a411a775 100644 --- a/store/memo_relation.go +++ b/store/memo_relation.go @@ -14,20 +14,20 @@ const ( ) type MemoRelation struct { - MemoID int - RelatedMemoID int + MemoID int32 + RelatedMemoID int32 Type MemoRelationType } type FindMemoRelation struct { - MemoID *int - RelatedMemoID *int + MemoID *int32 + RelatedMemoID *int32 Type *MemoRelationType } type DeleteMemoRelation struct { - MemoID *int - RelatedMemoID *int + MemoID *int32 + RelatedMemoID *int32 Type *MemoRelationType } diff --git a/store/memo_resource.go b/store/memo_resource.go index 80d993d0..6bde5ee6 100644 --- a/store/memo_resource.go +++ b/store/memo_resource.go @@ -7,27 +7,27 @@ import ( ) type MemoResource struct { - MemoID int - ResourceID int + MemoID int32 + ResourceID int32 CreatedTs int64 UpdatedTs int64 } type UpsertMemoResource struct { - MemoID int - ResourceID int + MemoID int32 + ResourceID int32 CreatedTs int64 UpdatedTs *int64 } type FindMemoResource struct { - MemoID *int - ResourceID *int + MemoID *int32 + ResourceID *int32 } type DeleteMemoResource struct { - MemoID *int - ResourceID *int + MemoID *int32 + ResourceID *int32 } func (s *Store) UpsertMemoResource(ctx context.Context, upsert *UpsertMemoResource) (*MemoResource, error) { diff --git a/store/resource.go b/store/resource.go index 706b6957..b72c12b6 100644 --- a/store/resource.go +++ b/store/resource.go @@ -8,10 +8,10 @@ import ( ) type Resource struct { - ID int + ID int32 // Standard fields - CreatorID int + CreatorID int32 CreatedTs int64 UpdatedTs int64 @@ -27,16 +27,16 @@ type Resource struct { type FindResource struct { GetBlob bool - ID *int - CreatorID *int + ID *int32 + CreatorID *int32 Filename *string - MemoID *int + MemoID *int32 Limit *int Offset *int } type UpdateResource struct { - ID int + ID int32 UpdatedTs *int64 Filename *string InternalPath *string @@ -44,7 +44,7 @@ type UpdateResource struct { } type DeleteResource struct { - ID int + ID int32 } func (s *Store) CreateResource(ctx context.Context, create *Resource) (*Resource, error) { diff --git a/store/storage.go b/store/storage.go index 8e8c7802..2562c2c8 100644 --- a/store/storage.go +++ b/store/storage.go @@ -6,24 +6,24 @@ import ( ) type Storage struct { - ID int + ID int32 Name string Type string Config string } type FindStorage struct { - ID *int + ID *int32 } type UpdateStorage struct { - ID int + ID int32 Name *string Config *string } type DeleteStorage struct { - ID int + ID int32 } func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) { diff --git a/store/tag.go b/store/tag.go index 37b0077f..713a076e 100644 --- a/store/tag.go +++ b/store/tag.go @@ -8,16 +8,16 @@ import ( type Tag struct { Name string - CreatorID int + CreatorID int32 } type FindTag struct { - CreatorID int + CreatorID int32 } type DeleteTag struct { Name string - CreatorID int + CreatorID int32 } func (s *Store) UpsertTag(ctx context.Context, upsert *Tag) (*Tag, error) { diff --git a/store/user.go b/store/user.go index 4ce11d70..3c10795b 100644 --- a/store/user.go +++ b/store/user.go @@ -30,7 +30,7 @@ func (e Role) String() string { } type User struct { - ID int + ID int32 // Standard fields RowStatus RowStatus @@ -48,7 +48,7 @@ type User struct { } type UpdateUser struct { - ID int + ID int32 UpdatedTs *int64 RowStatus *RowStatus @@ -63,7 +63,7 @@ type UpdateUser struct { } type FindUser struct { - ID *int + ID *int32 RowStatus *RowStatus Username *string Role *Role @@ -73,7 +73,7 @@ type FindUser struct { } type DeleteUser struct { - ID int + ID int32 } func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) { diff --git a/store/user_setting.go b/store/user_setting.go index fa948167..bd6e38a6 100644 --- a/store/user_setting.go +++ b/store/user_setting.go @@ -7,13 +7,13 @@ import ( ) type UserSetting struct { - UserID int + UserID int32 Key string Value string } type FindUserSetting struct { - UserID *int + UserID *int32 Key string } diff --git a/test/server/memo_relation_test.go b/test/server/memo_relation_test.go index a7d705c5..ca4deade 100644 --- a/test/server/memo_relation_test.go +++ b/test/server/memo_relation_test.go @@ -61,7 +61,7 @@ func TestMemoRelationServer(t *testing.T) { require.Len(t, memo2.RelationList, 1) } -func (s *TestingServer) postMemoRelationUpsert(memoID int, memoRelationUpsert *apiv1.UpsertMemoRelationRequest) (*apiv1.MemoRelation, error) { +func (s *TestingServer) postMemoRelationUpsert(memoID int32, memoRelationUpsert *apiv1.UpsertMemoRelationRequest) (*apiv1.MemoRelation, error) { rawData, err := json.Marshal(&memoRelationUpsert) if err != nil { return nil, errors.Wrap(err, "failed to marshal memo relation upsert") @@ -85,7 +85,7 @@ func (s *TestingServer) postMemoRelationUpsert(memoID int, memoRelationUpsert *a return memoRelation, nil } -func (s *TestingServer) deleteMemoRelation(memoID int, relatedMemoID int, relationType apiv1.MemoRelationType) error { +func (s *TestingServer) deleteMemoRelation(memoID int32, relatedMemoID int32, relationType apiv1.MemoRelationType) error { _, err := s.delete(fmt.Sprintf("/api/v1/memo/%d/relation/%d/type/%s", memoID, relatedMemoID, relationType), nil) return err } diff --git a/test/server/memo_test.go b/test/server/memo_test.go index 571163f7..5a24435b 100644 --- a/test/server/memo_test.go +++ b/test/server/memo_test.go @@ -59,7 +59,7 @@ func TestMemoServer(t *testing.T) { require.Len(t, memoList, 0) } -func (s *TestingServer) getMemo(memoID int) (*apiv1.Memo, error) { +func (s *TestingServer) getMemo(memoID int32) (*apiv1.Memo, error) { body, err := s.get(fmt.Sprintf("/api/v1/memo/%d", memoID), nil) if err != nil { return nil, err @@ -145,12 +145,12 @@ func (s *TestingServer) patchMemo(memoPatch *apiv1.PatchMemoRequest) (*apiv1.Mem return memo, nil } -func (s *TestingServer) deleteMemo(memoID int) error { +func (s *TestingServer) deleteMemo(memoID int32) error { _, err := s.delete(fmt.Sprintf("/api/v1/memo/%d", memoID), nil) return err } -func (s *TestingServer) postMemoOrganizer(memoID int, memosOrganizer *apiv1.UpsertMemoOrganizerRequest) (*apiv1.Memo, error) { +func (s *TestingServer) postMemoOrganizer(memoID int32, memosOrganizer *apiv1.UpsertMemoOrganizerRequest) (*apiv1.Memo, error) { rawData, err := json.Marshal(&memosOrganizer) if err != nil { return nil, errors.Wrap(err, "failed to marshal memos organizer") diff --git a/test/store/resource_test.go b/test/store/resource_test.go index a861bd39..9a601076 100644 --- a/test/store/resource_test.go +++ b/test/store/resource_test.go @@ -29,15 +29,15 @@ func TestResourceStore(t *testing.T) { }) require.NoError(t, err) require.Equal(t, correctFilename, res.Filename) - require.Equal(t, 1, res.ID) + require.Equal(t, int32(1), res.ID) notFoundResource, err := ts.GetResource(ctx, &store.FindResource{ Filename: &incorrectFilename, }) require.NoError(t, err) require.Nil(t, notFoundResource) - correctCreatorID := 101 - incorrectCreatorID := 102 + var correctCreatorID int32 = 101 + var incorrectCreatorID int32 = 102 _, err = ts.GetResource(ctx, &store.FindResource{ CreatorID: &correctCreatorID, })