github.com/decred/politeia@v1.4.0/politeiawww/legacy/cmsevents.go (about)

     1  // Copyright (c) 2020 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package legacy
     6  
     7  import (
     8  	"github.com/decred/politeia/politeiawww/legacy/user"
     9  )
    10  
    11  const (
    12  	// CMS events
    13  	eventInvoiceComment      = "eventInvoiceComment"
    14  	eventInvoiceStatusUpdate = "eventInvoiceStatusUpdate"
    15  	eventDCCNew              = "eventDCCNew"
    16  	eventDCCSupportOppose    = "eventDCCSupportOppose"
    17  )
    18  
    19  func (p *Politeiawww) setupEventListenersCMS() {
    20  	// Setup invoice comment event
    21  	ch := make(chan interface{})
    22  	p.events.Register(eventInvoiceComment, ch)
    23  	go p.handleEventInvoiceComment(ch)
    24  
    25  	// Setup invoice status update event
    26  	ch = make(chan interface{})
    27  	p.events.Register(eventInvoiceStatusUpdate, ch)
    28  	go p.handleEventInvoiceStatusUpdate(ch)
    29  
    30  	// Setup DCC new update event
    31  	ch = make(chan interface{})
    32  	p.events.Register(eventDCCNew, ch)
    33  	go p.handleEventDCCNew(ch)
    34  
    35  	// Setup DCC support/oppose event
    36  	ch = make(chan interface{})
    37  	p.events.Register(eventDCCSupportOppose, ch)
    38  	go p.handleEventDCCSupportOppose(ch)
    39  }
    40  
    41  type dataInvoiceComment struct {
    42  	token string // Comment token
    43  	email string // User email
    44  }
    45  
    46  func (p *Politeiawww) handleEventInvoiceComment(ch chan interface{}) {
    47  	for msg := range ch {
    48  		d, ok := msg.(dataInvoiceComment)
    49  		if !ok {
    50  			log.Errorf("handleEventInvoiceComment invalid msg: %v", msg)
    51  			continue
    52  		}
    53  
    54  		err := p.emailInvoiceNewComment(d.email)
    55  		if err != nil {
    56  			log.Errorf("emailInvoiceNewComment %v: %v", err)
    57  		}
    58  
    59  		log.Debugf("Sent invoice comment notification %v", d.token)
    60  	}
    61  }
    62  
    63  type dataInvoiceStatusUpdate struct {
    64  	token string // Comment token
    65  	email string // User email
    66  }
    67  
    68  func (p *Politeiawww) handleEventInvoiceStatusUpdate(ch chan interface{}) {
    69  	for msg := range ch {
    70  		d, ok := msg.(dataInvoiceStatusUpdate)
    71  		if !ok {
    72  			log.Errorf("handleEventInvoiceStatusUpdate invalid msg: %v", msg)
    73  			continue
    74  		}
    75  
    76  		err := p.emailInvoiceStatusUpdate(d.token, d.email)
    77  		if err != nil {
    78  			log.Errorf("emailInvoiceStatusUpdate %v: %v", err)
    79  		}
    80  
    81  		log.Debugf("Sent invoice status update notification %v", d.token)
    82  	}
    83  }
    84  
    85  type dataDCCNew struct {
    86  	token string // DCC token
    87  }
    88  
    89  func (p *Politeiawww) handleEventDCCNew(ch chan interface{}) {
    90  	for msg := range ch {
    91  		d, ok := msg.(dataDCCNew)
    92  		if !ok {
    93  			log.Errorf("handleEventDCCNew invalid msg: %v", msg)
    94  			continue
    95  		}
    96  
    97  		emails := make([]string, 0, 256)
    98  		err := p.db.AllUsers(func(u *user.User) {
    99  			// Check circumstances where we don't notify
   100  			switch {
   101  			case !u.Admin:
   102  				// Only notify admin users
   103  				return
   104  			case u.Deactivated:
   105  				// Never notify deactivated users
   106  				return
   107  			}
   108  
   109  			emails = append(emails, u.Email)
   110  		})
   111  		if err != nil {
   112  			log.Errorf("handleEventDCCNew: AllUsers: %v", err)
   113  		}
   114  
   115  		err = p.emailDCCSubmitted(d.token, emails)
   116  		if err != nil {
   117  			log.Errorf("emailDCCSubmitted %v: %v", err)
   118  		}
   119  
   120  		log.Debugf("Sent DCC new notification %v", d.token)
   121  	}
   122  }
   123  
   124  type dataDCCSupportOppose struct {
   125  	token string // DCC token
   126  }
   127  
   128  func (p *Politeiawww) handleEventDCCSupportOppose(ch chan interface{}) {
   129  	for msg := range ch {
   130  		d, ok := msg.(dataDCCSupportOppose)
   131  		if !ok {
   132  			log.Errorf("handleEventDCCSupportOppose invalid msg: %v", msg)
   133  			continue
   134  		}
   135  
   136  		emails := make([]string, 0, 256)
   137  		err := p.db.AllUsers(func(u *user.User) {
   138  			// Check circumstances where we don't notify
   139  			switch {
   140  			case !u.Admin:
   141  				// Only notify admin users
   142  				return
   143  			case u.Deactivated:
   144  				// Never notify deactivated users
   145  				return
   146  			}
   147  
   148  			emails = append(emails, u.Email)
   149  		})
   150  		if err != nil {
   151  			log.Errorf("handleEventDCCSupportOppose: AllUsers: %v", err)
   152  		}
   153  
   154  		err = p.emailDCCSupportOppose(d.token, emails)
   155  		if err != nil {
   156  			log.Errorf("emailDCCSupportOppose %v: %v", err)
   157  		}
   158  
   159  		log.Debugf("Sent DCC support/oppose notification %v", d.token)
   160  	}
   161  }