π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify selected space API handlers
Neeraj Gupta7bb1763c731d
diff --git a/server/space/api/assets.go b/server/space/api/assets.goindex 3b20d37fdf..8ee1c8e356 100644--- a/server/space/api/assets.go+++ b/server/space/api/assets.go@@ -2,18 +2,15 @@ package apiimport ("github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/gin-gonic/gin")-func (h *Handlers) PresignUpload(c *gin.Context) {+func (h *Handlers) PresignUpload(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.PresignUploadRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Assets.PresignUpload(c, space, req)respondJSON(c, resp, err)}diff --git a/server/space/api/common.go b/server/space/api/common.goindex 08d5eba7c3..7bd9478727 100644--- a/server/space/api/common.go+++ b/server/space/api/common.go@@ -57,13 +57,17 @@ func stringParam(c *gin.Context, name string) (string, bool) {return value, true}-func selectedSpace(h *Handlers, c *gin.Context) (*spacerepo.SpaceRecord, bool) {- space, err := h.Module.SelectedSpace(c)- if err != nil {- respondJSON(c, nil, err)- return nil, false+type selectedSpaceHandler func(*gin.Context, *spacerepo.SpaceRecord)++func (h *Handlers) withSelectedSpace(fn selectedSpaceHandler) gin.HandlerFunc {+ return func(c *gin.Context) {+ space, err := h.Module.SelectedSpace(c)+ if err != nil {+ respondJSON(c, nil, err)+ return+ }+ fn(c, space)}- return space, true}func bindJSON(c *gin.Context, req any) bool {diff --git a/server/space/api/friends.go b/server/space/api/friends.goindex fbdecc6520..851f6cf2d1 100644--- a/server/space/api/friends.go+++ b/server/space/api/friends.go@@ -2,52 +2,37 @@ package apiimport ("github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/gin-gonic/gin")-func (h *Handlers) AddFriend(c *gin.Context) {+func (h *Handlers) AddFriend(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.AddFriendPayloadif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Friends.Add(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) Unfriend(c *gin.Context) {+func (h *Handlers) Unfriend(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.FriendTargetPayloadif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }respondStatus(c, h.Module.Friends.Unfriend(c, space, req))}-func (h *Handlers) ListFriendRequests(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) ListFriendRequests(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Friends.ListRequests(c, space)respondJSON(c, resp, err)}-func (h *Handlers) ConfirmFriendRequest(c *gin.Context) {+func (h *Handlers) ConfirmFriendRequest(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.ConfirmFriendRequestPayloadif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }requestID, ok := positiveInt64Param(c, "requestID")if !ok {return@@ -56,11 +41,7 @@ func (h *Handlers) ConfirmFriendRequest(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) DeleteFriendRequest(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) DeleteFriendRequest(c *gin.Context, space *spacerepo.SpaceRecord) {requestID, ok := positiveInt64Param(c, "requestID")if !ok {return@@ -68,45 +49,29 @@ func (h *Handlers) DeleteFriendRequest(c *gin.Context) {respondStatus(c, h.Module.Friends.DeleteRequest(c, space, requestID))}-func (h *Handlers) ListSpaceFriends(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) ListSpaceFriends(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Friends.ListFriends(c, space)respondJSON(c, resp, err)}-func (h *Handlers) FriendRelationship(c *gin.Context) {+func (h *Handlers) FriendRelationship(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.FriendRelationshipRequestif !bindQuery(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Friends.Relationship(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) RefreshFriendShares(c *gin.Context) {+func (h *Handlers) RefreshFriendShares(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.RefreshFriendSharesRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }respondStatus(c, h.Module.Friends.RefreshShares(c, space, req))}-func (h *Handlers) ListFriendShares(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) ListFriendShares(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Friends.ListShares(c, space)respondJSON(c, resp, err)}diff --git a/server/space/api/links.go b/server/space/api/links.goindex 91b90909fd..f3f111f252 100644--- a/server/space/api/links.go+++ b/server/space/api/links.go@@ -2,49 +2,34 @@ package apiimport ("github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/gin-gonic/gin")-func (h *Handlers) GetSpaceLink(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) GetSpaceLink(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Links.Get(c, space)respondJSON(c, resp, err)}-func (h *Handlers) CreateSpaceLink(c *gin.Context) {+func (h *Handlers) CreateSpaceLink(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.SpaceLinkCreateRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Links.Create(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) RotateSpaceLink(c *gin.Context) {+func (h *Handlers) RotateSpaceLink(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.SpaceLinkCreateRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Links.Rotate(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) DeleteSpaceLink(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) DeleteSpaceLink(c *gin.Context, space *spacerepo.SpaceRecord) {respondStatus(c, h.Module.Links.Delete(c, space))}diff --git a/server/space/api/messages.go b/server/space/api/messages.goindex 33fe4291b9..3a21e481c9 100644--- a/server/space/api/messages.go+++ b/server/space/api/messages.go@@ -2,18 +2,15 @@ package apiimport ("github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/gin-gonic/gin")-func (h *Handlers) CreateMessage(c *gin.Context) {+func (h *Handlers) CreateMessage(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.CreateMessageRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }friendSpaceID, ok := stringParam(c, "friendSpaceID")if !ok {return@@ -22,19 +19,15 @@ func (h *Handlers) CreateMessage(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) LikeMessage(c *gin.Context) {- h.setMessageLike(c, true)+func (h *Handlers) LikeMessage(c *gin.Context, space *spacerepo.SpaceRecord) {+ h.setMessageLike(c, space, true)}-func (h *Handlers) UnlikeMessage(c *gin.Context) {- h.setMessageLike(c, false)+func (h *Handlers) UnlikeMessage(c *gin.Context, space *spacerepo.SpaceRecord) {+ h.setMessageLike(c, space, false)}-func (h *Handlers) setMessageLike(c *gin.Context, like bool) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) setMessageLike(c *gin.Context, space *spacerepo.SpaceRecord, like bool) {messageID, ok := stringParam(c, "messageID")if !ok {return@@ -43,11 +36,7 @@ func (h *Handlers) setMessageLike(c *gin.Context, like bool) {respondJSON(c, resp, err)}-func (h *Handlers) DeleteMessage(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) DeleteMessage(c *gin.Context, space *spacerepo.SpaceRecord) {messageID, ok := stringParam(c, "messageID")if !ok {return@@ -56,15 +45,11 @@ func (h *Handlers) DeleteMessage(c *gin.Context) {respondJSON(c, nil, err)}-func (h *Handlers) ReplyToPost(c *gin.Context) {+func (h *Handlers) ReplyToPost(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.CreateMessageRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }postID, ok := positiveInt64Param(c, "postID")if !ok {return@@ -73,24 +58,16 @@ func (h *Handlers) ReplyToPost(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) ListConversations(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) ListConversations(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Messages.ListConversations(c, space)respondJSON(c, resp, err)}-func (h *Handlers) ListMessageThread(c *gin.Context) {+func (h *Handlers) ListMessageThread(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.ListMessageThreadRequestif !bindQuery(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }friendSpaceID, ok := stringParam(c, "friendSpaceID")if !ok {returndiff --git a/server/space/api/posts.go b/server/space/api/posts.goindex ecaca66f63..d64ac45748 100644--- a/server/space/api/posts.go+++ b/server/space/api/posts.go@@ -2,31 +2,24 @@ package apiimport ("github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/gin-gonic/gin")-func (h *Handlers) CreatePost(c *gin.Context) {+func (h *Handlers) CreatePost(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.CreatePostRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Posts.Create(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) ListFeed(c *gin.Context) {+func (h *Handlers) ListFeed(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.ListFeedRequestif !bindQuery(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Posts.ListFeed(c, space, req)respondJSON(c, resp, err)}@@ -55,15 +48,11 @@ func (h *Handlers) GetPost(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) UpdatePostCaption(c *gin.Context) {+func (h *Handlers) UpdatePostCaption(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.UpdatePostCaptionRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }postID, ok := positiveInt64Param(c, "postID")if !ok {return@@ -72,19 +61,15 @@ func (h *Handlers) UpdatePostCaption(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) LikePost(c *gin.Context) {- h.setPostLike(c, true)+func (h *Handlers) LikePost(c *gin.Context, space *spacerepo.SpaceRecord) {+ h.setPostLike(c, space, true)}-func (h *Handlers) UnlikePost(c *gin.Context) {- h.setPostLike(c, false)+func (h *Handlers) UnlikePost(c *gin.Context, space *spacerepo.SpaceRecord) {+ h.setPostLike(c, space, false)}-func (h *Handlers) setPostLike(c *gin.Context, like bool) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) setPostLike(c *gin.Context, space *spacerepo.SpaceRecord, like bool) {postID, ok := positiveInt64Param(c, "postID")if !ok {return@@ -93,11 +78,7 @@ func (h *Handlers) setPostLike(c *gin.Context, like bool) {respondJSON(c, resp, err)}-func (h *Handlers) DeletePost(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) DeletePost(c *gin.Context, space *spacerepo.SpaceRecord) {postID, ok := positiveInt64Param(c, "postID")if !ok {returndiff --git a/server/space/api/read_markers.go b/server/space/api/read_markers.goindex c986d22357..84f6b7946a 100644--- a/server/space/api/read_markers.go+++ b/server/space/api/read_markers.go@@ -1,21 +1,16 @@package api-import "github.com/gin-gonic/gin"+import (+ spacerepo "github.com/ente-io/museum/space/repo"+ "github.com/gin-gonic/gin"+)-func (h *Handlers) GetUnreadStatus(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) GetUnreadStatus(c *gin.Context, space *spacerepo.SpaceRecord) {resp, err := h.Module.Read.GetUnreadStatus(c, space)respondJSON(c, resp, err)}-func (h *Handlers) MarkNotificationsRead(c *gin.Context) {- space, ok := selectedSpace(h, c)- if !ok {- return- }+func (h *Handlers) MarkNotificationsRead(c *gin.Context, space *spacerepo.SpaceRecord) {friendSpaceID, ok := stringParam(c, "friendSpaceID")if !ok {returndiff --git a/server/space/api/register.go b/server/space/api/register.goindex 2f1ee2cba3..ab88bd058d 100644--- a/server/space/api/register.go+++ b/server/space/api/register.go@@ -7,40 +7,41 @@ func Register(privateAPI, publicAPI gin.IRouter, handlers *Handlers) {privateAPI.POST("/account/space", handlers.CreateSpace)spaceAPI := privateAPI.Group("/spaces/:spaceID", handlers.RequireSelectedSpace())- spaceAPI.POST("/profile", handlers.UpdateSpaceProfile)- spaceAPI.PUT("/slug", handlers.UpdateSpaceSlug)- spaceAPI.PATCH("/slug", handlers.UpdateSpaceSlug)- spaceAPI.POST("/uploads/presign", handlers.PresignUpload)- spaceAPI.GET("/unread", handlers.GetUnreadStatus)- spaceAPI.POST("/posts", handlers.CreatePost)- spaceAPI.GET("/feed", handlers.ListFeed)- spaceAPI.POST("/posts/:postID/caption", handlers.UpdatePostCaption)- spaceAPI.PATCH("/posts/:postID/caption", handlers.UpdatePostCaption)- spaceAPI.PUT("/posts/:postID/like", handlers.LikePost)- spaceAPI.DELETE("/posts/:postID/like", handlers.UnlikePost)- spaceAPI.POST("/posts/:postID/reply", handlers.ReplyToPost)- spaceAPI.DELETE("/posts/:postID", handlers.DeletePost)- spaceAPI.GET("/conversations", handlers.ListConversations)- spaceAPI.POST("/friends/:friendSpaceID/read", handlers.MarkNotificationsRead)- spaceAPI.GET("/friends/:friendSpaceID/messages", handlers.ListMessageThread)- spaceAPI.POST("/friends/:friendSpaceID/messages", handlers.CreateMessage)- spaceAPI.PUT("/messages/:messageID/like", handlers.LikeMessage)- spaceAPI.DELETE("/messages/:messageID/like", handlers.UnlikeMessage)- spaceAPI.DELETE("/messages/:messageID", handlers.DeleteMessage)- spaceAPI.POST("/friends/add", handlers.AddFriend)- spaceAPI.GET("/friends/requests", handlers.ListFriendRequests)- spaceAPI.POST("/friends/requests/:requestID/confirm", handlers.ConfirmFriendRequest)- spaceAPI.DELETE("/friends/requests/:requestID", handlers.DeleteFriendRequest)- spaceAPI.POST("/friends/unfriend", handlers.Unfriend)- spaceAPI.GET("/friends", handlers.ListSpaceFriends)- spaceAPI.GET("/friends/relationship", handlers.FriendRelationship)- spaceAPI.POST("/friends/shares/refresh", handlers.RefreshFriendShares)- spaceAPI.GET("/friends/shares", handlers.ListFriendShares)- spaceAPI.POST("/rotate", handlers.RotateSpaceKey)- spaceAPI.GET("/links", handlers.GetSpaceLink)- spaceAPI.POST("/links", handlers.CreateSpaceLink)- spaceAPI.POST("/links/rotate", handlers.RotateSpaceLink)- spaceAPI.DELETE("/links", handlers.DeleteSpaceLink)+ selected := handlers.withSelectedSpace+ spaceAPI.POST("/profile", selected(handlers.UpdateSpaceProfile))+ spaceAPI.PUT("/slug", selected(handlers.UpdateSpaceSlug))+ spaceAPI.PATCH("/slug", selected(handlers.UpdateSpaceSlug))+ spaceAPI.POST("/uploads/presign", selected(handlers.PresignUpload))+ spaceAPI.GET("/unread", selected(handlers.GetUnreadStatus))+ spaceAPI.POST("/posts", selected(handlers.CreatePost))+ spaceAPI.GET("/feed", selected(handlers.ListFeed))+ spaceAPI.POST("/posts/:postID/caption", selected(handlers.UpdatePostCaption))+ spaceAPI.PATCH("/posts/:postID/caption", selected(handlers.UpdatePostCaption))+ spaceAPI.PUT("/posts/:postID/like", selected(handlers.LikePost))+ spaceAPI.DELETE("/posts/:postID/like", selected(handlers.UnlikePost))+ spaceAPI.POST("/posts/:postID/reply", selected(handlers.ReplyToPost))+ spaceAPI.DELETE("/posts/:postID", selected(handlers.DeletePost))+ spaceAPI.GET("/conversations", selected(handlers.ListConversations))+ spaceAPI.POST("/friends/:friendSpaceID/read", selected(handlers.MarkNotificationsRead))+ spaceAPI.GET("/friends/:friendSpaceID/messages", selected(handlers.ListMessageThread))+ spaceAPI.POST("/friends/:friendSpaceID/messages", selected(handlers.CreateMessage))+ spaceAPI.PUT("/messages/:messageID/like", selected(handlers.LikeMessage))+ spaceAPI.DELETE("/messages/:messageID/like", selected(handlers.UnlikeMessage))+ spaceAPI.DELETE("/messages/:messageID", selected(handlers.DeleteMessage))+ spaceAPI.POST("/friends/add", selected(handlers.AddFriend))+ spaceAPI.GET("/friends/requests", selected(handlers.ListFriendRequests))+ spaceAPI.POST("/friends/requests/:requestID/confirm", selected(handlers.ConfirmFriendRequest))+ spaceAPI.DELETE("/friends/requests/:requestID", selected(handlers.DeleteFriendRequest))+ spaceAPI.POST("/friends/unfriend", selected(handlers.Unfriend))+ spaceAPI.GET("/friends", selected(handlers.ListSpaceFriends))+ spaceAPI.GET("/friends/relationship", selected(handlers.FriendRelationship))+ spaceAPI.POST("/friends/shares/refresh", selected(handlers.RefreshFriendShares))+ spaceAPI.GET("/friends/shares", selected(handlers.ListFriendShares))+ spaceAPI.POST("/rotate", selected(handlers.RotateSpaceKey))+ spaceAPI.GET("/links", selected(handlers.GetSpaceLink))+ spaceAPI.POST("/links", selected(handlers.CreateSpaceLink))+ spaceAPI.POST("/links/rotate", selected(handlers.RotateSpaceLink))+ spaceAPI.DELETE("/links", selected(handlers.DeleteSpaceLink))publicAPI.GET("/spaces/:spaceID/profile", handlers.GetSpaceProfile)publicAPI.GET("/spaces/:spaceID/assets/redirect", handlers.AssetRedirect)diff --git a/server/space/api/spaces.go b/server/space/api/spaces.goindex d506bebaf8..4d9ce338da 100644--- a/server/space/api/spaces.go+++ b/server/space/api/spaces.go@@ -5,6 +5,7 @@ import ("net/http""github.com/ente-io/museum/space/models"+ spacerepo "github.com/ente-io/museum/space/repo""github.com/ente-io/stacktrace""github.com/gin-gonic/gin")@@ -33,28 +34,20 @@ func (h *Handlers) GetSpaceProfile(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) UpdateSpaceProfile(c *gin.Context) {+func (h *Handlers) UpdateSpaceProfile(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.UpdateSpaceProfileRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Spaces.UpdateProfile(c, space, req)respondJSON(c, resp, err)}-func (h *Handlers) UpdateSpaceSlug(c *gin.Context) {+func (h *Handlers) UpdateSpaceSlug(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.UpdateSpaceSlugRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Spaces.UpdateSlug(c, space, req)respondJSON(c, resp, err)}@@ -73,15 +66,11 @@ func (h *Handlers) SpaceSlugAvailability(c *gin.Context) {respondJSON(c, resp, err)}-func (h *Handlers) RotateSpaceKey(c *gin.Context) {+func (h *Handlers) RotateSpaceKey(c *gin.Context, space *spacerepo.SpaceRecord) {var req models.RotateSpaceKeyRequestif !bindJSON(c, &req) {return}- space, ok := selectedSpace(h, c)- if !ok {- return- }resp, err := h.Module.Spaces.RotateKey(c, space, req)respondJSON(c, resp, err)}