Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix cast device deletion by id

Priyanshu Dangare9e9c8a5d6fe2
diff --git a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
index 2390db4e23..dc6797b900 100644
--- a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
+++ b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
@@ -3,13 +3,13 @@ import "package:dio/dio.dart";
class CastInfo {
final int collectionID;
final String deviceIP;
- final String deviceCode;
+ final String deviceID;
final DateTime lastUsedAt;
CastInfo({
required this.collectionID,
required this.deviceIP,
- required this.deviceCode,
+ required this.deviceID,
required this.lastUsedAt,
});
@@ -17,7 +17,7 @@ class CastInfo {
return CastInfo(
collectionID: json["collectionID"],
deviceIP: json["deviceIP"],
- deviceCode: json["deviceCode"],
+ deviceID: json["deviceID"],
lastUsedAt: DateTime.fromMicrosecondsSinceEpoch(json["lastUsedAt"]),
);
}
@@ -78,9 +78,7 @@ class CastGateway {
}
Future<void> revokeSession(CastInfo session) async {
- await _enteDio.delete(
- "/cast/device-info/${session.deviceCode}?collectionID=${session.collectionID}",
- );
+ await _enteDio.delete("/cast/device-info/${session.deviceID}");
}
}
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index f4e48a2c10..f8e2546efd 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -819,7 +819,7 @@ func main() {
publicAPI.POST("/cast/device-info", castHandler.RegisterDevice)
privateAPI.GET("/cast/device-info", castHandler.GetAllDevices)
- privateAPI.DELETE("/cast/device-info/:deviceCode", castHandler.DeleteDevice)
+ privateAPI.DELETE("/cast/device-info/:deviceID", castHandler.DeleteDevice)
// Deprecated Nov 2024. Remove in a few months.
//
// This (and below) are deprecated copy of endpoints with a trailing slash.
diff --git a/server/ente/cast/entity.go b/server/ente/cast/entity.go
index c76c3dfcca..62befaf82a 100644
--- a/server/ente/cast/entity.go
+++ b/server/ente/cast/entity.go
@@ -1,5 +1,7 @@
package cast
+import "github.com/google/uuid"
+
// CastRequest ..
type CastRequest struct {
CollectionID int64 `json:"collectionID" binding:"required"`
@@ -18,8 +20,8 @@ type AuthContext struct {
}
type CastInfo struct {
- DeviceCode string `json:"deviceCode"`
- CollectionID int64 `json:"collectionID"`
- DeviceIP string `json:"deviceIP"`
- LastUsedAt int64 `json:"lastUsedAt"`
+ DeviceID uuid.UUID `json:"deviceID"`
+ CollectionID int64 `json:"collectionID"`
+ DeviceIP string `json:"deviceIP"`
+ LastUsedAt int64 `json:"lastUsedAt"`
}
diff --git a/server/pkg/api/cast.go b/server/pkg/api/cast.go
index ca078434cd..208215a02d 100644
--- a/server/pkg/api/cast.go
+++ b/server/pkg/api/cast.go
@@ -14,6 +14,7 @@ import (
"github.com/ente-io/museum/pkg/utils/handler"
"github.com/ente-io/stacktrace"
"github.com/gin-gonic/gin"
+ "github.com/google/uuid"
)
// CastHandler exposes request handlers for publicly accessible collections
@@ -64,18 +65,14 @@ func (h *CastHandler) GetAllDevices(c *gin.Context) {
}
func (h *CastHandler) DeleteDevice(c *gin.Context) {
- userID := auth.GetUserID(c.Request.Header)
- deviceCode := getDeviceCode(c)
- collectionID, err := strconv.ParseInt(c.Query("collectionID"), 10, 64)
+ deviceID, err := uuid.Parse(c.Param("deviceID"))
if err != nil {
- handler.Error(c, stacktrace.Propagate(err, "failed to parse collection ID"))
+ handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, "failed to parse device id"))
return
}
if err := h.Ctrl.DeleteDevice(
c,
- userID,
- collectionID,
- deviceCode,
+ deviceID,
); err != nil {
handler.Error(c, stacktrace.Propagate(err, "failed to delete device"))
return
diff --git a/server/pkg/controller/cast/controller.go b/server/pkg/controller/cast/controller.go
index e01b435350..92e4422914 100644
--- a/server/pkg/controller/cast/controller.go
+++ b/server/pkg/controller/cast/controller.go
@@ -10,6 +10,7 @@ import (
"github.com/ente-io/museum/pkg/utils/network"
"github.com/ente-io/stacktrace"
"github.com/gin-gonic/gin"
+ "github.com/google/uuid"
"github.com/sirupsen/logrus"
)
@@ -35,8 +36,8 @@ func (c *Controller) GetAllDevices(ctx *gin.Context, userID int64) ([]cast.CastI
return c.CastRepo.GetAllDevices(ctx, userID)
}
-func (c *Controller) DeleteDevice(ctx *gin.Context, userID int64, collectionID int64, deviceCode string) error {
- return c.CastRepo.RevokeForGivenUserAndCollectionAndDevice(ctx, userID, collectionID, deviceCode)
+func (c *Controller) DeleteDevice(ctx *gin.Context, deviceID uuid.UUID) error {
+ return c.CastRepo.RevokeForGivenDeviceID(ctx, deviceID)
}
func (c *Controller) GetPublicKey(ctx *gin.Context, deviceCode string) (string, error) {
diff --git a/server/pkg/repo/cast/repo.go b/server/pkg/repo/cast/repo.go
index dd17a59a42..4776130b1d 100644
--- a/server/pkg/repo/cast/repo.go
+++ b/server/pkg/repo/cast/repo.go
@@ -31,7 +31,7 @@ func (r *Repository) AddCode(ctx context.Context, pubKey string, ip string) (str
}
func (r *Repository) GetAllDevices(ctx context.Context, userID int64) ([]cast.CastInfo, error) {
- rows, err := r.DB.QueryContext(ctx, "SELECT code, collection_id, ip, last_used_at FROM casting WHERE cast_user = $1 and is_deleted=false ORDER BY created_at DESC", userID)
+ rows, err := r.DB.QueryContext(ctx, "SELECT id, collection_id, ip, last_used_at FROM casting WHERE cast_user = $1 and is_deleted=false ORDER BY created_at DESC", userID)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to query devices")
}
@@ -39,7 +39,7 @@ func (r *Repository) GetAllDevices(ctx context.Context, userID int64) ([]cast.Ca
devices := make([]cast.CastInfo, 0)
for rows.Next() {
var device cast.CastInfo
- if err := rows.Scan(&device.DeviceCode, &device.CollectionID, &device.DeviceIP, &device.LastUsedAt); err != nil {
+ if err := rows.Scan(&device.DeviceID, &device.CollectionID, &device.DeviceIP, &device.LastUsedAt); err != nil {
return nil, stacktrace.Propagate(err, "failed to scan device row")
}
devices = append(devices, device)
@@ -152,8 +152,8 @@ func (r *Repository) RevokeForGivenUserAndCollection(ctx context.Context, collec
return stacktrace.Propagate(err, "")
}
-func (r *Repository) RevokeForGivenUserAndCollectionAndDevice(ctx context.Context, userID int64, collectionID int64, deviceCode string) error {
- _, err := r.DB.ExecContext(ctx, "UPDATE casting SET is_deleted=true where collection_id=$1 and cast_user=$2 and code=$3", collectionID, userID, deviceCode)
+func (r *Repository) RevokeForGivenDeviceID(ctx context.Context, deviceID uuid.UUID) error {
+ _, err := r.DB.ExecContext(ctx, "UPDATE casting SET is_deleted=true where id=$1", deviceID)
if err != nil {
return stacktrace.Propagate(err, "failed to revoke token for given user, collection and device")
}