code.gitea.io/gitea@v1.22.3/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 api "code.gitea.io/gitea/modules/structs" 12 "code.gitea.io/gitea/modules/web" 13 "code.gitea.io/gitea/services/context" 14 "code.gitea.io/gitea/services/convert" 15 user_service "code.gitea.io/gitea/services/user" 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 60 form := web.GetForm(ctx).(*api.CreateEmailOption) 61 if len(form.Emails) == 0 { 62 ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty") 63 return 64 } 65 66 if err := user_service.AddEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil { 67 if user_model.IsErrEmailAlreadyUsed(err) { 68 ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email) 69 } else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) { 70 email := "" 71 if typedError, ok := err.(user_model.ErrEmailInvalid); ok { 72 email = typedError.Email 73 } 74 if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok { 75 email = typedError.Email 76 } 77 78 errMsg := fmt.Sprintf("Email address %q invalid", email) 79 ctx.Error(http.StatusUnprocessableEntity, "", errMsg) 80 } else { 81 ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err) 82 } 83 return 84 } 85 86 emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID) 87 if err != nil { 88 ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err) 89 return 90 } 91 92 apiEmails := make([]*api.Email, 0, len(emails)) 93 for _, email := range emails { 94 apiEmails = append(apiEmails, convert.ToEmail(email)) 95 } 96 ctx.JSON(http.StatusCreated, apiEmails) 97 } 98 99 // DeleteEmail delete email 100 func DeleteEmail(ctx *context.APIContext) { 101 // swagger:operation DELETE /user/emails user userDeleteEmail 102 // --- 103 // summary: Delete email addresses 104 // produces: 105 // - application/json 106 // parameters: 107 // - name: body 108 // in: body 109 // schema: 110 // "$ref": "#/definitions/DeleteEmailOption" 111 // responses: 112 // "204": 113 // "$ref": "#/responses/empty" 114 // "404": 115 // "$ref": "#/responses/notFound" 116 117 form := web.GetForm(ctx).(*api.DeleteEmailOption) 118 if len(form.Emails) == 0 { 119 ctx.Status(http.StatusNoContent) 120 return 121 } 122 123 if err := user_service.DeleteEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil { 124 if user_model.IsErrEmailAddressNotExist(err) { 125 ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err) 126 } else { 127 ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err) 128 } 129 return 130 } 131 ctx.Status(http.StatusNoContent) 132 }