mirror of
https://github.com/gogs/gogs.git
synced 2026-05-07 14:36:32 +02:00
internal: move packages under this directory (#5836)
* Rename pkg -> internal * Rename routes -> route * Move route -> internal/route * Rename models -> db * Move db -> internal/db * Fix route2 -> route * Move cmd -> internal/cmd * Bump version
This commit is contained in:
45
internal/route/api/v1/user/app.go
Normal file
45
internal/route/api/v1/user/app.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
"gogs.io/gogs/internal/db/errors"
|
||||
)
|
||||
|
||||
func ListAccessTokens(c *context.APIContext) {
|
||||
tokens, err := db.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 := &db.AccessToken{
|
||||
UID: c.User.ID,
|
||||
Name: form.Name,
|
||||
}
|
||||
if err := db.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})
|
||||
}
|
||||
81
internal/route/api/v1/user/email.go
Normal file
81
internal/route/api/v1/user/email.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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 (
|
||||
convert2 "gogs.io/gogs/internal/route/api/v1/convert"
|
||||
"net/http"
|
||||
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
||||
"gogs.io/gogs/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
)
|
||||
|
||||
func ListEmails(c *context.APIContext) {
|
||||
emails, err := db.GetEmailAddresses(c.User.ID)
|
||||
if err != nil {
|
||||
c.ServerError("GetEmailAddresses", err)
|
||||
return
|
||||
}
|
||||
apiEmails := make([]*api.Email, len(emails))
|
||||
for i := range emails {
|
||||
apiEmails[i] = convert2.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([]*db.EmailAddress, len(form.Emails))
|
||||
for i := range form.Emails {
|
||||
emails[i] = &db.EmailAddress{
|
||||
UID: c.User.ID,
|
||||
Email: form.Emails[i],
|
||||
IsActivated: !setting.Service.RegisterEmailConfirm,
|
||||
}
|
||||
}
|
||||
|
||||
if err := db.AddEmailAddresses(emails); err != nil {
|
||||
if db.IsErrEmailAlreadyUsed(err) {
|
||||
c.Error(http.StatusUnprocessableEntity, "", "email address has been used: "+err.(db.ErrEmailAlreadyUsed).Email)
|
||||
} else {
|
||||
c.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
apiEmails := make([]*api.Email, len(emails))
|
||||
for i := range emails {
|
||||
apiEmails[i] = convert2.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([]*db.EmailAddress, len(form.Emails))
|
||||
for i := range form.Emails {
|
||||
emails[i] = &db.EmailAddress{
|
||||
UID: c.User.ID,
|
||||
Email: form.Emails[i],
|
||||
}
|
||||
}
|
||||
|
||||
if err := db.DeleteEmailAddresses(emails); err != nil {
|
||||
c.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
|
||||
return
|
||||
}
|
||||
c.NoContent()
|
||||
}
|
||||
114
internal/route/api/v1/user/follower.go
Normal file
114
internal/route/api/v1/user/follower.go
Normal file
@@ -0,0 +1,114 @@
|
||||
// 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/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
)
|
||||
|
||||
func responseApiUsers(c *context.APIContext, users []*db.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 *db.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 *db.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 *db.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 := db.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 := db.UnfollowUser(c.User.ID, target.ID); err != nil {
|
||||
c.ServerError("UnfollowUser", err)
|
||||
return
|
||||
}
|
||||
c.NoContent()
|
||||
}
|
||||
108
internal/route/api/v1/user/key.go
Normal file
108
internal/route/api/v1/user/key.go
Normal file
@@ -0,0 +1,108 @@
|
||||
// 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"
|
||||
convert2 "gogs.io/gogs/internal/route/api/v1/convert"
|
||||
repo2 "gogs.io/gogs/internal/route/api/v1/repo"
|
||||
"net/http"
|
||||
|
||||
"gogs.io/gogs/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
"gogs.io/gogs/internal/db/errors"
|
||||
"gogs.io/gogs/internal/setting"
|
||||
)
|
||||
|
||||
func GetUserByParamsName(c *context.APIContext, name string) *db.User {
|
||||
user, err := db.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) *db.User {
|
||||
return GetUserByParamsName(c, ":username")
|
||||
}
|
||||
|
||||
func composePublicKeysAPILink() string {
|
||||
return setting.AppURL + "api/v1/user/keys/"
|
||||
}
|
||||
|
||||
func listPublicKeys(c *context.APIContext, uid int64) {
|
||||
keys, err := db.ListPublicKeys(uid)
|
||||
if err != nil {
|
||||
c.ServerError("ListPublicKeys", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiLink := composePublicKeysAPILink()
|
||||
apiKeys := make([]*api.PublicKey, len(keys))
|
||||
for i := range keys {
|
||||
apiKeys[i] = convert2.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 := db.GetPublicKeyByID(c.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
c.NotFoundOrServerError("GetPublicKeyByID", db.IsErrKeyNotExist, err)
|
||||
return
|
||||
}
|
||||
|
||||
apiLink := composePublicKeysAPILink()
|
||||
c.JSONSuccess(convert2.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 := db.CheckPublicKeyString(form.Key)
|
||||
if err != nil {
|
||||
repo2.HandleCheckKeyStringError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := db.AddPublicKey(uid, form.Title, content)
|
||||
if err != nil {
|
||||
repo2.HandleAddKeyError(c, err)
|
||||
return
|
||||
}
|
||||
apiLink := composePublicKeysAPILink()
|
||||
c.JSON(http.StatusCreated, convert2.ToPublicKey(apiLink, key))
|
||||
}
|
||||
|
||||
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
|
||||
CreateUserPublicKey(c, form, c.User.ID)
|
||||
}
|
||||
|
||||
func DeletePublicKey(c *context.APIContext) {
|
||||
if err := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
|
||||
if db.IsErrKeyAccessDenied(err) {
|
||||
c.Error(http.StatusForbidden, "", "you do not have access to this key")
|
||||
} else {
|
||||
c.Error(http.StatusInternalServerError, "DeletePublicKey", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.NoContent()
|
||||
}
|
||||
74
internal/route/api/v1/user/user.go
Normal file
74
internal/route/api/v1/user/user.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// 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/internal/context"
|
||||
"gogs.io/gogs/internal/db"
|
||||
"gogs.io/gogs/internal/db/errors"
|
||||
"gogs.io/gogs/internal/markup"
|
||||
)
|
||||
|
||||
func Search(c *context.APIContext) {
|
||||
opts := &db.SearchUserOptions{
|
||||
Keyword: c.Query("q"),
|
||||
Type: db.USER_TYPE_INDIVIDUAL,
|
||||
PageSize: com.StrTo(c.Query("limit")).MustInt(),
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = 10
|
||||
}
|
||||
|
||||
users, _, err := db.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 := db.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())
|
||||
}
|
||||
Reference in New Issue
Block a user