gosora/common/subscription.go

49 lines
1.5 KiB
Go
Raw Normal View History

package common
import (
2022-02-21 03:53:13 +00:00
"database/sql"
2022-02-21 03:53:13 +00:00
qgen "git.tuxpa.in/a/gosora/query_gen"
)
var Subscriptions SubscriptionStore
// ? Should we have a subscription store for each zone? topic, forum, etc?
type SubscriptionStore interface {
2022-02-21 03:32:53 +00:00
Add(uid, elementID int, elementType string) error
Delete(uid, targetID int, targetType string) error
DeleteResource(targetID int, targetType string) error
}
type DefaultSubscriptionStore struct {
2022-02-21 03:32:53 +00:00
add *sql.Stmt
delete *sql.Stmt
deleteResource *sql.Stmt
}
func NewDefaultSubscriptionStore() (*DefaultSubscriptionStore, error) {
2022-02-21 03:32:53 +00:00
acc := qgen.NewAcc()
ast := "activity_subscriptions"
return &DefaultSubscriptionStore{
add: acc.Insert(ast).Columns("user,targetID,targetType,level").Fields("?,?,?,2").Prepare(),
delete: acc.Delete(ast).Where("user=? AND targetID=? AND targetType=?").Prepare(),
deleteResource: acc.Delete(ast).Where("targetID=? AND targetType=?").Prepare(),
}, acc.FirstError()
}
func (s *DefaultSubscriptionStore) Add(uid, elementID int, elementType string) error {
2022-02-21 03:32:53 +00:00
_, err := s.add.Exec(uid, elementID, elementType)
return err
}
// TODO: Add a primary key to the activity subscriptions table
func (s *DefaultSubscriptionStore) Delete(uid, targetID int, targetType string) error {
2022-02-21 03:32:53 +00:00
_, err := s.delete.Exec(uid, targetID, targetType)
return err
}
func (s *DefaultSubscriptionStore) DeleteResource(targetID int, targetType string) error {
2022-02-21 03:32:53 +00:00
_, err := s.deleteResource.Exec(targetID, targetType)
return err
}