aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/code.gitea.io/sdk/gitea/user.go
blob: c37627bfaaad747e4a5e1137decc311cedc6d6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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 gitea

import (
	"fmt"
	"net/url"
	"strconv"
	"time"
)

// 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
	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"`
}

// GetUserInfo get user info by user's name
func (c *Client) GetUserInfo(user string) (*User, *Response, error) {
	if err := escapeValidatePathSegments(&user); err != nil {
		return nil, nil, err
	}
	u := new(User)
	resp, err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
	return u, resp, err
}

// GetMyUserInfo get user info of current user
func (c *Client) GetMyUserInfo() (*User, *Response, error) {
	u := new(User)
	resp, err := c.getParsedResponse("GET", "/user", nil, nil, u)
	return u, resp, err
}

// 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)
}