2024-02-23 00:26:49 +00:00
|
|
|
// Copyright 2024 The Forgjo Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-11-14 22:07:41 +00:00
|
|
|
// 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.
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
package forgejo
|
2014-11-14 22:07:41 +00:00
|
|
|
|
2014-11-18 16:06:47 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 04:23:50 +01:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-10-13 02:34:01 +01:00
|
|
|
"time"
|
2019-05-11 16:38:52 +01:00
|
|
|
)
|
2014-11-18 16:06:47 +00:00
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// User represents a user
|
|
|
|
type User struct {
|
|
|
|
// the user's id
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
// the user's username
|
|
|
|
UserName string `json:"login"`
|
|
|
|
// the user's full name
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
// URL to the user's avatar
|
|
|
|
AvatarURL string `json:"avatar_url"`
|
|
|
|
// User locale
|
|
|
|
Language string `json:"language"`
|
|
|
|
// Is the user an administrator
|
2021-07-01 16:10:21 +01:00
|
|
|
IsAdmin bool `json:"is_admin"`
|
|
|
|
// Date and Time of last login
|
|
|
|
LastLogin time.Time `json:"last_login"`
|
|
|
|
// Date and Time of user creation
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// Is user restricted
|
|
|
|
Restricted bool `json:"restricted"`
|
|
|
|
// Is user active
|
|
|
|
IsActive bool `json:"active"`
|
|
|
|
// Is user login prohibited
|
|
|
|
ProhibitLogin bool `json:"prohibit_login"`
|
|
|
|
// the user's location
|
|
|
|
Location string `json:"location"`
|
|
|
|
// the user's website
|
|
|
|
Website string `json:"website"`
|
|
|
|
// the user's description
|
|
|
|
Description string `json:"description"`
|
|
|
|
// User visibility level option
|
|
|
|
Visibility VisibleType `json:"visibility"`
|
|
|
|
|
|
|
|
// user counts
|
|
|
|
FollowerCount int `json:"followers_count"`
|
|
|
|
FollowingCount int `json:"following_count"`
|
|
|
|
StarredRepoCount int `json:"starred_repos_count"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
2016-12-15 16:13:48 +00:00
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// GetUserInfo get user info by user's name
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetUserInfo(user string) (*User, *Response, error) {
|
2021-03-21 20:20:32 +00:00
|
|
|
if err := escapeValidatePathSegments(&user); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-11-18 16:06:47 +00:00
|
|
|
u := new(User)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
|
|
|
|
return u, resp, err
|
2014-11-18 16:06:47 +00:00
|
|
|
}
|
2018-06-24 12:13:26 +01:00
|
|
|
|
|
|
|
// GetMyUserInfo get user info of current user
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetMyUserInfo() (*User, *Response, error) {
|
2018-06-24 12:13:26 +01:00
|
|
|
u := new(User)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", "/user", nil, nil, u)
|
|
|
|
return u, resp, err
|
2018-06-24 12:13:26 +01:00
|
|
|
}
|
2021-04-05 04:23:50 +01:00
|
|
|
|
|
|
|
// GetUserByID returns user by a given user ID
|
|
|
|
func (c *Client) GetUserByID(id int64) (*User, *Response, error) {
|
|
|
|
if id < 0 {
|
|
|
|
return nil, nil, fmt.Errorf("invalid user id %d", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Add("uid", strconv.FormatInt(id, 10))
|
|
|
|
users, resp, err := c.searchUsers(query.Encode())
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(users) == 1 {
|
|
|
|
return users[0], resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, resp, fmt.Errorf("user not found with id %d", id)
|
|
|
|
}
|