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"
|
|
|
|
"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
|
|
|
|
delete *sql.Stmt
|
|
|
|
addLikesToReply *sql.Stmt
|
|
|
|
removeRepliesFromTopic *sql.Stmt
|
|
|
|
getParent *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
DbInits.Add(func() error {
|
|
|
|
acc := qgen.Builder.Accumulator()
|
|
|
|
replyStmts = ReplyStmts{
|
|
|
|
isLiked: acc.SimpleSelect("likes", "targetItem", "sentBy = ? and targetItem = ? and targetType = 'replies'", "", ""),
|
|
|
|
createLike: acc.SimpleInsert("likes", "weight, targetItem, targetType, sentBy", "?,?,?,?"),
|
|
|
|
delete: acc.SimpleDelete("replies", "rid = ?"),
|
|
|
|
addLikesToReply: acc.SimpleUpdate("replies", "likeCount = likeCount + ?", "rid = ?"),
|
|
|
|
removeRepliesFromTopic: acc.SimpleUpdate("topics", "postCount = postCount - ?", "tid = ?"),
|
|
|
|
getParent: acc.SimpleLeftJoin("replies", "topics", "topics.tid, topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, topics.ipaddress, topics.postCount, topics.likeCount, topics.data", "replies.tid = topics.tid", "rid = ?", "", ""),
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
tcache, ok := Topics.(TopicCache)
|
2017-10-12 03:24:14 +00:00
|
|
|
if ok {
|
|
|
|
tcache.CacheRemove(reply.ParentID)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
func (reply *Reply) Topic() (*Topic, error) {
|
|
|
|
topic := Topic{ID: 0}
|
|
|
|
err := replyStmts.getParent.QueryRow(reply.ID).Scan(&topic.ID, &topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
topic.Link = BuildTopicURL(NameToSlug(topic.Title), topic.ID)
|
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
|
|
|
|
func BlankReply() *Reply {
|
|
|
|
return &Reply{ID: 0}
|
|
|
|
}
|