this repo has no description
1package server 2 3import ( 4 "fmt" 5 "time" 6 7 "github.com/haileyok/cocoon/internal/helpers" 8 "github.com/haileyok/cocoon/models" 9 "github.com/labstack/echo/v4" 10) 11 12func (s *Server) handleServerRequestAccountDelete(e echo.Context) error { 13 ctx := e.Request().Context() 14 15 urepo := e.Get("repo").(*models.RepoActor) 16 17 token := fmt.Sprintf("%s-%s", helpers.RandomVarchar(5), helpers.RandomVarchar(5)) 18 expiresAt := time.Now().UTC().Add(15 * time.Minute) 19 20 if err := s.db.Exec(ctx, "UPDATE repos SET account_delete_code = ?, account_delete_code_expires_at = ? WHERE did = ?", nil, token, expiresAt, urepo.Repo.Did).Error; err != nil { 21 s.logger.Error("error setting deletion token", "error", err) 22 return helpers.ServerError(e, nil) 23 } 24 25 if urepo.Email != "" { 26 if err := s.sendAccountDeleteEmail(urepo.Email, urepo.Actor.Handle, token); err != nil { 27 s.logger.Error("error sending account deletion email", "error", err) 28 } 29 } 30 31 return e.NoContent(200) 32} 33 34func (s *Server) sendAccountDeleteEmail(email, handle, token string) error { 35 if s.mail == nil { 36 return nil 37 } 38 39 s.mailLk.Lock() 40 defer s.mailLk.Unlock() 41 42 s.mail.To(email) 43 s.mail.Subject("Account Deletion Request for " + s.config.Hostname) 44 s.mail.Plain().Set(fmt.Sprintf("Hello %s. Your account deletion code is %s. This code will expire in fifteen minutes. If you did not request this, please ignore this email.", handle, token)) 45 46 if err := s.mail.Send(); err != nil { 47 return err 48 } 49 50 return nil 51}