mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
3.6 KiB
170 lines
3.6 KiB
9 years ago
|
// 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 (
|
||
|
"fmt"
|
||
9 years ago
|
"path"
|
||
9 years ago
|
"strings"
|
||
|
|
||
9 years ago
|
"github.com/Unknwon/paginater"
|
||
|
|
||
9 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/routes/repo"
|
||
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
FOLLOWERS = "user/meta/followers"
|
||
|
STARS = "user/meta/stars"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func GetUserByName(c *context.Context, name string) *models.User {
|
||
9 years ago
|
user, err := models.GetUserByName(name)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
return user
|
||
|
}
|
||
|
|
||
9 years ago
|
// GetUserByParams returns user whose name is presented in URL paramenter.
|
||
8 years ago
|
func GetUserByParams(c *context.Context) *models.User {
|
||
|
return GetUserByName(c, c.Params(":username"))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Profile(c *context.Context) {
|
||
|
uname := c.Params(":username")
|
||
9 years ago
|
// Special handle for FireFox requests favicon.ico.
|
||
|
if uname == "favicon.ico" {
|
||
8 years ago
|
c.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
|
||
9 years ago
|
return
|
||
|
} else if strings.HasSuffix(uname, ".png") {
|
||
8 years ago
|
c.Error(404)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
isShowKeys := false
|
||
|
if strings.HasSuffix(uname, ".keys") {
|
||
|
isShowKeys = true
|
||
|
}
|
||
|
|
||
8 years ago
|
ctxUser := GetUserByName(c, strings.TrimSuffix(uname, ".keys"))
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Show SSH keys.
|
||
|
if isShowKeys {
|
||
8 years ago
|
ShowSSHKeys(c, ctxUser.ID)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if ctxUser.IsOrganization() {
|
||
8 years ago
|
showOrgProfile(c)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Title"] = ctxUser.DisplayName()
|
||
|
c.Data["PageIsUserProfile"] = true
|
||
|
c.Data["Owner"] = ctxUser
|
||
9 years ago
|
|
||
8 years ago
|
orgs, err := models.GetOrgsByUserID(ctxUser.ID, c.IsLogged && (c.User.IsAdmin || c.User.ID == ctxUser.ID))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetOrgsByUserIDDesc", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.Data["Orgs"] = orgs
|
||
9 years ago
|
|
||
8 years ago
|
tab := c.Query("tab")
|
||
|
c.Data["TabName"] = tab
|
||
9 years ago
|
switch tab {
|
||
|
case "activity":
|
||
8 years ago
|
retrieveFeeds(c, ctxUser, -1, true)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
default:
|
||
8 years ago
|
page := c.QueryInt("page")
|
||
9 years ago
|
if page <= 0 {
|
||
|
page = 1
|
||
|
}
|
||
|
|
||
8 years ago
|
showPrivate := c.IsLogged && (ctxUser.ID == c.User.ID || c.User.IsAdmin)
|
||
|
c.Data["Repos"], err = models.GetUserRepositories(&models.UserRepoOptions{
|
||
8 years ago
|
UserID: ctxUser.ID,
|
||
8 years ago
|
Private: showPrivate,
|
||
8 years ago
|
Page: page,
|
||
|
PageSize: setting.UI.User.RepoPagingNum,
|
||
|
})
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
count := models.CountUserRepositories(ctxUser.ID, showPrivate)
|
||
8 years ago
|
c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.HTML(200, PROFILE)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Followers(c *context.Context) {
|
||
|
u := GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = u.DisplayName()
|
||
|
c.Data["CardsTitle"] = c.Tr("user.followers")
|
||
|
c.Data["PageIsFollowers"] = true
|
||
|
c.Data["Owner"] = u
|
||
|
repo.RenderUserCards(c, u.NumFollowers, u.GetFollowers, FOLLOWERS)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Following(c *context.Context) {
|
||
|
u := GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = u.DisplayName()
|
||
|
c.Data["CardsTitle"] = c.Tr("user.following")
|
||
|
c.Data["PageIsFollowing"] = true
|
||
|
c.Data["Owner"] = u
|
||
|
repo.RenderUserCards(c, u.NumFollowing, u.GetFollowing, FOLLOWERS)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Stars(c *context.Context) {
|
||
9 years ago
|
|
||
|
}
|
||
|
|
||
8 years ago
|
func Action(c *context.Context) {
|
||
|
u := GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var err error
|
||
8 years ago
|
switch c.Params(":action") {
|
||
9 years ago
|
case "follow":
|
||
8 years ago
|
err = models.FollowUser(c.User.ID, u.ID)
|
||
9 years ago
|
case "unfollow":
|
||
8 years ago
|
err = models.UnfollowUser(c.User.ID, u.ID)
|
||
9 years ago
|
}
|
||
|
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, fmt.Sprintf("Action (%s)", c.Params(":action")), err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
redirectTo := c.Query("redirect_to")
|
||
9 years ago
|
if len(redirectTo) == 0 {
|
||
|
redirectTo = u.HomeLink()
|
||
|
}
|
||
8 years ago
|
c.Redirect(redirectTo)
|
||
9 years ago
|
}
|