diff --git a/chatwoot.go b/chatwoot.go index 1ed874b..471fc7a 100644 --- a/chatwoot.go +++ b/chatwoot.go @@ -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 diff --git a/chatwootapi/api.go b/chatwootapi/api.go index 270d5fa..6c7255f 100644 --- a/chatwootapi/api.go +++ b/chatwootapi/api.go @@ -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) @@ -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 @@ -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 } @@ -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 } @@ -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, @@ -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 } @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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) @@ -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 } @@ -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 @@ -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 } diff --git a/matrix-handler.go b/matrix-handler.go index 06c6fd7..6028904 100644 --- a/matrix-handler.go +++ b/matrix-handler.go @@ -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) } @@ -100,7 +100,7 @@ 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") } @@ -108,7 +108,7 @@ func createChatwootConversation(ctx context.Context, roomID id.RoomID, contactMX 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") } @@ -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) } @@ -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) } @@ -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") }