initial interface for notifications

This commit is contained in:
Umputun
2018-02-16 20:42:15 -06:00
parent 01f61f8fbd
commit a83db17d90
3 changed files with 25 additions and 0 deletions
+2
View File
@@ -24,6 +24,7 @@ func NewLoadingCache(defaultExpiration, cleanupInterval time.Duration, postFlush
return &loadingCache{bytesCache: cache.New(defaultExpiration, cleanupInterval), postFlushFn: postFlushFn}
}
// Get is loading cache method to get value by key or load via fn if not found
func (lc *loadingCache) Get(key string, ttl time.Duration, fn func() ([]byte, error)) (data []byte, err error) {
if b, ok := lc.bytesCache.Get(key); ok {
log.Printf("[DEBUG] cache hit %s", key)
@@ -38,6 +39,7 @@ func (lc *loadingCache) Get(key string, ttl time.Duration, fn func() ([]byte, er
return data, nil
}
// Flush clears cache and calls postFlushFn async
func (lc *loadingCache) Flush() {
lc.bytesCache.Flush()
if lc.postFlushFn != nil {
+6
View File
@@ -58,6 +58,12 @@ type BlockedUser struct {
Timestamp time.Time `json:"time"`
}
// NotifUser holds id and destination for notifiable user
type NotifUser struct {
ID string `json:"id"`
Destination string `json:"destination"`
}
// Sanitize clean dangerous html/js from the comment
func (c *Comment) Sanitize() {
p := bluemonday.UGCPolicy()
+17
View File
@@ -33,6 +33,23 @@ type Admin interface {
Blocked(siteID string) ([]BlockedUser, error) // get list of blocked users
}
// Notifier defines all store ops for update modifications
type Notifier interface {
Set(locator Locator, user NotifUser, scope NotifScope, status bool) error // subscribe / unsubscribe user to locator updates
Status(locator Locator, userID string) bool // get subscription status for user & locator
List(locator Locator) ([]NotifUser, error) // list all subscribed users
}
// NotifScope defines "enum" of notification scopes
type NotifScope int
// All NotifScope values
const (
ScopeSite NotifScope = 1
ScopePost NotifScope = 2
ScopeReply NotifScope = 3
)
func sortComments(comments []Comment, sortFld string) []Comment {
sort.Slice(comments, func(i, j int) bool {
switch sortFld {