MCPcopy Create free account
hub / github.com/daodst/chat / claimRemoteKeys

Method claimRemoteKeys

keyserver/internal/internal.go:118–173  ·  view source on GitHub ↗
(
	ctx context.Context, timeout time.Duration, res *api.PerformClaimKeysResponse, domainToDeviceKeys map[string]map[string]map[string]string,
)

Source from the content-addressed store, hash-verified

116}
117
118func (a *KeyInternalAPI) claimRemoteKeys(
119 ctx context.Context, timeout time.Duration, res *api.PerformClaimKeysResponse, domainToDeviceKeys map[string]map[string]map[string]string,
120) {
121 resultCh := make(chan *gomatrixserverlib.RespClaimKeys, len(domainToDeviceKeys))
122 // allows us to wait until all federation servers have been poked
123 var wg sync.WaitGroup
124 wg.Add(len(domainToDeviceKeys))
125 // mutex for failures
126 var failMu sync.Mutex
127 util.GetLogger(ctx).WithField("num_servers", len(domainToDeviceKeys)).Info("Claiming remote keys from servers")
128
129 // fan out
130 for d, k := range domainToDeviceKeys {
131 go func(domain string, keysToClaim map[string]map[string]string) {
132 defer wg.Done()
133 fedCtx, cancel := context.WithTimeout(ctx, timeout)
134 defer cancel()
135 claimKeyRes, err := a.FedClient.ClaimKeys(fedCtx, gomatrixserverlib.ServerName(domain), keysToClaim)
136 if err != nil {
137 util.GetLogger(ctx).WithError(err).WithField("server", domain).Error("ClaimKeys failed")
138 failMu.Lock()
139 res.Failures[domain] = map[string]interface{}{
140 "message": err.Error(),
141 }
142 failMu.Unlock()
143 return
144 }
145 resultCh <- &claimKeyRes
146 }(d, k)
147 }
148
149 // Close the result channel when the goroutines have quit so the for .. range exits
150 go func() {
151 wg.Wait()
152 close(resultCh)
153 }()
154
155 keysClaimed := 0
156 for result := range resultCh {
157 for userID, nest := range result.OneTimeKeys {
158 res.OneTimeKeys[userID] = make(map[string]map[string]json.RawMessage)
159 for deviceID, nest2 := range nest {
160 res.OneTimeKeys[userID][deviceID] = make(map[string]json.RawMessage)
161 for keyIDWithAlgo, otk := range nest2 {
162 keyJSON, err := json.Marshal(otk)
163 if err != nil {
164 continue
165 }
166 res.OneTimeKeys[userID][deviceID][keyIDWithAlgo] = keyJSON
167 keysClaimed++
168 }
169 }
170 }
171 }
172 util.GetLogger(ctx).WithField("num_keys", keysClaimed).Info("Claimed remote keys")
173}
174
175func (a *KeyInternalAPI) PerformDeleteKeys(ctx context.Context, req *api.PerformDeleteKeysRequest, res *api.PerformDeleteKeysResponse) {

Callers 1

PerformClaimKeysMethod · 0.95

Calls 5

AddMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
ClaimKeysMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected