MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / getAllUserChannelsResponse

Method getAllUserChannelsResponse

rest/diagnostic_api.go:32–148  ·  view source on GitHub ↗
(user auth.User)

Source from the content-addressed store, hash-verified

30}
31
32func (h *handler) getAllUserChannelsResponse(user auth.User) (map[string]map[string]channelHistory, error) {
33 resp := make(map[string]map[string]channelHistory)
34
35 _, err := h.db.Authenticator(h.ctx()).RebuildChannels(user)
36 if err != nil {
37 return nil, err
38 }
39 err = h.db.Authenticator(h.ctx()).RebuildRoles(user)
40 if err != nil {
41 return nil, err
42 }
43
44 for _, dsName := range h.db.DataStoreNames() {
45 keyspace := dsName.ScopeName() + "." + dsName.CollectionName()
46 currentChannels, err := user.InheritedCollectionChannels(dsName.ScopeName(), dsName.CollectionName())
47 if err != nil {
48 return nil, fmt.Errorf("error retrieving inherited user channels: %w", err)
49 }
50 chanHistory := user.CollectionChannelHistory(dsName.ScopeName(), dsName.CollectionName())
51
52 // If no channels aside from public and no channels in history, don't make a key for this keyspace
53 if len(currentChannels) == 1 && len(chanHistory) == 0 {
54 continue
55 }
56 resp[keyspace] = make(map[string]channelHistory)
57 for chanName, chanEntry := range currentChannels {
58 if chanName == channels.DocumentStarChannel {
59 continue
60 }
61 resp[keyspace][chanName] = channelHistory{Entries: []auth.GrantHistorySequencePair{{StartSeq: chanEntry.Sequence, EndSeq: 0}}}
62 }
63
64 for chanName, chanEntry := range chanHistory {
65 chanHistoryEntry := channelHistory{Entries: chanEntry.Entries}
66 // if channel is also in history, append current entry to history entries
67 if _, chanCurrentlyAssigned := resp[keyspace][chanName]; chanCurrentlyAssigned {
68 var newEntries []auth.GrantHistorySequencePair
69 newEntries = append(chanEntry.Entries, resp[keyspace][chanName].Entries...)
70 chanHistoryEntry.Entries = newEntries
71 }
72 resp[keyspace][chanName] = chanHistoryEntry
73 }
74 }
75
76 for roleName, roleVbSeq := range user.RoleNames() {
77 role, err := h.db.Authenticator(h.ctx()).GetRoleIncDeleted(roleName)
78 if err != nil {
79 return nil, fmt.Errorf("error getting role: %w", err)
80 }
81 if role == nil {
82 continue
83 }
84
85 // default keyspace chan history
86 defaultKs := "_default._default"
87 if _, ok := resp[defaultKs]; !ok && len(maps.Keys(role.ChannelHistory())) != 0 {
88 resp[defaultKs] = make(map[string]channelHistory)
89 }

Callers 2

handleGetAllChannelsMethod · 0.95

Calls 15

ctxMethod · 0.95
RebuildChannelsMethod · 0.80
RebuildRolesMethod · 0.80
DataStoreNamesMethod · 0.80
ErrorfMethod · 0.80
GetRoleIncDeletedMethod · 0.80
RoleNamesMethod · 0.65
ChannelHistoryMethod · 0.65

Tested by

no test coverage detected