MCPcopy
hub / github.com/ddworken/hishtory / apiRegisterHandler

Method apiRegisterHandler

backend/server/internal/server/api_handlers.go:203–247  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

201}
202
203func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
204 userId := getRequiredQueryParam(r, "user_id")
205 deviceId := getRequiredQueryParam(r, "device_id")
206 isIntegrationTestDevice := getOptionalQueryParam(r, "is_integration_test_device", false) == "true"
207
208 if getMaximumNumberOfAllowedUsers() < math.MaxInt {
209 userAlreadyExist, err := s.db.UserAlreadyExist(r.Context(), userId)
210 if err != nil {
211 panic(fmt.Errorf("db.UserAlreadyExist: %w", err))
212 }
213
214 if !userAlreadyExist {
215 numDistinctUsers, err := s.db.DistinctUsers(r.Context())
216 if err != nil {
217 panic(fmt.Errorf("db.DistinctUsers: %w", err))
218 }
219 if numDistinctUsers >= int64(getMaximumNumberOfAllowedUsers()) {
220 panic(fmt.Sprintf("Refusing to allow registration of new device since there are currently %d users and this server allows a max of %d users", numDistinctUsers, getMaximumNumberOfAllowedUsers()))
221 }
222 }
223 }
224
225 existingDevicesCount, err := s.db.CountDevicesForUser(r.Context(), userId)
226 checkGormError(err)
227 fmt.Printf("apiRegisterHandler: existingDevicesCount=%d\n", existingDevicesCount)
228 if err := s.db.CreateDevice(r.Context(), &database.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: getRemoteAddr(r), RegistrationDate: time.Now(), IsIntegrationTestDevice: isIntegrationTestDevice}); err != nil {
229 checkGormError(err)
230 }
231
232 if existingDevicesCount > 0 {
233 err := s.db.DumpRequestCreate(r.Context(), &shared.DumpRequest{UserId: userId, RequestingDeviceId: deviceId, RequestTime: time.Now()})
234 checkGormError(err)
235 }
236
237 version := getHishtoryVersion(r)
238 remoteIPAddr := getRemoteAddr(r)
239 s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false))
240
241 if s.statsd != nil && !isIntegrationTestDevice {
242 s.statsd.Incr("hishtory.register", []string{}, 1.0)
243 }
244
245 w.Header().Set("Content-Length", "0")
246 w.WriteHeader(http.StatusOK)
247}
248
249func (s *Server) getDeletionRequestsHandler(w http.ResponseWriter, r *http.Request) {
250 userId := getRequiredQueryParam(r, "user_id")

Callers 5

TestESubmitThenQueryFunction · 0.95
TestDeletionRequestsFunction · 0.95
TestLimitRegistrationsFunction · 0.95

Calls 14

updateUsageDataMethod · 0.95
getRequiredQueryParamFunction · 0.85
getOptionalQueryParamFunction · 0.85
checkGormErrorFunction · 0.85
getRemoteAddrFunction · 0.85
getHishtoryVersionFunction · 0.85
UserAlreadyExistMethod · 0.80
DistinctUsersMethod · 0.80
CountDevicesForUserMethod · 0.80
CreateDeviceMethod · 0.80

Tested by 5

TestESubmitThenQueryFunction · 0.76
TestDeletionRequestsFunction · 0.76
TestLimitRegistrationsFunction · 0.76