2021-04-07 14:23:11 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-04-07 14:23:11 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2021-04-07 14:23:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ForumActionStore ForumActionStoreInt
|
|
|
|
|
|
|
|
//var ForumActionRunnableStore ForumActionRunnableStoreInt
|
|
|
|
|
|
|
|
const (
|
2022-02-21 03:32:53 +00:00
|
|
|
ForumActionDelete = iota
|
|
|
|
ForumActionLock
|
|
|
|
ForumActionUnlock
|
|
|
|
ForumActionMove
|
2021-04-07 14:23:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ConvStringToAct(s string) int {
|
2022-02-21 03:32:53 +00:00
|
|
|
switch s {
|
|
|
|
case "delete":
|
|
|
|
return ForumActionDelete
|
|
|
|
case "lock":
|
|
|
|
return ForumActionLock
|
|
|
|
case "unlock":
|
|
|
|
return ForumActionUnlock
|
|
|
|
case "move":
|
|
|
|
return ForumActionMove
|
|
|
|
}
|
|
|
|
return -1
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
func ConvActToString(a int) string {
|
2022-02-21 03:32:53 +00:00
|
|
|
switch a {
|
|
|
|
case ForumActionDelete:
|
|
|
|
return "delete"
|
|
|
|
case ForumActionLock:
|
|
|
|
return "lock"
|
|
|
|
case ForumActionUnlock:
|
|
|
|
return "unlock"
|
|
|
|
case ForumActionMove:
|
|
|
|
return "move"
|
|
|
|
}
|
|
|
|
return ""
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var forumActionStmts ForumActionStmts
|
|
|
|
|
|
|
|
type ForumActionStmts struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
get1 *sql.Stmt
|
|
|
|
get2 *sql.Stmt
|
|
|
|
lock1 *sql.Stmt
|
|
|
|
lock2 *sql.Stmt
|
|
|
|
unlock1 *sql.Stmt
|
|
|
|
unlock2 *sql.Stmt
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ForumAction struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
ID int
|
|
|
|
Forum int
|
|
|
|
RunOnTopicCreation bool
|
|
|
|
RunDaysAfterTopicCreation int
|
|
|
|
RunDaysAfterTopicLastReply int
|
|
|
|
Action int
|
|
|
|
Extra string
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-02-21 03:32:53 +00:00
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
|
|
|
t := "topics"
|
|
|
|
forumActionStmts = ForumActionStmts{
|
|
|
|
get1: acc.Select(t).Cols("tid,createdBy,poll").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
|
|
|
|
get2: acc.Select(t).Cols("tid,createdBy,poll").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),
|
|
|
|
|
|
|
|
/*lock1: acc.Update(t).Set("is_closed=1").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
|
|
|
|
lock2: acc.Update(t).Set("is_closed=1").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),
|
|
|
|
unlock1: acc.Update(t).Set("is_closed=0").Where("parentID=?").DateOlderThanQ("createdAt", "day").Stmt(),
|
|
|
|
unlock2: acc.Update(t).Set("is_closed=0").Where("parentID=?").DateOlderThanQ("lastReplyAt", "day").Stmt(),*/
|
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ForumAction) Run() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
if a.RunDaysAfterTopicCreation > 0 {
|
|
|
|
if e := a.runDaysAfterTopicCreation(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.RunDaysAfterTopicLastReply > 0 {
|
|
|
|
if e := a.runDaysAfterTopicLastReply(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ForumAction) runQ(stmt *sql.Stmt, days int, f func(t *Topic) error) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, e := stmt.Query(days, a.Forum)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
// TODO: Decouple this
|
|
|
|
t := &Topic{ParentID: a.Forum}
|
|
|
|
if e := rows.Scan(&t.ID, &t.CreatedBy, &t.Poll); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e = f(t); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows.Err()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ForumAction) runDaysAfterTopicCreation() (e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
switch a.Action {
|
|
|
|
case ForumActionDelete:
|
|
|
|
// TODO: Bulk delete?
|
|
|
|
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
|
|
|
|
return t.Delete()
|
|
|
|
})
|
|
|
|
case ForumActionLock:
|
|
|
|
/*_, e := forumActionStmts.lock1.Exec(a.Forum)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}*/
|
|
|
|
// TODO: Bulk lock? Lock and get resultset of changed topics somehow?
|
|
|
|
fmt.Println("ForumActionLock")
|
|
|
|
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
|
|
|
|
fmt.Printf("t: %+v\n", t)
|
|
|
|
return t.Lock()
|
|
|
|
})
|
|
|
|
case ForumActionUnlock:
|
|
|
|
// TODO: Bulk unlock? Unlock and get resultset of changed topics somehow?
|
|
|
|
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
|
|
|
|
return t.Unlock()
|
|
|
|
})
|
|
|
|
case ForumActionMove:
|
|
|
|
destForum, e := strconv.Atoi(a.Extra)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e = a.runQ(forumActionStmts.get1, a.RunDaysAfterTopicCreation, func(t *Topic) error {
|
|
|
|
return t.MoveTo(destForum)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ForumAction) runDaysAfterTopicLastReply() (e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
switch a.Action {
|
|
|
|
case ForumActionDelete:
|
|
|
|
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
|
|
|
|
return t.Delete()
|
|
|
|
})
|
|
|
|
case ForumActionLock:
|
|
|
|
// TODO: Bulk lock? Lock and get resultset of changed topics somehow?
|
|
|
|
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
|
|
|
|
return t.Lock()
|
|
|
|
})
|
|
|
|
case ForumActionUnlock:
|
|
|
|
// TODO: Bulk unlock? Unlock and get resultset of changed topics somehow?
|
|
|
|
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
|
|
|
|
return t.Unlock()
|
|
|
|
})
|
|
|
|
case ForumActionMove:
|
|
|
|
destForum, e := strconv.Atoi(a.Extra)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e = a.runQ(forumActionStmts.get2, a.RunDaysAfterTopicLastReply, func(t *Topic) error {
|
|
|
|
return t.MoveTo(destForum)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ForumAction) TopicCreation(tid int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
if !a.RunOnTopicCreation {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ForumActionStoreInt interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
Get(faid int) (*ForumAction, error)
|
|
|
|
GetInForum(fid int) ([]*ForumAction, error)
|
|
|
|
GetAll() ([]*ForumAction, error)
|
|
|
|
GetNewTopicActions(fid int) ([]*ForumAction, error)
|
|
|
|
|
|
|
|
Add(fa *ForumAction) (int, error)
|
|
|
|
Delete(faid int) error
|
|
|
|
Exists(faid int) bool
|
|
|
|
Count() int
|
|
|
|
CountInForum(fid int) int
|
|
|
|
|
|
|
|
DailyTick() error
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultForumActionStore struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
getInForum *sql.Stmt
|
|
|
|
getAll *sql.Stmt
|
|
|
|
getNewTopicActions *sql.Stmt
|
|
|
|
|
|
|
|
add *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
countInForum *sql.Stmt
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultForumActionStore(acc *qgen.Accumulator) (*DefaultForumActionStore, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
fa := "forums_actions"
|
|
|
|
allCols := "faid,fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra"
|
|
|
|
return &DefaultForumActionStore{
|
|
|
|
get: acc.Select(fa).Columns("fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Where("faid=?").Prepare(),
|
|
|
|
getInForum: acc.Select(fa).Columns("faid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Where("fid=?").Prepare(),
|
|
|
|
getAll: acc.Select(fa).Columns(allCols).Prepare(),
|
|
|
|
getNewTopicActions: acc.Select(fa).Columns(allCols).Where("fid=? AND runOnTopicCreation=1").Prepare(),
|
|
|
|
|
|
|
|
add: acc.Insert(fa).Columns("fid,runOnTopicCreation,runDaysAfterTopicCreation,runDaysAfterTopicLastReply,action,extra").Fields("?,?,?,?,?,?").Prepare(),
|
|
|
|
delete: acc.Delete(fa).Where("faid=?").Prepare(),
|
|
|
|
exists: acc.Exists(fa, "faid").Prepare(),
|
|
|
|
count: acc.Count(fa).Prepare(),
|
|
|
|
countInForum: acc.Count(fa).Where("fid=?").Prepare(),
|
|
|
|
}, acc.FirstError()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) DailyTick() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
fas, e := s.GetAll()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
for _, fa := range fas {
|
|
|
|
if e := fa.Run(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) Get(id int) (*ForumAction, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
fa := ForumAction{ID: id}
|
|
|
|
var str string
|
|
|
|
e := s.get.QueryRow(id).Scan(&fa.Forum, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra)
|
|
|
|
fa.Action = ConvStringToAct(str)
|
|
|
|
return &fa, e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) GetInForum(fid int) (fas []*ForumAction, e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, e := s.getInForum.Query(fid)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var str string
|
|
|
|
for rows.Next() {
|
|
|
|
fa := ForumAction{Forum: fid}
|
|
|
|
if e := rows.Scan(&fa.ID, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
fa.Action = ConvStringToAct(str)
|
|
|
|
fas = append(fas, &fa)
|
|
|
|
}
|
|
|
|
return fas, rows.Err()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) GetAll() (fas []*ForumAction, e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, e := s.getAll.Query()
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var str string
|
|
|
|
for rows.Next() {
|
|
|
|
fa := ForumAction{}
|
|
|
|
if e := rows.Scan(&fa.ID, &fa.Forum, &fa.RunOnTopicCreation, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
fa.Action = ConvStringToAct(str)
|
|
|
|
fas = append(fas, &fa)
|
|
|
|
}
|
|
|
|
return fas, rows.Err()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) GetNewTopicActions(fid int) (fas []*ForumAction, e error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
rows, e := s.getNewTopicActions.Query(fid)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var str string
|
|
|
|
for rows.Next() {
|
|
|
|
fa := ForumAction{RunOnTopicCreation: true}
|
|
|
|
if e := rows.Scan(&fa.ID, &fa.Forum, &fa.RunDaysAfterTopicCreation, &fa.RunDaysAfterTopicLastReply, &str, &fa.Extra); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
fa.Action = ConvStringToAct(str)
|
|
|
|
fas = append(fas, &fa)
|
|
|
|
}
|
|
|
|
return fas, rows.Err()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) Add(fa *ForumAction) (int, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
res, e := s.add.Exec(fa.Forum, fa.RunOnTopicCreation, fa.RunDaysAfterTopicCreation, fa.RunDaysAfterTopicLastReply, ConvActToString(fa.Action), fa.Extra)
|
|
|
|
if e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
lastID, e := res.LastInsertId()
|
|
|
|
return int(lastID), e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) Delete(id int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
_, e := s.delete.Exec(id)
|
|
|
|
return e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) Exists(id int) bool {
|
2022-02-21 03:32:53 +00:00
|
|
|
err := s.exists.QueryRow(id).Scan(&id)
|
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return err != ErrNoRows
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) Count() (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
err := s.count.QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionStore) CountInForum(fid int) (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.countInForum, fid)
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*type ForumActionRunnable struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
ID int
|
|
|
|
ActionID int
|
|
|
|
TargetID int
|
|
|
|
TargetType int // 0 = topic
|
|
|
|
RunAfter int //unixtime
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ForumActionRunnableStoreInt interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
GetAfterTime(unix int) ([]*ForumActionRunnable, error)
|
|
|
|
GetInForum(fid int) ([]*ForumActionRunnable, error)
|
|
|
|
Delete(faid int) error
|
|
|
|
DeleteInForum(fid int) error
|
|
|
|
DeleteByActionID(faid int) error
|
|
|
|
Count() int
|
|
|
|
CountInForum(fid int) int
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultForumActionRunnableStore struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
delete *sql.Stmt
|
|
|
|
deleteInForum *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
countInForum *sql.Stmt
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultForumActionRunnableStore(acc *qgen.Accumulator) (*DefaultForumActionRunnableStore, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
fa := "forums_actions"
|
|
|
|
return &DefaultForumActionRunnableStore{
|
|
|
|
delete: acc.Delete(fa).Where("faid=?").Prepare(),
|
|
|
|
deleteInForum: acc.Delete(fa).Where("fid=?").Prepare(),
|
|
|
|
count: acc.Count(fa).Prepare(),
|
|
|
|
countInForum: acc.Count(fa).Where("faid=?").Prepare(),
|
|
|
|
}, acc.FirstError()
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionRunnableStore) Delete(id int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
_, e := s.delete.Exec(id)
|
|
|
|
return e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionRunnableStore) DeleteInForum(fid int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
_, e := s.deleteInForum.Exec(id)
|
|
|
|
return e
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionRunnableStore) Count() (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
err := s.count.QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultForumActionRunnableStore) CountInForum(fid int) (count int) {
|
2022-02-21 03:32:53 +00:00
|
|
|
return Countf(s.countInForum, fid)
|
2021-04-07 14:23:11 +00:00
|
|
|
}
|
|
|
|
*/
|