notification

package module
v0.0.0-...-803885c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2021 License: MIT Imports: 7 Imported by: 21

README

Notification 通知模块

对通知系统进行规范和统一管理,建立必要的接口

状态:快速开发中,不稳定

Documentation

Index

Constants

View Source
const (
	//ConditionTopic store serach condition keyword for notification topic
	ConditionTopic = "topic"

	//ConditionNotificationID store serach condition keyword for notification id
	ConditionNotificationID = "notificationid"

	//ConditionTarget store serach condition keyword for notification target
	ConditionTarget = "target"

	//ConditionBatch store serach condition keyword for notification batch id
	ConditionBatch = "batch"

	//ConditionInContent store serach condition keyword for text in notification content
	ConditionInContent = "incontent"

	//ConditionSender store serach condition keyword for text in notification delivery keyword
	ConditionSender = "sender"

	//ConditionDelivery store serach condition keyword for text in notification delivery keyword
	ConditionDelivery = "delivery"

	//ConditionBeforeTimestamp store serach condition keyword for notification created-time before given timestamp
	ConditionBeforeTimestamp = "beforetimestamp"

	//ConditionAfterTimestamp store serach condition keyword for notification created-time after given timestamp
	ConditionAfterTimestamp = "aftertimestamp"

	//ConditionExpired store serach condition keyword for notification expired before given timestamp
	ConditionExpired = "expired"
)
View Source
const (
	//ErrnoSuccess error no for success delivery.
	ErrnoSuccess = int32(0)
	//ErrnoInvalidContent error  rasied when content invalid
	ErrnoInvalidContent = int32(1000)
)
View Source
const (
	//HeaderNameTarget header name for message targe
	HeaderNameTarget = "target"
	//HeaderNameBatch header name for notification batch id
	HeaderNameBatch = "batch"
	//HeaderNameMessage header name for mesage id
	HeaderNameMessage = "mesasge"
	//HeaderNameTopic header name for message topic
	HeaderNameTopic = "topic"
	//HeaderNameSender header name for message sender
	HeaderNameSender = "sender"
	//HeaderNameDraftMode header name for draft-mode
	HeaderNameDraftMode = "draftmode"
)
View Source
const DefaultStoreListLimit = 10

DefaultStoreListLimit default store list limit

Variables

View Source
var CheckerAlways = CheckerFunc(func(n *Notification) (bool, error) {
	return true, nil
})
View Source
var CheckerNever = CheckerFunc(func(n *Notification) (bool, error) {
	return false, nil
})
View Source
var ErrStoreFeatureNotSupported = errors.New("store feature not supported")

ErrStoreFeatureNotSupported error raised when store feature not supported

View Source
var InvalidContentTypeRequired = "required"

PlainFilterSupportedConditions plain filter suppoerted conditions

View Source
var SuggestedNotificationTTL = 24 * time.Hour

SuggestedNotificationTTL suggrested notification ttl

Functions

func ApplyToFilter

func ApplyToFilter(f Filter, conds []*Condition) error

ApplyToFilter apply condiitons to filter.

func CheckRequiredContent

func CheckRequiredContent(c Content, fields []string) []string

CheckRequiredContent check if fields in content. If give fields is not in missed fields will be returned.

func CheckRequiredContentError

func CheckRequiredContentError(c Content, fields []string) error

CheckRequiredContentError check if fields in content. If give fields is not in content,InvalidContentError will be returned. Otherwise nil will be returned.

func IsErrConditionNotSupported

func IsErrConditionNotSupported(err error) bool

IsErrConditionNotSupported check if given error is ErrConditionNotSupported

func IsErrInvalidConditionValue

func IsErrInvalidConditionValue(err error) bool

IsErrInvalidConditionValue check if given error is ErrInvalidConditionValue

func IsErrInvalidContent

func IsErrInvalidContent(err error) bool

IsErrInvalidContent check if given error is invalid content error.

func IsErrNotificationIDNotFound

func IsErrNotificationIDNotFound(err error) bool

IsErrNotificationIDNotFound check if given error is ErrNotificationIDNotFound.

Types

type Checker

type Checker interface {
	Check(n *Notification) (bool, error)
}

Checker notification checker interface Return if notification passed check

type CheckerFunc

type CheckerFunc func(n *Notification) (bool, error)

func (CheckerFunc) Check

func (f CheckerFunc) Check(n *Notification) (bool, error)

type Condition

type Condition struct {
	//Keyword condition keyword
	Keyword string
	//Value condition value to filter notification
	Value string
}

Condition store search condition

type ConditionContext

type ConditionContext struct {
	Time time.Time
}

func NewConditionContext

func NewConditionContext() *ConditionContext

type Content

type Content map[string]string

Content notification content type Content stores all messages in notification.

func NewContent

func NewContent() Content

NewContent create new content.

func (Content) Get

func (c Content) Get(name string) string

Get get give value with given name from content Name will be converted to lower

func (Content) Length

func (c Content) Length() int

Length return ccntent length

func (Content) MustJSON

func (c Content) MustJSON() string

MustJSON must convent content to json format Panic if any error raised

func (Content) Range

func (c Content) Range(f func(string, string) bool)

Range range over content with given function. Stop range if function return false,

func (Content) Set

func (c Content) Set(name string, value string)

Set set give value with given name to content Name will be converted to lower

type ErrConditionNotSupported

type ErrConditionNotSupported struct {
	Condition string
}

ErrConditionNotSupported error rasied when condition not supproted

func NewErrConditionNotSupported

func NewErrConditionNotSupported(condition string) *ErrConditionNotSupported

NewErrConditionNotSupported create new ErrConditionNotSupported

func (*ErrConditionNotSupported) Error

func (e *ErrConditionNotSupported) Error() string

Error return error message

type ErrInvalidConditionValue

type ErrInvalidConditionValue struct {
	Condition string
}

ErrInvalidConditionValue error raised when condition value invalid

func NewErrInvalidConditionValue

func NewErrInvalidConditionValue(condition string) *ErrInvalidConditionValue

NewErrInvalidConditionValue create new ErrInvalidConditionValue

func (*ErrInvalidConditionValue) Error

func (e *ErrInvalidConditionValue) Error() string

Error return error message

type ErrInvalidContent

type ErrInvalidContent struct {
	Fields []string
	Type   string
}

func NewErrInvalidContent

func NewErrInvalidContent(t string, fields ...string) *ErrInvalidContent

func NewRequiredContentError

func NewRequiredContentError(fields []string) *ErrInvalidContent

NewRequiredContentError create required content error with given fields.

func (*ErrInvalidContent) Error

func (e *ErrInvalidContent) Error() string

type ErrNotificationIDNotFound

type ErrNotificationIDNotFound struct {
	NID string
}

ErrNotificationIDNotFound error raised if given notification not found

func NewErrNotificationIDNotFound

func NewErrNotificationIDNotFound(nid string) *ErrNotificationIDNotFound

NewErrNotificationIDNotFound create new ErrNotificationIDNotFound

func (*ErrNotificationIDNotFound) Error

func (e *ErrNotificationIDNotFound) Error() string

Error return error message

type Filter

type Filter interface {
	//FilterNotification filter notification with given timestamp
	//Return if notification is valid
	FilterNotification(n *Notification, ctx *ConditionContext) (bool, error)
	//ApplyCondition apply search condition to filter
	//ErrConditionNotSupported should be returned if condition keyword is not supported
	ApplyCondition(cond *Condition) error
}

Filter notification filter interface

type HasHeaderChecker

type HasHeaderChecker string

func (HasHeaderChecker) Check

func (c HasHeaderChecker) Check(n *Notification) (bool, error)
type Header map[string]string

Header notification header struct

func NewHeader

func NewHeader() Header

NewHeader create new header

func (Header) Get

func (h Header) Get(name string) string

Get get give value with given name from header Name will be converted to lower

func (Header) Length

func (h Header) Length() int

Length return header length

func (Header) Range

func (h Header) Range(f func(string, string) bool)

Range range over header with given function. Stop range if function return false,

func (Header) Set

func (h Header) Set(name string, value string)

Set set give value with given name to header Name will be converted to lower

func (Header) String

func (h Header) String() string

String convert header to urlencoded format string

type NopStore

type NopStore struct{}

func (NopStore) Close

func (n NopStore) Close() error

Close close store and return any error if raised

func (NopStore) Count

func (n NopStore) Count(condition []*Condition) (int, error)

Count count store with given search conditions

func (NopStore) List

func (n NopStore) List(condition []*Condition, start string, asc bool, count int) (result []*Notification, iter string, err error)

List list no more than count notifactions in store with given search conditions form start position . Count should be greater than 0. Found notifications and next list position iter will be returned. Return largest id notification if asc is false.

func (NopStore) Open

func (n NopStore) Open() error

Open open store and return any error if raised

func (NopStore) Remove

func (n NopStore) Remove(id string) (*Notification, error)

Remove remove notification by given id and return removed notification.

func (NopStore) Save

func (n NopStore) Save(notification *Notification) error

Save save given notificaiton to store. Notification with same id will be overwritten.

func (NopStore) SupportedConditions

func (n NopStore) SupportedConditions() ([]string, error)

SupportedConditions return supported condition keyword list

type Notification

type Notification struct {
	//ID notification id
	ID string
	//Delivery notification delivery keyword
	Delivery string
	//CreatedTime notification created unix timestmamp
	CreatedTime int64
	//ExpiredTime notification expired unix timestamp
	//ExpiredTime 0 or less than 0 means never expired
	ExpiredTime int64
	//Header notification header
	Header Header
	//Content notification content
	Content Content
}

Notification notification struct

func New

func New() *Notification

New create new notification

func (*Notification) String

func (n *Notification) String() string

String return notification info in string format

type PlainFilter

type PlainFilter struct {
	BatchID        string
	NotificationID string
	Delivery       string
	Target         string
	Topic          string
	Sender         string
	InContent      string
	After          int64
	Before         int64
	Expired        bool
}

PlainFilter plain filter struct

func NewFilter

func NewFilter() *PlainFilter

NewFilter create new plain filter

func (*PlainFilter) ApplyCondition

func (c *PlainFilter) ApplyCondition(cond *Condition) error

ApplyCondition apply search condition to filter ErrConditionNotSupported should be returned if condition keyword is not supported

func (*PlainFilter) FilterNotification

func (c *PlainFilter) FilterNotification(n *Notification, ctx *ConditionContext) (bool, error)

FilterNotification filter notification with given timestamp Return if notification is valid

type Searchable

type Searchable interface {
	//Count count store with given search conditions
	Count(condition []*Condition) (int, error)
	//SupportedConditions return supported condition keyword list
	SupportedConditions() ([]string, error)
}

Searchable searchable interface

type Sender

type Sender interface {
	//Send send notification and return any error if raised.
	Send(*Notification) error
}

Sender sender interafce

type SenderFunc

type SenderFunc func(*Notification) error

SenderFunc sender func interface

func (SenderFunc) Send

func (f SenderFunc) Send(n *Notification) error

Send send notification and return any error if raised.

type Store

type Store interface {
	//Open open store and return any error if raised
	Open() error
	//Close close store and return any error if raised
	Close() error
	//Save save given notificaiton to store.
	//Notification with same id will be overwritten.
	Save(notification *Notification) error
	//List list no more than count notifactions in store with given search conditions form start position .
	//Count should be greater than 0.
	//Found notifications and next list position iter will be returned.
	//Return largest id notification if asc is false.
	List(condition []*Condition, start string, asc bool, count int) (result []*Notification, iter string, err error)
	//Count count store with given search conditions
	Count(condition []*Condition) (int, error)
	//SupportedConditions return supported condition keyword list
	SupportedConditions() ([]string, error)
	//Remove remove notification by given id and return removed notification.
	Remove(id string) (*Notification, error)
}

Store notitication store interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL