UsersForTopic loads users' subscriptions for a given topic (not channel readers). Public & Trusted are loaded.
(topic string, keepDeleted bool, opts *t.QueryOpt)
| 1707 | // UsersForTopic loads users' subscriptions for a given topic (not channel readers). |
| 1708 | // Public & Trusted are loaded. |
| 1709 | func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) { |
| 1710 | tcat := t.GetTopicCat(topic) |
| 1711 | |
| 1712 | // Fetch all subscribed users. The number of users is not large. |
| 1713 | filter := b.M{"topic": topic} |
| 1714 | if !keepDeleted && tcat != t.TopicCatP2P { |
| 1715 | // Filter out rows with DeletedAt being not null. |
| 1716 | // P2P topics must load all subscriptions otherwise it will be impossible |
| 1717 | // to swap Public values. |
| 1718 | filter["deletedat"] = b.M{"$exists": false} |
| 1719 | } |
| 1720 | |
| 1721 | limit := a.maxResults |
| 1722 | var oneUser t.Uid |
| 1723 | if opts != nil { |
| 1724 | // Ignore IfModifiedSince - we must return all entries |
| 1725 | // Those unmodified will be stripped of Public, Trusted & Private. |
| 1726 | |
| 1727 | if !opts.User.IsZero() { |
| 1728 | if tcat != t.TopicCatP2P { |
| 1729 | filter["user"] = opts.User.String() |
| 1730 | } |
| 1731 | oneUser = opts.User |
| 1732 | } |
| 1733 | if opts.Limit > 0 && opts.Limit < limit { |
| 1734 | limit = opts.Limit |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | cur, err := a.db.Collection("subscriptions").Find(a.ctx, filter, mdbopts.Find().SetLimit(int64(limit))) |
| 1739 | if err != nil { |
| 1740 | return nil, err |
| 1741 | } |
| 1742 | |
| 1743 | // Fetch subscriptions. |
| 1744 | var subs []t.Subscription |
| 1745 | join := make(map[string]t.Subscription) |
| 1746 | usrq := make([]any, 0, 16) |
| 1747 | for cur.Next(a.ctx) { |
| 1748 | var sub t.Subscription |
| 1749 | if err = cur.Decode(&sub); err != nil { |
| 1750 | break |
| 1751 | } |
| 1752 | join[sub.User] = sub |
| 1753 | usrq = append(usrq, sub.User) |
| 1754 | } |
| 1755 | cur.Close(a.ctx) |
| 1756 | if err != nil { |
| 1757 | return nil, err |
| 1758 | } |
| 1759 | |
| 1760 | // Fetch users by a list of subscriptions. |
| 1761 | if len(usrq) > 0 { |
| 1762 | subs = make([]t.Subscription, 0, len(usrq)) |
| 1763 | cur, err = a.db.Collection("users").Find(a.ctx, b.M{ |
| 1764 | "_id": b.M{"$in": usrq}, |
| 1765 | "state": b.M{"$ne": t.StateDeleted}}) |
| 1766 | if err != nil { |
nothing calls this directly
no test coverage detected