From 501c0dff0a3f1a399714a232ec1fbb685befef2a Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 7 Dec 2019 16:27:01 +1000 Subject: [PATCH] Shorten things. --- common/activity_stream.go | 2 +- common/auth.go | 6 ++---- common/forum_perms_store.go | 2 +- common/group_store.go | 7 ++++--- common/poll_store.go | 42 ++++++++++++++++++------------------- common/routes_common.go | 12 +++++------ common/thumbnailer.go | 15 ++++++------- common/word_filters.go | 2 +- routes/forum_list.go | 1 - routes/reports.go | 2 +- 10 files changed, 45 insertions(+), 46 deletions(-) diff --git a/common/activity_stream.go b/common/activity_stream.go index 5003ac9e..5b4ab188 100644 --- a/common/activity_stream.go +++ b/common/activity_stream.go @@ -6,7 +6,7 @@ import "github.com/Azareal/Gosora/query_gen" var Activity ActivityStream type ActivityStream interface { - Add(alert Alert) (int, error) + Add(a Alert) (int, error) Get(id int) (Alert, error) Count() (count int) } diff --git a/common/auth.go b/common/auth.go index 948201b8..3c5b8e2a 100644 --- a/common/auth.go +++ b/common/auth.go @@ -169,11 +169,9 @@ func (auth *DefaultAuth) ForceLogout(uid int) error { } // Flush the user out of the cache - ucache := Users.GetCache() - if ucache != nil { - ucache.Remove(uid) + if uc := Users.GetCache(); uc != nil { + uc.Remove(uid) } - return nil } diff --git a/common/forum_perms_store.go b/common/forum_perms_store.go index d9e16621..14e2b226 100644 --- a/common/forum_perms_store.go +++ b/common/forum_perms_store.go @@ -36,7 +36,7 @@ func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) { acc := qgen.NewAcc() fp := "forums_permissions" return &MemoryForumPermsStore{ - getByForum: acc.Select(fp).Columns("gid, permissions").Where("fid = ?").Orderby("gid ASC").Prepare(), + getByForum: acc.Select(fp).Columns("gid,permissions").Where("fid = ?").Orderby("gid ASC").Prepare(), getByForumGroup: acc.Select(fp).Columns("permissions").Where("fid = ? AND gid = ?").Prepare(), evenForums: make(map[int]map[int]*ForumPerms), diff --git a/common/group_store.go b/common/group_store.go index 71f5a753..6fd4ebd3 100644 --- a/common/group_store.go +++ b/common/group_store.go @@ -48,12 +48,13 @@ type MemoryGroupStore struct { func NewMemoryGroupStore() (*MemoryGroupStore, error) { acc := qgen.NewAcc() + ug := "users_groups" return &MemoryGroupStore{ groups: make(map[int]*Group), groupCount: 0, - getAll: acc.Select("users_groups").Columns("gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Prepare(), - get: acc.Select("users_groups").Columns("name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Where("gid = ?").Prepare(), - count: acc.Count("users_groups").Prepare(), + getAll: acc.Select(ug).Columns("gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Prepare(), + get: acc.Select(ug).Columns("name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Where("gid = ?").Prepare(), + count: acc.Count(ug).Prepare(), userCount: acc.Count("users").Where("group = ?").Prepare(), }, acc.FirstError() } diff --git a/common/poll_store.go b/common/poll_store.go index 8e02d08a..5e4feec1 100644 --- a/common/poll_store.go +++ b/common/poll_store.go @@ -7,7 +7,7 @@ import ( "log" "strconv" - "github.com/Azareal/Gosora/query_gen" + qgen "github.com/Azareal/Gosora/query_gen" ) var Polls PollStore @@ -60,14 +60,14 @@ type PollStore interface { type DefaultPollStore struct { cache PollCache - get *sql.Stmt - exists *sql.Stmt - createPoll *sql.Stmt - createPollOption *sql.Stmt - addVote *sql.Stmt - incrementVoteCount *sql.Stmt - incrementVoteCountForOption *sql.Stmt - delete *sql.Stmt + get *sql.Stmt + exists *sql.Stmt + createPoll *sql.Stmt + createPollOption *sql.Stmt + addVote *sql.Stmt + incVoteCount *sql.Stmt + incVoteCountForOption *sql.Stmt + delete *sql.Stmt //count *sql.Stmt } @@ -78,14 +78,14 @@ func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) { } // TODO: Add an admin version of registerStmt with more flexibility? return &DefaultPollStore{ - cache: cache, - get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(), - exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(), - createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(), - createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(), - addVote: acc.Insert("polls_votes").Columns("pollID, uid, option, castAt, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(), - incrementVoteCount: acc.Update("polls").Set("votes = votes + 1").Where("pollID = ?").Prepare(), - incrementVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option = ? AND pollID = ?").Prepare(), + cache: cache, + get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(), + exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(), + createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(), + createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(), + addVote: acc.Insert("polls_votes").Columns("pollID, uid, option, castAt, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(), + incVoteCount: acc.Update("polls").Set("votes = votes + 1").Where("pollID = ?").Prepare(), + incVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option = ? AND pollID = ?").Prepare(), //count: acc.SimpleCount("polls", "", ""), }, acc.FirstError() } @@ -146,7 +146,7 @@ func (s *DefaultPollStore) BulkGetMap(ids []int) (list map[int]*Poll, err error) // TODO: Add a function for the qlist stuff var q string - idList := make([]interface{},len(ids)) + idList := make([]interface{}, len(ids)) for i, id := range ids { idList[i] = strconv.Itoa(id) q += "?," @@ -239,11 +239,11 @@ func (s *DefaultPollStore) CastVote(optionIndex int, pollID int, uid int, ipaddr if err != nil { return err } - _, err = s.incrementVoteCount.Exec(pollID) + _, err = s.incVoteCount.Exec(pollID) if err != nil { return err } - _, err = s.incrementVoteCountForOption.Exec(optionIndex, pollID) + _, err = s.incVoteCountForOption.Exec(optionIndex, pollID) return err } @@ -269,7 +269,7 @@ func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map return 0, err } } - + id = int(lastID) return id, parent.SetPoll(id) // TODO: Delete the poll (and options) if SetPoll fails } diff --git a/common/routes_common.go b/common/routes_common.go index 516c6552..27003863 100644 --- a/common/routes_common.go +++ b/common/routes_common.go @@ -232,8 +232,8 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head return header, nil } -func PrepResources(user *User, header *Header, theme *Theme) { - header.AddSheet(theme.Name + "/main.css") +func PrepResources(user *User, h *Header, theme *Theme) { + h.AddSheet(theme.Name + "/main.css") if len(theme.Resources) > 0 { rlist := theme.Resources @@ -245,12 +245,12 @@ func PrepResources(user *User, header *Header, theme *Theme) { extarr := strings.Split(resource.Name, ".") ext := extarr[len(extarr)-1] if ext == "css" { - header.AddSheet(resource.Name) + h.AddSheet(resource.Name) } else if ext == "js" { if resource.Async { - header.AddScriptAsync(resource.Name) + h.AddScriptAsync(resource.Name) } else { - header.AddScript(resource.Name) + h.AddScript(resource.Name) } } } @@ -269,7 +269,7 @@ func PrepResources(user *User, header *Header, theme *Theme) { } } //fmt.Printf("tname %+v\n", tname) - header.AddPreScriptAsync("template_" + name + tname + ".js") + h.AddPreScriptAsync("template_" + name + tname + ".js") } addPreScript("topics_topic") addPreScript("paginator") diff --git a/common/thumbnailer.go b/common/thumbnailer.go index 1fa4b8ae..b2b64e96 100644 --- a/common/thumbnailer.go +++ b/common/thumbnailer.go @@ -24,17 +24,17 @@ func ThumbTask(thumbChan chan bool) { acc := qgen.NewAcc() err := acc.Select("users_avatar_queue").Columns("uid").Limit("0,5").EachInt(func(uid int) error { // TODO: Do a bulk user fetch instead? - user, err := Users.Get(uid) + u, err := Users.Get(uid) if err != nil { return errors.WithStack(err) } // Has the avatar been removed or already been processed by the thumbnailer? - if len(user.RawAvatar) < 2 || user.RawAvatar[1] == '.' { + if len(u.RawAvatar) < 2 || u.RawAvatar[1] == '.' { _, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid) return nil } - _, err = os.Stat("./uploads/avatar_" + strconv.Itoa(user.ID) + user.RawAvatar) + _, err = os.Stat("./uploads/avatar_" + strconv.Itoa(u.ID) + u.RawAvatar) if os.IsNotExist(err) { _, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid) return nil @@ -43,22 +43,23 @@ func ThumbTask(thumbChan chan bool) { } // This means it's an external image, they aren't currently implemented, but this is here for when they are - if user.RawAvatar[0] != '.' { + if u.RawAvatar[0] != '.' { return nil } /*if user.RawAvatar == ".gif" { return nil }*/ - if user.RawAvatar != ".png" && user.RawAvatar != ".jpg" && user.RawAvatar != ".jpe" && user.RawAvatar != ".jpeg" && user.RawAvatar != ".jif" && user.RawAvatar != ".jfi" && user.RawAvatar != ".jfif" && user.RawAvatar != ".gif" && user.RawAvatar != "tiff" && user.RawAvatar != "tif" { + if u.RawAvatar != ".png" && u.RawAvatar != ".jpg" && u.RawAvatar != ".jpe" && u.RawAvatar != ".jpeg" && u.RawAvatar != ".jif" && u.RawAvatar != ".jfi" && u.RawAvatar != ".jfif" && u.RawAvatar != ".gif" && u.RawAvatar != "tiff" && u.RawAvatar != "tif" { return nil } - err = Thumbnailer.Resize(user.RawAvatar[1:], "./uploads/avatar_"+strconv.Itoa(user.ID)+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_tmp"+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_w48"+user.RawAvatar, 48) + ap := "./uploads/avatar_" + err = Thumbnailer.Resize(u.RawAvatar[1:], ap+strconv.Itoa(u.ID)+u.RawAvatar, ap+strconv.Itoa(u.ID)+"_tmp"+u.RawAvatar, ap+strconv.Itoa(u.ID)+"_w48"+u.RawAvatar, 48) if err != nil { return errors.WithStack(err) } - err = user.ChangeAvatar("." + user.RawAvatar) + err = u.ChangeAvatar("." + u.RawAvatar) if err != nil { return errors.WithStack(err) } diff --git a/common/word_filters.go b/common/word_filters.go index 1d27cee7..1fe2f3fb 100644 --- a/common/word_filters.go +++ b/common/word_filters.go @@ -28,7 +28,7 @@ type WordFilterStore interface { Get(id int) (*WordFilter, error) Create(find string, replace string) (int, error) Delete(id int) error - Update(id int, find string, replacement string) error + Update(id int, find string, replace string) error Length() int EstCount() int Count() (count int) diff --git a/routes/forum_list.go b/routes/forum_list.go index e4e0e4e7..83b618ab 100644 --- a/routes/forum_list.go +++ b/routes/forum_list.go @@ -13,7 +13,6 @@ func ForumList(w http.ResponseWriter, r *http.Request, user c.User, header *c.He if skip || rerr != nil { return rerr } - header.Title = phrases.GetTitlePhrase("forums") header.Zone = "forums" header.Path = "/forums/" diff --git a/routes/reports.go b/routes/reports.go index 05a0d3a7..ed3d0dd4 100644 --- a/routes/reports.go +++ b/routes/reports.go @@ -14,7 +14,7 @@ func ReportSubmit(w http.ResponseWriter, r *http.Request, user c.User, sitemID s if ferr != nil { return ferr } - js := (r.PostFormValue("js") == "1") + js := r.PostFormValue("js") == "1" itemID, err := strconv.Atoi(sitemID) if err != nil {