Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework LN handler to new send #700

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions handlers/line/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,10 @@
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
// TODO convert functionality from legacy method below
return nil
}

func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
authToken := msg.Channel().StringConfigForKey(courier.ConfigAuthToken, "")
if authToken == "" {
return nil, fmt.Errorf("no auth token set for LN channel: %s", msg.Channel().UUID())
return courier.ErrChannelConfig

Check warning on line 288 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L288

Added line #L288 was not covered by tests
}
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)

// all msg parts in JSON
var jsonMsgs []string
Expand All @@ -301,7 +295,7 @@

attachments, err := handlers.ResolveAttachments(ctx, h.Backend(), msg.Attachments(), mediaSupport, false)
if err != nil {
return nil, errors.Wrap(err, "error resolving attachments")
return errors.Wrap(err, "error resolving attachments")

Check warning on line 298 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L298

Added line #L298 was not covered by tests
}

// fill all msg parts with attachment parts
Expand Down Expand Up @@ -361,7 +355,7 @@
if batchCount == maxMsgSend || (i == len(jsonMsgs)-1) {
req, err := buildSendMsgRequest(authToken, msg.URN().Path(), msg.ResponseToExternalID(), batch)
if err != nil {
return status, err
return err

Check warning on line 358 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L358

Added line #L358 was not covered by tests
}

resp, respBody, err := h.RequestHTTP(req, clog)
Expand All @@ -375,38 +369,34 @@
respPayload := &mtResponse{}
err = json.Unmarshal(respBody, respPayload)
if err != nil {
clog.Error(courier.ErrorResponseUnparseable("JSON"))
return status, nil
return courier.ErrResponseUnparseable
}

errMsg := respPayload.Message
if errMsg == "Invalid reply token" {
req, err = buildSendMsgRequest(authToken, msg.URN().Path(), "", batch)
if err != nil {
return status, err
return err

Check warning on line 379 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L379

Added line #L379 was not covered by tests
}

resp, respBody, _ := h.RequestHTTP(req, clog)

respPayload := &mtResponse{}
err = json.Unmarshal(respBody, respPayload)
if err != nil {
clog.Error(courier.ErrorResponseUnparseable("JSON"))
return status, nil
return courier.ErrResponseUnparseable

Check warning on line 387 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L387

Added line #L387 was not covered by tests
}

if resp.StatusCode/100 != 2 {
clog.Error(courier.ErrorExternal(strconv.Itoa(resp.StatusCode), respPayload.Message))
return status, nil
return courier.ErrFailedWithReason(strconv.Itoa(resp.StatusCode), respPayload.Message)

Check warning on line 391 in handlers/line/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/line/handler.go#L391

Added line #L391 was not covered by tests
}
} else {
clog.Error(courier.ErrorExternal(strconv.Itoa(resp.StatusCode), respPayload.Message))
return status, err
return courier.ErrFailedWithReason(strconv.Itoa(resp.StatusCode), respPayload.Message)
}
}
}
status.SetStatus(courier.MsgStatusWired)
return status, nil

return nil
}

func buildSendMsgRequest(authToken, to string, replyToken string, jsonMsgs []string) (*http.Request, error) {
Expand Down
16 changes: 2 additions & 14 deletions handlers/line/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"Simple Message"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Long Send",
Expand All @@ -414,7 +413,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say,"},{"type":"text","text":"I need to keep adding more things to make it work"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Audio Attachment",
Expand All @@ -429,7 +427,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"audio","originalContentUrl":"http://mock.com/2345/test.m4a","duration":200},{"type":"text","text":"My Audio!"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Video Attachment",
Expand All @@ -444,7 +441,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"video","originalContentUrl":"http://mock.com/5678/test.mp4","previewImageUrl":"http://mock.com/4567/test.jpg"},{"type":"text","text":"My Video!"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Image Attachment",
Expand All @@ -459,7 +455,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"image","originalContentUrl":"http://mock.com/1234/test.jpg","previewImageUrl":"http://mock.com/1234/test.jpg"},{"type":"text","text":"My pic!"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Other Attachment",
Expand All @@ -474,7 +469,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"http://mock.com/7890/test.pdf"},{"type":"text","text":"My doc!"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Reply Message",
Expand All @@ -489,7 +483,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA","messages":[{"type":"text","text":"Simple Message"}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Quick Reply",
Expand All @@ -504,7 +497,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"Are you happy?","quickReply":{"items":[{"type":"action","action":{"type":"message","label":"Yes","text":"Yes"}},{"type":"action","action":{"type":"message","label":"No","text":"No"}}]}}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Quick Reply combined and attachment",
Expand All @@ -520,7 +512,6 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"image","originalContentUrl":"http://mock.com/1234/test.jpg","previewImageUrl":"http://mock.com/1234/test.jpg"},{"type":"text","text":"Are you happy?","quickReply":{"items":[{"type":"action","action":{"type":"message","label":"Yes","text":"Yes"}},{"type":"action","action":{"type":"message","label":"No","text":"No"}}]}}]}`,
},
},
ExpectedMsgStatus: "W",
},
{
Label: "Send Push Message If Invalid Reply",
Expand All @@ -535,7 +526,6 @@ var defaultSendTestCases = []OutgoingTestCase{
{Path: "/v2/bot/message/reply", Body: `{"replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA","messages":[{"type":"text","text":"Simple Message"}]}`},
{Path: "/v2/bot/message/push", Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"Simple Message"}]}`},
},
ExpectedMsgStatus: "W",
},
{
Label: "Invalid JSON response sending",
Expand All @@ -549,8 +539,7 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"Error Sending"}]}`,
},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorResponseUnparseable("JSON")},
ExpectedError: courier.ErrResponseUnparseable,
},
{
Label: "Error Sending",
Expand All @@ -564,8 +553,7 @@ var defaultSendTestCases = []OutgoingTestCase{
Body: `{"to":"uabcdefghij","messages":[{"type":"text","text":"Error Sending"}]}`,
},
},
ExpectedMsgStatus: "E",
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorExternal("403", "Failed to send messages")},
ExpectedError: courier.ErrFailedWithReason("403", "Failed to send messages"),
},
}

Expand Down
Loading