Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] Add endpoint to configure emergency contact recovery days

Neeraj Gupta954d72de26b5
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 80f835cc98..69fce1af2c 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -691,6 +691,7 @@ func main() {
privateAPI.POST("/emergency-contacts/add", emergencyHandler.AddContact)
privateAPI.GET("/emergency-contacts/info", emergencyHandler.GetInfo)
privateAPI.POST("/emergency-contacts/update", emergencyHandler.UpdateContact)
+ privateAPI.POST("/emergency-contacts/update-recovery-notice", emergencyHandler.UpdateRecoveryNotice)
privateAPI.POST("/emergency-contacts/start-recovery", emergencyHandler.StartRecovery)
privateAPI.POST("/emergency-contacts/stop-recovery", emergencyHandler.StopRecovery)
privateAPI.POST("/emergency-contacts/reject-recovery", emergencyHandler.RejectRecovery)
diff --git a/server/ente/emergency.go b/server/ente/emergency.go
index 0dcab7ab17..4a2cd2dff9 100644
--- a/server/ente/emergency.go
+++ b/server/ente/emergency.go
@@ -16,6 +16,11 @@ type UpdateContact struct {
State ContactState `json:"state" binding:"required"`
}
+type UpdateRecoveryNotice struct {
+ EmergencyContactID int64 `json:"emergencyContactID" binding:"required"`
+ RecoveryNoticeInDays int `json:"recoveryNoticeInDays" binding:"required"`
+}
+
type ContactIdentifier struct {
UserID int64 `json:"userID" binding:"required"`
EmergencyContactID int64 `json:"emergencyContactID" binding:"required"`
diff --git a/server/pkg/api/emergency.go b/server/pkg/api/emergency.go
index 7fef6a27ca..36b40fd7ea 100644
--- a/server/pkg/api/emergency.go
+++ b/server/pkg/api/emergency.go
@@ -54,6 +54,20 @@ func (h *EmergencyHandler) UpdateContact(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
+func (h *EmergencyHandler) UpdateRecoveryNotice(c *gin.Context) {
+ var request ente.UpdateRecoveryNotice
+ if err := c.ShouldBindJSON(&request); err != nil {
+ handler.Error(c, stacktrace.Propagate(ente.NewBadRequestWithMessage("failed to validate req param"), err.Error()))
+ return
+ }
+ err := h.Controller.UpdateRecoveryNotice(c, auth.GetUserID(c.Request.Header), request)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{})
+}
+
func (h *EmergencyHandler) StartRecovery(c *gin.Context) {
var request ente.ContactIdentifier
if err := c.ShouldBindJSON(&request); err != nil {
diff --git a/server/pkg/controller/emergency/account_owner.go b/server/pkg/controller/emergency/account_owner.go
index 27a747d1c5..162d8365d6 100644
--- a/server/pkg/controller/emergency/account_owner.go
+++ b/server/pkg/controller/emergency/account_owner.go
@@ -127,3 +127,28 @@ func (c *Controller) GetInfo(ctx *gin.Context, userID int64) (*ente.EmergencyDat
}
return response, nil
}
+
+func (c *Controller) UpdateRecoveryNotice(ctx *gin.Context, userID int64, request ente.UpdateRecoveryNotice) error {
+ // Validate recovery notice is between 1 and 60 days
+ if request.RecoveryNoticeInDays < 1 || request.RecoveryNoticeInDays > 60 {
+ return stacktrace.Propagate(ente.NewBadRequestWithMessage("recovery notice must be between 1 and 60 days"), "")
+ }
+
+ // Check if there's an active recovery session
+ activeSessions, err := c.Repo.GetActiveSessions(ctx, userID, request.EmergencyContactID)
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to check active recovery sessions")
+ }
+ if len(activeSessions) > 0 {
+ return stacktrace.Propagate(ente.NewBadRequestWithMessage("cannot update recovery notice while there is an active recovery session"), "")
+ }
+
+ // Update the recovery notice period
+ noticePeriodInHrs := request.RecoveryNoticeInDays * 24
+ err = c.Repo.UpdateRecoveryNotice(ctx, userID, request.EmergencyContactID, noticePeriodInHrs)
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to update recovery notice")
+ }
+
+ return nil
+}
diff --git a/server/pkg/repo/emergency/repository.go b/server/pkg/repo/emergency/repository.go
index 5bf4eba9c3..fd3d86af1a 100644
--- a/server/pkg/repo/emergency/repository.go
+++ b/server/pkg/repo/emergency/repository.go
@@ -106,6 +106,27 @@ func (r *Repository) UpdateState(ctx context.Context,
return count > 0, stacktrace.Propagate(err2, "")
}
+// UpdateRecoveryNotice updates the notice period for an emergency contact
+// Only allows update if the contact state is INVITED or ACCEPTED
+func (r *Repository) UpdateRecoveryNotice(ctx context.Context,
+ userID int64,
+ emergencyContactID int64,
+ noticePeriodInHrs int) error {
+ res, err := r.DB.ExecContext(ctx, `UPDATE emergency_contact SET notice_period_in_hrs=$1 WHERE user_id=$2 and emergency_contact_id=$3 and state = ANY($4)`,
+ noticePeriodInHrs, userID, emergencyContactID, pq.Array([]ente.ContactState{ente.UserInvitedContact, ente.ContactAccepted}))
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to update notice period")
+ }
+ count, err := res.RowsAffected()
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to get rows affected")
+ }
+ if count == 0 {
+ return ente.NewBadRequestWithMessage("emergency contact not found or not in valid state")
+ }
+ return nil
+}
+
func getValidPreviousState(cs ente.ContactState) []ente.ContactState {
switch cs {
case ente.UserInvitedContact: