go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/count/mail.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package count
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/gae/service/mail"
    21  )
    22  
    23  // MailCounter is the counter object for the Mail service.
    24  type MailCounter struct {
    25  	Send         Entry
    26  	SendToAdmins Entry
    27  }
    28  
    29  type mailCounter struct {
    30  	c *MailCounter
    31  
    32  	m mail.RawInterface
    33  }
    34  
    35  var _ mail.RawInterface = (*mailCounter)(nil)
    36  
    37  func (m *mailCounter) Send(msg *mail.Message) error {
    38  	return m.c.Send.up(m.m.Send(msg))
    39  }
    40  
    41  func (m *mailCounter) SendToAdmins(msg *mail.Message) error {
    42  	return m.c.SendToAdmins.up(m.m.SendToAdmins(msg))
    43  }
    44  
    45  func (m *mailCounter) GetTestable() mail.Testable {
    46  	return m.m.GetTestable()
    47  }
    48  
    49  // FilterMail installs a counter Mail filter in the context.
    50  func FilterMail(c context.Context) (context.Context, *MailCounter) {
    51  	state := &MailCounter{}
    52  	return mail.AddFilters(c, func(ic context.Context, u mail.RawInterface) mail.RawInterface {
    53  		return &mailCounter{state, u}
    54  	}), state
    55  }