Skip to content

Commit

Permalink
reduce frequency of checks for bots that seems to be offline
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Feb 4, 2024
1 parent 80668ab commit d6d5cd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/deltachat-bot/deltabot-cli-go v0.5.0
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240203211634-c7ed4b2cd4c4
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240204004424-951cc3a470da
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deltachat-bot/deltabot-cli-go v0.5.0 h1:WHHbG5++bO65risHzBj2jo0AOpBhJjVTKt6XH7d1KYo=
github.com/deltachat-bot/deltabot-cli-go v0.5.0/go.mod h1:Lj4cmrHgOk0LKUOei5RAjx94VLTs11CfPhIqOBT+g8c=
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240203211634-c7ed4b2cd4c4 h1:XLOZK7ktfVlZbkAVW6yJvRA48ytmZFkxWKsmbYrbT/I=
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240203211634-c7ed4b2cd4c4/go.mod h1:Ctd0M0o87y2B0QSOn8QN6IMDWjHD7XzDKsjNMYwP208=
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240204004424-951cc3a470da h1:j7ghoKrimFDeXQit5d11XhLZHhiKIdxbPyUZveQcwg8=
github.com/deltachat/deltachat-rpc-client-go v1.127.1-0.20240204004424-951cc3a470da/go.mod h1:Ctd0M0o87y2B0QSOn8QN6IMDWjHD7XzDKsjNMYwP208=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down
28 changes: 23 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io"
"math"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -57,18 +58,35 @@ func updateStatusLoop(rpc *deltachat.Rpc) {
continue
}
logger := logger.With("acc", accId, "bot", bot.Addr)
contactId, err := rpc.CreateContact(accId, bot.Addr, "")
if err != nil {
logger.Error(err)
continue
}
chatId, err := rpc.GetChatIdByContactId(accId, contactId)
if err != nil {
logger.Error(err)
continue
}
if chatId != 0 {
contact, err := rpc.GetContact(accId, contactId)
if err != nil {
logger.Error(err)
continue
}
lastSeen := int(math.Round(time.Since(contact.LastSeen.Time).Hours()))
if lastSeen >= 1 && lastSeen%2 != 0 {
logger.Debug("skipping status check for offline bot")
continue
}
}
logger.Debug("checking bot status")
if strings.HasPrefix(strings.ToLower(bot.Url), "openpgp4fpr:") {
_, err := rpc.SecureJoin(accId, bot.Url)
if err != nil {
logger.Error(err)
}
} else {
contactId, err := rpc.CreateContact(accId, bot.Addr, "")
if err != nil {
logger.Error(err)
continue
}
chatId, err := rpc.CreateChatByContactId(accId, contactId)
if err != nil {
logger.Error(err)
Expand Down

0 comments on commit d6d5cd1

Please sign in to comment.