You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.4 KiB

  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. "code.gitea.io/gitea/modules/setting"
  11. api "code.gitea.io/gitea/modules/structs"
  12. )
  13. // ListEmails list all of the authenticated user's email addresses
  14. // see https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
  15. func ListEmails(ctx *context.APIContext) {
  16. // swagger:operation GET /user/emails user userListEmails
  17. // ---
  18. // summary: List the authenticated user's email addresses
  19. // produces:
  20. // - application/json
  21. // responses:
  22. // "200":
  23. // "$ref": "#/responses/EmailList"
  24. emails, err := models.GetEmailAddresses(ctx.User.ID)
  25. if err != nil {
  26. ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
  27. return
  28. }
  29. apiEmails := make([]*api.Email, len(emails))
  30. for i := range emails {
  31. apiEmails[i] = convert.ToEmail(emails[i])
  32. }
  33. ctx.JSON(http.StatusOK, &apiEmails)
  34. }
  35. // AddEmail add an email address
  36. func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) {
  37. // swagger:operation POST /user/emails user userAddEmail
  38. // ---
  39. // summary: Add email addresses
  40. // produces:
  41. // - application/json
  42. // parameters:
  43. // - name: options
  44. // in: body
  45. // schema:
  46. // "$ref": "#/definitions/CreateEmailOption"
  47. // parameters:
  48. // - name: body
  49. // in: body
  50. // schema:
  51. // "$ref": "#/definitions/CreateEmailOption"
  52. // responses:
  53. // '201':
  54. // "$ref": "#/responses/EmailList"
  55. // "422":
  56. // "$ref": "#/responses/validationError"
  57. if len(form.Emails) == 0 {
  58. ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
  59. return
  60. }
  61. emails := make([]*models.EmailAddress, len(form.Emails))
  62. for i := range form.Emails {
  63. emails[i] = &models.EmailAddress{
  64. UID: ctx.User.ID,
  65. Email: form.Emails[i],
  66. IsActivated: !setting.Service.RegisterEmailConfirm,
  67. }
  68. }
  69. if err := models.AddEmailAddresses(emails); err != nil {
  70. if models.IsErrEmailAlreadyUsed(err) {
  71. ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email)
  72. } else {
  73. ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
  74. }
  75. return
  76. }
  77. apiEmails := make([]*api.Email, len(emails))
  78. for i := range emails {
  79. apiEmails[i] = convert.ToEmail(emails[i])
  80. }
  81. ctx.JSON(http.StatusCreated, &apiEmails)
  82. }
  83. // DeleteEmail delete email
  84. func DeleteEmail(ctx *context.APIContext, form api.DeleteEmailOption) {
  85. // swagger:operation DELETE /user/emails user userDeleteEmail
  86. // ---
  87. // summary: Delete email addresses
  88. // produces:
  89. // - application/json
  90. // parameters:
  91. // - name: body
  92. // in: body
  93. // schema:
  94. // "$ref": "#/definitions/DeleteEmailOption"
  95. // responses:
  96. // "204":
  97. // "$ref": "#/responses/empty"
  98. if len(form.Emails) == 0 {
  99. ctx.Status(http.StatusNoContent)
  100. return
  101. }
  102. emails := make([]*models.EmailAddress, len(form.Emails))
  103. for i := range form.Emails {
  104. emails[i] = &models.EmailAddress{
  105. Email: form.Emails[i],
  106. UID: ctx.User.ID,
  107. }
  108. }
  109. if err := models.DeleteEmailAddresses(emails); err != nil {
  110. ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
  111. return
  112. }
  113. ctx.Status(http.StatusNoContent)
  114. }