gosora/routes/profile.go
Azareal 8ecc637ab9 Moved routeProfile to routes.ViewProfile.
Requests are now logged as one big chunk to help prevent them getting torn apart and improve log throughput.
Newlines are now stripped from dumped requests.
Prepared statement logging now only happens in debug mode for speedier server startups.
More suspicious request tracking is done outside of debug mode now.
Only log the initial message in plugin_markdown in debug mode.
Moved the CreateTopicSubmit attachment logging to super debug mode.
Removed a couple of unneccesary menu items from the Account Manager menu.
Hid the add friend option on the profiles for now.
Moved the remaining analytics results loops into a common function.
Only log the graph for the analytics routes in debug mode.

Every Theme:
Added CSS for the Don't have an Account link on the Login Page.

Cosora:
Fixed the header CSS on mobile.
The header CSS now covers optheads on mobile.
Revamped the profile comment UI.

Shadow:
Added CSS for sticky topics.

Tempra Simple:
Button CSS now applies to type=submit inputs.
Fixed the IP Search CSS.

Tempra Conflux:
Button CSS now applies to type=submit inputs.
Fixed the IP Search CSS.

Tempra Cursive (Deprecated):
Button CSS now applies to type=submit inputs.
2018-02-26 09:07:00 +00:00

127 lines
3.7 KiB
Go

package routes
import (
"database/sql"
"net/http"
"strconv"
"strings"
"time"
"../common"
"../query_gen/lib"
)
type ProfileStmts struct {
getReplies *sql.Stmt
}
var profileStmts ProfileStmts
// TODO: Move these DbInits into some sort of abstraction
func init() {
common.DbInits.Add(func(acc *qgen.Accumulator) error {
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()
})
}
func ViewProfile(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError {
headerVars, ferr := common.UserCheck(w, r, &user)
if ferr != nil {
return ferr
}
var err error
var replyCreatedAt time.Time
var replyContent, replyCreatedByName, replyRelativeCreatedAt, replyAvatar, replyTag, replyClassName string
var rid, replyCreatedBy, replyLastEdit, replyLastEditBy, replyLines, replyGroup int
var replyList []common.ReplyUser
// SEO URLs...
// TODO: Do a 301 if it's the wrong username? Do a canonical too?
halves := strings.Split(r.URL.Path[len("/user/"):], ".")
if len(halves) < 2 {
halves = append(halves, halves[0])
}
pid, err := strconv.Atoi(halves[1])
if err != nil {
return common.LocalError("The provided UserID is not a valid number.", w, r, user)
}
var puser *common.User
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?
puser, err = common.Users.Get(pid)
if err == sql.ErrNoRows {
return common.NotFound(w, r, headerVars)
} else if err != nil {
return common.InternalError(err, w, r)
}
}
// Get the replies..
rows, err := profileStmts.getReplies.Query(puser.ID)
if err != nil {
return common.InternalError(err, w, r)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&rid, &replyContent, &replyCreatedBy, &replyCreatedAt, &replyLastEdit, &replyLastEditBy, &replyAvatar, &replyCreatedByName, &replyGroup)
if err != nil {
return common.InternalError(err, w, r)
}
group, err := common.Groups.Get(replyGroup)
if err != nil {
return common.InternalError(err, w, r)
}
replyLines = strings.Count(replyContent, "\n")
if group.IsMod || group.IsAdmin {
replyClassName = common.Config.StaffCSS
} else {
replyClassName = ""
}
replyAvatar = common.BuildAvatar(replyCreatedBy, replyAvatar)
if group.Tag != "" {
replyTag = group.Tag
} else if puser.ID == replyCreatedBy {
replyTag = "Profile Owner"
} else {
replyTag = ""
}
replyLiked := false
replyLikeCount := 0
replyRelativeCreatedAt = common.RelativeTime(replyCreatedAt)
// TODO: Add a hook here
replyList = append(replyList, common.ReplyUser{rid, puser.ID, replyContent, common.ParseMessage(replyContent, 0, ""), replyCreatedBy, common.BuildProfileURL(common.NameToSlug(replyCreatedByName), replyCreatedBy), replyCreatedByName, replyGroup, replyCreatedAt, replyRelativeCreatedAt, replyLastEdit, replyLastEditBy, replyAvatar, replyClassName, replyLines, replyTag, "", "", "", 0, "", replyLiked, replyLikeCount, "", ""})
}
err = rows.Err()
if err != nil {
return common.InternalError(err, w, r)
}
// TODO: Add a phrase for this title
ppage := common.ProfilePage{puser.Name + "'s Profile", user, headerVars, replyList, *puser}
if common.RunPreRenderHook("pre_render_profile", w, r, &user, &ppage) {
return nil
}
err = common.RunThemeTemplate(headerVars.Theme.Name, "profile", ppage, w)
if err != nil {
return common.InternalError(err, w, r)
}
return nil
}