-// groupName returns the name of the group id, or whatsappUnknownGroup. A group
-// that is not in the names of the groups reads the groups from WhatsApp again,
-// at most once in whatsappGroupsRefresh.
-func (w *WhatsApp) groupName(ctx context.Context, id string) string {
- w.mutex.Lock()
- name, found := w.groupNames[id]
- fresh := time.Since(w.groupsAt) < whatsappGroupsRefresh
- w.mutex.Unlock()
-
- if !found && !fresh {
- w.readGroups(ctx)
-
- w.mutex.Lock()
- name, found = w.groupNames[id]
- w.mutex.Unlock()
- }
-
- if !found || name == "" {
- return whatsappUnknownGroup
- }
-
- return name
-}
-
-// readGroups reads the names of the groups of the account from WhatsApp, and
-// tells if it could. They stay in memory only.
-func (w *WhatsApp) readGroups(ctx context.Context) bool {
- w.mutex.Lock()
- client := w.client
- w.mutex.Unlock()
-
- if client == nil {
- return false
- }
-
- ctx, cancel := context.WithTimeout(ctx, whatsappLookupTime)
- defer cancel()
-
- groups, err := client.GetJoinedGroups(ctx)
- if err != nil {
- w.logger.Printf("read the groups: %s", w.redact(err.Error()))
- }
-
- w.mutex.Lock()
- defer w.mutex.Unlock()
-
- w.groupsAt = time.Now()
- if err != nil {
- return false
- }
-
- w.groupsRead = w.groupsAt
- for _, group := range groups {
- w.groupNames[group.JID.String()] = group.Name
- }
-
- return true
-}
-