Skip to content

Commit 2c88b61

Browse files
authored
[server] Avoid duplicate first upload email (#7844)
## Description ## Tests
2 parents 6b9e2c2 + 0fdf83f commit 2c88b61

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

server/pkg/controller/file.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,28 @@ func (c *FileController) Create(ctx *gin.Context, userID int64, file ente.File,
210210
}
211211
return file, stacktrace.Propagate(err, "")
212212
}
213-
if usage == fileSize+thumbnailSize {
214-
go c.EmailNotificationCtrl.OnFirstFileUpload(file.OwnerID, userAgent)
213+
if usage == fileSize+thumbnailSize && app == ente.Photos {
214+
go c.maybeSendFirstUploadEmail(file.OwnerID, userAgent)
215215
}
216216
return file, nil
217217
}
218218

219+
func (c *FileController) maybeSendFirstUploadEmail(userID int64, userAgent string) {
220+
ctx := context.Background()
221+
hasTrashItems, err := c.TrashRepository.HasItems(ctx, userID)
222+
if err != nil {
223+
log.WithFields(log.Fields{
224+
"user_id": userID,
225+
}).WithError(err).Error("Failed to determine trash state before sending first upload email")
226+
return
227+
}
228+
if !hasTrashItems {
229+
c.EmailNotificationCtrl.OnFirstFileUpload(userID, userAgent)
230+
return
231+
}
232+
log.WithField("user_id", userID).Debug("Skipping first upload email because trash is not empty")
233+
}
234+
219235
// Update verifies permissions and updates the specified file
220236
func (c *FileController) Update(ctx context.Context, userID int64, file ente.File, app ente.App) (ente.UpdateFileResponse, error) {
221237
var response ente.UpdateFileResponse

server/pkg/repo/trash.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ func (t *TrashRepository) GetTimeStampForLatestNonDeletedEntry(userID int64) (*i
344344
return updatedAt, stacktrace.Propagate(err, "")
345345
}
346346

347+
// HasItems returns true if the user has any entries in trash.
348+
func (t *TrashRepository) HasItems(ctx context.Context, userID int64) (bool, error) {
349+
row := t.DB.QueryRowContext(ctx, `
350+
SELECT exists(
351+
SELECT 1 FROM trash
352+
WHERE user_id = $1
353+
)`, userID)
354+
var hasItems bool
355+
err := row.Scan(&hasItems)
356+
if errors.Is(err, sql.ErrNoRows) {
357+
return false, nil
358+
}
359+
return hasItems, stacktrace.Propagate(err, "")
360+
}
361+
347362
// GetUserIDToFileIDsMapForDeletion returns map of userID to fileIds, where the file ids which should be deleted by now
348363
func (t *TrashRepository) GetUserIDToFileIDsMapForDeletion() (map[int64][]int64, error) {
349364
rows, err := t.DB.Query(`SELECT user_id, file_id FROM trash

0 commit comments

Comments
 (0)