2018-01-20 06:50:29 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"html"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-01-20 06:50:29 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
2018-01-20 06:50:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var profileReplyStmts ProfileReplyStmts
|
|
|
|
|
|
|
|
type ProfileReply struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
ID int
|
|
|
|
ParentID int
|
|
|
|
Content string
|
|
|
|
CreatedBy int
|
|
|
|
Group int
|
|
|
|
CreatedAt time.Time
|
|
|
|
LastEdit int
|
|
|
|
LastEditBy int
|
|
|
|
ContentLines int
|
|
|
|
IP string
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProfileReplyStmts struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
edit *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-02-21 03:32:53 +00:00
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
|
|
|
ur := "users_replies"
|
|
|
|
profileReplyStmts = ProfileReplyStmts{
|
|
|
|
edit: acc.Update(ur).Set("content=?,parsed_content=?").Where("rid=?").Prepare(),
|
|
|
|
delete: acc.Delete(ur).Where("rid=?").Prepare(),
|
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 06:27:12 +00:00
|
|
|
// Mostly for tests, so we don't wind up with out-of-date profile reply initialisation logic there
|
|
|
|
func BlankProfileReply(id int) *ProfileReply {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &ProfileReply{ID: id}
|
2018-05-28 06:27:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 06:50:29 +00:00
|
|
|
// TODO: Write tests for this
|
2019-08-31 22:34:43 +00:00
|
|
|
func (r *ProfileReply) Delete() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
_, err := profileReplyStmts.delete.Exec(r.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Better coupling between the two paramsextra queries
|
|
|
|
aids, err := Activity.AidsByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, aid := range aids {
|
|
|
|
DismissAlert(r.ParentID, aid)
|
|
|
|
}
|
|
|
|
err = Activity.DeleteByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
|
|
|
|
return err
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:34:43 +00:00
|
|
|
func (r *ProfileReply) SetBody(content string) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
content = PreparseMessage(html.UnescapeString(content))
|
|
|
|
_, err := profileReplyStmts.edit.Exec(content, ParseMessage(content, 0, "", nil, nil), r.ID)
|
|
|
|
return err
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We can get this from the topic store instead of a query which will always miss the cache...
|
2019-08-31 22:34:43 +00:00
|
|
|
func (r *ProfileReply) Creator() (*User, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Users.Get(r.CreatedBy)
|
2018-01-20 06:50:29 +00:00
|
|
|
}
|