2018-02-26 09:07:00 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2019-10-18 00:35:13 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2018-02-26 09:07:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProfileStmts struct {
|
|
|
|
getReplies *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
var profileStmts ProfileStmts
|
|
|
|
|
|
|
|
// TODO: Move these DbInits into some sort of abstraction
|
|
|
|
func init() {
|
2019-04-19 06:36:26 +00:00
|
|
|
c.DbInits.Add(func(acc *qgen.Accumulator) error {
|
2018-02-26 09:07:00 +00:00
|
|
|
profileStmts = ProfileStmts{
|
|
|
|
getReplies: acc.SimpleLeftJoin("users_replies", "users", "users_replies.rid, users_replies.content, users_replies.createdBy, users_replies.createdAt, users_replies.lastEdit, users_replies.lastEditBy, users.avatar, users.name, users.group", "users_replies.createdBy = users.uid", "users_replies.uid = ?", "", ""),
|
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-14 05:08:44 +00:00
|
|
|
// TODO: Remove the View part of the name?
|
2019-04-19 06:36:26 +00:00
|
|
|
func ViewProfile(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
2018-02-26 09:07:00 +00:00
|
|
|
var err error
|
|
|
|
var replyCreatedAt time.Time
|
2019-05-17 08:40:41 +00:00
|
|
|
var replyContent, replyCreatedByName, replyAvatar string
|
|
|
|
var rid, replyCreatedBy, replyLastEdit, replyLastEditBy, replyGroup int
|
|
|
|
var replyList []*c.ReplyUser
|
2018-02-26 09:07:00 +00:00
|
|
|
|
|
|
|
// TODO: Do a 301 if it's the wrong username? Do a canonical too?
|
2018-11-12 09:23:36 +00:00
|
|
|
_, pid, err := ParseSEOURL(r.URL.Path[len("/user/"):])
|
2018-02-26 09:07:00 +00:00
|
|
|
if err != nil {
|
2019-10-18 00:35:13 +00:00
|
|
|
return c.SimpleError(phrases.GetErrorPhrase("url_id_must_be_integer"), w, r, header)
|
2019-07-28 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Preload this?
|
|
|
|
header.AddSheet(header.Theme.Name + "/profile.css")
|
|
|
|
if user.Loggedin {
|
|
|
|
header.AddScriptAsync("profile_member.js")
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
var puser *c.User
|
2018-02-26 09:07:00 +00:00
|
|
|
if pid == user.ID {
|
|
|
|
user.IsMod = true
|
|
|
|
puser = &user
|
|
|
|
} else {
|
|
|
|
// Fetch the user data
|
|
|
|
// TODO: Add a shared function for checking for ErrNoRows and internal erroring if it's not that case?
|
2019-04-19 06:36:26 +00:00
|
|
|
puser, err = c.Users.Get(pid)
|
2018-02-26 09:07:00 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, header)
|
2018-02-26 09:07:00 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
header.Title = phrases.GetTitlePhrasef("profile", puser.Name)
|
2019-04-19 06:36:26 +00:00
|
|
|
header.Path = c.BuildProfileURL(c.NameToSlug(puser.Name), puser.ID)
|
2018-02-26 09:07:00 +00:00
|
|
|
|
|
|
|
// Get the replies..
|
|
|
|
rows, err := profileStmts.getReplies.Query(puser.ID)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&rid, &replyContent, &replyCreatedBy, &replyCreatedAt, &replyLastEdit, &replyLastEditBy, &replyAvatar, &replyCreatedByName, &replyGroup)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
replyLiked := false
|
|
|
|
replyLikeCount := 0
|
2019-05-19 01:01:11 +00:00
|
|
|
ru := &c.ReplyUser{Reply: c.Reply{rid, puser.ID, replyContent, replyCreatedBy, replyGroup, replyCreatedAt, replyLastEdit, replyLastEditBy, 0, "", replyLiked, replyLikeCount, 0, ""}, ContentHtml: c.ParseMessage(replyContent, 0, ""), CreatedByName: replyCreatedByName, Avatar: replyAvatar, Level: 0}
|
|
|
|
ru.Init()
|
2019-05-17 08:40:41 +00:00
|
|
|
|
|
|
|
group, err := c.Groups.Get(ru.Group)
|
2018-02-26 09:07:00 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
if group.Tag != "" {
|
2019-05-17 08:40:41 +00:00
|
|
|
ru.Tag = group.Tag
|
|
|
|
} else if puser.ID == ru.CreatedBy {
|
|
|
|
ru.Tag = phrases.GetTmplPhrase("profile_owner_tag")
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add a hook here
|
2019-05-17 08:40:41 +00:00
|
|
|
replyList = append(replyList, ru)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
2019-10-18 00:35:13 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 05:34:25 +00:00
|
|
|
// Normalise the score so that the user sees their relative progress to the next level rather than showing them their total score
|
2019-04-19 06:36:26 +00:00
|
|
|
prevScore := c.GetLevelScore(puser.Level)
|
2018-10-08 05:34:25 +00:00
|
|
|
currentScore := puser.Score - prevScore
|
2019-04-19 06:36:26 +00:00
|
|
|
nextScore := c.GetLevelScore(puser.Level+1) - prevScore
|
2019-10-20 01:42:19 +00:00
|
|
|
var blocked, blockedInv bool
|
|
|
|
if user.Loggedin {
|
|
|
|
blocked, err = c.UserBlocks.IsBlockedBy(user.ID, puser.ID)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
blockedInv, err = c.UserBlocks.IsBlockedBy(puser.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2019-10-19 10:33:59 +00:00
|
|
|
}
|
|
|
|
canMessage := (!blockedInv && user.Perms.UseConvos) || user.IsSuperMod
|
|
|
|
canComment := !blockedInv && user.Perms.ViewTopic && user.Perms.CreateReply
|
|
|
|
|
|
|
|
ppage := c.ProfilePage{header, replyList, *puser, currentScore, nextScore, blocked, canMessage, canComment}
|
2018-11-12 09:23:36 +00:00
|
|
|
return renderTemplate("profile", w, r, header, ppage)
|
2018-02-26 09:07:00 +00:00
|
|
|
}
|