2019-06-01 12:31:48 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import "database/sql"
|
|
|
|
import "github.com/Azareal/Gosora/query_gen"
|
|
|
|
|
|
|
|
var Activity ActivityStream
|
|
|
|
|
|
|
|
type ActivityStream interface {
|
2019-12-07 06:27:01 +00:00
|
|
|
Add(a Alert) (int, error)
|
2019-06-01 12:31:48 +00:00
|
|
|
Get(id int) (Alert, error)
|
|
|
|
Count() (count int)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultActivityStream struct {
|
|
|
|
add *sql.Stmt
|
|
|
|
get *sql.Stmt
|
|
|
|
count *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultActivityStream(acc *qgen.Accumulator) (*DefaultActivityStream, error) {
|
2019-10-27 23:13:24 +00:00
|
|
|
as := "activity_stream"
|
2019-06-01 12:31:48 +00:00
|
|
|
return &DefaultActivityStream{
|
2019-10-27 23:13:24 +00:00
|
|
|
add: acc.Insert(as).Columns("actor, targetUser, event, elementType, elementID, createdAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
|
|
|
|
get: acc.Select(as).Columns("actor, targetUser, event, elementType, elementID, createdAt").Where("asid = ?").Prepare(),
|
|
|
|
count: acc.Count(as).Prepare(),
|
2019-06-01 12:31:48 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-09-29 04:56:39 +00:00
|
|
|
func (s *DefaultActivityStream) Add(a Alert) (int, error) {
|
|
|
|
res, err := s.add.Exec(a.ActorID, a.TargetUserID, a.Event, a.ElementType, a.ElementID)
|
2019-06-01 12:31:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
return int(lastID), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultActivityStream) Get(id int) (Alert, error) {
|
2019-09-29 04:56:39 +00:00
|
|
|
a := Alert{ASID: id}
|
2019-06-01 12:31:48 +00:00
|
|
|
err := s.get.QueryRow(id).Scan(&a.ActorID, &a.TargetUserID, &a.Event, &a.ElementType, &a.ElementID, &a.CreatedAt)
|
|
|
|
return a, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Write a test for this
|
|
|
|
// Count returns the total number of activity stream items
|
|
|
|
func (s *DefaultActivityStream) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|