Skip to content

Commit

Permalink
chatwoot/objects: strongly type contact ID
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 0e9cc5e commit 9aeff1c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion chatwoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func AllowKeyShare(ctx context.Context, device *id.Device, info event.RequestedK
log.Info().Err(err).Msg("couldn't get Chatwoot conversation")
return &crypto.KeyShareRejectNoResponse
}
log = log.With().Int("sender_identifier", conversation.Meta.Sender.ID).Logger()
log = log.With().Int("sender_identifier", int(conversation.Meta.Sender.ID)).Logger()

// This is the user that we expected for this Chatwoot conversation.
if conversation.Meta.Sender.Identifier == device.UserID.String() {
Expand Down
6 changes: 3 additions & 3 deletions chatwootapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (api *ChatwootAPI) MakeURI(endpoint string) string {
return url.String()
}

func (api *ChatwootAPI) CreateContact(ctx context.Context, userID id.UserID, name string) (int, error) {
func (api *ChatwootAPI) CreateContact(ctx context.Context, userID id.UserID, name string) (ContactID, error) {
log := zerolog.Ctx(ctx).With().
Str("component", "create_contact").
Stringer("user_id", userID).
Expand Down Expand Up @@ -125,7 +125,7 @@ func (api *ChatwootAPI) CreateContact(ctx context.Context, userID id.UserID, nam
return contactPayload.Payload.Contact.ID, nil
}

func (api *ChatwootAPI) ContactIDForMXID(ctx context.Context, userID id.UserID) (int, error) {
func (api *ChatwootAPI) ContactIDForMXID(ctx context.Context, userID id.UserID) (ContactID, error) {
log := zerolog.Ctx(ctx)
query := userID.String()
if userID.Homeserver() == "beeper.local" {
Expand Down Expand Up @@ -194,7 +194,7 @@ func (api *ChatwootAPI) GetChatwootConversation(ctx context.Context, conversatio
return &conversation, err
}

func (api *ChatwootAPI) CreateConversation(ctx context.Context, sourceID string, contactID int, additionalAttrs map[string]string) (*Conversation, error) {
func (api *ChatwootAPI) CreateConversation(ctx context.Context, sourceID string, contactID ContactID, additionalAttrs map[string]string) (*Conversation, error) {
values := map[string]any{
"source_id": sourceID,
"inbox_id": api.InboxID,
Expand Down
10 changes: 6 additions & 4 deletions chatwootapi/objects.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package chatwootapi

type ContactID int

// Contact
type Contact struct {
ID int `json:"id"`
Identifier string `json:"identifier"`
PhoneNumber string `json:"phone_number,omitempty"`
Email string `json:"email,omitempty"`
ID ContactID `json:"id"`
Identifier string `json:"identifier"`
PhoneNumber string `json:"phone_number,omitempty"`
Email string `json:"email,omitempty"`
}

type ContactsPayload struct {
Expand Down
4 changes: 2 additions & 2 deletions matrix-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func createChatwootConversation(ctx context.Context, roomID id.RoomID, contactMX
if err != nil {
return 0, fmt.Errorf("create contact failed for %s: %w", contactMXID, err)
}
log.Info().Int("contact_id", contactID).Msg("Contact created")
log.Info().Int("contact_id", int(contactID)).Msg("Contact created")
}

log = log.With().Int("contact_id", contactID).Logger()
log = log.With().Int("contact_id", int(contactID)).Logger()

log.Info().Msg("creating Chatwoot conversation")
conversation, err := chatwootAPI.CreateConversation(ctx, roomID.String(), contactID, customAttrs)
Expand Down

0 comments on commit 9aeff1c

Please sign in to comment.