2017-09-03 04:50:31 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Reply Resources File
|
|
|
|
* Copyright Azareal 2016 - 2018
|
|
|
|
*
|
|
|
|
*/
|
2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2016-12-02 07:38:54 +00:00
|
|
|
|
2017-11-02 02:52:21 +00:00
|
|
|
import (
|
2017-11-11 04:06:16 +00:00
|
|
|
"database/sql"
|
2017-11-02 02:52:21 +00:00
|
|
|
"errors"
|
2018-01-20 06:50:29 +00:00
|
|
|
"html"
|
2017-11-02 02:52:21 +00:00
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
|
|
|
"../query_gen/lib"
|
2017-11-02 02:52:21 +00:00
|
|
|
)
|
2017-10-12 03:24:14 +00:00
|
|
|
|
2017-09-28 22:16:34 +00:00
|
|
|
type ReplyUser struct {
|
2017-11-02 02:52:21 +00:00
|
|
|
ID int
|
|
|
|
ParentID int
|
|
|
|
Content string
|
|
|
|
ContentHtml string
|
|
|
|
CreatedBy int
|
|
|
|
UserLink string
|
|
|
|
CreatedByName string
|
|
|
|
Group int
|
|
|
|
CreatedAt time.Time
|
|
|
|
RelativeCreatedAt string
|
|
|
|
LastEdit int
|
|
|
|
LastEditBy int
|
|
|
|
Avatar 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
|
2016-12-02 07:38:54 +00:00
|
|
|
}
|
2017-02-10 13:39:13 +00:00
|
|
|
|
2017-09-28 22:16:34 +00:00
|
|
|
type Reply struct {
|
2017-11-02 02:52:21 +00:00
|
|
|
ID int
|
|
|
|
ParentID int
|
|
|
|
Content string
|
|
|
|
CreatedBy int
|
|
|
|
Group int
|
|
|
|
CreatedAt time.Time
|
|
|
|
RelativeCreatedAt string
|
|
|
|
LastEdit int
|
|
|
|
LastEditBy int
|
|
|
|
ContentLines int
|
|
|
|
IPAddress string
|
|
|
|
Liked bool
|
|
|
|
LikeCount int
|
2017-02-28 09:27:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:24:14 +00:00
|
|
|
var ErrAlreadyLiked = errors.New("You already liked this!")
|
2017-11-11 04:06:16 +00:00
|
|
|
var replyStmts ReplyStmts
|
|
|
|
|
|
|
|
type ReplyStmts struct {
|
|
|
|
isLiked *sql.Stmt
|
|
|
|
createLike *sql.Stmt
|
2018-01-20 06:50:29 +00:00
|
|
|
edit *sql.Stmt
|
2017-11-11 04:06:16 +00:00
|
|
|
delete *sql.Stmt
|
|
|
|
addLikesToReply *sql.Stmt
|
|
|
|
removeRepliesFromTopic *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-11-12 03:29:05 +00:00
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
replyStmts = ReplyStmts{
|
2017-11-12 03:29:05 +00:00
|
|
|
isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(),
|
2017-11-11 08:46:30 +00:00
|
|
|
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(),
|
2018-01-20 06:50:29 +00:00
|
|
|
edit: acc.Update("replies").Set("content = ?, parsed_content = ?").Where("rid = ?").Prepare(),
|
2017-11-12 03:29:05 +00:00
|
|
|
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(),
|
2017-11-11 04:06:16 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
|
|
|
}
|
2017-10-12 03:24:14 +00:00
|
|
|
|
|
|
|
// 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
|
2017-11-11 04:06:16 +00:00
|
|
|
err = replyStmts.isLiked.QueryRow(uid, reply.ID).Scan(&rid)
|
2017-10-12 03:24:14 +00:00
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
return err
|
|
|
|
} else if err != ErrNoRows {
|
|
|
|
return ErrAlreadyLiked
|
|
|
|
}
|
|
|
|
|
|
|
|
score := 1
|
2017-11-11 04:06:16 +00:00
|
|
|
_, err = replyStmts.createLike.Exec(score, reply.ID, "replies", uid)
|
2017-10-12 03:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
_, err = replyStmts.addLikesToReply.Exec(1, reply.ID)
|
2017-10-12 03:24:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Write tests for this
|
|
|
|
func (reply *Reply) Delete() error {
|
2017-11-11 04:06:16 +00:00
|
|
|
_, err := replyStmts.delete.Exec(reply.ID)
|
2017-10-12 03:24:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
// TODO: Move this bit to *Topic
|
|
|
|
_, err = replyStmts.removeRepliesFromTopic.Exec(1, reply.ParentID)
|
2017-11-23 05:37:08 +00:00
|
|
|
tcache := Topics.GetCache()
|
|
|
|
if tcache != nil {
|
|
|
|
tcache.Remove(reply.ParentID)
|
2017-10-12 03:24:14 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-20 06:50:29 +00:00
|
|
|
func (reply *Reply) SetBody(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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
func (reply *Reply) Topic() (*Topic, error) {
|
2018-01-20 06:50:29 +00:00
|
|
|
return Topics.Get(reply.ParentID)
|
2017-11-11 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// Copy gives you a non-pointer concurrency safe copy of the reply
|
2017-09-28 22:16:34 +00:00
|
|
|
func (reply *Reply) Copy() Reply {
|
|
|
|
return *reply
|
|
|
|
}
|