Debian Codegraph @

pushrules/action.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package pushrules
8
9 import "encoding/json"
10
11 // PushActionType is the type of a PushAction
12 type PushActionType string
13
14 // The allowed push action types as specified in spec section 11.12.1.4.1.
15 const (
16 ActionNotify PushActionType = "notify"
17 ActionDontNotify PushActionType = "dont_notify"
18 ActionCoalesce PushActionType = "coalesce"
19 ActionSetTweak PushActionType = "set_tweak"
20 )
21
22 // PushActionTweak is the type of the tweak in SetTweak push actions.
23 type PushActionTweak string
24
25 // The allowed tweak types as specified in spec section 11.12.1.4.1.1.
26 const (
27 TweakSound PushActionTweak = "sound"
28 TweakHighlight PushActionTweak = "highlight"
29 )
30
31 // PushActionArray is an array of PushActions.
32 type PushActionArray []*PushAction
33
34 // PushActionArrayShould contains the important information parsed from a PushActionArray.
35 type PushActionArrayShould struct {
36 // Whether or not the array contained a Notify, DontNotify or Coalesce action type.
37 NotifySpecified bool
38 // Whether or not the event in question should trigger a notification.
39 Notify bool
40 // Whether or not the event in question should be highlighted.
41 Highlight bool
42
43 // Whether or not the event in question should trigger a sound alert.
44 PlaySound bool
45 // The name of the sound to play if PlaySound is true.
46 SoundName string
47 }
48
49 // Should parses this push action array and returns the relevant details wrapped in a PushActionArrayShould struct.
50 func (actions PushActionArray) Should() (should PushActionArrayShould) {
51 for _, action := range actions {
52 switch action.Action {
53 case ActionNotify, ActionCoalesce:
54 should.Notify = true
55 should.NotifySpecified = true
56 case ActionDontNotify:
57 should.Notify = false
58 should.NotifySpecified = true
59 case ActionSetTweak:
60 switch action.Tweak {
61 case TweakHighlight:
62 var ok bool
63 should.Highlight, ok = action.Value.(bool)
64 if !ok {
65 // Highlight value not specified, so assume true since the tweak is set.
66 should.Highlight = true
67 }
68 case TweakSound:
69 should.SoundName = action.Value.(string)
70 should.PlaySound = len(should.SoundName) > 0
71 }
72 }
73 }
74 return
75 }
76
77 // PushAction is a single action that should be triggered when receiving a message.
78 type PushAction struct {
79 Action PushActionType
80 Tweak PushActionTweak
81 Value interface{}
82 }
83
84 // UnmarshalJSON parses JSON into this PushAction.
85 //
86 // * If the JSON is a single string, the value is stored in the Action field.
87 // * If the JSON is an object with the set_tweak field, Action will be set to
88 // "set_tweak", Tweak will be set to the value of the set_tweak field and
89 // and Value will be set to the value of the value field.
90 // * In any other case, the function does nothing.
91 func (action *PushAction) UnmarshalJSON(raw []byte) error {
92 var data interface{}
93
94 err := json.Unmarshal(raw, &data)
95 if err != nil {
96 return err
97 }
98
99 switch val := data.(type) {
100 case string:
101 action.Action = PushActionType(val)
102 case map[string]interface{}:
103 tweak, ok := val["set_tweak"].(string)
104 if ok {
105 action.Action = ActionSetTweak
106 action.Tweak = PushActionTweak(tweak)
107 action.Value, _ = val["value"]
108 }
109 }
110 return nil
111 }
112
113 // MarshalJSON is the reverse of UnmarshalJSON()
114 func (action *PushAction) MarshalJSON() (raw []byte, err error) {
115 if action.Action == ActionSetTweak {
116 data := map[string]interface{}{
117 "set_tweak": action.Tweak,
118 "value": action.Value,
119 }
120 return json.Marshal(&data)
121 }
122 data := string(action.Action)
123 return json.Marshal(&data)
124 }