code.gitea.io/gitea@v1.21.7/routers/api/v1/user/email.go (about)

     1  // Copyright 2015 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package user
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	user_model "code.gitea.io/gitea/models/user"
    11  	"code.gitea.io/gitea/modules/context"
    12  	"code.gitea.io/gitea/modules/setting"
    13  	api "code.gitea.io/gitea/modules/structs"
    14  	"code.gitea.io/gitea/modules/web"
    15  	"code.gitea.io/gitea/services/convert"
    16  )
    17  
    18  // ListEmails list all of the authenticated user's email addresses
    19  // see https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
    20  func ListEmails(ctx *context.APIContext) {
    21  	// swagger:operation GET /user/emails user userListEmails
    22  	// ---
    23  	// summary: List the authenticated user's email addresses
    24  	// produces:
    25  	// - application/json
    26  	// responses:
    27  	//   "200":
    28  	//     "$ref": "#/responses/EmailList"
    29  
    30  	emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID)
    31  	if err != nil {
    32  		ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
    33  		return
    34  	}
    35  	apiEmails := make([]*api.Email, len(emails))
    36  	for i := range emails {
    37  		apiEmails[i] = convert.ToEmail(emails[i])
    38  	}
    39  	ctx.JSON(http.StatusOK, &apiEmails)
    40  }
    41  
    42  // AddEmail add an email address
    43  func AddEmail(ctx *context.APIContext) {
    44  	// swagger:operation POST /user/emails user userAddEmail
    45  	// ---
    46  	// summary: Add email addresses
    47  	// produces:
    48  	// - application/json
    49  	// parameters:
    50  	// - name: body
    51  	//   in: body
    52  	//   schema:
    53  	//     "$ref": "#/definitions/CreateEmailOption"
    54  	// responses:
    55  	//   '201':
    56  	//     "$ref": "#/responses/EmailList"
    57  	//   "422":
    58  	//     "$ref": "#/responses/validationError"
    59  	form := web.GetForm(ctx).(*api.CreateEmailOption)
    60  	if len(form.Emails) == 0 {
    61  		ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
    62  		return
    63  	}
    64  
    65  	emails := make([]*user_model.EmailAddress, len(form.Emails))
    66  	for i := range form.Emails {
    67  		emails[i] = &user_model.EmailAddress{
    68  			UID:         ctx.Doer.ID,
    69  			Email:       form.Emails[i],
    70  			IsActivated: !setting.Service.RegisterEmailConfirm,
    71  		}
    72  	}
    73  
    74  	if err := user_model.AddEmailAddresses(ctx, emails); err != nil {
    75  		if user_model.IsErrEmailAlreadyUsed(err) {
    76  			ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
    77  		} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
    78  			email := ""
    79  			if typedError, ok := err.(user_model.ErrEmailInvalid); ok {
    80  				email = typedError.Email
    81  			}
    82  			if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok {
    83  				email = typedError.Email
    84  			}
    85  
    86  			errMsg := fmt.Sprintf("Email address %q invalid", email)
    87  			ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
    88  		} else {
    89  			ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
    90  		}
    91  		return
    92  	}
    93  
    94  	apiEmails := make([]*api.Email, len(emails))
    95  	for i := range emails {
    96  		apiEmails[i] = convert.ToEmail(emails[i])
    97  	}
    98  	ctx.JSON(http.StatusCreated, &apiEmails)
    99  }
   100  
   101  // DeleteEmail delete email
   102  func DeleteEmail(ctx *context.APIContext) {
   103  	// swagger:operation DELETE /user/emails user userDeleteEmail
   104  	// ---
   105  	// summary: Delete email addresses
   106  	// produces:
   107  	// - application/json
   108  	// parameters:
   109  	// - name: body
   110  	//   in: body
   111  	//   schema:
   112  	//     "$ref": "#/definitions/DeleteEmailOption"
   113  	// responses:
   114  	//   "204":
   115  	//     "$ref": "#/responses/empty"
   116  	//   "404":
   117  	//     "$ref": "#/responses/notFound"
   118  	form := web.GetForm(ctx).(*api.DeleteEmailOption)
   119  	if len(form.Emails) == 0 {
   120  		ctx.Status(http.StatusNoContent)
   121  		return
   122  	}
   123  
   124  	emails := make([]*user_model.EmailAddress, len(form.Emails))
   125  	for i := range form.Emails {
   126  		emails[i] = &user_model.EmailAddress{
   127  			Email: form.Emails[i],
   128  			UID:   ctx.Doer.ID,
   129  		}
   130  	}
   131  
   132  	if err := user_model.DeleteEmailAddresses(ctx, emails); err != nil {
   133  		if user_model.IsErrEmailAddressNotExist(err) {
   134  			ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
   135  			return
   136  		}
   137  		ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
   138  		return
   139  	}
   140  	ctx.Status(http.StatusNoContent)
   141  }