gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

emailtest.go (3758B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package admin
     19 
     20 import (
     21 	"fmt"
     22 	"net/http"
     23 	"net/mail"
     24 
     25 	"github.com/gin-gonic/gin"
     26 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     27 	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     29 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     30 )
     31 
     32 // EmailTestPostHandler swagger:operation POST /api/v1/admin/email/test testEmailSend
     33 //
     34 // Send a generic test email to a specified email address.
     35 //
     36 // This can be used to validate an instance's SMTP configuration, and to debug any potential issues.
     37 //
     38 // If an error is returned by the SMTP connection, this handler will return code 422 to indicate that
     39 // the request could not be processed, and the SMTP error will be returned to the caller.
     40 //
     41 //	---
     42 //	tags:
     43 //	- admin
     44 //
     45 //	consumes:
     46 //	- multipart/form-data
     47 //
     48 //	produces:
     49 //	- application/json
     50 //
     51 //	parameters:
     52 //	-
     53 //		name: email
     54 //		in: formData
     55 //		description: The email address that the test email should be sent to.
     56 //		type: string
     57 //
     58 //	security:
     59 //	- OAuth2 Bearer:
     60 //		- admin
     61 //
     62 //	responses:
     63 //		'202':
     64 //			description: Test email was sent.
     65 //		'400':
     66 //			description: bad request
     67 //		'401':
     68 //			description: unauthorized
     69 //		'403':
     70 //			description: forbidden
     71 //		'404':
     72 //			description: not found
     73 //		'406':
     74 //			description: not acceptable
     75 //		'422':
     76 //			description: >-
     77 //				An smtp occurred while the email attempt was in progress.
     78 //				Check the returned json for more information. The smtp error
     79 //				will be included, to help you debug communication with the
     80 //				smtp server.
     81 //		'500':
     82 //			description: internal server error
     83 func (m *Module) EmailTestPOSTHandler(c *gin.Context) {
     84 	authed, err := oauth.Authed(c, true, true, true, true)
     85 	if err != nil {
     86 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     87 		return
     88 	}
     89 
     90 	if !*authed.User.Admin {
     91 		err := fmt.Errorf("user %s not an admin", authed.User.ID)
     92 		apiutil.ErrorHandler(c, gtserror.NewErrorForbidden(err, err.Error()), m.processor.InstanceGetV1)
     93 		return
     94 	}
     95 
     96 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
     97 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
     98 		return
     99 	}
    100 
    101 	form := &apimodel.AdminSendTestEmailRequest{}
    102 	if err := c.ShouldBind(form); err != nil {
    103 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    104 		return
    105 	}
    106 
    107 	email, err := mail.ParseAddress(form.Email)
    108 	if err != nil {
    109 		apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    110 		return
    111 	}
    112 
    113 	errWithCode := m.processor.Admin().EmailTest(c.Request.Context(), authed.Account, email.Address)
    114 	if errWithCode != nil {
    115 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    116 		return
    117 	}
    118 
    119 	c.JSON(http.StatusAccepted, gin.H{"status": "test email sent"})
    120 }