func templateString(data interface{}, customTemplate []byte, message *notificationpb.Message, man template.Manager, suffix string, html bool) (str string, set bool, err error) { set = true var temp template.Template if len(customTemplate) > 0 { temp, err = template.NewTemplate(string(customTemplate), html) } else { if man.Exist(message.Event, message.Language, suffix) == nil { temp, err = man.Template(message.Event, message.Language, suffix) } else { return "", false, nil } } if err != nil { return "", true, err } str, err = temp.String(data) return }
func putContent(n *notification, message *notificationpb.Message, man template.Manager) error { p := message.GetPush() langs := man.Languages(message.Event, "push") data := make(map[string]interface{}) if err := json.Unmarshal(message.DataJson, &data); err != nil { return err } for k, v := range message.Tags { if _, ok := data[k]; !ok { data[k] = v } } for _, l := range langs { t, err := man.Template(message.Event, l, "push") if err != nil { return err } s, err := t.String(data) if err != nil { return err } n.Contents[l] = s } for _, l := range man.Languages(message.Event, "tit") { t, err := man.Template(message.Event, l, "tit") if err != nil { return err } s, err := t.String(data) if err != nil { return err } n.Headings[l] = s } if len(p.Template) > 0 { cts := make(map[string]string) if err := json.Unmarshal(p.Template, &cts); err != nil { if len(langs) == 0 { t, e := template.NewTemplate(string(p.Template), false) if e != nil { return e } s, _ := t.String(data) n.Contents["en"] = s n.Contents[message.Language] = s return nil } } else { for k, v := range cts { t, e := template.NewTemplate(v, false) if e != nil { continue } n.Contents[k], _ = t.String(data) } if _, ok := n.Contents["en"]; !ok { n.Contents["en"] = "" } return nil } } if len(p.Headings) > 0 { cts := make(map[string]string) if err := json.Unmarshal(p.Template, &cts); err != nil { if len(langs) == 0 { t, e := template.NewTemplate(string(p.Template), false) if e != nil { return e } s, _ := t.String(data) n.Headings["en"] = s n.Headings[message.Language] = s return nil } } else { for k, v := range cts { t, e := template.NewTemplate(v, false) if e != nil { continue } n.Headings[k], _ = t.String(data) } if _, ok := n.Headings["en"]; !ok { n.Headings["en"] = "" } return nil } } return nil }