| 201 | } |
| 202 | |
| 203 | func (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 | |
| 249 | func (s *Server) getDeletionRequestsHandler(w http.ResponseWriter, r *http.Request) { |
| 250 | userId := getRequiredQueryParam(r, "user_id") |