github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/subscription.go (about) 1 package common 2 3 import ( 4 "database/sql" 5 6 qgen "github.com/Azareal/Gosora/query_gen" 7 ) 8 9 var Subscriptions SubscriptionStore 10 11 // ? Should we have a subscription store for each zone? topic, forum, etc? 12 type SubscriptionStore interface { 13 Add(uid, elementID int, elementType string) error 14 Delete(uid, targetID int, targetType string) error 15 DeleteResource(targetID int, targetType string) error 16 } 17 18 type DefaultSubscriptionStore struct { 19 add *sql.Stmt 20 delete *sql.Stmt 21 deleteResource *sql.Stmt 22 } 23 24 func NewDefaultSubscriptionStore() (*DefaultSubscriptionStore, error) { 25 acc := qgen.NewAcc() 26 ast := "activity_subscriptions" 27 return &DefaultSubscriptionStore{ 28 add: acc.Insert(ast).Columns("user,targetID,targetType,level").Fields("?,?,?,2").Prepare(), 29 delete: acc.Delete(ast).Where("user=? AND targetID=? AND targetType=?").Prepare(), 30 deleteResource: acc.Delete(ast).Where("targetID=? AND targetType=?").Prepare(), 31 }, acc.FirstError() 32 } 33 34 func (s *DefaultSubscriptionStore) Add(uid, elementID int, elementType string) error { 35 _, err := s.add.Exec(uid, elementID, elementType) 36 return err 37 } 38 39 // TODO: Add a primary key to the activity subscriptions table 40 func (s *DefaultSubscriptionStore) Delete(uid, targetID int, targetType string) error { 41 _, err := s.delete.Exec(uid, targetID, targetType) 42 return err 43 } 44 45 func (s *DefaultSubscriptionStore) DeleteResource(targetID int, targetType string) error { 46 _, err := s.deleteResource.Exec(targetID, targetType) 47 return err 48 }