|
4 | 4 | "encoding/json"
|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
| 7 | + "path" |
7 | 8 | "regexp"
|
8 | 9 | "strings"
|
9 | 10 | "time"
|
@@ -41,6 +42,12 @@ func (p *Push) GetIface() (PushIface, error) {
|
41 | 42 | return nil, err
|
42 | 43 | }
|
43 | 44 | return iface, nil
|
| 45 | + case PushTypeBark: |
| 46 | + var iface PushIfaceBark |
| 47 | + err := json.Unmarshal(p.Iface, &iface) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
44 | 51 | }
|
45 | 52 | return nil, errors.New("unknown push type")
|
46 | 53 | }
|
@@ -98,6 +105,7 @@ const (
|
98 | 105 | PushTypeIOS PushType = "ios"
|
99 | 106 | PushTypeWebhook = "webhook"
|
100 | 107 | PushTypeServerChan = "server_chan"
|
| 108 | + PushTypeBark = "bark" |
101 | 109 | )
|
102 | 110 |
|
103 | 111 | type PushIface interface {
|
@@ -218,3 +226,55 @@ func (p PushIfaceServerChan) push(args []*PushPair) error {
|
218 | 226 | }
|
219 | 227 | return nil
|
220 | 228 | }
|
| 229 | + |
| 230 | +type barkLevel string |
| 231 | + |
| 232 | +const ( |
| 233 | + barkLevelActive barkLevel = "active" |
| 234 | + barkLevelSensitive = "timeSensitive" |
| 235 | + barkLevelPassive = "passive" |
| 236 | +) |
| 237 | + |
| 238 | +type PushIfaceBark struct { |
| 239 | + Server string `json:"server"` |
| 240 | + Title string `json:"title"` |
| 241 | + Body string `json:"body"` |
| 242 | + Level barkLevel `json:"level"` |
| 243 | + BodyRegex string `json:"body_regex"` |
| 244 | + Code int `json:"code"` |
| 245 | +} |
| 246 | + |
| 247 | +func (p PushIfaceBark) push(args []*PushPair) error { |
| 248 | + body := p.Body |
| 249 | + for _, arg := range args { |
| 250 | + body = strings.Replace(body, arg.key, arg.value, 1) |
| 251 | + } |
| 252 | + if len(p.Server) == 0 { |
| 253 | + p.Server = "https://api.day.app" |
| 254 | + } |
| 255 | + url := path.Join( |
| 256 | + p.Server, |
| 257 | + p.Title, |
| 258 | + body, |
| 259 | + ) |
| 260 | + if len(p.Level) != 0 { |
| 261 | + url += fmt.Sprintf("?level=%s", p.Level) |
| 262 | + } |
| 263 | + resp, code, err := http.Do("GET", url, nil, nil) |
| 264 | + if err != nil { |
| 265 | + return err |
| 266 | + } |
| 267 | + if p.Code != 0 && code != p.Code { |
| 268 | + return fmt.Errorf("code: %d, resp: %s", code, string(resp)) |
| 269 | + } |
| 270 | + if p.BodyRegex != "" { |
| 271 | + reg, err := regexp.Compile(p.BodyRegex) |
| 272 | + if err != nil { |
| 273 | + return fmt.Errorf("compile regex failed: %s", err.Error()) |
| 274 | + } |
| 275 | + if !reg.Match(resp) { |
| 276 | + return fmt.Errorf("resp: %s", string(resp)) |
| 277 | + } |
| 278 | + } |
| 279 | + return nil |
| 280 | +} |
0 commit comments