1// Copyright 2014 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
5package gitea
6
7import (
8	"fmt"
9	"net/url"
10	"strconv"
11	"time"
12)
13
14// User represents a user
15type User struct {
16	// the user's id
17	ID int64 `json:"id"`
18	// the user's username
19	UserName string `json:"login"`
20	// the user's full name
21	FullName string `json:"full_name"`
22	Email    string `json:"email"`
23	// URL to the user's avatar
24	AvatarURL string `json:"avatar_url"`
25	// User locale
26	Language string `json:"language"`
27	// Is the user an administrator
28	IsAdmin bool `json:"is_admin"`
29	// Date and Time of last login
30	LastLogin time.Time `json:"last_login"`
31	// Date and Time of user creation
32	Created time.Time `json:"created"`
33	// Is user restricted
34	Restricted bool `json:"restricted"`
35	// Is user active
36	IsActive bool `json:"active"`
37	// Is user login prohibited
38	ProhibitLogin bool `json:"prohibit_login"`
39	// the user's location
40	Location string `json:"location"`
41	// the user's website
42	Website string `json:"website"`
43	// the user's description
44	Description string `json:"description"`
45	// User visibility level option
46	Visibility VisibleType `json:"visibility"`
47
48	// user counts
49	FollowerCount    int `json:"followers_count"`
50	FollowingCount   int `json:"following_count"`
51	StarredRepoCount int `json:"starred_repos_count"`
52}
53
54// GetUserInfo get user info by user's name
55func (c *Client) GetUserInfo(user string) (*User, *Response, error) {
56	if err := escapeValidatePathSegments(&user); err != nil {
57		return nil, nil, err
58	}
59	u := new(User)
60	resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
61	return u, resp, err
62}
63
64// GetMyUserInfo get user info of current user
65func (c *Client) GetMyUserInfo() (*User, *Response, error) {
66	u := new(User)
67	resp, err := c.getParsedResponse("GET", "/user", nil, nil, u)
68	return u, resp, err
69}
70
71// GetUserByID returns user by a given user ID
72func (c *Client) GetUserByID(id int64) (*User, *Response, error) {
73	if id < 0 {
74		return nil, nil, fmt.Errorf("invalid user id %d", id)
75	}
76
77	query := make(url.Values)
78	query.Add("uid", strconv.FormatInt(id, 10))
79	users, resp, err := c.searchUsers(query.Encode())
80
81	if err != nil {
82		return nil, resp, err
83	}
84
85	if len(users) == 1 {
86		return users[0], resp, err
87	}
88
89	return nil, resp, fmt.Errorf("user not found with id %d", id)
90}
91