Skip to content

Commit 10aad1d

Browse files
authored
Merge pull request #688 from nyaruka/fetch_attachment_with_msg_id
Send msg id to courier fetch-attachments endpoint
2 parents fd9b784 + 3a02cfe commit 10aad1d

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

core/handlers/base_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func RunTestCases(t *testing.T, ctx context.Context, rt *runtime.Runtime, tcs []
188188
for _, s := range session {
189189
msg := msgsByContactID[s.ContactID()]
190190
if msg != nil {
191-
s.SetIncomingMsg(msg.ID(), "")
191+
s.SetIncomingMsg(models.MsgID(msg.ID()), "")
192192
}
193193
}
194194
return nil

core/ivr/ivr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func ResumeIVRFlow(
481481
case InputResume:
482482
resume, svcErr, err = buildMsgResume(ctx, rt, svc, channel, contact, urn, call, oa, r, res)
483483
if resume != nil {
484-
session.SetIncomingMsg(resume.(*resumes.MsgResume).Msg().ID(), null.NullString)
484+
session.SetIncomingMsg(models.MsgID(resume.(*resumes.MsgResume).Msg().ID()), null.NullString)
485485
}
486486

487487
case DialResume:

core/models/msgs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ RETURNING
627627
`
628628

629629
// UpdateMessage updates a message after handling
630-
func UpdateMessage(ctx context.Context, tx Queryer, msgID flows.MsgID, status MsgStatus, visibility MsgVisibility, msgType MsgType, flow FlowID, attachments []utils.Attachment, logUUIDs []ChannelLogUUID) error {
630+
func UpdateMessage(ctx context.Context, tx Queryer, msgID MsgID, status MsgStatus, visibility MsgVisibility, msgType MsgType, flow FlowID, attachments []utils.Attachment, logUUIDs []ChannelLogUUID) error {
631631
_, err := tx.ExecContext(ctx,
632632
`UPDATE
633633
msgs_msg

core/models/msgs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestNewOutgoingFlowMsg(t *testing.T) {
173173

174174
session := insertTestSession(t, ctx, rt, testdata.Org1, tc.Contact, testdata.Favorites)
175175
if tc.ResponseTo != models.NilMsgID {
176-
session.SetIncomingMsg(flows.MsgID(tc.ResponseTo), null.NullString)
176+
session.SetIncomingMsg(tc.ResponseTo, null.NullString)
177177
}
178178

179179
flowMsg := flows.NewMsgOut(tc.URN, assets.NewChannelReference(tc.ChannelUUID, "Test Channel"), tc.Text, tc.Attachments, tc.QuickReplies, nil, tc.Topic, tc.Unsendable)
@@ -327,7 +327,7 @@ func TestMarshalMsg(t *testing.T) {
327327
flows.NilUnsendableReason,
328328
)
329329
in1 := testdata.InsertIncomingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "test", models.MsgStatusHandled)
330-
session.SetIncomingMsg(flows.MsgID(in1.ID()), null.String("EX123"))
330+
session.SetIncomingMsg(models.MsgID(in1.ID()), null.String("EX123"))
331331
msg2, err := models.NewOutgoingFlowMsg(rt, oa.Org(), channel, session, flow, flowMsg2, time.Date(2021, 11, 9, 14, 3, 30, 0, time.UTC))
332332
require.NoError(t, err)
333333

core/models/sessions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func (s *Session) OutputMD5() string {
165165
}
166166

167167
// SetIncomingMsg set the incoming message that this session should be associated with in this sprint
168-
func (s *Session) SetIncomingMsg(id flows.MsgID, externalID null.String) {
169-
s.incomingMsgID = MsgID(id)
168+
func (s *Session) SetIncomingMsg(id MsgID, externalID null.String) {
169+
s.incomingMsgID = id
170170
s.incomingExternalID = externalID
171171
}
172172

core/msgio/courier.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ type fetchAttachmentRequest struct {
156156
ChannelType models.ChannelType `json:"channel_type"`
157157
ChannelUUID assets.ChannelUUID `json:"channel_uuid"`
158158
URL string `json:"url"`
159+
MsgID models.MsgID `json:"msg_id"`
159160
}
160161

161162
type fetchAttachmentResponse struct {
@@ -168,11 +169,12 @@ type fetchAttachmentResponse struct {
168169
}
169170

170171
// FetchAttachment calls courier to fetch the given attachment
171-
func FetchAttachment(ctx context.Context, rt *runtime.Runtime, ch *models.Channel, attURL string) (utils.Attachment, models.ChannelLogUUID, error) {
172+
func FetchAttachment(ctx context.Context, rt *runtime.Runtime, ch *models.Channel, attURL string, msgID models.MsgID) (utils.Attachment, models.ChannelLogUUID, error) {
172173
payload := jsonx.MustMarshal(&fetchAttachmentRequest{
173174
ChannelType: ch.Type(),
174175
ChannelUUID: ch.UUID(),
175176
URL: attURL,
177+
MsgID: msgID,
176178
})
177179
req, _ := http.NewRequest("POST", fmt.Sprintf("https://%s/c/_fetch-attachment", rt.Config.Domain), bytes.NewReader(payload))
178180
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rt.Config.CourierAuthToken))

core/tasks/handler/handler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func TestMsgEvents(t *testing.T) {
303303
ContactID: contact.ID,
304304
OrgID: org.ID,
305305
ChannelID: channel.ID,
306-
MsgID: dbMsg.ID(),
306+
MsgID: models.MsgID(dbMsg.ID()),
307307
MsgUUID: dbMsg.UUID(),
308308
URN: contact.URN,
309309
URNID: contact.URNID,
@@ -647,7 +647,7 @@ func TestTimedEvents(t *testing.T) {
647647
ContactID: tc.Contact.ID,
648648
OrgID: tc.Org.ID,
649649
ChannelID: tc.Channel.ID,
650-
MsgID: flows.MsgID(1),
650+
MsgID: models.MsgID(1),
651651
MsgUUID: flows.MsgUUID(uuids.New()),
652652
URN: tc.Contact.URN,
653653
URNID: tc.Contact.URNID,

core/tasks/handler/worker.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
495495
if utils.Attachment(attURL).ContentType() != "" {
496496
attachments = append(attachments, utils.Attachment(attURL))
497497
} else {
498-
attachment, logUUID, err := msgio.FetchAttachment(ctx, rt, channel, attURL)
498+
attachment, logUUID, err := msgio.FetchAttachment(ctx, rt, channel, attURL, event.MsgID)
499499
if err != nil {
500500
return errors.Wrapf(err, "error fetching attachment '%s'", attURL)
501501
}
@@ -598,7 +598,7 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
598598

599599
msgIn := flows.NewMsgIn(event.MsgUUID, event.URN, channel.ChannelReference(), event.Text, availableAttachments)
600600
msgIn.SetExternalID(string(event.MsgExternalID))
601-
msgIn.SetID(event.MsgID)
601+
msgIn.SetID(flows.MsgID(event.MsgID))
602602

603603
// build our hook to mark a flow message as handled
604604
flowMsgHook := func(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *models.OrgAssets, sessions []*models.Session) error {
@@ -783,7 +783,7 @@ func markMsgHandled(ctx context.Context, db models.Queryer, contact *flows.Conta
783783
flowID = flow.ID()
784784
}
785785

786-
err := models.UpdateMessage(ctx, db, msg.ID(), models.MsgStatusHandled, models.VisibilityVisible, msgType, flowID, attachments, logUUIDs)
786+
err := models.UpdateMessage(ctx, db, models.MsgID(msg.ID()), models.MsgStatusHandled, models.VisibilityVisible, msgType, flowID, attachments, logUUIDs)
787787
if err != nil {
788788
return errors.Wrapf(err, "error marking message as handled")
789789
}
@@ -813,7 +813,7 @@ type MsgEvent struct {
813813
ContactID models.ContactID `json:"contact_id"`
814814
OrgID models.OrgID `json:"org_id"`
815815
ChannelID models.ChannelID `json:"channel_id"`
816-
MsgID flows.MsgID `json:"msg_id"`
816+
MsgID models.MsgID `json:"msg_id"`
817817
MsgUUID flows.MsgUUID `json:"msg_uuid"`
818818
MsgExternalID null.String `json:"msg_external_id"`
819819
URN urns.URN `json:"urn"`

0 commit comments

Comments
 (0)