github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/activity_stream.go (about) 1 package common 2 3 import ( 4 "database/sql" 5 6 qgen "github.com/Azareal/Gosora/query_gen" 7 ) 8 9 var Activity ActivityStream 10 11 type ActivityStream interface { 12 Add(a Alert) (int, error) 13 Get(id int) (Alert, error) 14 Delete(id int) error 15 DeleteByParams(event string, targetID int, targetType string) error 16 DeleteByParamsExtra(event string, targetID int, targetType, extra string) error 17 AidsByParams(event string, elementID int, elementType string) (aids []int, err error) 18 AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, err error) 19 Count() (count int) 20 } 21 22 type DefaultActivityStream struct { 23 add *sql.Stmt 24 get *sql.Stmt 25 delete *sql.Stmt 26 deleteByParams *sql.Stmt 27 deleteByParamsExtra *sql.Stmt 28 aidsByParams *sql.Stmt 29 aidsByParamsExtra *sql.Stmt 30 count *sql.Stmt 31 } 32 33 func NewDefaultActivityStream(acc *qgen.Accumulator) (*DefaultActivityStream, error) { 34 as := "activity_stream" 35 cols := "actor,targetUser,event,elementType,elementID,createdAt,extra" 36 return &DefaultActivityStream{ 37 add: acc.Insert(as).Columns(cols).Fields("?,?,?,?,?,UTC_TIMESTAMP(),?").Prepare(), 38 get: acc.Select(as).Columns(cols).Where("asid=?").Prepare(), 39 delete: acc.Delete(as).Where("asid=?").Prepare(), 40 deleteByParams: acc.Delete(as).Where("event=? AND elementID=? AND elementType=?").Prepare(), 41 deleteByParamsExtra: acc.Delete(as).Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(), 42 aidsByParams: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=?").Prepare(), 43 aidsByParamsExtra: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(), 44 count: acc.Count(as).Prepare(), 45 }, acc.FirstError() 46 } 47 48 func (s *DefaultActivityStream) Add(a Alert) (int, error) { 49 res, err := s.add.Exec(a.ActorID, a.TargetUserID, a.Event, a.ElementType, a.ElementID, a.Extra) 50 if err != nil { 51 return 0, err 52 } 53 lastID, err := res.LastInsertId() 54 return int(lastID), err 55 } 56 57 func (s *DefaultActivityStream) Get(id int) (Alert, error) { 58 a := Alert{ASID: id} 59 err := s.get.QueryRow(id).Scan(&a.ActorID, &a.TargetUserID, &a.Event, &a.ElementType, &a.ElementID, &a.CreatedAt, &a.Extra) 60 return a, err 61 } 62 63 func (s *DefaultActivityStream) Delete(id int) error { 64 _, err := s.delete.Exec(id) 65 return err 66 } 67 68 func (s *DefaultActivityStream) DeleteByParams(event string, elementID int, elementType string) error { 69 _, err := s.deleteByParams.Exec(event, elementID, elementType) 70 return err 71 } 72 73 func (s *DefaultActivityStream) DeleteByParamsExtra(event string, elementID int, elementType, extra string) error { 74 _, err := s.deleteByParamsExtra.Exec(event, elementID, elementType, extra) 75 return err 76 } 77 78 func (s *DefaultActivityStream) AidsByParams(event string, elementID int, elementType string) (aids []int, err error) { 79 rows, err := s.aidsByParams.Query(event, elementID, elementType) 80 if err != nil { 81 return nil, err 82 } 83 defer rows.Close() 84 for rows.Next() { 85 var aid int 86 if err := rows.Scan(&aid); err != nil { 87 return nil, err 88 } 89 aids = append(aids, aid) 90 } 91 return aids, rows.Err() 92 } 93 94 func (s *DefaultActivityStream) AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, e error) { 95 rows, e := s.aidsByParamsExtra.Query(event, elementID, elementType, extra) 96 if e != nil { 97 return nil, e 98 } 99 defer rows.Close() 100 for rows.Next() { 101 var aid int 102 if e := rows.Scan(&aid); e != nil { 103 return nil, e 104 } 105 aids = append(aids, aid) 106 } 107 return aids, rows.Err() 108 } 109 110 // Count returns the total number of activity stream items 111 func (s *DefaultActivityStream) Count() (count int) { 112 return Count(s.count) 113 }