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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// 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 (
"strings"
"github.com/unknwon/paginater"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/route/repo"
"gogs.io/gogs/internal/tool"
)
const (
FOLLOWERS = "user/meta/followers"
STARS = "user/meta/stars"
)
func Profile(c *context.Context, puser *context.ParamsUser) {
isShowKeys := false
if strings.HasSuffix(c.Params(":username"), ".keys") {
isShowKeys = true
}
// Show SSH keys.
if isShowKeys {
ShowSSHKeys(c, puser.ID)
return
}
if puser.IsOrganization() {
showOrgProfile(c)
return
}
c.Title(puser.DisplayName())
c.PageIs("UserProfile")
c.Data["Owner"] = puser
orgs, err := db.GetOrgsByUserID(puser.ID, c.IsLogged && (c.User.IsAdmin || c.User.ID == puser.ID))
if err != nil {
c.Error(err, "get organizations by user ID")
return
}
c.Data["Orgs"] = orgs
tab := c.Query("tab")
c.Data["TabName"] = tab
switch tab {
case "activity":
retrieveFeeds(c, puser.User, c.UserID(), true)
if c.Written() {
return
}
default:
page := c.QueryInt("page")
if page <= 0 {
page = 1
}
showPrivate := c.IsLogged && (puser.ID == c.User.ID || c.User.IsAdmin)
c.Data["Repos"], err = db.GetUserRepositories(&db.UserRepoOptions{
UserID: puser.ID,
Private: showPrivate,
Page: page,
PageSize: conf.UI.User.RepoPagingNum,
})
if err != nil {
c.Error(err, "get user repositories")
return
}
count := db.CountUserRepositories(puser.ID, showPrivate)
c.Data["Page"] = paginater.New(int(count), conf.UI.User.RepoPagingNum, page, 5)
}
c.Success(PROFILE)
}
func Followers(c *context.Context, puser *context.ParamsUser) {
c.Title(puser.DisplayName())
c.PageIs("Followers")
c.Data["CardsTitle"] = c.Tr("user.followers")
c.Data["Owner"] = puser
repo.RenderUserCards(
c,
puser.NumFollowers,
func(page int) ([]*db.User, error) {
return db.Users.ListFollowers(c.Req.Context(), puser.ID, page, db.ItemsPerPage)
},
FOLLOWERS,
)
}
func Following(c *context.Context, puser *context.ParamsUser) {
c.Title(puser.DisplayName())
c.PageIs("Following")
c.Data["CardsTitle"] = c.Tr("user.following")
c.Data["Owner"] = puser
repo.RenderUserCards(
c,
puser.NumFollowing,
func(page int) ([]*db.User, error) {
return db.Users.ListFollowings(c.Req.Context(), puser.ID, page, db.ItemsPerPage)
},
FOLLOWERS,
)
}
func Stars(_ *context.Context) {
}
func Action(c *context.Context, puser *context.ParamsUser) {
var err error
switch c.Params(":action") {
case "follow":
err = db.Follows.Follow(c.Req.Context(), c.UserID(), puser.ID)
case "unfollow":
err = db.Follows.Unfollow(c.Req.Context(), c.UserID(), puser.ID)
}
if err != nil {
c.Errorf(err, "action %q", c.Params(":action"))
return
}
redirectTo := c.Query("redirect_to")
if !tool.IsSameSiteURLPath(redirectTo) {
redirectTo = puser.HomeURLPath()
}
c.Redirect(redirectTo)
}
|