Skip to content

Commit

Permalink
HACK do not merge
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Dec 14, 2023
1 parent 814d9d1 commit 64ffea5
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions chatwoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -218,6 +220,68 @@ func main() {
}
}()

go func() {
// TEMP HACK: Go through all of the rooms and check for corresponding
// contacts over in Chatwoot.

log := log.With().Str("component", "contact_sync").Logger()

log.Info().Msg("starting contact fixing")

joined, err := client.JoinedRooms()
if err != nil {
log.Fatal().Err(err).Msg("Failed to get joined rooms")
}

for _, roomID := range joined.JoinedRooms {
stateMap, err := client.State(roomID)
if err != nil {
log.Warn().Err(err).Msg("Failed to get state for room")
continue
}

for mxid, membership := range stateMap[event.StateMember] {
if !strings.HasPrefix(mxid, "@twitter_") {
continue
}

req, err := http.NewRequest(http.MethodGet, chatwootAPI.MakeUri("contacts/search"), nil)
if err != nil {
log.Warn().Err(err).Msg("Failed to create request")
continue
}

q := req.URL.Query()
q.Add("q", mxid)
req.URL.RawQuery = q.Encode()

resp, err := chatwootAPI.DoRequest(req)
if err != nil {
log.Warn().Err(err).Msg("Failed to do request")
continue
}
if resp.StatusCode != 200 {
log.Warn().Int("status_code", resp.StatusCode).Msg("GET contacts/search returned non-200 status code")
continue
}

var contactsPayload chatwootapi.ContactsPayload
if err := json.NewDecoder(resp.Body).Decode(&contactsPayload); err != nil {
log.Warn().Err(err).Msg("Failed to decode response body")
continue
}

for _, contact := range contactsPayload.Payload {
if contact.Identifier == mxid {
log.Info().Str("mxid", mxid).Any("membership", membership).Msg("Found contact")
break
}
}
}
break // TODO remove
}
}()

// Make sure that there are conversations for all of the rooms that the bot
// is in.
// This is run every 24 hours.
Expand Down

0 comments on commit 64ffea5

Please sign in to comment.