diff options
Diffstat (limited to 'routes/api/v1/user')
-rw-r--r-- | routes/api/v1/user/app.go | 45 | ||||
-rw-r--r-- | routes/api/v1/user/email.go | 81 | ||||
-rw-r--r-- | routes/api/v1/user/follower.go | 114 | ||||
-rw-r--r-- | routes/api/v1/user/key.go | 108 | ||||
-rw-r--r-- | routes/api/v1/user/user.go | 74 |
5 files changed, 0 insertions, 422 deletions
diff --git a/routes/api/v1/user/app.go b/routes/api/v1/user/app.go deleted file mode 100644 index 4e0b2fee..00000000 --- a/routes/api/v1/user/app.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2014 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 ( - "net/http" - - api "github.com/gogs/go-gogs-client" - - "gogs.io/gogs/models" - "gogs.io/gogs/models/errors" - "gogs.io/gogs/pkg/context" -) - -func ListAccessTokens(c *context.APIContext) { - tokens, err := models.ListAccessTokens(c.User.ID) - if err != nil { - c.ServerError("ListAccessTokens", err) - return - } - - apiTokens := make([]*api.AccessToken, len(tokens)) - for i := range tokens { - apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1} - } - c.JSONSuccess(&apiTokens) -} - -func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) { - t := &models.AccessToken{ - UID: c.User.ID, - Name: form.Name, - } - if err := models.NewAccessToken(t); err != nil { - if errors.IsAccessTokenNameAlreadyExist(err) { - c.Error(http.StatusUnprocessableEntity, "", err) - } else { - c.ServerError("NewAccessToken", err) - } - return - } - c.JSON(http.StatusCreated, &api.AccessToken{t.Name, t.Sha1}) -} diff --git a/routes/api/v1/user/email.go b/routes/api/v1/user/email.go deleted file mode 100644 index 7091beb6..00000000 --- a/routes/api/v1/user/email.go +++ /dev/null @@ -1,81 +0,0 @@ -// 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 ( - "net/http" - - api "github.com/gogs/go-gogs-client" - - "gogs.io/gogs/models" - "gogs.io/gogs/pkg/context" - "gogs.io/gogs/pkg/setting" - "gogs.io/gogs/routes/api/v1/convert" -) - -func ListEmails(c *context.APIContext) { - emails, err := models.GetEmailAddresses(c.User.ID) - if err != nil { - c.ServerError("GetEmailAddresses", err) - return - } - apiEmails := make([]*api.Email, len(emails)) - for i := range emails { - apiEmails[i] = convert.ToEmail(emails[i]) - } - c.JSONSuccess(&apiEmails) -} - -func AddEmail(c *context.APIContext, form api.CreateEmailOption) { - if len(form.Emails) == 0 { - c.Status(http.StatusUnprocessableEntity) - return - } - - emails := make([]*models.EmailAddress, len(form.Emails)) - for i := range form.Emails { - emails[i] = &models.EmailAddress{ - UID: c.User.ID, - Email: form.Emails[i], - IsActivated: !setting.Service.RegisterEmailConfirm, - } - } - - if err := models.AddEmailAddresses(emails); err != nil { - if models.IsErrEmailAlreadyUsed(err) { - c.Error(http.StatusUnprocessableEntity, "", "email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email) - } else { - c.Error(http.StatusInternalServerError, "AddEmailAddresses", err) - } - return - } - - apiEmails := make([]*api.Email, len(emails)) - for i := range emails { - apiEmails[i] = convert.ToEmail(emails[i]) - } - c.JSON(http.StatusCreated, &apiEmails) -} - -func DeleteEmail(c *context.APIContext, form api.CreateEmailOption) { - if len(form.Emails) == 0 { - c.NoContent() - return - } - - emails := make([]*models.EmailAddress, len(form.Emails)) - for i := range form.Emails { - emails[i] = &models.EmailAddress{ - UID: c.User.ID, - Email: form.Emails[i], - } - } - - if err := models.DeleteEmailAddresses(emails); err != nil { - c.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err) - return - } - c.NoContent() -} diff --git a/routes/api/v1/user/follower.go b/routes/api/v1/user/follower.go deleted file mode 100644 index 022bd8c5..00000000 --- a/routes/api/v1/user/follower.go +++ /dev/null @@ -1,114 +0,0 @@ -// 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/gogs/go-gogs-client" - - "gogs.io/gogs/models" - "gogs.io/gogs/pkg/context" -) - -func responseApiUsers(c *context.APIContext, users []*models.User) { - apiUsers := make([]*api.User, len(users)) - for i := range users { - apiUsers[i] = users[i].APIFormat() - } - c.JSONSuccess(&apiUsers) -} - -func listUserFollowers(c *context.APIContext, u *models.User) { - users, err := u.GetFollowers(c.QueryInt("page")) - if err != nil { - c.ServerError("GetUserFollowers", err) - return - } - responseApiUsers(c, users) -} - -func ListMyFollowers(c *context.APIContext) { - listUserFollowers(c, c.User) -} - -func ListFollowers(c *context.APIContext) { - u := GetUserByParams(c) - if c.Written() { - return - } - listUserFollowers(c, u) -} - -func listUserFollowing(c *context.APIContext, u *models.User) { - users, err := u.GetFollowing(c.QueryInt("page")) - if err != nil { - c.ServerError("GetFollowing", err) - return - } - responseApiUsers(c, users) -} - -func ListMyFollowing(c *context.APIContext) { - listUserFollowing(c, c.User) -} - -func ListFollowing(c *context.APIContext) { - u := GetUserByParams(c) - if c.Written() { - return - } - listUserFollowing(c, u) -} - -func checkUserFollowing(c *context.APIContext, u *models.User, followID int64) { - if u.IsFollowing(followID) { - c.NoContent() - } else { - c.NotFound() - } -} - -func CheckMyFollowing(c *context.APIContext) { - target := GetUserByParams(c) - if c.Written() { - return - } - checkUserFollowing(c, c.User, target.ID) -} - -func CheckFollowing(c *context.APIContext) { - u := GetUserByParams(c) - if c.Written() { - return - } - target := GetUserByParamsName(c, ":target") - if c.Written() { - return - } - checkUserFollowing(c, u, target.ID) -} - -func Follow(c *context.APIContext) { - target := GetUserByParams(c) - if c.Written() { - return - } - if err := models.FollowUser(c.User.ID, target.ID); err != nil { - c.ServerError("FollowUser", err) - return - } - c.NoContent() -} - -func Unfollow(c *context.APIContext) { - target := GetUserByParams(c) - if c.Written() { - return - } - if err := models.UnfollowUser(c.User.ID, target.ID); err != nil { - c.ServerError("UnfollowUser", err) - return - } - c.NoContent() -} diff --git a/routes/api/v1/user/key.go b/routes/api/v1/user/key.go deleted file mode 100644 index c0757811..00000000 --- a/routes/api/v1/user/key.go +++ /dev/null @@ -1,108 +0,0 @@ -// 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/gogs/go-gogs-client" - "net/http" - - "gogs.io/gogs/models" - "gogs.io/gogs/models/errors" - "gogs.io/gogs/pkg/context" - "gogs.io/gogs/pkg/setting" - "gogs.io/gogs/routes/api/v1/convert" - "gogs.io/gogs/routes/api/v1/repo" -) - -func GetUserByParamsName(c *context.APIContext, name string) *models.User { - user, err := models.GetUserByName(c.Params(name)) - if err != nil { - c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) - return nil - } - return user -} - -// GetUserByParams returns user whose name is presented in URL paramenter. -func GetUserByParams(c *context.APIContext) *models.User { - return GetUserByParamsName(c, ":username") -} - -func composePublicKeysAPILink() string { - return setting.AppURL + "api/v1/user/keys/" -} - -func listPublicKeys(c *context.APIContext, uid int64) { - keys, err := models.ListPublicKeys(uid) - if err != nil { - c.ServerError("ListPublicKeys", err) - return - } - - apiLink := composePublicKeysAPILink() - apiKeys := make([]*api.PublicKey, len(keys)) - for i := range keys { - apiKeys[i] = convert.ToPublicKey(apiLink, keys[i]) - } - - c.JSONSuccess(&apiKeys) -} - -func ListMyPublicKeys(c *context.APIContext) { - listPublicKeys(c, c.User.ID) -} - -func ListPublicKeys(c *context.APIContext) { - user := GetUserByParams(c) - if c.Written() { - return - } - listPublicKeys(c, user.ID) -} - -func GetPublicKey(c *context.APIContext) { - key, err := models.GetPublicKeyByID(c.ParamsInt64(":id")) - if err != nil { - c.NotFoundOrServerError("GetPublicKeyByID", models.IsErrKeyNotExist, err) - return - } - - apiLink := composePublicKeysAPILink() - c.JSONSuccess(convert.ToPublicKey(apiLink, key)) -} - -// CreateUserPublicKey creates new public key to given user by ID. -func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) { - content, err := models.CheckPublicKeyString(form.Key) - if err != nil { - repo.HandleCheckKeyStringError(c, err) - return - } - - key, err := models.AddPublicKey(uid, form.Title, content) - if err != nil { - repo.HandleAddKeyError(c, err) - return - } - apiLink := composePublicKeysAPILink() - c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key)) -} - -func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) { - CreateUserPublicKey(c, form, c.User.ID) -} - -func DeletePublicKey(c *context.APIContext) { - if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil { - if models.IsErrKeyAccessDenied(err) { - c.Error(http.StatusForbidden, "", "you do not have access to this key") - } else { - c.Error(http.StatusInternalServerError, "DeletePublicKey", err) - } - return - } - - c.NoContent() -} diff --git a/routes/api/v1/user/user.go b/routes/api/v1/user/user.go deleted file mode 100644 index 121deeb7..00000000 --- a/routes/api/v1/user/user.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2014 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 ( - "net/http" - - "github.com/unknwon/com" - - api "github.com/gogs/go-gogs-client" - - "gogs.io/gogs/models" - "gogs.io/gogs/models/errors" - "gogs.io/gogs/pkg/context" - "gogs.io/gogs/pkg/markup" -) - -func Search(c *context.APIContext) { - opts := &models.SearchUserOptions{ - Keyword: c.Query("q"), - Type: models.USER_TYPE_INDIVIDUAL, - PageSize: com.StrTo(c.Query("limit")).MustInt(), - } - if opts.PageSize == 0 { - opts.PageSize = 10 - } - - users, _, err := models.SearchUserByName(opts) - if err != nil { - c.JSON(http.StatusInternalServerError, map[string]interface{}{ - "ok": false, - "error": err.Error(), - }) - return - } - - results := make([]*api.User, len(users)) - for i := range users { - results[i] = &api.User{ - ID: users[i].ID, - UserName: users[i].Name, - AvatarUrl: users[i].AvatarLink(), - FullName: markup.Sanitize(users[i].FullName), - } - if c.IsLogged { - results[i].Email = users[i].Email - } - } - - c.JSONSuccess(map[string]interface{}{ - "ok": true, - "data": results, - }) -} - -func GetInfo(c *context.APIContext) { - u, err := models.GetUserByName(c.Params(":username")) - if err != nil { - c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) - return - } - - // Hide user e-mail when API caller isn't signed in. - if !c.IsLogged { - u.Email = "" - } - c.JSONSuccess(u.APIFormat()) -} - -func GetAuthenticatedUser(c *context.APIContext) { - c.JSONSuccess(c.User.APIFormat()) -} |