Skip to content

Commit

Permalink
chatwoot/api: add contexts everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Mar 26, 2024
1 parent 0f42214 commit 0e9cc5e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion chatwoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func AllowKeyShare(ctx context.Context, device *id.Device, info event.RequestedK
}
log = log.With().Int("conversation_id", conversationID).Logger()

conversation, err := chatwootAPI.GetChatwootConversation(conversationID)
conversation, err := chatwootAPI.GetChatwootConversation(ctx, conversationID)
if err != nil {
log.Info().Err(err).Msg("couldn't get Chatwoot conversation")
return &crypto.KeyShareRejectNoResponse
Expand Down
36 changes: 18 additions & 18 deletions chatwootapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (api *ChatwootAPI) DoRequest(req *http.Request) (*http.Response, error) {
return api.Client.Do(req)
}

func (api *ChatwootAPI) MakeUri(endpoint string) string {
func (api *ChatwootAPI) MakeURI(endpoint string) string {
url, err := url.Parse(api.BaseURL)
if err != nil {
panic(err)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (api *ChatwootAPI) CreateContact(ctx context.Context, userID id.UserID, nam
Identifier: name,
}
jsonValue, _ := json.Marshal(payload)
req, err := http.NewRequest(http.MethodPost, api.MakeUri("contacts"), bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI("contacts"), bytes.NewBuffer(jsonValue))
if err != nil {
log.Err(err).Msg("Failed to create request")
return 0, err
Expand Down Expand Up @@ -140,7 +140,7 @@ func (api *ChatwootAPI) ContactIDForMXID(ctx context.Context, userID id.UserID)

log.Info().Str("query", query).Msg("Searching for contact")

req, err := http.NewRequest(http.MethodGet, api.MakeUri("contacts/search"), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, api.MakeURI("contacts/search"), nil)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -175,8 +175,8 @@ func (api *ChatwootAPI) ContactIDForMXID(ctx context.Context, userID id.UserID)
return 0, fmt.Errorf("couldn't find user with user ID %s", query)
}

func (api *ChatwootAPI) GetChatwootConversation(conversationID int) (*Conversation, error) {
req, err := http.NewRequest(http.MethodGet, api.MakeUri(fmt.Sprintf("conversations/%d", conversationID)), nil)
func (api *ChatwootAPI) GetChatwootConversation(ctx context.Context, conversationID int) (*Conversation, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, api.MakeURI(fmt.Sprintf("conversations/%d", conversationID)), nil)
if err != nil {
return nil, err
}
Expand All @@ -194,7 +194,7 @@ func (api *ChatwootAPI) GetChatwootConversation(conversationID int) (*Conversati
return &conversation, err
}

func (api *ChatwootAPI) CreateConversation(sourceID string, contactID int, additionalAttrs map[string]string) (*Conversation, error) {
func (api *ChatwootAPI) CreateConversation(ctx context.Context, sourceID string, contactID int, additionalAttrs map[string]string) (*Conversation, error) {
values := map[string]any{
"source_id": sourceID,
"inbox_id": api.InboxID,
Expand All @@ -203,7 +203,7 @@ func (api *ChatwootAPI) CreateConversation(sourceID string, contactID int, addit
"additional_attributes": additionalAttrs,
}
jsonValue, _ := json.Marshal(values)
req, err := http.NewRequest(http.MethodPost, api.MakeUri("conversations"), bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI("conversations"), bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
Expand All @@ -222,8 +222,8 @@ func (api *ChatwootAPI) CreateConversation(sourceID string, contactID int, addit
return &conversation, err
}

func (api *ChatwootAPI) GetConversationLabels(conversationID int) ([]string, error) {
req, err := http.NewRequest(http.MethodGet, api.MakeUri(fmt.Sprintf("conversations/%d/labels", conversationID)), nil)
func (api *ChatwootAPI) GetConversationLabels(ctx context.Context, conversationID int) ([]string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, api.MakeURI(fmt.Sprintf("conversations/%d/labels", conversationID)), nil)
if err != nil {
return nil, err
}
Expand All @@ -242,12 +242,12 @@ func (api *ChatwootAPI) GetConversationLabels(conversationID int) ([]string, err
return labels.Payload, err
}

func (api *ChatwootAPI) SetConversationLabels(conversationID int, labels []string) error {
func (api *ChatwootAPI) SetConversationLabels(ctx context.Context, conversationID int, labels []string) error {
jsonValue, err := json.Marshal(map[string]any{"labels": labels})
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, api.MakeUri(fmt.Sprintf("conversations/%d/labels", conversationID)), bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI(fmt.Sprintf("conversations/%d/labels", conversationID)), bytes.NewBuffer(jsonValue))
if err != nil {
return err
}
Expand All @@ -269,7 +269,7 @@ func (api *ChatwootAPI) doSendTextMessage(ctx context.Context, conversationID in
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, api.MakeUri(fmt.Sprintf("conversations/%d/messages", conversationID)), bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI(fmt.Sprintf("conversations/%d/messages", conversationID)), bytes.NewBuffer(jsonValue))
if err != nil {
log.Err(err).Msg("Failed to create request")
return nil, err
Expand Down Expand Up @@ -306,7 +306,7 @@ func (api *ChatwootAPI) ToggleStatus(ctx context.Context, conversationID int, st
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, api.MakeUri(fmt.Sprintf("conversations/%d/toggle_status", conversationID)), bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI(fmt.Sprintf("conversations/%d/toggle_status", conversationID)), bytes.NewBuffer(jsonValue))
if err != nil {
return err
}
Expand All @@ -323,7 +323,7 @@ func (api *ChatwootAPI) ToggleStatus(ctx context.Context, conversationID int, st

var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")

func (api *ChatwootAPI) SendAttachmentMessage(conversationID int, filename string, mimeType string, fileData io.Reader, messageType MessageType) (*Message, error) {
func (api *ChatwootAPI) SendAttachmentMessage(ctx context.Context, conversationID int, filename string, mimeType string, fileData io.Reader, messageType MessageType) (*Message, error) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)

Expand Down Expand Up @@ -364,7 +364,7 @@ func (api *ChatwootAPI) SendAttachmentMessage(conversationID int, filename strin

bodyWriter.Close()

req, err := http.NewRequest(http.MethodPost, api.MakeUri(fmt.Sprintf("conversations/%d/messages", conversationID)), bodyBuf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, api.MakeURI(fmt.Sprintf("conversations/%d/messages", conversationID)), bodyBuf)
if err != nil {
return nil, err
}
Expand All @@ -391,7 +391,7 @@ func (api *ChatwootAPI) SendAttachmentMessage(conversationID int, filename strin

func (api *ChatwootAPI) DownloadAttachment(ctx context.Context, url string) ([]byte, error) {
log := zerolog.Ctx(ctx)
req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
log.Err(err).Msg("failed to create request")
return nil, err
Expand All @@ -413,8 +413,8 @@ func (api *ChatwootAPI) DownloadAttachment(ctx context.Context, url string) ([]b
return data, err
}

func (api *ChatwootAPI) DeleteMessage(conversationID int, messageID int) error {
req, err := http.NewRequest(http.MethodDelete, api.MakeUri(fmt.Sprintf("conversations/%d/messages/%d", conversationID, messageID)), nil)
func (api *ChatwootAPI) DeleteMessage(ctx context.Context, conversationID int, messageID int) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, api.MakeURI(fmt.Sprintf("conversations/%d/messages/%d", conversationID, messageID)), nil)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions matrix-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func createChatwootConversation(ctx context.Context, roomID id.RoomID, contactMX
log = log.With().Int("contact_id", contactID).Logger()

log.Info().Msg("creating Chatwoot conversation")
conversation, err := chatwootAPI.CreateConversation(roomID.String(), contactID, customAttrs)
conversation, err := chatwootAPI.CreateConversation(ctx, roomID.String(), contactID, customAttrs)
if err != nil {
return 0, fmt.Errorf("failed to create chatwoot conversation for %s: %w", roomID, err)
}
Expand Down Expand Up @@ -100,15 +100,15 @@ func createChatwootConversation(ctx context.Context, roomID id.RoomID, contactMX
time.Sleep(30 * time.Second)
log.Info().Msg("Adding canonical-dm label to conversation")

labels, err := chatwootAPI.GetConversationLabels(conversation.ID)
labels, err := chatwootAPI.GetConversationLabels(ctx, conversation.ID)
if err != nil {
log.Err(err).Msg("Failed to list conversation labels")
}
log.Debug().Msg("Got current conversation labels")
labels = append(labels, "canonical-dm")

log.Info().Strs("labels", labels).Msg("Setting conversation labels")
err = chatwootAPI.SetConversationLabels(conversation.ID, labels)
err = chatwootAPI.SetConversationLabels(ctx, conversation.ID, labels)
if err != nil {
log.Err(err).Msg("failed to add canonical-dm label to conversation")
}
Expand Down Expand Up @@ -436,7 +436,7 @@ func HandleMatrixMessageContent(ctx context.Context, evt *event.Event, conversat
mimeType = content.Info.MimeType
}

cm, err := chatwootAPI.SendAttachmentMessage(conversationID, filename, mimeType, bytes.NewReader(data), messageType)
cm, err := chatwootAPI.SendAttachmentMessage(ctx, conversationID, filename, mimeType, bytes.NewReader(data), messageType)
if err != nil {
return nil, fmt.Errorf("failed to send attachment message. Error: %w", err)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func HandleMatrixMessageContent(ctx context.Context, evt *event.Event, conversat
mimeType = part.Info.MimeType
}

cm, err := chatwootAPI.SendAttachmentMessage(conversationID, filename, mimeType, bytes.NewReader(data), messageType)
cm, err := chatwootAPI.SendAttachmentMessage(ctx, conversationID, filename, mimeType, bytes.NewReader(data), messageType)
if err != nil {
return nil, fmt.Errorf("failed to send attachment message. Error: %w", err)
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func HandleRedaction(ctx context.Context, evt *event.Event) {
}

for _, messageID := range messageIDs {
err = chatwootAPI.DeleteMessage(conversationID, messageID)
err = chatwootAPI.DeleteMessage(ctx, conversationID, messageID)
if err != nil {
log.Err(err).Msg("failed to delete Chatwoot message")
}
Expand Down

0 comments on commit 0e9cc5e

Please sign in to comment.