From e4cfe610f65fe62e3ee871aebd94ac4a3612dbcc Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 14 Jan 2020 20:29:23 +1000 Subject: [PATCH] Update last reply on topics properly upon reply delete. --- common/reply.go | 25 ++++++++++++++++++++----- common/topic.go | 22 +++++++++++----------- query_gen/mysql.go | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/common/reply.go b/common/reply.go index 2d0f226c..ae094c55 100644 --- a/common/reply.go +++ b/common/reply.go @@ -63,6 +63,9 @@ type ReplyStmts struct { delete *sql.Stmt addLikesToReply *sql.Stmt removeRepliesFromTopic *sql.Stmt + + updateTopicReplies *sql.Stmt + updateTopicReplies2 *sql.Stmt } func init() { @@ -71,11 +74,15 @@ func init() { 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(re).Set("content = ?, parsed_content = ?").Where("rid = ? AND poll = 0").Prepare(), - setPoll: acc.Update(re).Set("poll = ?").Where("rid = ? AND poll = 0").Prepare(), - delete: acc.Delete(re).Where("rid = ?").Prepare(), - addLikesToReply: acc.Update(re).Set("likeCount = likeCount + ?").Where("rid = ?").Prepare(), - removeRepliesFromTopic: acc.Update("topics").Set("postCount = postCount - ?").Where("tid = ?").Prepare(), + edit: acc.Update(re).Set("content=?,parsed_content=?").Where("rid=? AND poll=0").Prepare(), + setPoll: acc.Update(re).Set("poll=?").Where("rid=? AND poll=0").Prepare(), + delete: acc.Delete(re).Where("rid=?").Prepare(), + addLikesToReply: acc.Update(re).Set("likeCount=likeCount+?").Where("rid=?").Prepare(), + removeRepliesFromTopic: acc.Update("topics").Set("postCount=postCount-?").Where("tid=?").Prepare(), + + // TODO: Optimise this to avoid firing an update if it's not the last reply in a topic. We will need to set lastReplyID properly in other places and in the patcher first so we can use it here. + updateTopicReplies: acc.RawPrepare("UPDATE topics t INNER JOIN replies r ON t.tid = r.tid SET t.lastReplyBy = r.createdBy, t.lastReplyAt = r.createdAt, t.lastReplyID = r.rid WHERE t.tid = ?"), + updateTopicReplies2: acc.Update("topics").Set("lastReplyAt=createdAt,lastReplyBy=createdBy,lastReplyID=0").Where("postCount=1 AND tid=?").Prepare(), } return acc.FirstError() }) @@ -115,6 +122,14 @@ func (r *Reply) Delete() error { } // TODO: Move this bit to *Topic _, err = replyStmts.removeRepliesFromTopic.Exec(1, r.ParentID) + if err != nil { + return err + } + _, err = replyStmts.updateTopicReplies.Exec(r.ParentID) + if err != nil { + return err + } + _, err = replyStmts.updateTopicReplies2.Exec(r.ParentID) tc := Topics.GetCache() if tc != nil { tc.Remove(r.ParentID) diff --git a/common/topic.go b/common/topic.go index fa77e1f6..5742d973 100644 --- a/common/topic.go +++ b/common/topic.go @@ -216,19 +216,19 @@ func init() { getRids: acc.Select("replies").Columns("rid").Where("tid = ?").Orderby("rid ASC").Limit("?,?").Prepare(), getReplies: acc.SimpleLeftJoin("replies AS r", "users AS u", "r.rid, r.content, r.createdBy, r.createdAt, r.lastEdit, r.lastEditBy, u.avatar, u.name, u.group, u.level, r.ipaddress, r.likeCount, r.attachCount, r.actionType", "r.createdBy = u.uid", "r.tid = ?", "r.rid ASC", "?,?"), addReplies: acc.Update(t).Set("postCount = postCount + ?, lastReplyBy = ?, lastReplyAt = UTC_TIMESTAMP()").Where("tid = ?").Prepare(), - updateLastReply: acc.Update(t).Set("lastReplyID = ?").Where("lastReplyID > ? AND tid = ?").Prepare(), - lock: acc.Update(t).Set("is_closed = 1").Where("tid = ?").Prepare(), - unlock: acc.Update(t).Set("is_closed = 0").Where("tid = ?").Prepare(), - moveTo: acc.Update(t).Set("parentID = ?").Where("tid = ?").Prepare(), - stick: acc.Update(t).Set("sticky = 1").Where("tid = ?").Prepare(), - unstick: acc.Update(t).Set("sticky = 0").Where("tid = ?").Prepare(), - hasLikedTopic: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'topics'").Prepare(), + updateLastReply: acc.Update(t).Set("lastReplyID=?").Where("lastReplyID > ? AND tid = ?").Prepare(), + lock: acc.Update(t).Set("is_closed=1").Where("tid=?").Prepare(), + unlock: acc.Update(t).Set("is_closed=0").Where("tid=?").Prepare(), + moveTo: acc.Update(t).Set("parentID=?").Where("tid=?").Prepare(), + stick: acc.Update(t).Set("sticky=1").Where("tid=?").Prepare(), + unstick: acc.Update(t).Set("sticky=0").Where("tid=?").Prepare(), + hasLikedTopic: acc.Select("likes").Columns("targetItem").Where("sentBy=? and targetItem=? and targetType='topics'").Prepare(), createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy, createdAt").Fields("?,?,?,?,UTC_TIMESTAMP()").Prepare(), - addLikesToTopic: acc.Update(t).Set("likeCount = likeCount + ?").Where("tid = ?").Prepare(), + addLikesToTopic: acc.Update(t).Set("likeCount=likeCount+?").Where("tid = ?").Prepare(), delete: acc.Delete(t).Where("tid = ?").Prepare(), - deleteActivity: acc.Delete("activity_stream").Where("elementID = ? AND elementType = 'topic'").Prepare(), - deleteActivitySubs: acc.Delete("activity_subscriptions").Where("targetID = ? AND targetType = 'topic'").Prepare(), - edit: acc.Update(t).Set("title = ?, content = ?, parsed_content = ?").Where("tid = ?").Prepare(), // TODO: Only run the content update bits on non-polls, does this matter? + deleteActivity: acc.Delete("activity_stream").Where("elementID=? AND elementType='topic'").Prepare(), + deleteActivitySubs: acc.Delete("activity_subscriptions").Where("targetID=? AND targetType='topic'").Prepare(), + edit: acc.Update(t).Set("title=?,content=?,parsed_content=?").Where("tid=?").Prepare(), // TODO: Only run the content update bits on non-polls, does this matter? setPoll: acc.Update(t).Set("poll = ?").Where("tid = ? AND poll = 0").Prepare(), createAction: acc.Insert("replies").Columns("tid, actionType, ipaddress, createdBy, createdAt, lastUpdated, content, parsed_content").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),'',''").Prepare(), diff --git a/query_gen/mysql.go b/query_gen/mysql.go index 443f27ca..bae07024 100644 --- a/query_gen/mysql.go +++ b/query_gen/mysql.go @@ -52,7 +52,7 @@ func (a *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error) { // First try opening a pipe as those are faster if runtime.GOOS == "linux" { - var dbsocket = "/tmp/mysql.sock" + dbsocket := "/tmp/mysql.sock" if config["socket"] != "" { dbsocket = config["socket"] }