import (
"context"
- "log"
+ "log/slog"
"maps"
"sync"
"time"
)
const (
+ // whatsappGroupsLookupTime is the longest time of a read of group
+ // names.
+ whatsappGroupsLookupTime = 10 * time.Second
+
// whatsappGroupsRefresh is the shortest time between two reads of the
// groups from WhatsApp for a group with no name.
whatsappGroupsRefresh = time.Minute
// newWhatsAppGroups returns the names of the groups with no names yet. logger
// gets the failures of a read.
-func newWhatsAppGroups(logger *log.Logger) *WhatsAppGroups {
+func newWhatsAppGroups(logger *slog.Logger) *WhatsAppGroups {
return &WhatsAppGroups{
logger: logger,
names: make(map[string]string),
// read, and readAt the time of the last read that succeeded. The mutex
// protects all fields after logger.
type WhatsAppGroups struct {
- logger *log.Logger
+ logger *slog.Logger
mutex sync.Mutex
names map[string]string
// again, at most once in whatsappGroupsRefresh.
func (g *WhatsAppGroups) name(ctx context.Context, link WhatsAppLink, id string) string {
g.mutex.Lock()
+
name, found := g.names[id]
fresh := time.Since(g.triedAt) < whatsappGroupsRefresh
g.mutex.Unlock()
g.read(ctx, link)
g.mutex.Lock()
+
name, found = g.names[id]
g.mutex.Unlock()
}
return false
}
- ctx, cancel := context.WithTimeout(ctx, whatsappLookupTime)
+ ctx, cancel := context.WithTimeout(ctx, whatsappGroupsLookupTime)
defer cancel()
groups, err := link.client.GetJoinedGroups(ctx)
if err != nil {
- g.logger.Printf("group names can be missing, because the list of groups did not load: %s", whatsappRedact(link.own, err.Error()))
+ g.logger.Warn("group list failed", "err", whatsappRedact(link.own, err.Error()))
}
g.mutex.Lock()