From cdb2f0711d852d9b68a6e1e0541fc88f317cb930 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 13 Apr 2019 21:54:22 +1000 Subject: [PATCH] Attachments continue to function after the parent topic is moved now. Changed the signature of AttachmentStore.Add Added the MoveTo method to AttachmentStore. Added the MoveToByExtra method to AttachmentStore. Don't forget to actually add the pre script for topic_c_edit_post x.x Added the extra column to the attachments table. You will need to run the updater / patcher for this commit. --- cmd/query_gen/tables.go | 2 +- common/attachments.go | 36 ++++++++++++++++++++-------- common/routes_common.go | 1 + common/topic.go | 9 ++++++- main.go | 2 +- patcher/patches.go | 38 ++++++++++++++++++++++++++++++ patcher/utils.go | 2 +- routes/attachments.go | 4 ++-- routes/reply.go | 4 ++-- routes/topic.go | 10 ++++++-- schema/mssql/query_attachments.sql | 1 + schema/mysql/query_attachments.sql | 1 + schema/pgsql/query_attachments.sql | 1 + 13 files changed, 91 insertions(+), 20 deletions(-) diff --git a/cmd/query_gen/tables.go b/cmd/query_gen/tables.go index 0064c832..752e1057 100644 --- a/cmd/query_gen/tables.go +++ b/cmd/query_gen/tables.go @@ -277,7 +277,7 @@ func createTables(adapter qgen.Adapter) error { tblColumn{"originTable", "varchar", 200, false, false, "replies"}, tblColumn{"uploadedBy", "int", 0, false, false, ""}, // TODO; Make this a foreign key tblColumn{"path", "varchar", 200, false, false, ""}, - //tblColumn{"extra", "varchar", 200, false, false, ""}, + tblColumn{"extra", "varchar", 200, false, false, ""}, }, []tblKey{ tblKey{"attachID", "primary"}, diff --git a/common/attachments.go b/common/attachments.go index c3e36bdf..98bf409f 100644 --- a/common/attachments.go +++ b/common/attachments.go @@ -16,6 +16,7 @@ type MiniAttachment struct { OriginID int UploadedBy int Path string + Extra string Image bool Ext string @@ -25,7 +26,9 @@ type AttachmentStore interface { Get(id int) (*MiniAttachment, error) MiniGetList(originTable string, originID int) (alist []*MiniAttachment, err error) BulkMiniGetList(originTable string, ids []int) (amap map[int][]*MiniAttachment, err error) - Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) (int, error) + Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string, extra string) (int, error) + MoveTo(sectionID int, originID int, originTable string) error + MoveToByExtra(sectionID int, originTable string, extra string) error GlobalCount() int CountIn(originTable string, oid int) int CountInPath(path string) int @@ -39,18 +42,21 @@ type DefaultAttachmentStore struct { count *sql.Stmt countIn *sql.Stmt countInPath *sql.Stmt + move *sql.Stmt + moveByExtra *sql.Stmt delete *sql.Stmt } -func NewDefaultAttachmentStore() (*DefaultAttachmentStore, error) { - acc := qgen.NewAcc() +func NewDefaultAttachmentStore(acc *qgen.Accumulator) (*DefaultAttachmentStore, error) { return &DefaultAttachmentStore{ - get: acc.Select("attachments").Columns("originID, sectionID, uploadedBy, path").Where("attachID = ?").Prepare(), - getByObj: acc.Select("attachments").Columns("attachID, sectionID, uploadedBy, path").Where("originTable = ? AND originID = ?").Prepare(), - add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path").Fields("?,?,?,?,?,?").Prepare(), + get: acc.Select("attachments").Columns("originID, sectionID, uploadedBy, path, extra").Where("attachID = ?").Prepare(), + getByObj: acc.Select("attachments").Columns("attachID, sectionID, uploadedBy, path, extra").Where("originTable = ? AND originID = ?").Prepare(), + add: acc.Insert("attachments").Columns("sectionID, sectionTable, originID, originTable, uploadedBy, path, extra").Fields("?,?,?,?,?,?,?").Prepare(), count: acc.Count("attachments").Prepare(), countIn: acc.Count("attachments").Where("originTable = ? and originID = ?").Prepare(), countInPath: acc.Count("attachments").Where("path = ?").Prepare(), + move: acc.Update("attachments").Set("sectionID = ?").Where("originID = ? AND originTable = ?").Prepare(), + moveByExtra: acc.Update("attachments").Set("sectionID = ?").Where("originTable = ? AND extra = ?").Prepare(), delete: acc.Delete("attachments").Where("attachID = ?").Prepare(), }, acc.FirstError() } @@ -60,7 +66,7 @@ func (store *DefaultAttachmentStore) MiniGetList(originTable string, originID in defer rows.Close() for rows.Next() { attach := &MiniAttachment{OriginID: originID} - err := rows.Scan(&attach.ID, &attach.SectionID, &attach.UploadedBy, &attach.Path) + err := rows.Scan(&attach.ID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra) if err != nil { return nil, err } @@ -114,7 +120,7 @@ func (store *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []i func (store *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) { attach := &MiniAttachment{ID: id} - err := store.get.QueryRow(id).Scan(&attach.OriginID, &attach.SectionID, &attach.UploadedBy, &attach.Path) + err := store.get.QueryRow(id).Scan(&attach.OriginID, &attach.SectionID, &attach.UploadedBy, &attach.Path, &attach.Extra) if err != nil { return nil, err } @@ -127,8 +133,8 @@ func (store *DefaultAttachmentStore) Get(id int) (*MiniAttachment, error) { return attach, nil } -func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string) (int, error) { - res, err := store.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path) +func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, originID int, originTable string, uploadedBy int, path string, extra string) (int, error) { + res, err := store.add.Exec(sectionID, sectionTable, originID, originTable, uploadedBy, path, extra) if err != nil { return 0, err } @@ -136,6 +142,16 @@ func (store *DefaultAttachmentStore) Add(sectionID int, sectionTable string, ori return int(lid), err } +func (store *DefaultAttachmentStore) MoveTo(sectionID int, originID int, originTable string) error { + _, err := store.move.Exec(sectionID, originID, originTable) + return err +} + +func (store *DefaultAttachmentStore) MoveToByExtra(sectionID int, originTable string, extra string) error { + _, err := store.moveByExtra.Exec(sectionID, originTable, extra) + return err +} + func (store *DefaultAttachmentStore) GlobalCount() (count int) { err := store.count.QueryRow().Scan(&count) if err != nil { diff --git a/common/routes_common.go b/common/routes_common.go index 082cce70..2378e913 100644 --- a/common/routes_common.go +++ b/common/routes_common.go @@ -265,6 +265,7 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head addPreScript("topics_topic") addPreScript("paginator") addPreScript("alert") + addPreScript("topic_c_edit_post") return header, nil } diff --git a/common/topic.go b/common/topic.go index 242322b5..ae5cac61 100644 --- a/common/topic.go +++ b/common/topic.go @@ -251,7 +251,14 @@ func (topic *Topic) Unlock() (err error) { func (topic *Topic) MoveTo(destForum int) (err error) { _, err = topicStmts.moveTo.Exec(destForum, topic.ID) topic.cacheRemove() - return err + if err != nil { + return err + } + err = Attachments.MoveTo(destForum, topic.ID, "topics") + if err != nil { + return err + } + return Attachments.MoveToByExtra(destForum, "replies", strconv.Itoa(topic.ID)) } // TODO: We might want more consistent terminology rather than using stick in some places and pin in others. If you don't understand the difference, there is none, they are one and the same. diff --git a/main.go b/main.go index de7e75ae..2d4a94f4 100644 --- a/main.go +++ b/main.go @@ -152,7 +152,7 @@ func afterDBInit() (err error) { if err != nil { return errors.WithStack(err) } - common.Attachments, err = common.NewDefaultAttachmentStore() + common.Attachments, err = common.NewDefaultAttachmentStore(acc) if err != nil { return errors.WithStack(err) } diff --git a/patcher/patches.go b/patcher/patches.go index f2849da9..3206c51a 100644 --- a/patcher/patches.go +++ b/patcher/patches.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "database/sql" "strconv" "github.com/Azareal/Gosora/query_gen" @@ -28,6 +29,7 @@ func init() { addPatch(14, patch14) addPatch(15, patch15) addPatch(16, patch16) + addPatch(17, patch17) } func patch0(scanner *bufio.Scanner) (err error) { @@ -550,3 +552,39 @@ func patch16(scanner *bufio.Scanner) error { }, nil, )) } + +func patch17(scanner *bufio.Scanner) error { + err := execStmt(qgen.Builder.AddColumn("attachments", tblColumn{"extra", "varchar", 200, false, false, ""}, nil)) + if err != nil { + return err + } + + err = acc().Select("topics").Cols("tid, parentID").Where("attachCount > 0").Each(func(rows *sql.Rows) error { + var tid, parentID int + err := rows.Scan(&tid, &parentID) + if err != nil { + return err + } + _, err = acc().Update("attachments").Set("sectionID = ?").Where("originTable = 'topics' AND originID = ?").Exec(parentID, tid) + return err + }) + if err != nil { + return err + } + + return acc().Select("replies").Cols("rid, tid").Where("attachCount > 0").Each(func(rows *sql.Rows) error { + var rid, tid, sectionID int + err := rows.Scan(&rid, &tid) + if err != nil { + return err + } + + err = acc().Select("topics").Cols("parentID").Where("tid = ?").QueryRow(tid).Scan(§ionID) + if err != nil { + return err + } + + _, err = acc().Update("attachments").Set("sectionID = ?, extra = ?").Where("originTable = 'replies' AND originID = ?").Exec(sectionID, tid, rid) + return err + }) +} diff --git a/patcher/utils.go b/patcher/utils.go index cb4b3556..855d00fb 100644 --- a/patcher/utils.go +++ b/patcher/utils.go @@ -32,7 +32,7 @@ func execStmt(stmt *sql.Stmt, err error) error { }*/ func eachUser(handle func(int) error) error { - err := qgen.NewAcc().Select("users").Each(func(rows *sql.Rows) error { + err := qgen.NewAcc().Select("users").Cols("uid").Each(func(rows *sql.Rows) error { var uid int err := rows.Scan(&uid) if err != nil { diff --git a/routes/attachments.go b/routes/attachments.go index 5e308c0d..1a583834 100644 --- a/routes/attachments.go +++ b/routes/attachments.go @@ -117,7 +117,7 @@ func deleteAttachment(w http.ResponseWriter, r *http.Request, user common.User, // TODO: Stop duplicating this code // TODO: Use a transaction here // TODO: Move this function to neutral ground -func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User, sid int, sectionTable string, oid int, originTable string) (pathMap map[string]string, rerr common.RouteError) { +func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User, sid int, sectionTable string, oid int, originTable string, extra string) (pathMap map[string]string, rerr common.RouteError) { pathMap = make(map[string]string) files, rerr := uploadFilesWithHash(w, r, user, "./attachs/") if rerr != nil { @@ -125,7 +125,7 @@ func uploadAttachment(w http.ResponseWriter, r *http.Request, user common.User, } for _, filename := range files { - aid, err := common.Attachments.Add(sid, sectionTable, oid, originTable, user.ID, filename) + aid, err := common.Attachments.Add(sid, sectionTable, oid, originTable, user.ID, filename, extra) if err != nil { return nil, common.InternalError(err, w, r) } diff --git a/routes/reply.go b/routes/reply.go index 009fc596..9b8cff43 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -79,7 +79,7 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) // Handle the file attachments // TODO: Stop duplicating this code if user.Perms.UploadFiles { - _, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies") + _, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies", strconv.Itoa(topic.ID)) if rerr != nil { return rerr } @@ -380,7 +380,7 @@ func AddAttachToReplySubmit(w http.ResponseWriter, r *http.Request, user common. } // Handle the file attachments - pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies") + pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", rid, "replies", strconv.Itoa(topic.ID)) if rerr != nil { // TODO: This needs to be a JS error... return rerr diff --git a/routes/topic.go b/routes/topic.go index 6a3dcca5..fa44e598 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -258,12 +258,18 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user common.User, header } if user.Perms.EditReply && len(attachQueryList) > 0 { + //log.Printf("attachQueryList: %+v\n", attachQueryList) amap, err := common.Attachments.BulkMiniGetList("replies", attachQueryList) if err != nil && err != sql.ErrNoRows { return common.InternalError(err, w, r) } + //log.Printf("amap: %+v\n", amap) + //log.Printf("attachMap: %+v\n", attachMap) for id, attach := range amap { tpage.ItemList[attachMap[id]].Attachments = attach + /*for _, a := range attach { + log.Printf("a: %+v\n", a) + }*/ } } } @@ -302,7 +308,7 @@ func AddAttachToTopicSubmit(w http.ResponseWriter, r *http.Request, user common. } // Handle the file attachments - pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", tid, "topics") + pathMap, rerr := uploadAttachment(w, r, user, topic.ParentID, "forums", tid, "topics", "") if rerr != nil { // TODO: This needs to be a JS error... return rerr @@ -529,7 +535,7 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) // Handle the file attachments if user.Perms.UploadFiles { - _, rerr := uploadAttachment(w, r, user, fid, "forums", tid, "topics") + _, rerr := uploadAttachment(w, r, user, fid, "forums", tid, "topics", "") if rerr != nil { return rerr } diff --git a/schema/mssql/query_attachments.sql b/schema/mssql/query_attachments.sql index e70193fc..6649574e 100644 --- a/schema/mssql/query_attachments.sql +++ b/schema/mssql/query_attachments.sql @@ -6,5 +6,6 @@ CREATE TABLE [attachments] ( [originTable] nvarchar (200) DEFAULT 'replies' not null, [uploadedBy] int not null, [path] nvarchar (200) not null, + [extra] nvarchar (200) not null, primary key([attachID]) ); \ No newline at end of file diff --git a/schema/mysql/query_attachments.sql b/schema/mysql/query_attachments.sql index 6f297d8e..9692df59 100644 --- a/schema/mysql/query_attachments.sql +++ b/schema/mysql/query_attachments.sql @@ -6,5 +6,6 @@ CREATE TABLE `attachments` ( `originTable` varchar(200) DEFAULT 'replies' not null, `uploadedBy` int not null, `path` varchar(200) not null, + `extra` varchar(200) not null, primary key(`attachID`) ) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci; \ No newline at end of file diff --git a/schema/pgsql/query_attachments.sql b/schema/pgsql/query_attachments.sql index 4669a2da..aaec6a34 100644 --- a/schema/pgsql/query_attachments.sql +++ b/schema/pgsql/query_attachments.sql @@ -6,5 +6,6 @@ CREATE TABLE "attachments" ( `originTable` varchar (200) DEFAULT 'replies' not null, `uploadedBy` int not null, `path` varchar (200) not null, + `extra` varchar (200) not null, primary key(`attachID`) ); \ No newline at end of file