]> git.giacomolli.uk - onechat/blobdiff - whatsapp_groups.go
Order model and chat declarations
[onechat] / whatsapp_groups.go
index e8111c30864e278bcdcfac07c2654c2ce6bc1cba..5eedd5dc29e804472fcc4e6ca803c84c62454a4b 100644 (file)
@@ -2,13 +2,17 @@ package main
 
 import (
        "context"
 
 import (
        "context"
-       "log"
+       "log/slog"
        "maps"
        "sync"
        "time"
 )
 
 const (
        "maps"
        "sync"
        "time"
 )
 
 const (
+       // whatsappGroupLookupTimeout is the longest time of a read of group
+       // names.
+       whatsappGroupLookupTimeout = 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
        // whatsappGroupsRefresh is the shortest time between two reads of the
        // groups from WhatsApp for a group with no name.
        whatsappGroupsRefresh = time.Minute
@@ -21,22 +25,13 @@ const (
        whatsappGroupsAge = time.Hour
 )
 
        whatsappGroupsAge = time.Hour
 )
 
-// newWhatsAppGroups returns the names of the groups with no names yet. logger
-// gets the failures of a read.
-func newWhatsAppGroups(logger *log.Logger) *WhatsAppGroups {
-       return &WhatsAppGroups{
-               logger: logger,
-               names:  make(map[string]string),
-       }
-}
-
 // WhatsAppGroups holds the names of the groups of the account, by the ID of the
 // group. They come from the events of the groups and from reads of the groups
 // from WhatsApp, and they stay in memory only. triedAt is the time of the last
 // read, and readAt the time of the last read that succeeded. The mutex
 // protects all fields after logger.
 type WhatsAppGroups struct {
 // WhatsAppGroups holds the names of the groups of the account, by the ID of the
 // group. They come from the events of the groups and from reads of the groups
 // from WhatsApp, and they stay in memory only. triedAt is the time of the last
 // 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
 
        mutex   sync.Mutex
        names   map[string]string
@@ -44,11 +39,21 @@ type WhatsAppGroups struct {
        readAt  time.Time
 }
 
        readAt  time.Time
 }
 
+// newWhatsAppGroups returns the names of the groups with no names yet. logger
+// gets the failures of a read.
+func newWhatsAppGroups(logger *slog.Logger) *WhatsAppGroups {
+       return &WhatsAppGroups{
+               logger: logger,
+               names:  make(map[string]string),
+       }
+}
+
 // name returns the name of the group id, or whatsappUnknownGroup. A group that
 // is not in the names reads the groups from WhatsApp with the client of link
 // again, at most once in whatsappGroupsRefresh.
 func (g *WhatsAppGroups) name(ctx context.Context, link WhatsAppLink, id string) string {
        g.mutex.Lock()
 // name returns the name of the group id, or whatsappUnknownGroup. A group that
 // is not in the names reads the groups from WhatsApp with the client of link
 // 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()
        name, found := g.names[id]
        fresh := time.Since(g.triedAt) < whatsappGroupsRefresh
        g.mutex.Unlock()
@@ -57,6 +62,7 @@ func (g *WhatsAppGroups) name(ctx context.Context, link WhatsAppLink, id string)
                g.read(ctx, link)
 
                g.mutex.Lock()
                g.read(ctx, link)
 
                g.mutex.Lock()
+
                name, found = g.names[id]
                g.mutex.Unlock()
        }
                name, found = g.names[id]
                g.mutex.Unlock()
        }
@@ -75,12 +81,12 @@ func (g *WhatsAppGroups) read(ctx context.Context, link WhatsAppLink) bool {
                return false
        }
 
                return false
        }
 
-       ctx, cancel := context.WithTimeout(ctx, whatsappLookupTime)
+       ctx, cancel := context.WithTimeout(ctx, whatsappGroupLookupTimeout)
        defer cancel()
 
        groups, err := link.client.GetJoinedGroups(ctx)
        if err != nil {
        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()
        }
 
        g.mutex.Lock()