Handle no avatar, add delete avatar support
This commit is contained in:
1 parent
35bff424f1
commit
3864a4c1cc
9 files changed
+110
-11
No files matched your search
@@ -179,6 +179,28 @@ func (r *Repository) SetAvatar(ctx context.Context, id int64, data []byte) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteAvatar(ctx context.Context, id int64) error {
|
||||
query := "DELETE FROM avatars WHERE id = ?"
|
||||
_, err := r.q.ExecContext(ctx, query, id)
|
||||
if err != nil {
|
||||
slog.Error("delete avatar", slog.String("op", "repo.DeleteAvatar"), slog.Int64("user_id", id), slog.Any("error", err))
|
||||
return ErrInternal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasAvatar reports whether the user currently has an avatar stored.
|
||||
func (r *Repository) HasAvatar(ctx context.Context, id int64) (bool, error) {
|
||||
query := "SELECT EXISTS(SELECT 1 FROM avatars WHERE id = ?)"
|
||||
var exists bool
|
||||
err := r.q.QueryRowContext(ctx, query, id).Scan(&exists)
|
||||
if err != nil {
|
||||
slog.Error("has avatar", slog.String("op", "repo.HasAvatar"), slog.Int64("user_id", id), slog.Any("error", err))
|
||||
return false, ErrInternal
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (r *Repository) CleanupUsers(ctx context.Context) error {
|
||||
query := "DELETE FROM users WHERE activated_at IS NULL and created_at < ?"
|
||||
_, err := r.q.ExecContext(ctx, query, time.Now().UTC().Add(-5*time.Minute))
|
||||
|
||||
@@ -163,6 +163,17 @@ func (s *Service) Avatar(ctx context.Context, user *User) ([]byte, error) {
|
||||
return s.r.Avatar(ctx, user.Id)
|
||||
}
|
||||
|
||||
// DeleteAvatar removes a user's avatar. It is a no-op when no avatar is stored,
|
||||
// so the operation is idempotent.
|
||||
func (s *Service) DeleteAvatar(ctx context.Context, user *User) error {
|
||||
return s.r.DeleteAvatar(ctx, user.Id)
|
||||
}
|
||||
|
||||
// HasAvatar reports whether the user currently has an avatar stored.
|
||||
func (s *Service) HasAvatar(ctx context.Context, user *User) (bool, error) {
|
||||
return s.r.HasAvatar(ctx, user.Id)
|
||||
}
|
||||
|
||||
func (s *Service) ActivateUser(ctx context.Context, user *User) (*User, error) {
|
||||
tx, err := s.r.Db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user