From 06deed820d9f48ecf972c8716b17ad58b91ab9b9 Mon Sep 17 00:00:00 2001 From: slene Date: Sat, 15 Mar 2014 15:28:06 +0800 Subject: [PATCH] Add HomeLink and AvatarLink to User model. Please use .SignedUser in template. --- models/user.go | 8 ++++++ modules/auth/user.go | 12 ++++++-- modules/base/tool.go | 56 +++++++++++++++++++++++++++++++++++++ public/css/gogs.css | 32 ++++++++++++++++++++- routers/user/user.go | 23 +++++++++++---- templates/base/navbar.tmpl | 4 +-- templates/repo/nav.tmpl | 2 +- templates/user/profile.tmpl | 33 ++++++++++++++-------- web.go | 1 + 9 files changed, 147 insertions(+), 24 deletions(-) diff --git a/models/user.go b/models/user.go index 8f7a37cba9..a0e4bb8004 100644 --- a/models/user.go +++ b/models/user.go @@ -56,6 +56,14 @@ type User struct { Updated time.Time `xorm:"updated"` } +func (user *User) HomeLink() string { + return "/user/" + user.LowerName +} + +func (user *User) AvatarLink() string { + return "http://1.gravatar.com/avatar/" + user.Avatar +} + // A Follow represents type Follow struct { Id int64 diff --git a/modules/auth/user.go b/modules/auth/user.go index ef595c6000..d950b25002 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -74,10 +74,16 @@ func SignInRequire(redirect bool) martini.Handler { return } + user := SignedInUser(session) + if user == nil { + r.Redirect("/") + return + } + data["IsSigned"] = true - data["SignedUserId"] = SignedInId(session) - data["SignedUserName"] = SignedInName(session) - data["SignedAvatar"] = SignedInUser(session).Avatar + data["SignedUser"] = user + data["SignedUserId"] = user.Id + data["SignedUserName"] = user.LowerName } } diff --git a/modules/base/tool.go b/modules/base/tool.go index 49a2a2b5d1..4b84ac0bdd 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -9,6 +9,7 @@ import ( "encoding/hex" "fmt" "html/template" + "strings" "time" ) @@ -134,6 +135,61 @@ func Subtract(left interface{}, right interface{}) interface{} { } } +// DateFormat pattern rules. +var datePatterns = []string{ + // year + "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 + "y", "06", //A two digit representation of a year Examples: 99 or 03 + + // month + "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 + "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 + "M", "Jan", // A short textual representation of a month, three letters Jan through Dec + "F", "January", // A full textual representation of a month, such as January or March January through December + + // day + "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 + "j", "2", // Day of the month without leading zeros 1 to 31 + + // week + "D", "Mon", // A textual representation of a day, three letters Mon through Sun + "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday + + // time + "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 + "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 + "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 + "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 + + "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm + "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM + + "i", "04", // Minutes with leading zeros 00 to 59 + "s", "05", // Seconds, with leading zeros 00 through 59 + + // time zone + "T", "MST", + "P", "-07:00", + "O", "-0700", + + // RFC 2822 + "r", time.RFC1123Z, +} + +// Parse Date use PHP time format. +func DateParse(dateString, format string) (time.Time, error) { + replacer := strings.NewReplacer(datePatterns...) + format = replacer.Replace(format) + return time.ParseInLocation(format, dateString, time.Local) +} + +// Date takes a PHP like date func to Go's time format. +func DateFormat(t time.Time, format string) string { + replacer := strings.NewReplacer(datePatterns...) + format = replacer.Replace(format) + return t.Format(format) +} + type Actioner interface { GetOpType() int GetActUserName() string diff --git a/public/css/gogs.css b/public/css/gogs.css index 160f561ca2..e7730b2d41 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -520,4 +520,34 @@ body { #gogs-source-table.table-hover > tbody > tr:hover > td { background-color: #FEFEFE; -} \ No newline at end of file +} + +.activity-list { + font-size: 14px; +} + +.activity-list .icon { + font-size: 20px; + color: #aaa; + float: left; +} + +.activity-list .info { + float: left; + padding:0 0 0 10px; + line-height: 1.7em; +} + +.activity-list .meta { + color: #aaa; +} + +.activity-list li { + padding: 15px 0; + border-top: 1px solid #ddd; +} + +.activity-list li:first-child { + border-top: none; +} + diff --git a/routers/user/user.go b/routers/user/user.go index 4dd9abe52a..680055f661 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -29,7 +29,7 @@ func Dashboard(r render.Render, data base.TmplData, session sessions.Session) { r.HTML(200, "user/dashboard", data) } -func Profile(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { +func Profile(params martini.Params, r render.Render, req *http.Request, data base.TmplData, session sessions.Session) { data["Title"] = "Profile" // TODO: Need to check view self or others. @@ -40,12 +40,23 @@ func Profile(params martini.Params, r render.Render, data base.TmplData, session } data["Owner"] = user - feeds, err := models.GetFeeds(user.Id, 0, true) - if err != nil { - log.Handle(200, "user.Profile", data, r, err) - return + + req.ParseForm() + tab := req.Form.Get("tab") + data["TabName"] = tab + + switch tab { + case "activity": + feeds, err := models.GetFeeds(user.Id, 0, true) + if err != nil { + log.Handle(200, "user.Profile", data, r, err) + return + } + data["Feeds"] = feeds + default: + } - data["Feeds"] = feeds + r.HTML(200, "user/profile", data) } diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 38fc0a3992..9d5cc07bd3 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -6,8 +6,8 @@ Explore Help{{if .IsSigned}} - - user-avatar + + user-avatar diff --git a/templates/repo/nav.tmpl b/templates/repo/nav.tmpl index 1a9434e860..6637062a84 100644 --- a/templates/repo/nav.tmpl +++ b/templates/repo/nav.tmpl @@ -37,6 +37,6 @@ -

{{.Owner.Name}} / {{.Repository.Name}}

+

{{.Owner.Name}} / {{.Repository.Name}}

\ No newline at end of file diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index b04ddbbed7..fd4313efdc 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -3,10 +3,10 @@
- - user-avatar + + user-avatar - {{.Owner.Name}} + {{.Owner.Name}}
    @@ -19,22 +19,33 @@ {{if .Owner.Website}}
  • {{.Owner.Website}}
  • {{end}} -
  • {{.Owner.Created}}
  • +
  • Joined on {{DateFormat .Owner.Created "M d, Y"}}
-
repo
-
- {{range .Feeds}}
- {{ActionDesc . | str2html}} {{TimeSince .Created}} -
{{end}} + {{if eq .TabName "activity"}} +
+
    + {{range .Feeds}} +
  • + +
    {{TimeSince .Created}}
    {{ActionDesc . | str2html}}
    + +
  • + {{else}} +
  • Not found any activity
  • + {{end}} +
+ {{else}} +
repo
+ {{end}}
diff --git a/web.go b/web.go index 60992f4541..cb6ee4aece 100644 --- a/web.go +++ b/web.go @@ -45,6 +45,7 @@ var AppHelpers template.FuncMap = map[string]interface{}{ "Subtract": base.Subtract, "ActionIcon": base.ActionIcon, "ActionDesc": base.ActionDesc, + "DateFormat": base.DateFormat, } func runWeb(*cli.Context) {