3465e4c08f
The update system now uses the database as the source of truth for the last version rather than lastSchema.json Refactored several structs and bits of code, so we can avoid allocations for contexts where we never use a relative time. Clicking on the relative times on the topic list and the forum page should now take you to the post on the last page rather than just the last page. Added the reltime template function. Fixed some obsolete bits of code. Fixed some spelling mistakes. Fixed a bug where MaxBytesReader was capped at the maxFileSize rather than r.ContentLength. All of the client side templates should work again now. Shortened some statement names to save some horizontal space. accUpdateBuilder and SimpleUpdate now use updatePrebuilder behind the scenes to simplify things. Renamed selectItem to builder in AccSelectBuilder. Added a Total() method to accCountBuilder to reduce the amount of boilerplate used for row count queries. The "_builder" strings have been replaced with empty strings to help save memory, to make things slightly faster and to open the door to removing the query name in many contexts down the line. Added the open_edit and close_edit client hooks. Removed many query name checks. Split the attachment logic into separate functions and de-duplicated it between replies and topics. Improved the UI for editing topics in Nox. Used type aliases to reduce the amount of boilerplate in tables.go and patches.go Reduced the amount of boilerplate in the action post logic. Eliminated a map and a slice in the topic page for users who haven't given any likes. E.g. Guests. Fixed some long out-dated parts of the update instructions. Updated the update instructions to remove mention of the obsolete lastSchema.json Fixed a bug in init.js where /api/me was being loaded for guests. Added the MiniTopicGet, GlobalCount and CountInTopic methods to AttachmentStore. Added the MiniAttachment struct. Split the mod floaters out into their own template to reduce duplication. Removed a couple of redundant ParseForms. Added the common.skipUntilIfExistsOrLine function. Added the NotFoundJS and NotFoundJSQ functions. Added the lastReplyID and attachCount columns to the topics table.
160 lines
4.1 KiB
Go
160 lines
4.1 KiB
Go
/*
|
|
*
|
|
* Reply Resources File
|
|
* Copyright Azareal 2016 - 2018
|
|
*
|
|
*/
|
|
package common
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"html"
|
|
"time"
|
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
|
)
|
|
|
|
type ReplyUser struct {
|
|
ID int
|
|
ParentID int
|
|
Content string
|
|
ContentHtml string
|
|
CreatedBy int
|
|
UserLink string
|
|
CreatedByName string
|
|
Group int
|
|
CreatedAt time.Time
|
|
LastEdit int
|
|
LastEditBy int
|
|
Avatar string
|
|
MicroAvatar string
|
|
ClassName string
|
|
ContentLines int
|
|
Tag string
|
|
URL string
|
|
URLPrefix string
|
|
URLName string
|
|
Level int
|
|
IPAddress string
|
|
Liked bool
|
|
LikeCount int
|
|
ActionType string
|
|
ActionIcon string
|
|
}
|
|
|
|
type Reply struct {
|
|
ID int
|
|
ParentID int
|
|
Content string
|
|
CreatedBy int
|
|
Group int
|
|
CreatedAt time.Time
|
|
LastEdit int
|
|
LastEditBy int
|
|
ContentLines int
|
|
IPAddress string
|
|
Liked bool
|
|
LikeCount int
|
|
}
|
|
|
|
var ErrAlreadyLiked = errors.New("You already liked this!")
|
|
var replyStmts ReplyStmts
|
|
|
|
type ReplyStmts struct {
|
|
isLiked *sql.Stmt
|
|
createLike *sql.Stmt
|
|
edit *sql.Stmt
|
|
setPoll *sql.Stmt
|
|
delete *sql.Stmt
|
|
addLikesToReply *sql.Stmt
|
|
removeRepliesFromTopic *sql.Stmt
|
|
}
|
|
|
|
func init() {
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
|
replyStmts = ReplyStmts{
|
|
isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(),
|
|
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
|
|
edit: acc.Update("replies").Set("content = ?, parsed_content = ?").Where("rid = ? AND poll = 0").Prepare(),
|
|
setPoll: acc.Update("replies").Set("poll = ?").Where("rid = ? AND poll = 0").Prepare(),
|
|
delete: acc.Delete("replies").Where("rid = ?").Prepare(),
|
|
addLikesToReply: acc.Update("replies").Set("likeCount = likeCount + ?").Where("rid = ?").Prepare(),
|
|
removeRepliesFromTopic: acc.Update("topics").Set("postCount = postCount - ?").Where("tid = ?").Prepare(),
|
|
}
|
|
return acc.FirstError()
|
|
})
|
|
}
|
|
|
|
// TODO: Write tests for this
|
|
// TODO: Wrap these queries in a transaction to make sure the state is consistent
|
|
func (reply *Reply) Like(uid int) (err error) {
|
|
var rid int // unused, just here to avoid mutating reply.ID
|
|
err = replyStmts.isLiked.QueryRow(uid, reply.ID).Scan(&rid)
|
|
if err != nil && err != ErrNoRows {
|
|
return err
|
|
} else if err != ErrNoRows {
|
|
return ErrAlreadyLiked
|
|
}
|
|
|
|
score := 1
|
|
_, err = replyStmts.createLike.Exec(score, reply.ID, "replies", uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = replyStmts.addLikesToReply.Exec(1, reply.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = userStmts.incrementLiked.Exec(1, uid)
|
|
return err
|
|
}
|
|
|
|
// TODO: Write tests for this
|
|
func (reply *Reply) Delete() error {
|
|
_, err := replyStmts.delete.Exec(reply.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// TODO: Move this bit to *Topic
|
|
_, err = replyStmts.removeRepliesFromTopic.Exec(1, reply.ParentID)
|
|
tcache := Topics.GetCache()
|
|
if tcache != nil {
|
|
tcache.Remove(reply.ParentID)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (reply *Reply) SetPost(content string) error {
|
|
topic, err := reply.Topic()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
content = PreparseMessage(html.UnescapeString(content))
|
|
parsedContent := ParseMessage(content, topic.ParentID, "forums")
|
|
_, err = replyStmts.edit.Exec(content, parsedContent, reply.ID) // TODO: Sniff if this changed anything to see if we hit an existing poll
|
|
return err
|
|
}
|
|
|
|
func (reply *Reply) SetPoll(pollID int) error {
|
|
_, err := replyStmts.setPoll.Exec(pollID, reply.ID) // TODO: Sniff if this changed anything to see if we hit a poll
|
|
return err
|
|
}
|
|
|
|
func (reply *Reply) Topic() (*Topic, error) {
|
|
return Topics.Get(reply.ParentID)
|
|
}
|
|
|
|
func (reply *Reply) GetID() int {
|
|
return reply.ID
|
|
}
|
|
|
|
func (reply *Reply) GetTable() string {
|
|
return "replies"
|
|
}
|
|
|
|
// Copy gives you a non-pointer concurrency safe copy of the reply
|
|
func (reply *Reply) Copy() Reply {
|
|
return *reply
|
|
}
|