MCPcopy
hub / github.com/tinode/chat / UsersForTopic

Method UsersForTopic

server/db/postgres/adapter.go:1809–1928  ·  view source on GitHub ↗

UsersForTopic loads users subscribed to the given topic. The difference between UsersForTopic vs SubsForTopic is that the former loads user.Public, the latter does not.

(topic string, keepDeleted bool, opts *t.QueryOpt)

Source from the content-addressed store, hash-verified

1807// The difference between UsersForTopic vs SubsForTopic is that the former loads user.Public,
1808// the latter does not.
1809func (a *adapter) UsersForTopic(topic string, keepDeleted bool, opts *t.QueryOpt) ([]t.Subscription, error) {
1810 tcat := t.GetTopicCat(topic)
1811
1812 // Fetch all subscribed users. The number of users is not large
1813 q := `SELECT s.createdat,s.updatedat,s.deletedat,s.userid,s.topic,s.delid,s.recvseqid,
1814 s.readseqid,s.modewant,s.modegiven,u.public,u.trusted,u.lastseen,u.useragent,s.private
1815 FROM subscriptions AS s JOIN users AS u ON s.userid=u.id
1816 WHERE s.topic=?`
1817 args := []any{topic}
1818 if !keepDeleted {
1819 // Filter out rows with users deleted
1820 q += " AND u.state!=?"
1821 args = append(args, t.StateDeleted)
1822
1823 // For p2p topics we must load all subscriptions including deleted.
1824 // Otherwise it will be impossible to swipe Public values.
1825 if tcat != t.TopicCatP2P {
1826 // Filter out deleted subscriptions.
1827 q += " AND s.deletedat IS NULL"
1828 }
1829 }
1830
1831 limit := a.maxResults
1832 var oneUser t.Uid
1833 if opts != nil {
1834 // Ignore IfModifiedSince: loading all entries because a topic cannot have too many subscribers.
1835 // Those unmodified will be stripped of Public & Private.
1836
1837 if !opts.User.IsZero() {
1838 // For p2p topics we have to fetch both users otherwise public cannot be swapped.
1839 if tcat != t.TopicCatP2P {
1840 q += " AND s.userid=?"
1841 args = append(args, store.DecodeUid(opts.User))
1842 }
1843 oneUser = opts.User
1844 }
1845 if opts.Limit > 0 && opts.Limit < limit {
1846 limit = opts.Limit
1847 }
1848 }
1849 q += " LIMIT ?"
1850 args = append(args, limit)
1851 q, args = expandQuery(q, args...)
1852
1853 ctx, cancel := a.getContext()
1854 if cancel != nil {
1855 defer cancel()
1856 }
1857 rows, err := a.db.Query(ctx, q, args...)
1858 if err != nil {
1859 return nil, err
1860 }
1861 defer rows.Close()
1862
1863 // Fetch subscriptions
1864 var sub t.Subscription
1865 var subs []t.Subscription
1866 var userId int64

Callers

nothing calls this directly

Calls 15

IsZeroMethod · 0.95
getContextMethod · 0.95
SetPublicMethod · 0.95
SetTrustedMethod · 0.95
SetLastSeenAndUAMethod · 0.95
DecodeUidFunction · 0.92
EncodeUidFunction · 0.92
expandQueryFunction · 0.85
NextMethod · 0.80
GetLastSeenMethod · 0.80
UidMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected