github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/profile_reply.go (about)

     1  package common
     2  
     3  import (
     4  	"database/sql"
     5  	"html"
     6  	"strconv"
     7  	"time"
     8  
     9  	qgen "github.com/Azareal/Gosora/query_gen"
    10  )
    11  
    12  var profileReplyStmts ProfileReplyStmts
    13  
    14  type ProfileReply struct {
    15  	ID           int
    16  	ParentID     int
    17  	Content      string
    18  	CreatedBy    int
    19  	Group        int
    20  	CreatedAt    time.Time
    21  	LastEdit     int
    22  	LastEditBy   int
    23  	ContentLines int
    24  	IP           string
    25  }
    26  
    27  type ProfileReplyStmts struct {
    28  	edit   *sql.Stmt
    29  	delete *sql.Stmt
    30  }
    31  
    32  func init() {
    33  	DbInits.Add(func(acc *qgen.Accumulator) error {
    34  		ur := "users_replies"
    35  		profileReplyStmts = ProfileReplyStmts{
    36  			edit:   acc.Update(ur).Set("content=?,parsed_content=?").Where("rid=?").Prepare(),
    37  			delete: acc.Delete(ur).Where("rid=?").Prepare(),
    38  		}
    39  		return acc.FirstError()
    40  	})
    41  }
    42  
    43  // Mostly for tests, so we don't wind up with out-of-date profile reply initialisation logic there
    44  func BlankProfileReply(id int) *ProfileReply {
    45  	return &ProfileReply{ID: id}
    46  }
    47  
    48  // TODO: Write tests for this
    49  func (r *ProfileReply) Delete() error {
    50  	_, err := profileReplyStmts.delete.Exec(r.ID)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	// TODO: Better coupling between the two paramsextra queries
    55  	aids, err := Activity.AidsByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
    56  	if err != nil {
    57  		return err
    58  	}
    59  	for _, aid := range aids {
    60  		DismissAlert(r.ParentID, aid)
    61  	}
    62  	err = Activity.DeleteByParamsExtra("reply", r.ParentID, "user", strconv.Itoa(r.ID))
    63  	return err
    64  }
    65  
    66  func (r *ProfileReply) SetBody(content string) error {
    67  	content = PreparseMessage(html.UnescapeString(content))
    68  	_, err := profileReplyStmts.edit.Exec(content, ParseMessage(content, 0, "", nil, nil), r.ID)
    69  	return err
    70  }
    71  
    72  // TODO: We can get this from the topic store instead of a query which will always miss the cache...
    73  func (r *ProfileReply) Creator() (*User, error) {
    74  	return Users.Get(r.CreatedBy)
    75  }