mirror of https://github.com/gogits/gogs.git
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.
83 lines
2.1 KiB
83 lines
2.1 KiB
9 years ago
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package user
|
||
|
|
||
|
import (
|
||
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/routes/api/v1/convert"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
|
||
8 years ago
|
func ListEmails(c *context.APIContext) {
|
||
|
emails, err := models.GetEmailAddresses(c.User.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "GetEmailAddresses", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
apiEmails := make([]*api.Email, len(emails))
|
||
|
for i := range emails {
|
||
9 years ago
|
apiEmails[i] = convert.ToEmail(emails[i])
|
||
9 years ago
|
}
|
||
8 years ago
|
c.JSON(200, &apiEmails)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#add-email-addresses
|
||
8 years ago
|
func AddEmail(c *context.APIContext, form api.CreateEmailOption) {
|
||
9 years ago
|
if len(form.Emails) == 0 {
|
||
8 years ago
|
c.Status(422)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
emails := make([]*models.EmailAddress, len(form.Emails))
|
||
|
for i := range form.Emails {
|
||
|
emails[i] = &models.EmailAddress{
|
||
8 years ago
|
UID: c.User.ID,
|
||
9 years ago
|
Email: form.Emails[i],
|
||
|
IsActivated: !setting.Service.RegisterEmailConfirm,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := models.AddEmailAddresses(emails); err != nil {
|
||
|
if models.IsErrEmailAlreadyUsed(err) {
|
||
8 years ago
|
c.Error(422, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "AddEmailAddresses", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
apiEmails := make([]*api.Email, len(emails))
|
||
|
for i := range emails {
|
||
9 years ago
|
apiEmails[i] = convert.ToEmail(emails[i])
|
||
9 years ago
|
}
|
||
8 years ago
|
c.JSON(201, &apiEmails)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#delete-email-addresses
|
||
8 years ago
|
func DeleteEmail(c *context.APIContext, form api.CreateEmailOption) {
|
||
9 years ago
|
if len(form.Emails) == 0 {
|
||
8 years ago
|
c.Status(204)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
emails := make([]*models.EmailAddress, len(form.Emails))
|
||
|
for i := range form.Emails {
|
||
|
emails[i] = &models.EmailAddress{
|
||
8 years ago
|
UID: c.User.ID,
|
||
9 years ago
|
Email: form.Emails[i],
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := models.DeleteEmailAddresses(emails); err != nil {
|
||
8 years ago
|
c.Error(500, "DeleteEmailAddresses", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Status(204)
|
||
9 years ago
|
}
|