@ -8,7 +8,6 @@ package user
import (
import (
"fmt"
"fmt"
"net/http"
"net/http"
"path"
"strings"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models"
@ -24,121 +23,51 @@ import (
"code.gitea.io/gitea/routers/web/org"
"code.gitea.io/gitea/routers/web/org"
)
)
// GetUserByName get user by name
func GetUserByName ( ctx * context . Context , name string ) * user_model . User {
user , err := user_model . GetUserByName ( name )
if err != nil {
if user_model . IsErrUserNotExist ( err ) {
if redirectUserID , err := user_model . LookupUserRedirect ( name ) ; err == nil {
context . RedirectToUser ( ctx , name , redirectUserID )
} else {
ctx . NotFound ( "GetUserByName" , err )
}
} else {
ctx . ServerError ( "GetUserByName" , err )
}
return nil
}
return user
}
// GetUserByParams returns user whose name is presented in URL paramenter.
func GetUserByParams ( ctx * context . Context ) * user_model . User {
return GetUserByName ( ctx , ctx . Params ( ":username" ) )
}
// Profile render user's profile page
// Profile render user's profile page
func Profile ( ctx * context . Context ) {
func Profile ( ctx * context . Context ) {
uname := ctx . Params ( ":username" )
if strings . Contains ( ctx . Req . Header . Get ( "Accept" ) , "application/rss+xml" ) {
feed . ShowUserFeedRSS ( ctx )
// Special handle for FireFox requests favicon.ico.
if uname == "favicon.ico" {
ctx . ServeFile ( path . Join ( setting . StaticRootPath , "public/img/favicon.png" ) )
return
return
}
}
if strings . Contains ( ctx . Req . Header . Get ( "Accept" ) , "application/atom+xml" ) {
if strings . HasSuffix ( uname , ".png" ) {
feed . ShowUserFeedAtom ( ctx )
ctx . Error ( http . StatusNotFound )
return
}
isShowKeys := false
if strings . HasSuffix ( uname , ".keys" ) {
isShowKeys = true
uname = strings . TrimSuffix ( uname , ".keys" )
}
isShowGPG := false
if strings . HasSuffix ( uname , ".gpg" ) {
isShowGPG = true
uname = strings . TrimSuffix ( uname , ".gpg" )
}
isShowFeed , uname , showFeedType := feed . GetFeedType ( uname , ctx . Req )
ctxUser := GetUserByName ( ctx , uname )
if ctx . Written ( ) {
return
}
if ctxUser . IsOrganization ( ) {
// Show Org RSS feed
if isShowFeed {
feed . ShowUserFeed ( ctx , ctxUser , showFeedType )
return
return
}
}
if ctx . ContextUser . IsOrganization ( ) {
org . Home ( ctx )
org . Home ( ctx )
return
return
}
}
// check view permissions
// check view permissions
if ! models . IsUserVisibleToViewer ( ctxUser , ctx . Doer ) {
if ! models . IsUserVisibleToViewer ( ctx . ContextUser , ctx . Doer ) {
ctx . NotFound ( "user" , fmt . Errorf ( uname ) )
ctx . NotFound ( "user" , fmt . Errorf ( ctx . ContextUser . Name ) )
return
}
// Show SSH keys.
if isShowKeys {
ShowSSHKeys ( ctx , ctxUser . ID )
return
}
// Show GPG keys.
if isShowGPG {
ShowGPGKeys ( ctx , ctxUser . ID )
return
}
// Show User RSS feed
if isShowFeed {
feed . ShowUserFeed ( ctx , ctxUser , showFeedType )
return
return
}
}
// advertise feed via meta tag
// advertise feed via meta tag
ctx . Data [ "FeedURL" ] = ctxUser . HTMLURL ( )
ctx . Data [ "FeedURL" ] = ctx . ContextUser . HTMLURL ( )
// Show OpenID URIs
// Show OpenID URIs
openIDs , err := user_model . GetUserOpenIDs ( ctxUser . ID )
openIDs , err := user_model . GetUserOpenIDs ( ctx . ContextUser . ID )
if err != nil {
if err != nil {
ctx . ServerError ( "GetUserOpenIDs" , err )
ctx . ServerError ( "GetUserOpenIDs" , err )
return
return
}
}
var isFollowing bool
var isFollowing bool
if ctx . Doer != nil && ctxUser != nil {
if ctx . Doer != nil {
isFollowing = user_model . IsFollowing ( ctx . Doer . ID , ctxUser . ID )
isFollowing = user_model . IsFollowing ( ctx . Doer . ID , ctx . ContextUser . ID )
}
}
ctx . Data [ "Title" ] = ctxUser . DisplayName ( )
ctx . Data [ "Title" ] = ctx . Context User. DisplayName ( )
ctx . Data [ "PageIsUserProfile" ] = true
ctx . Data [ "PageIsUserProfile" ] = true
ctx . Data [ "Owner" ] = ctxUser
ctx . Data [ "Owner" ] = ctx . Context User
ctx . Data [ "OpenIDs" ] = openIDs
ctx . Data [ "OpenIDs" ] = openIDs
ctx . Data [ "IsFollowing" ] = isFollowing
ctx . Data [ "IsFollowing" ] = isFollowing
if setting . Service . EnableUserHeatmap {
if setting . Service . EnableUserHeatmap {
data , err := models . GetUserHeatmapDataByUser ( ctxUser , ctx . Doer )
data , err := models . GetUserHeatmapDataByUser ( ctx . Context User, ctx . Doer )
if err != nil {
if err != nil {
ctx . ServerError ( "GetUserHeatmapDataByUser" , err )
ctx . ServerError ( "GetUserHeatmapDataByUser" , err )
return
return
@ -146,13 +75,13 @@ func Profile(ctx *context.Context) {
ctx . Data [ "HeatmapData" ] = data
ctx . Data [ "HeatmapData" ] = data
}
}
if len ( ctxUser . Description ) != 0 {
if len ( ctx . Context User. Description ) != 0 {
content , err := markdown . RenderString ( & markup . RenderContext {
content , err := markdown . RenderString ( & markup . RenderContext {
URLPrefix : ctx . Repo . RepoLink ,
URLPrefix : ctx . Repo . RepoLink ,
Metas : map [ string ] string { "mode" : "document" } ,
Metas : map [ string ] string { "mode" : "document" } ,
GitRepo : ctx . Repo . GitRepo ,
GitRepo : ctx . Repo . GitRepo ,
Ctx : ctx ,
Ctx : ctx ,
} , ctxUser . Description )
} , ctx . Context User. Description )
if err != nil {
if err != nil {
ctx . ServerError ( "RenderString" , err )
ctx . ServerError ( "RenderString" , err )
return
return
@ -160,10 +89,10 @@ func Profile(ctx *context.Context) {
ctx . Data [ "RenderedDescription" ] = content
ctx . Data [ "RenderedDescription" ] = content
}
}
showPrivate := ctx . IsSigned && ( ctx . Doer . IsAdmin || ctx . Doer . ID == ctxUser . ID )
showPrivate := ctx . IsSigned && ( ctx . Doer . IsAdmin || ctx . Doer . ID == ctx . Context User. ID )
orgs , err := models . FindOrgs ( models . FindOrgOptions {
orgs , err := models . FindOrgs ( models . FindOrgOptions {
UserID : ctxUser . ID ,
UserID : ctx . Context User. ID ,
IncludePrivate : showPrivate ,
IncludePrivate : showPrivate ,
} )
} )
if err != nil {
if err != nil {
@ -226,7 +155,7 @@ func Profile(ctx *context.Context) {
switch tab {
switch tab {
case "followers" :
case "followers" :
items , err := user_model . GetUserFollowers ( ctxUser , db . ListOptions {
items , err := user_model . GetUserFollowers ( ctx . Context User, db . ListOptions {
PageSize : setting . UI . User . RepoPagingNum ,
PageSize : setting . UI . User . RepoPagingNum ,
Page : page ,
Page : page ,
} )
} )
@ -236,9 +165,9 @@ func Profile(ctx *context.Context) {
}
}
ctx . Data [ "Cards" ] = items
ctx . Data [ "Cards" ] = items
total = ctxUser . NumFollowers
total = ctx . Context User. NumFollowers
case "following" :
case "following" :
items , err := user_model . GetUserFollowing ( ctxUser , db . ListOptions {
items , err := user_model . GetUserFollowing ( ctx . Context User, db . ListOptions {
PageSize : setting . UI . User . RepoPagingNum ,
PageSize : setting . UI . User . RepoPagingNum ,
Page : page ,
Page : page ,
} )
} )
@ -248,10 +177,10 @@ func Profile(ctx *context.Context) {
}
}
ctx . Data [ "Cards" ] = items
ctx . Data [ "Cards" ] = items
total = ctxUser . NumFollowing
total = ctx . Context User. NumFollowing
case "activity" :
case "activity" :
ctx . Data [ "Feeds" ] , err = models . GetFeeds ( ctx , models . GetFeedsOptions {
ctx . Data [ "Feeds" ] , err = models . GetFeeds ( ctx , models . GetFeedsOptions {
RequestedUser : ctxUser ,
RequestedUser : ctx . Context User,
Actor : ctx . Doer ,
Actor : ctx . Doer ,
IncludePrivate : showPrivate ,
IncludePrivate : showPrivate ,
OnlyPerformedBy : true ,
OnlyPerformedBy : true ,
@ -273,7 +202,7 @@ func Profile(ctx *context.Context) {
Keyword : keyword ,
Keyword : keyword ,
OrderBy : orderBy ,
OrderBy : orderBy ,
Private : ctx . IsSigned ,
Private : ctx . IsSigned ,
StarredByID : ctxUser . ID ,
StarredByID : ctx . Context User. ID ,
Collaborate : util . OptionalBoolFalse ,
Collaborate : util . OptionalBoolFalse ,
TopicOnly : topicOnly ,
TopicOnly : topicOnly ,
Language : language ,
Language : language ,
@ -305,7 +234,7 @@ func Profile(ctx *context.Context) {
Keyword : keyword ,
Keyword : keyword ,
OrderBy : orderBy ,
OrderBy : orderBy ,
Private : ctx . IsSigned ,
Private : ctx . IsSigned ,
WatchedByID : ctxUser . ID ,
WatchedByID : ctx . Context User. ID ,
Collaborate : util . OptionalBoolFalse ,
Collaborate : util . OptionalBoolFalse ,
TopicOnly : topicOnly ,
TopicOnly : topicOnly ,
Language : language ,
Language : language ,
@ -325,7 +254,7 @@ func Profile(ctx *context.Context) {
} ,
} ,
Actor : ctx . Doer ,
Actor : ctx . Doer ,
Keyword : keyword ,
Keyword : keyword ,
OwnerID : ctxUser . ID ,
OwnerID : ctx . Context User. ID ,
OrderBy : orderBy ,
OrderBy : orderBy ,
Private : ctx . IsSigned ,
Private : ctx . IsSigned ,
Collaborate : util . OptionalBoolFalse ,
Collaborate : util . OptionalBoolFalse ,
@ -350,24 +279,19 @@ func Profile(ctx *context.Context) {
}
}
ctx . Data [ "Page" ] = pager
ctx . Data [ "Page" ] = pager
ctx . Data [ "ShowUserEmail" ] = len ( ctxUser . Email ) > 0 && ctx . IsSigned && ( ! ctxUser . KeepEmailPrivate || ctxUser . ID == ctx . Doer . ID )
ctx . Data [ "ShowUserEmail" ] = len ( ctx . Context User. Email ) > 0 && ctx . IsSigned && ( ! ctx . Context User. KeepEmailPrivate || ctx . Context User. ID == ctx . Doer . ID )
ctx . HTML ( http . StatusOK , tplProfile )
ctx . HTML ( http . StatusOK , tplProfile )
}
}
// Action response for follow/unfollow user request
// Action response for follow/unfollow user request
func Action ( ctx * context . Context ) {
func Action ( ctx * context . Context ) {
u := GetUserByParams ( ctx )
if ctx . Written ( ) {
return
}
var err error
var err error
switch ctx . FormString ( "action" ) {
switch ctx . FormString ( "action" ) {
case "follow" :
case "follow" :
err = user_model . FollowUser ( ctx . Doer . ID , u . ID )
err = user_model . FollowUser ( ctx . Doer . ID , ctx . ContextUser . ID )
case "unfollow" :
case "unfollow" :
err = user_model . UnfollowUser ( ctx . Doer . ID , u . ID )
err = user_model . UnfollowUser ( ctx . Doer . ID , ctx . ContextUser . ID )
}
}
if err != nil {
if err != nil {
@ -375,5 +299,5 @@ func Action(ctx *context.Context) {
return
return
}
}
// FIXME: We should check this URL and make sure that it's a valid Gitea URL
// FIXME: We should check this URL and make sure that it's a valid Gitea URL
ctx . RedirectToFirst ( ctx . FormString ( "redirect_to" ) , u . HomeLink ( ) )
ctx . RedirectToFirst ( ctx . FormString ( "redirect_to" ) , ctx . ContextUser . HomeLink ( ) )
}
}