forked from rapidpro/mailroom
/
input_labels_added.go
73 lines (60 loc) · 2.15 KB
/
input_labels_added.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package hooks
import (
"context"
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/models"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func init() {
models.RegisterEventHook(events.TypeInputLabelsAdded, handleInputLabelsAdded)
}
// CommitAddedLabelsHook is our hook for input labels being added
type CommitAddedLabelsHook struct{}
var commitAddedLabelsHook = &CommitAddedLabelsHook{}
// Apply applies our input labels added, committing them in a single batch
func (h *CommitAddedLabelsHook) Apply(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, org *models.OrgAssets, sessions map[*models.Session][]interface{}) error {
// build our list of msg label adds, we dedupe these so we never double add in the same transaction
seen := make(map[string]bool)
adds := make([]*models.MsgLabelAdd, 0, len(sessions))
for _, as := range sessions {
for _, a := range as {
add := a.(*models.MsgLabelAdd)
key := fmt.Sprintf("%d:%d", add.LabelID, add.MsgID)
if !seen[key] {
adds = append(adds, add)
seen[key] = true
}
}
}
// insert our adds
return models.AddMsgLabels(ctx, tx, adds)
}
// handleInputLabelsAdded is called for each input labels added event in a session
func handleInputLabelsAdded(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, org *models.OrgAssets, session *models.Session, e flows.Event) error {
event := e.(*events.InputLabelsAddedEvent)
logrus.WithFields(logrus.Fields{
"contact_uuid": session.ContactUUID(),
"session_id": session.ID,
"labels": event.Labels,
}).Debug("input labels added")
// for each label add an insertion
for _, l := range event.Labels {
label := org.LabelByUUID(l.UUID)
if label == nil {
return errors.Errorf("unable to find label with UUID: %s", l.UUID)
}
if session.IncomingMsgID.IsZero() {
return errors.Errorf("cannot add label, no incoming message for session: %d", session.ID)
}
session.AddPreCommitEvent(commitAddedLabelsHook, &models.MsgLabelAdd{
MsgID: flows.MsgID(session.IncomingMsgID.Int64),
LabelID: label.ID(),
})
}
return nil
}