diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go index 958054cab..4cbc00612 100644 --- a/server/cmd/museum/main.go +++ b/server/cmd/museum/main.go @@ -37,7 +37,6 @@ import ( embeddingCtrl "github.com/ente-io/museum/pkg/controller/embedding" "github.com/ente-io/museum/pkg/controller/family" kexCtrl "github.com/ente-io/museum/pkg/controller/kex" - "github.com/ente-io/museum/pkg/controller/locationtag" "github.com/ente-io/museum/pkg/controller/lock" remoteStoreCtrl "github.com/ente-io/museum/pkg/controller/remotestore" "github.com/ente-io/museum/pkg/controller/storagebonus" @@ -50,7 +49,6 @@ import ( "github.com/ente-io/museum/pkg/repo/datacleanup" "github.com/ente-io/museum/pkg/repo/embedding" "github.com/ente-io/museum/pkg/repo/kex" - locationtagRepo "github.com/ente-io/museum/pkg/repo/locationtag" "github.com/ente-io/museum/pkg/repo/passkey" "github.com/ente-io/museum/pkg/repo/remotestore" storageBonusRepo "github.com/ente-io/museum/pkg/repo/storagebonus" @@ -150,7 +148,6 @@ func main() { twoFactorRecoveryRepo := &two_factor_recovery.Repository{Db: db, SecretEncryptionKey: secretEncryptionKeyBytes} billingRepo := &repo.BillingRepository{DB: db} userEntityRepo := &userEntityRepo.Repository{DB: db} - locationTagRepository := &locationtagRepo.Repository{DB: db} authRepo := &authenticatorRepo.Repository{DB: db} remoteStoreRepository := &remotestore.Repository{DB: db} dataCleanupRepository := &datacleanup.Repository{DB: db} @@ -641,13 +638,6 @@ func main() { privateAPI.DELETE("/user-entity/entity", userEntityHandler.DeleteEntity) privateAPI.GET("/user-entity/entity/diff", userEntityHandler.GetDiff) - locationTagController := &locationtag.Controller{Repo: locationTagRepository} - locationTagHandler := &api.LocationTagHandler{Controller: locationTagController} - privateAPI.POST("/locationtag/create", locationTagHandler.Create) - privateAPI.POST("/locationtag/update", locationTagHandler.Update) - privateAPI.DELETE("/locationtag/delete", locationTagHandler.Delete) - privateAPI.GET("/locationtag/diff", locationTagHandler.GetDiff) - authenticatorController := &authenticatorCtrl.Controller{Repo: authRepo} authenticatorHandler := &api.AuthenticatorHandler{Controller: authenticatorController} diff --git a/server/ente/locationtag.go b/server/ente/locationtag.go deleted file mode 100644 index 61c191006..000000000 --- a/server/ente/locationtag.go +++ /dev/null @@ -1,59 +0,0 @@ -package ente - -import ( - "database/sql/driver" - "encoding/json" - "github.com/ente-io/stacktrace" - "github.com/google/uuid" -) - -// LocationTag represents a location tag in the system. The location information -// is stored in an encrypted as Attributes -type LocationTag struct { - ID uuid.UUID `json:"id"` - OwnerID int64 `json:"ownerId,omitempty"` - EncryptedKey string `json:"encryptedKey" binding:"required"` - KeyDecryptionNonce string `json:"keyDecryptionNonce" binding:"required"` - Attributes LocationTagAttribute `json:"attributes" binding:"required"` - IsDeleted bool `json:"isDeleted"` - Provider string `json:"provider,omitempty"` - CreatedAt int64 `json:"createdAt,omitempty"` // utc epoch microseconds - UpdatedAt int64 `json:"updatedAt,omitempty"` // utc epoch microseconds -} - -// LocationTagAttribute holds encrypted data about user's location tag. -type LocationTagAttribute struct { - Version int `json:"version,omitempty" binding:"required"` - EncryptedData string `json:"encryptedData,omitempty" binding:"required"` - DecryptionNonce string `json:"decryptionNonce,omitempty" binding:"required"` -} - -// Value implements the driver.Valuer interface. This method -// simply returns the JSON-encoded representation of the struct. -func (la LocationTagAttribute) Value() (driver.Value, error) { - return json.Marshal(la) -} - -// Scan implements the sql.Scanner interface. This method -// simply decodes a JSON-encoded value into the struct fields. -func (la *LocationTagAttribute) Scan(value interface{}) error { - b, ok := value.([]byte) - if !ok { - return stacktrace.NewError("type assertion to []byte failed") - } - return json.Unmarshal(b, &la) -} - -// DeleteLocationTagRequest is request structure for deleting a location tag -type DeleteLocationTagRequest struct { - ID uuid.UUID `json:"id" binding:"required"` - OwnerID int64 // should be populated from req headers -} - -// GetLocationTagDiffRequest is request struct for fetching locationTag changes -type GetLocationTagDiffRequest struct { - // SinceTime *int64. Pointer allows us to pass 0 value otherwise binding fails for zero Value. - SinceTime *int64 `form:"sinceTime" binding:"required"` - Limit int16 `form:"limit" binding:"required"` - OwnerID int64 // should be populated from req headers -} diff --git a/server/migrations/82_drop_location_tag_table.down.sql b/server/migrations/82_drop_location_tag_table.down.sql new file mode 100644 index 000000000..9f7060a47 --- /dev/null +++ b/server/migrations/82_drop_location_tag_table.down.sql @@ -0,0 +1 @@ +-- no-op diff --git a/server/migrations/82_drop_location_tag_table.up.sql b/server/migrations/82_drop_location_tag_table.up.sql new file mode 100644 index 000000000..096ded0d7 --- /dev/null +++ b/server/migrations/82_drop_location_tag_table.up.sql @@ -0,0 +1,2 @@ +DROP TRIGGER IF EXISTS update_location_tag_updated_at ON location_tag; +DROP TABLE location_tag; diff --git a/server/pkg/api/locationtag.go b/server/pkg/api/locationtag.go deleted file mode 100644 index ad61b4778..000000000 --- a/server/pkg/api/locationtag.go +++ /dev/null @@ -1,88 +0,0 @@ -package api - -import ( - "fmt" - "net/http" - - "github.com/ente-io/museum/ente" - "github.com/ente-io/museum/pkg/controller/locationtag" - "github.com/ente-io/museum/pkg/utils/auth" - "github.com/ente-io/museum/pkg/utils/handler" - "github.com/ente-io/stacktrace" - "github.com/gin-gonic/gin" -) - -// LocationTagHandler expose request handlers to all location tag requests -type LocationTagHandler struct { - Controller *locationtag.Controller -} - -// Create handler for creating a new location tag -func (h *LocationTagHandler) Create(c *gin.Context) { - var request ente.LocationTag - if err := c.ShouldBindJSON(&request); err != nil { - handler.Error(c, - stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("Request binding failed %s", err))) - return - } - request.OwnerID = auth.GetUserID(c.Request.Header) - resp, err := h.Controller.Create(c, request) - if err != nil { - handler.Error(c, stacktrace.Propagate(err, "Failed to create locationTag")) - return - } - c.JSON(http.StatusOK, resp) -} - -// Update handler for updating location tag -func (h *LocationTagHandler) Update(c *gin.Context) { - var request ente.LocationTag - if err := c.ShouldBindJSON(&request); err != nil { - handler.Error(c, - stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("Request binding failed %s", err))) - return - } - request.OwnerID = auth.GetUserID(c.Request.Header) - resp, err := h.Controller.Update(c, request) - if err != nil { - handler.Error(c, stacktrace.Propagate(err, "Failed to update locationTag")) - return - } - c.JSON(http.StatusOK, gin.H{"locationTag": resp}) -} - -// Delete handler for deleting location tag -func (h *LocationTagHandler) Delete(c *gin.Context) { - var request ente.DeleteLocationTagRequest - if err := c.ShouldBindJSON(&request); err != nil { - handler.Error(c, - stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("Request binding failed %s", err))) - return - } - request.OwnerID = auth.GetUserID(c.Request.Header) - _, err := h.Controller.Delete(c, request) - if err != nil { - handler.Error(c, stacktrace.Propagate(err, "Failed to delete locationTag")) - return - } - c.Status(http.StatusOK) -} - -// GetDiff handler for fetching diff of location tag changes -func (h *LocationTagHandler) GetDiff(c *gin.Context) { - var request ente.GetLocationTagDiffRequest - if err := c.ShouldBindQuery(&request); err != nil { - handler.Error(c, - stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("Request binding failed %s", err))) - return - } - request.OwnerID = auth.GetUserID(c.Request.Header) - locationTags, err := h.Controller.GetDiff(c, request) - if err != nil { - handler.Error(c, stacktrace.Propagate(err, "Failed to fetch locationTag diff")) - return - } - c.JSON(http.StatusOK, gin.H{ - "diff": locationTags, - }) -} diff --git a/server/pkg/controller/locationtag/controller.go b/server/pkg/controller/locationtag/controller.go deleted file mode 100644 index 9b9618117..000000000 --- a/server/pkg/controller/locationtag/controller.go +++ /dev/null @@ -1,31 +0,0 @@ -package locationtag - -import ( - "github.com/ente-io/museum/ente" - "github.com/ente-io/museum/pkg/repo/locationtag" - "github.com/gin-gonic/gin" -) - -// Controller is interface for exposing business logic related to location tags -type Controller struct { - Repo *locationtag.Repository -} - -// Create a new location tag in the system -func (c *Controller) Create(ctx *gin.Context, req ente.LocationTag) (ente.LocationTag, error) { - return c.Repo.Create(ctx, req) -} -func (c *Controller) Update(ctx *gin.Context, req ente.LocationTag) (ente.LocationTag, error) { - // todo: verify ownership before updating - panic("implement me") -} - -// Delete the location tag for the given id and ownerId -func (c *Controller) Delete(ctx *gin.Context, req ente.DeleteLocationTagRequest) (bool, error) { - return c.Repo.Delete(ctx, req.ID.String(), req.OwnerID) -} - -// GetDiff fetches the locationTags which have changed after the specified time -func (c *Controller) GetDiff(ctx *gin.Context, req ente.GetLocationTagDiffRequest) ([]ente.LocationTag, error) { - return c.Repo.GetDiff(ctx, req.OwnerID, *req.SinceTime, req.Limit) -} diff --git a/server/pkg/repo/locationtag/repository.go b/server/pkg/repo/locationtag/repository.go deleted file mode 100644 index 0373ff6cd..000000000 --- a/server/pkg/repo/locationtag/repository.go +++ /dev/null @@ -1,89 +0,0 @@ -package locationtag - -import ( - "context" - "database/sql" - "fmt" - "github.com/ente-io/museum/ente" - "github.com/ente-io/stacktrace" - "github.com/google/uuid" - "github.com/sirupsen/logrus" -) - -// Repository defines the methods for inserting, updating and retrieving -// locationTag related entities from the underlying repository -type Repository struct { - DB *sql.DB -} - -// Create inserts a new &{ente.LocationTag} entry -func (r *Repository) Create(ctx context.Context, locationTag ente.LocationTag) (ente.LocationTag, error) { - err := r.DB.QueryRow(`INSERT into location_tag( - id, - user_id, - encrypted_key, - key_decryption_nonce, - attributes) VALUES ($1,$2,$3,$4,$5) RETURNING id,created_at,updated_at`, - uuid.New(), //$1 id - locationTag.OwnerID, // $2 user_id - locationTag.EncryptedKey, // $3 encrypted_key - locationTag.KeyDecryptionNonce, // $4 key_decryption_nonce - locationTag.Attributes). // %5 attributes - Scan(&locationTag.ID, &locationTag.CreatedAt, &locationTag.UpdatedAt) - if err != nil { - return ente.LocationTag{}, stacktrace.Propagate(err, "Failed to create locationTag") - } - return locationTag, nil -} - -// GetDiff returns the &{[]ente.LocationTag} which have been added or -// modified after the given sinceTime -func (r *Repository) GetDiff(ctx context.Context, ownerID int64, sinceTime int64, limit int16) ([]ente.LocationTag, error) { - rows, err := r.DB.Query(`SELECT - id, user_id, provider, encrypted_key, key_decryption_nonce, - attributes, is_deleted, created_at, updated_at - FROM location_tag - WHERE user_id = $1 - and updated_at > $2 - ORDER BY updated_at - LIMIT $3`, - ownerID, // $1 - sinceTime, // %2 - limit, // $3 - ) - if err != nil { - return nil, stacktrace.Propagate(err, "GetDiff query failed") - } - return convertRowsToLocationTags(rows) -} - -func (r *Repository) Delete(ctx context.Context, id string, ownerID int64) (bool, error) { - _, err := r.DB.ExecContext(ctx, - `UPDATE location_tag SET is_deleted=$1, attributes=$2 where id=$3 and user_id = $4`, - true, `{}`, // $1 is_deleted, $2 attr - id, ownerID) // $3 tagId, $4 ownerID - if err != nil { - return false, stacktrace.Propagate(err, fmt.Sprintf("faield to delele tag with id=%s", id)) - } - return true, nil -} - -func convertRowsToLocationTags(rows *sql.Rows) ([]ente.LocationTag, error) { - defer func() { - if err := rows.Close(); err != nil { - logrus.Error(err) - } - }() - locationTags := make([]ente.LocationTag, 0) - for rows.Next() { - tag := ente.LocationTag{} - err := rows.Scan( - &tag.ID, &tag.OwnerID, &tag.Provider, &tag.EncryptedKey, &tag.KeyDecryptionNonce, - &tag.Attributes, &tag.IsDeleted, &tag.CreatedAt, &tag.UpdatedAt) - if err != nil { - return nil, stacktrace.Propagate(err, "Failed to convert rowToLocationTag") - } - locationTags = append(locationTags, tag) - } - return locationTags, nil -}