Shorten things.

This commit is contained in:
Azareal 2019-12-07 16:27:01 +10:00
parent 0fa9a3fca1
commit 501c0dff0a
10 changed files with 45 additions and 46 deletions

View File

@ -6,7 +6,7 @@ import "github.com/Azareal/Gosora/query_gen"
var Activity ActivityStream var Activity ActivityStream
type ActivityStream interface { type ActivityStream interface {
Add(alert Alert) (int, error) Add(a Alert) (int, error)
Get(id int) (Alert, error) Get(id int) (Alert, error)
Count() (count int) Count() (count int)
} }

View File

@ -169,11 +169,9 @@ func (auth *DefaultAuth) ForceLogout(uid int) error {
} }
// Flush the user out of the cache // Flush the user out of the cache
ucache := Users.GetCache() if uc := Users.GetCache(); uc != nil {
if ucache != nil { uc.Remove(uid)
ucache.Remove(uid)
} }
return nil return nil
} }

View File

@ -36,7 +36,7 @@ func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
fp := "forums_permissions" fp := "forums_permissions"
return &MemoryForumPermsStore{ 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(), getByForumGroup: acc.Select(fp).Columns("permissions").Where("fid = ? AND gid = ?").Prepare(),
evenForums: make(map[int]map[int]*ForumPerms), evenForums: make(map[int]map[int]*ForumPerms),

View File

@ -48,12 +48,13 @@ type MemoryGroupStore struct {
func NewMemoryGroupStore() (*MemoryGroupStore, error) { func NewMemoryGroupStore() (*MemoryGroupStore, error) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
ug := "users_groups"
return &MemoryGroupStore{ return &MemoryGroupStore{
groups: make(map[int]*Group), groups: make(map[int]*Group),
groupCount: 0, groupCount: 0,
getAll: acc.Select("users_groups").Columns("gid, name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Prepare(), getAll: acc.Select(ug).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(), get: acc.Select(ug).Columns("name, permissions, plugin_perms, is_mod, is_admin, is_banned, tag").Where("gid = ?").Prepare(),
count: acc.Count("users_groups").Prepare(), count: acc.Count(ug).Prepare(),
userCount: acc.Count("users").Where("group = ?").Prepare(), userCount: acc.Count("users").Where("group = ?").Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }

View File

@ -7,7 +7,7 @@ import (
"log" "log"
"strconv" "strconv"
"github.com/Azareal/Gosora/query_gen" qgen "github.com/Azareal/Gosora/query_gen"
) )
var Polls PollStore var Polls PollStore
@ -60,14 +60,14 @@ type PollStore interface {
type DefaultPollStore struct { type DefaultPollStore struct {
cache PollCache cache PollCache
get *sql.Stmt get *sql.Stmt
exists *sql.Stmt exists *sql.Stmt
createPoll *sql.Stmt createPoll *sql.Stmt
createPollOption *sql.Stmt createPollOption *sql.Stmt
addVote *sql.Stmt addVote *sql.Stmt
incrementVoteCount *sql.Stmt incVoteCount *sql.Stmt
incrementVoteCountForOption *sql.Stmt incVoteCountForOption *sql.Stmt
delete *sql.Stmt delete *sql.Stmt
//count *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? // TODO: Add an admin version of registerStmt with more flexibility?
return &DefaultPollStore{ return &DefaultPollStore{
cache: cache, cache: cache,
get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(), get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(),
exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(), exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(),
createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(), createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(),
createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").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(), 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(), incVoteCount: acc.Update("polls").Set("votes = votes + 1").Where("pollID = ?").Prepare(),
incrementVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option = ? AND pollID = ?").Prepare(), incVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option = ? AND pollID = ?").Prepare(),
//count: acc.SimpleCount("polls", "", ""), //count: acc.SimpleCount("polls", "", ""),
}, acc.FirstError() }, 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 // TODO: Add a function for the qlist stuff
var q string var q string
idList := make([]interface{},len(ids)) idList := make([]interface{}, len(ids))
for i, id := range ids { for i, id := range ids {
idList[i] = strconv.Itoa(id) idList[i] = strconv.Itoa(id)
q += "?," q += "?,"
@ -239,11 +239,11 @@ func (s *DefaultPollStore) CastVote(optionIndex int, pollID int, uid int, ipaddr
if err != nil { if err != nil {
return err return err
} }
_, err = s.incrementVoteCount.Exec(pollID) _, err = s.incVoteCount.Exec(pollID)
if err != nil { if err != nil {
return err return err
} }
_, err = s.incrementVoteCountForOption.Exec(optionIndex, pollID) _, err = s.incVoteCountForOption.Exec(optionIndex, pollID)
return err return err
} }
@ -269,7 +269,7 @@ func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map
return 0, err return 0, err
} }
} }
id = int(lastID) id = int(lastID)
return id, parent.SetPoll(id) // TODO: Delete the poll (and options) if SetPoll fails return id, parent.SetPoll(id) // TODO: Delete the poll (and options) if SetPoll fails
} }

View File

@ -232,8 +232,8 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
return header, nil return header, nil
} }
func PrepResources(user *User, header *Header, theme *Theme) { func PrepResources(user *User, h *Header, theme *Theme) {
header.AddSheet(theme.Name + "/main.css") h.AddSheet(theme.Name + "/main.css")
if len(theme.Resources) > 0 { if len(theme.Resources) > 0 {
rlist := theme.Resources rlist := theme.Resources
@ -245,12 +245,12 @@ func PrepResources(user *User, header *Header, theme *Theme) {
extarr := strings.Split(resource.Name, ".") extarr := strings.Split(resource.Name, ".")
ext := extarr[len(extarr)-1] ext := extarr[len(extarr)-1]
if ext == "css" { if ext == "css" {
header.AddSheet(resource.Name) h.AddSheet(resource.Name)
} else if ext == "js" { } else if ext == "js" {
if resource.Async { if resource.Async {
header.AddScriptAsync(resource.Name) h.AddScriptAsync(resource.Name)
} else { } 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) //fmt.Printf("tname %+v\n", tname)
header.AddPreScriptAsync("template_" + name + tname + ".js") h.AddPreScriptAsync("template_" + name + tname + ".js")
} }
addPreScript("topics_topic") addPreScript("topics_topic")
addPreScript("paginator") addPreScript("paginator")

View File

@ -24,17 +24,17 @@ func ThumbTask(thumbChan chan bool) {
acc := qgen.NewAcc() acc := qgen.NewAcc()
err := acc.Select("users_avatar_queue").Columns("uid").Limit("0,5").EachInt(func(uid int) error { err := acc.Select("users_avatar_queue").Columns("uid").Limit("0,5").EachInt(func(uid int) error {
// TODO: Do a bulk user fetch instead? // TODO: Do a bulk user fetch instead?
user, err := Users.Get(uid) u, err := Users.Get(uid)
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
// Has the avatar been removed or already been processed by the thumbnailer? // 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) _, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid)
return nil 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) { if os.IsNotExist(err) {
_, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid) _, _ = acc.Delete("users_avatar_queue").Where("uid = ?").Run(uid)
return nil 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 // 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 return nil
} }
/*if user.RawAvatar == ".gif" { /*if user.RawAvatar == ".gif" {
return nil 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 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 { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
err = user.ChangeAvatar("." + user.RawAvatar) err = u.ChangeAvatar("." + u.RawAvatar)
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }

View File

@ -28,7 +28,7 @@ type WordFilterStore interface {
Get(id int) (*WordFilter, error) Get(id int) (*WordFilter, error)
Create(find string, replace string) (int, error) Create(find string, replace string) (int, error)
Delete(id int) error Delete(id int) error
Update(id int, find string, replacement string) error Update(id int, find string, replace string) error
Length() int Length() int
EstCount() int EstCount() int
Count() (count int) Count() (count int)

View File

@ -13,7 +13,6 @@ func ForumList(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
if skip || rerr != nil { if skip || rerr != nil {
return rerr return rerr
} }
header.Title = phrases.GetTitlePhrase("forums") header.Title = phrases.GetTitlePhrase("forums")
header.Zone = "forums" header.Zone = "forums"
header.Path = "/forums/" header.Path = "/forums/"

View File

@ -14,7 +14,7 @@ func ReportSubmit(w http.ResponseWriter, r *http.Request, user c.User, sitemID s
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
js := (r.PostFormValue("js") == "1") js := r.PostFormValue("js") == "1"
itemID, err := strconv.Atoi(sitemID) itemID, err := strconv.Atoi(sitemID)
if err != nil { if err != nil {