github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/notification/notifications.go (about)

     1  package notification
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/cozy/cozy-stack/model/permission"
     8  	"github.com/cozy/cozy-stack/pkg/consts"
     9  	"github.com/cozy/cozy-stack/pkg/couchdb"
    10  )
    11  
    12  // Properties is a notification type parameters, describing how a specific
    13  // notification group should behave.
    14  type Properties struct {
    15  	Description     string            `json:"description,omitempty"`
    16  	Collapsible     bool              `json:"collapsible,omitempty"`
    17  	Multiple        bool              `json:"multiple,omitempty"`
    18  	Stateful        bool              `json:"stateful,omitempty"`
    19  	DefaultPriority string            `json:"default_priority,omitempty"`
    20  	TimeToLive      time.Duration     `json:"time_to_live,omitempty"`
    21  	Templates       map[string]string `json:"templates,omitempty"`
    22  	MinInterval     time.Duration     `json:"min_interval,omitempty"`
    23  
    24  	MailTemplate string `json:"-"`
    25  }
    26  
    27  // Clone returns a cloned Properties struct pointer.
    28  func (p *Properties) Clone() *Properties {
    29  	cloned := *p
    30  	cloned.Templates = make(map[string]string, len(p.Templates))
    31  	for k, v := range p.Templates {
    32  		cloned.Templates[k] = v
    33  	}
    34  	return &cloned
    35  }
    36  
    37  // Notification data containing associated to an application a list of actions
    38  type Notification struct {
    39  	NID  string `json:"_id,omitempty"`
    40  	NRev string `json:"_rev,omitempty"`
    41  
    42  	SourceID   string `json:"source_id"`
    43  	Originator string `json:"originator,omitempty"`
    44  	Slug       string `json:"slug,omitempty"`
    45  	Category   string `json:"category"`
    46  	CategoryID string `json:"category_id,omitempty"`
    47  
    48  	CreatedAt time.Time `json:"created_at"`
    49  	LastSent  time.Time `json:"last_sent"`
    50  
    51  	Title    string                 `json:"title,omitempty"`
    52  	Message  string                 `json:"message,omitempty"`
    53  	Priority string                 `json:"priority,omitempty"`
    54  	Sound    string                 `json:"sound,omitempty"`
    55  	State    interface{}            `json:"state,omitempty"`
    56  	Data     map[string]interface{} `json:"data,omitempty"`
    57  
    58  	PreferredChannels []string `json:"preferred_channels,omitempty"`
    59  	At                string   `json:"at,omitempty"`
    60  
    61  	// XXX retro-compatible fields for sending rich mail
    62  	Content     string `json:"content,omitempty"`
    63  	ContentHTML string `json:"content_html,omitempty"`
    64  }
    65  
    66  // ID is used to implement the couchdb.Doc interface
    67  func (n *Notification) ID() string { return n.NID }
    68  
    69  // Rev is used to implement the couchdb.Doc interface
    70  func (n *Notification) Rev() string { return n.NRev }
    71  
    72  // DocType is used to implement the couchdb.Doc interface
    73  func (n *Notification) DocType() string { return consts.Notifications }
    74  
    75  // Clone implements couchdb.Doc
    76  func (n *Notification) Clone() couchdb.Doc {
    77  	cloned := *n
    78  	cloned.Data = make(map[string]interface{}, len(n.Data))
    79  	for k, v := range n.Data {
    80  		cloned.Data[k] = v
    81  	}
    82  	cloned.PreferredChannels = make([]string, len(n.PreferredChannels))
    83  	copy(cloned.PreferredChannels, n.PreferredChannels)
    84  	return &cloned
    85  }
    86  
    87  // SetID is used to implement the couchdb.Doc interface
    88  func (n *Notification) SetID(id string) { n.NID = id }
    89  
    90  // SetRev is used to implement the couchdb.Doc interface
    91  func (n *Notification) SetRev(rev string) { n.NRev = rev }
    92  
    93  // Fetch implements permissions.Fetcher
    94  func (n *Notification) Fetch(field string) []string { return nil }
    95  
    96  // Source returns the complete normalized source value. This should be recorded
    97  // in the `source_id` field.
    98  func (n *Notification) Source() string {
    99  	return fmt.Sprintf("cozy/%s/%s/%s/%s",
   100  		n.Originator,
   101  		n.Slug,
   102  		n.Category,
   103  		n.CategoryID)
   104  }
   105  
   106  var _ couchdb.Doc = &Notification{}
   107  var _ permission.Fetcher = &Notification{}