notification.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import "container/list"
  3. /*
  4. Notification Producer and Consumer Queue
  5. This module is designed to route the notification from module that produce it
  6. to all the devices or agent that can reach the user
  7. */
  8. type NotificationPayload struct {
  9. ID string //Notification ID, generate by producer
  10. Title string //Title of the notification
  11. Message string //Message of the notification
  12. Receiver []string //Receiver, username in arozos system
  13. Sender string //Sender, the sender or module of the notification
  14. ActionURL string //URL for futher action or open related pages (as url), leave empty if not appliable
  15. IsUrgent bool //Label this notification as urgent
  16. }
  17. //Notification Consumer, object that use to consume notification from queue
  18. type Consumer struct {
  19. Name string
  20. Desc string
  21. ListenTopicMode int
  22. Notify func(*NotificationPayload) error
  23. ListeningQueue *NotificationQueue
  24. }
  25. //Notification Producer, object that use to create and push notification into the queue
  26. type Producer struct {
  27. Name string
  28. Desc string
  29. PushTopicType int
  30. TargetQueue *NotificationQueue
  31. }
  32. type NotificationQueue struct {
  33. Producers []*Producer
  34. Consumers []*Consumer
  35. MasterQueue *list.List
  36. }
  37. func NewNotificationQueue() *NotificationQueue {
  38. thisQueue := list.New()
  39. return &NotificationQueue{
  40. Producers: []*Producer{},
  41. Consumers: []*Consumer{},
  42. MasterQueue: thisQueue,
  43. }
  44. }
  45. //Add a notification producer into the master queue
  46. func (n *NotificationQueue) AddNotificationProducer(p *Producer) {
  47. n.Producers = append(n.Producers, p)
  48. }
  49. //Add a notification consumer into the master queue
  50. func (n *NotificationQueue) AddNotificationConsumer(c *Consumer) {
  51. n.Consumers = append(n.Consumers, c)
  52. }
  53. //Push a notifiation to all consumers with same topic type
  54. func (n *NotificationQueue) PushNotification(TopicType int, message *NotificationPayload) {
  55. }