Coverage for src/couchers/models.py: 99%

1071 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1import enum 

2 

3from geoalchemy2.types import Geometry 

4from google.protobuf import empty_pb2 

5from sqlalchemy import ( 

6 ARRAY, 

7 BigInteger, 

8 Boolean, 

9 CheckConstraint, 

10 Column, 

11 Date, 

12 DateTime, 

13 Enum, 

14 Float, 

15 ForeignKey, 

16 Index, 

17 Integer, 

18 Interval, 

19 MetaData, 

20 Sequence, 

21 String, 

22 UniqueConstraint, 

23) 

24from sqlalchemy import LargeBinary as Binary 

25from sqlalchemy.dialects.postgresql import INET, TSTZRANGE, ExcludeConstraint 

26from sqlalchemy.ext.associationproxy import association_proxy 

27from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property 

28from sqlalchemy.orm import backref, column_property, declarative_base, deferred, relationship 

29from sqlalchemy.sql import and_, func, text 

30from sqlalchemy.sql import select as sa_select 

31 

32from couchers import urls 

33from couchers.config import config 

34from couchers.constants import ( 

35 DATETIME_INFINITY, 

36 DATETIME_MINUS_INFINITY, 

37 EMAIL_REGEX, 

38 GUIDELINES_VERSION, 

39 PHONE_VERIFICATION_LIFETIME, 

40 SMS_CODE_LIFETIME, 

41 TOS_VERSION, 

42) 

43from couchers.utils import date_in_timezone, get_coordinates, last_active_coarsen, now 

44from proto import notification_data_pb2 

45 

46meta = MetaData( 

47 naming_convention={ 

48 "ix": "ix_%(column_0_label)s", 

49 "uq": "uq_%(table_name)s_%(column_0_name)s", 

50 "ck": "ck_%(table_name)s_%(constraint_name)s", 

51 "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", 

52 "pk": "pk_%(table_name)s", 

53 } 

54) 

55 

56Base = declarative_base(metadata=meta) 

57 

58 

59class HostingStatus(enum.Enum): 

60 can_host = enum.auto() 

61 maybe = enum.auto() 

62 cant_host = enum.auto() 

63 

64 

65class MeetupStatus(enum.Enum): 

66 wants_to_meetup = enum.auto() 

67 open_to_meetup = enum.auto() 

68 does_not_want_to_meetup = enum.auto() 

69 

70 

71class SmokingLocation(enum.Enum): 

72 yes = enum.auto() 

73 window = enum.auto() 

74 outside = enum.auto() 

75 no = enum.auto() 

76 

77 

78class SleepingArrangement(enum.Enum): 

79 private = enum.auto() 

80 common = enum.auto() 

81 shared_room = enum.auto() 

82 shared_space = enum.auto() 

83 

84 

85class ParkingDetails(enum.Enum): 

86 free_onsite = enum.auto() 

87 free_offsite = enum.auto() 

88 paid_onsite = enum.auto() 

89 paid_offsite = enum.auto() 

90 

91 

92class TimezoneArea(Base): 

93 __tablename__ = "timezone_areas" 

94 id = Column(BigInteger, primary_key=True) 

95 

96 tzid = Column(String) 

97 geom = Column(Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=False) 

98 

99 __table_args__ = ( 

100 Index( 

101 "ix_timezone_areas_geom_tzid", 

102 geom, 

103 tzid, 

104 postgresql_using="gist", 

105 ), 

106 ) 

107 

108 

109class User(Base): 

110 """ 

111 Basic user and profile details 

112 """ 

113 

114 __tablename__ = "users" 

115 

116 id = Column(BigInteger, primary_key=True) 

117 

118 username = Column(String, nullable=False, unique=True) 

119 email = Column(String, nullable=False, unique=True) 

120 # stored in libsodium hash format, can be null for email login 

121 hashed_password = Column(Binary, nullable=False) 

122 # phone number in E.164 format with leading +, for example "+46701740605" 

123 phone = Column(String, nullable=True, server_default=text("NULL")) 

124 

125 # timezones should always be UTC 

126 ## location 

127 # point describing their location. EPSG4326 is the SRS (spatial ref system, = way to describe a point on earth) used 

128 # by GPS, it has the WGS84 geoid with lat/lon 

129 geom = Column(Geometry(geometry_type="POINT", srid=4326), nullable=True) 

130 # their display location (displayed to other users), in meters 

131 geom_radius = Column(Float, nullable=True) 

132 # the display address (text) shown on their profile 

133 city = Column(String, nullable=False) 

134 # "Grew up in" on profile 

135 hometown = Column(String, nullable=True) 

136 

137 regions_visited = relationship("Region", secondary="regions_visited", order_by="Region.name") 

138 regions_lived = relationship("Region", secondary="regions_lived", order_by="Region.name") 

139 

140 timezone = column_property( 

141 sa_select(TimezoneArea.tzid).where(func.ST_Contains(TimezoneArea.geom, geom)).limit(1).scalar_subquery(), 

142 deferred=True, 

143 ) 

144 

145 joined = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

146 last_active = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

147 

148 # id of the last message that they received a notification about 

149 last_notified_message_id = Column(BigInteger, nullable=False, default=0) 

150 # same as above for host requests 

151 last_notified_request_message_id = Column(BigInteger, nullable=False, server_default=text("0")) 

152 

153 # display name 

154 name = Column(String, nullable=False) 

155 gender = Column(String, nullable=False) 

156 pronouns = Column(String, nullable=True) 

157 birthdate = Column(Date, nullable=False) # in the timezone of birthplace 

158 

159 avatar_key = Column(ForeignKey("uploads.key"), nullable=True) 

160 

161 hosting_status = Column(Enum(HostingStatus), nullable=False) 

162 meetup_status = Column(Enum(MeetupStatus), nullable=False, server_default="open_to_meetup") 

163 

164 # community standing score 

165 community_standing = Column(Float, nullable=True) 

166 

167 occupation = Column(String, nullable=True) # CommonMark without images 

168 education = Column(String, nullable=True) # CommonMark without images 

169 

170 # "Who I am" under "About Me" tab 

171 about_me = Column(String, nullable=True) # CommonMark without images 

172 # "What I do in my free time" under "About Me" tab 

173 things_i_like = Column(String, nullable=True) # CommonMark without images 

174 # "About my home" under "My Home" tab 

175 about_place = Column(String, nullable=True) # CommonMark without images 

176 # "Additional information" under "About Me" tab 

177 additional_information = Column(String, nullable=True) # CommonMark without images 

178 

179 is_banned = Column(Boolean, nullable=False, server_default=text("false")) 

180 is_deleted = Column(Boolean, nullable=False, server_default=text("false")) 

181 is_superuser = Column(Boolean, nullable=False, server_default=text("false")) 

182 

183 # the undelete token allows a user to recover their account for a couple of days after deletion in case it was 

184 # accidental or they changed their mind 

185 # constraints make sure these are non-null only if is_deleted and that these are null in unison 

186 undelete_token = Column(String, nullable=True) 

187 # validity of the undelete token 

188 undelete_until = Column(DateTime(timezone=True), nullable=True) 

189 

190 # hosting preferences 

191 max_guests = Column(Integer, nullable=True) 

192 last_minute = Column(Boolean, nullable=True) 

193 has_pets = Column(Boolean, nullable=True) 

194 accepts_pets = Column(Boolean, nullable=True) 

195 pet_details = Column(String, nullable=True) # CommonMark without images 

196 has_kids = Column(Boolean, nullable=True) 

197 accepts_kids = Column(Boolean, nullable=True) 

198 kid_details = Column(String, nullable=True) # CommonMark without images 

199 has_housemates = Column(Boolean, nullable=True) 

200 housemate_details = Column(String, nullable=True) # CommonMark without images 

201 wheelchair_accessible = Column(Boolean, nullable=True) 

202 smoking_allowed = Column(Enum(SmokingLocation), nullable=True) 

203 smokes_at_home = Column(Boolean, nullable=True) 

204 drinking_allowed = Column(Boolean, nullable=True) 

205 drinks_at_home = Column(Boolean, nullable=True) 

206 # "Additional information" under "My Home" tab 

207 other_host_info = Column(String, nullable=True) # CommonMark without images 

208 

209 # "Sleeping privacy" (not long-form text) 

210 sleeping_arrangement = Column(Enum(SleepingArrangement), nullable=True) 

211 # "Sleeping arrangement" under "My Home" tab 

212 sleeping_details = Column(String, nullable=True) # CommonMark without images 

213 # "Local area information" under "My Home" tab 

214 area = Column(String, nullable=True) # CommonMark without images 

215 # "House rules" under "My Home" tab 

216 house_rules = Column(String, nullable=True) # CommonMark without images 

217 parking = Column(Boolean, nullable=True) 

218 parking_details = Column(Enum(ParkingDetails), nullable=True) # CommonMark without images 

219 camping_ok = Column(Boolean, nullable=True) 

220 

221 accepted_tos = Column(Integer, nullable=False, default=0) 

222 accepted_community_guidelines = Column(Integer, nullable=False, server_default="0") 

223 # whether the user has yet filled in the contributor form 

224 filled_contributor_form = Column(Boolean, nullable=False, server_default="false") 

225 

226 # number of onboarding emails sent 

227 onboarding_emails_sent = Column(Integer, nullable=False, server_default="0") 

228 last_onboarding_email_sent = Column(DateTime(timezone=True), nullable=True) 

229 

230 # whether we need to sync the user's newsletter preferences with the newsletter server 

231 in_sync_with_newsletter = Column(Boolean, nullable=False, server_default="false") 

232 # opted out of the newsletter 

233 opt_out_of_newsletter = Column(Boolean, nullable=False, server_default="false") 

234 

235 # set to null to receive no digests 

236 digest_frequency = Column(Interval, nullable=True) 

237 last_digest_sent = Column(DateTime(timezone=True), nullable=False, server_default=text("to_timestamp(0)")) 

238 

239 # for changing their email 

240 new_email = Column(String, nullable=True) 

241 

242 new_email_token = Column(String, nullable=True) 

243 new_email_token_created = Column(DateTime(timezone=True), nullable=True) 

244 new_email_token_expiry = Column(DateTime(timezone=True), nullable=True) 

245 

246 recommendation_score = Column(Float, nullable=False, server_default="0") 

247 

248 # Columns for verifying their phone number. State chart: 

249 # ,-------------------, 

250 # | Start | 

251 # | phone = None | someone else 

252 # ,-----------------, | token = None | verifies ,-----------------------, 

253 # | Code Expired | | sent = 1970 or zz | phone xx | Verification Expired | 

254 # | phone = xx | time passes | verified = None | <------, | phone = xx | 

255 # | token = yy | <------------, | attempts = 0 | | | token = None | 

256 # | sent = zz (exp.)| | '-------------------' | | sent = zz | 

257 # | verified = None | | V ^ +-----------< | verified = ww (exp.) | 

258 # | attempts = 0..2 | >--, | | | ChangePhone("") | | attempts = 0 | 

259 # '-----------------' +-------- | ------+----+--------------------+ '-----------------------' 

260 # | | | | ChangePhone(xx) | ^ time passes 

261 # | | ^ V | | 

262 # ,-----------------, | | ,-------------------, | ,-----------------------, 

263 # | Too Many | >--' '--< | Code sent | >------+ | Verified | 

264 # | phone = xx | | phone = xx | | | phone = xx | 

265 # | token = yy | VerifyPhone(wrong)| token = yy | '-----------< | token = None | 

266 # | sent = zz | <------+--------< | sent = zz | | sent = zz | 

267 # | verified = None | | | verified = None | VerifyPhone(correct) | verified = ww | 

268 # | attempts = 3 | '--------> | attempts = 0..2 | >------------------> | attempts = 0 | 

269 # '-----------------' '-------------------' '-----------------------' 

270 

271 # randomly generated Luhn 6-digit string 

272 phone_verification_token = Column(String(6), nullable=True, server_default=text("NULL")) 

273 

274 phone_verification_sent = Column(DateTime(timezone=True), nullable=False, server_default=text("to_timestamp(0)")) 

275 phone_verification_verified = Column(DateTime(timezone=True), nullable=True, server_default=text("NULL")) 

276 phone_verification_attempts = Column(Integer, nullable=False, server_default=text("0")) 

277 

278 # the stripe customer identifier if the user has donated to Couchers 

279 # e.g. cus_JjoXHttuZopv0t 

280 # for new US entity 

281 stripe_customer_id = Column(String, nullable=True) 

282 # for old AU entity 

283 stripe_customer_id_old = Column(String, nullable=True) 

284 

285 has_passport_sex_gender_exception = Column(Boolean, nullable=False, server_default=text("false")) 

286 

287 # checking for phone verification 

288 has_donated = Column(Boolean, nullable=False, server_default=text("false")) 

289 

290 # whether this user has all emails turned off 

291 do_not_email = Column(Boolean, nullable=False, server_default=text("false")) 

292 

293 avatar = relationship("Upload", foreign_keys="User.avatar_key") 

294 

295 admin_note = Column(String, nullable=False, server_default=text("''")) 

296 

297 age = column_property(func.date_part("year", func.age(birthdate))) 

298 

299 __table_args__ = ( 

300 # Verified phone numbers should be unique 

301 Index( 

302 "ix_users_unique_phone", 

303 phone, 

304 unique=True, 

305 postgresql_where=phone_verification_verified != None, 

306 ), 

307 Index( 

308 "ix_users_active", 

309 id, 

310 postgresql_where=~is_banned & ~is_deleted, 

311 ), 

312 # create index on users(geom, id, username) where not is_banned and not is_deleted and geom is not null; 

313 Index( 

314 "ix_users_geom_active", 

315 geom, 

316 id, 

317 username, 

318 postgresql_where=~is_banned & ~is_deleted & (geom != None), 

319 ), 

320 # There are two possible states for new_email_token, new_email_token_created, and new_email_token_expiry 

321 CheckConstraint( 

322 "(new_email_token IS NOT NULL AND new_email_token_created IS NOT NULL AND new_email_token_expiry IS NOT NULL) OR \ 

323 (new_email_token IS NULL AND new_email_token_created IS NULL AND new_email_token_expiry IS NULL)", 

324 name="check_new_email_token_state", 

325 ), 

326 # Whenever a phone number is set, it must either be pending verification or already verified. 

327 # Exactly one of the following must always be true: not phone, token, verified. 

328 CheckConstraint( 

329 "(phone IS NULL)::int + (phone_verification_verified IS NOT NULL)::int + (phone_verification_token IS NOT NULL)::int = 1", 

330 name="phone_verified_conditions", 

331 ), 

332 # Email must match our regex 

333 CheckConstraint( 

334 f"email ~ '{EMAIL_REGEX}'", 

335 name="valid_email", 

336 ), 

337 # Undelete token + time are coupled: either both null or neither; and if they're not null then the account is deleted 

338 CheckConstraint( 

339 "((undelete_token IS NULL) = (undelete_until IS NULL)) AND ((undelete_token IS NULL) OR is_deleted)", 

340 name="undelete_nullity", 

341 ), 

342 # If the user disabled all emails, then they can't host or meet up 

343 CheckConstraint( 

344 "(do_not_email IS FALSE) OR ((hosting_status = 'cant_host') AND (meetup_status = 'does_not_want_to_meetup'))", 

345 name="do_not_email_inactive", 

346 ), 

347 ) 

348 

349 @hybrid_property 

350 def has_completed_profile(self): 

351 return self.avatar_key is not None and self.about_me is not None and len(self.about_me) >= 150 

352 

353 @has_completed_profile.expression 

354 def has_completed_profile(cls): 

355 return (cls.avatar_key != None) & (func.character_length(cls.about_me) >= 150) 

356 

357 @hybrid_property 

358 def is_jailed(self): 

359 return ( 

360 (self.accepted_tos < TOS_VERSION) 

361 | (self.accepted_community_guidelines < GUIDELINES_VERSION) 

362 | self.is_missing_location 

363 | (self.mod_notes.where(ModNote.is_pending).count() > 0) 

364 ) 

365 

366 @hybrid_property 

367 def is_missing_location(self): 

368 return (self.geom == None) | (self.geom_radius == None) 

369 

370 @hybrid_property 

371 def is_visible(self): 

372 return ~(self.is_banned | self.is_deleted) 

373 

374 @property 

375 def coordinates(self): 

376 return get_coordinates(self.geom) 

377 

378 @property 

379 def display_joined(self): 

380 """ 

381 Returns the last active time rounded down to the nearest hour. 

382 """ 

383 return self.joined.replace(minute=0, second=0, microsecond=0) 

384 

385 @property 

386 def display_last_active(self): 

387 """ 

388 Returns the last active time rounded down whatever is the "last active" coarsening. 

389 """ 

390 return last_active_coarsen(self.last_active) 

391 

392 @hybrid_property 

393 def phone_is_verified(self): 

394 return ( 

395 self.phone_verification_verified is not None 

396 and now() - self.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

397 ) 

398 

399 @phone_is_verified.expression 

400 def phone_is_verified(cls): 

401 return (cls.phone_verification_verified != None) & ( 

402 now() - cls.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

403 ) 

404 

405 @hybrid_property 

406 def phone_code_expired(self): 

407 return now() - self.phone_verification_sent > SMS_CODE_LIFETIME 

408 

409 def __repr__(self): 

410 return f"User(id={self.id}, email={self.email}, username={self.username})" 

411 

412 

413class UserBadge(Base): 

414 """ 

415 A badge on a user's profile 

416 """ 

417 

418 __tablename__ = "user_badges" 

419 __table_args__ = (UniqueConstraint("user_id", "badge_id"),) 

420 

421 id = Column(BigInteger, primary_key=True) 

422 

423 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

424 # corresponds to "id" in badges.json 

425 badge_id = Column(String, nullable=False, index=True) 

426 

427 # take this with a grain of salt, someone may get then lose a badge for whatever reason 

428 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

429 

430 user = relationship("User", backref="badges") 

431 

432 

433class StrongVerificationAttemptStatus(enum.Enum): 

434 ## full data states 

435 # completed, this now provides verification for a user 

436 succeeded = enum.auto() 

437 

438 ## no data states 

439 # in progress: waiting for the user to scan the Iris code or open the app 

440 in_progress_waiting_on_user_to_open_app = enum.auto() 

441 # in progress: waiting for the user to scan MRZ or NFC/chip 

442 in_progress_waiting_on_user_in_app = enum.auto() 

443 # in progress, waiting for backend to pull verification data 

444 in_progress_waiting_on_backend = enum.auto() 

445 # failed, no data 

446 failed = enum.auto() 

447 

448 # duplicate, at our end, has data 

449 duplicate = enum.auto() 

450 

451 ## minimal data states 

452 # the data, except minimal deduplication data, was deleted 

453 deleted = enum.auto() 

454 

455 

456class PassportSex(enum.Enum): 

457 """ 

458 We don't care about sex, we use gender on the platform. But passports apparently do. 

459 """ 

460 

461 male = enum.auto() 

462 female = enum.auto() 

463 unspecified = enum.auto() 

464 

465 

466class StrongVerificationAttempt(Base): 

467 """ 

468 An attempt to perform strong verification 

469 """ 

470 

471 __tablename__ = "strong_verification_attempts" 

472 

473 # our verification id 

474 id = Column(BigInteger, primary_key=True) 

475 

476 # this is returned in the callback, and we look up the attempt via this 

477 verification_attempt_token = Column(String, nullable=False, unique=True) 

478 

479 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

480 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

481 

482 status = Column( 

483 Enum(StrongVerificationAttemptStatus), 

484 nullable=False, 

485 default=StrongVerificationAttemptStatus.in_progress_waiting_on_user_to_open_app, 

486 ) 

487 

488 ## full data 

489 has_full_data = Column(Boolean, nullable=False, default=False) 

490 # the data returned from iris, encrypted with a public key whose private key is kept offline 

491 passport_encrypted_data = Column(Binary, nullable=True) 

492 passport_date_of_birth = Column(Date, nullable=True) 

493 passport_sex = Column(Enum(PassportSex), nullable=True) 

494 

495 ## minimal data: this will not be deleted 

496 has_minimal_data = Column(Boolean, nullable=False, default=False) 

497 passport_expiry_date = Column(Date, nullable=True) 

498 passport_nationality = Column(String, nullable=True) 

499 # last three characters of the passport number 

500 passport_last_three_document_chars = Column(String, nullable=True) 

501 

502 iris_token = Column(String, nullable=False, unique=True) 

503 iris_session_id = Column(BigInteger, nullable=False, unique=True) 

504 

505 passport_expiry_datetime = column_property(date_in_timezone(passport_expiry_date, "Etc/UTC")) 

506 

507 user = relationship("User") 

508 

509 @hybrid_property 

510 def is_valid(self): 

511 """ 

512 This only checks whether the attempt is a success and the passport is not expired, use `has_strong_verification` for full check 

513 """ 

514 return (self.status == StrongVerificationAttemptStatus.succeeded) and (self.passport_expiry_datetime >= now()) 

515 

516 @is_valid.expression 

517 def is_valid(cls): 

518 return (cls.status == StrongVerificationAttemptStatus.succeeded) & ( 

519 func.coalesce(cls.passport_expiry_datetime >= func.now(), False) 

520 ) 

521 

522 @hybrid_property 

523 def is_visible(self): 

524 return self.status != StrongVerificationAttemptStatus.deleted 

525 

526 @hybrid_method 

527 def _raw_birthdate_match(self, user): 

528 """Does not check whether the SV attempt itself is not expired""" 

529 return self.passport_date_of_birth == user.birthdate 

530 

531 @hybrid_method 

532 def matches_birthdate(self, user): 

533 return self.is_valid & self._raw_birthdate_match(user) 

534 

535 @hybrid_method 

536 def _raw_gender_match(self, user): 

537 """Does not check whether the SV attempt itself is not expired""" 

538 return ( 

539 ((user.gender == "Woman") & (self.passport_sex == PassportSex.female)) 

540 | ((user.gender == "Man") & (self.passport_sex == PassportSex.male)) 

541 | (self.passport_sex == PassportSex.unspecified) 

542 | (user.has_passport_sex_gender_exception == True) 

543 ) 

544 

545 @hybrid_method 

546 def matches_gender(self, user): 

547 return self.is_valid & self._raw_gender_match(user) 

548 

549 @hybrid_method 

550 def has_strong_verification(self, user): 

551 return self.is_valid & self._raw_birthdate_match(user) & self._raw_gender_match(user) 

552 

553 __table_args__ = ( 

554 # used to look up verification status for a user 

555 Index( 

556 "ix_strong_verification_attempts_current", 

557 user_id, 

558 passport_expiry_date, 

559 postgresql_where=status == StrongVerificationAttemptStatus.succeeded, 

560 ), 

561 # each passport can be verified only once 

562 Index( 

563 "ix_strong_verification_attempts_unique_succeeded", 

564 passport_expiry_date, 

565 passport_nationality, 

566 passport_last_three_document_chars, 

567 unique=True, 

568 postgresql_where=( 

569 (status == StrongVerificationAttemptStatus.succeeded) 

570 | (status == StrongVerificationAttemptStatus.deleted) 

571 ), 

572 ), 

573 # full data check 

574 CheckConstraint( 

575 "(has_full_data IS TRUE AND passport_encrypted_data IS NOT NULL AND passport_date_of_birth IS NOT NULL) OR \ 

576 (has_full_data IS FALSE AND passport_encrypted_data IS NULL AND passport_date_of_birth IS NULL)", 

577 name="full_data_status", 

578 ), 

579 # minimal data check 

580 CheckConstraint( 

581 "(has_minimal_data IS TRUE AND passport_expiry_date IS NOT NULL AND passport_nationality IS NOT NULL AND passport_last_three_document_chars IS NOT NULL) OR \ 

582 (has_minimal_data IS FALSE AND passport_expiry_date IS NULL AND passport_nationality IS NULL AND passport_last_three_document_chars IS NULL)", 

583 name="minimal_data_status", 

584 ), 

585 # note on implications: p => q iff ~p OR q 

586 # full data implies minimal data, has_minimal_data => has_full_data 

587 CheckConstraint( 

588 "(has_full_data IS FALSE) OR (has_minimal_data IS TRUE)", 

589 name="full_data_implies_minimal_data", 

590 ), 

591 # succeeded implies full data 

592 CheckConstraint( 

593 "(NOT (status = 'succeeded')) OR (has_full_data IS TRUE)", 

594 name="succeeded_implies_full_data", 

595 ), 

596 # in_progress/failed implies no_data 

597 CheckConstraint( 

598 "(NOT ((status = 'in_progress_waiting_on_user_to_open_app') OR (status = 'in_progress_waiting_on_user_in_app') OR (status = 'in_progress_waiting_on_backend') OR (status = 'failed'))) OR (has_minimal_data IS FALSE)", 

599 name="in_progress_failed_iris_implies_no_data", 

600 ), 

601 # deleted or duplicate implies minimal data 

602 CheckConstraint( 

603 "(NOT ((status = 'deleted') OR (status = 'duplicate'))) OR (has_minimal_data IS TRUE)", 

604 name="deleted_duplicate_implies_minimal_data", 

605 ), 

606 ) 

607 

608 

609class ModNote(Base): 

610 """ 

611 A moderator note to a user. This could be a warning, just a note "hey, we did X", or any other similar message. 

612 

613 The user has to read and "acknowledge" the note before continuing onto the platform, and being un-jailed. 

614 """ 

615 

616 __tablename__ = "mod_notes" 

617 id = Column(BigInteger, primary_key=True) 

618 

619 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

620 

621 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

622 acknowledged = Column(DateTime(timezone=True), nullable=True) 

623 

624 # this is an internal ID to allow the mods to track different types of notes 

625 internal_id = Column(String, nullable=False) 

626 # the admin that left this note 

627 creator_user_id = Column(ForeignKey("users.id"), nullable=False) 

628 

629 note_content = Column(String, nullable=False) # CommonMark without images 

630 

631 user = relationship("User", backref=backref("mod_notes", lazy="dynamic"), foreign_keys="ModNote.user_id") 

632 

633 def __repr__(self): 

634 return f"ModeNote(id={self.id}, user={self.user}, created={self.created}, ack'd={self.acknowledged})" 

635 

636 @hybrid_property 

637 def is_pending(self): 

638 return self.acknowledged == None 

639 

640 __table_args__ = ( 

641 # used to look up pending notes 

642 Index( 

643 "ix_mod_notes_unacknowledged", 

644 user_id, 

645 postgresql_where=acknowledged == None, 

646 ), 

647 ) 

648 

649 

650class StrongVerificationCallbackEvent(Base): 

651 __tablename__ = "strong_verification_callback_events" 

652 

653 id = Column(BigInteger, primary_key=True) 

654 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

655 

656 verification_attempt_id = Column(ForeignKey("strong_verification_attempts.id"), nullable=False, index=True) 

657 

658 iris_status = Column(String, nullable=False) 

659 

660 

661class DonationType(enum.Enum): 

662 one_time = enum.auto() 

663 recurring = enum.auto() 

664 

665 

666class DonationInitiation(Base): 

667 """ 

668 Whenever someone initiaties a donation through the platform 

669 """ 

670 

671 __tablename__ = "donation_initiations" 

672 id = Column(BigInteger, primary_key=True) 

673 

674 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

675 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

676 

677 amount = Column(Integer, nullable=False) 

678 stripe_checkout_session_id = Column(String, nullable=False) 

679 

680 donation_type = Column(Enum(DonationType), nullable=False) 

681 

682 user = relationship("User", backref="donation_initiations") 

683 

684 

685class Invoice(Base): 

686 """ 

687 Successful donations, both one off and recurring 

688 

689 Triggered by `payment_intent.succeeded` webhook 

690 """ 

691 

692 __tablename__ = "invoices" 

693 

694 id = Column(BigInteger, primary_key=True) 

695 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

696 user_id = Column(ForeignKey("users.id"), nullable=False) 

697 

698 amount = Column(Float, nullable=False) 

699 

700 stripe_payment_intent_id = Column(String, nullable=False, unique=True) 

701 stripe_receipt_url = Column(String, nullable=False) 

702 

703 user = relationship("User", backref="invoices") 

704 

705 

706class LanguageFluency(enum.Enum): 

707 # note that the numbering is important here, these are ordinal 

708 beginner = 1 

709 conversational = 2 

710 fluent = 3 

711 

712 

713class LanguageAbility(Base): 

714 __tablename__ = "language_abilities" 

715 __table_args__ = ( 

716 # Users can only have one language ability per language 

717 UniqueConstraint("user_id", "language_code"), 

718 ) 

719 

720 id = Column(BigInteger, primary_key=True) 

721 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

722 language_code = Column(ForeignKey("languages.code", deferrable=True), nullable=False) 

723 fluency = Column(Enum(LanguageFluency), nullable=False) 

724 

725 user = relationship("User", backref="language_abilities") 

726 language = relationship("Language") 

727 

728 

729class RegionVisited(Base): 

730 __tablename__ = "regions_visited" 

731 __table_args__ = (UniqueConstraint("user_id", "region_code"),) 

732 

733 id = Column(BigInteger, primary_key=True) 

734 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

735 region_code = Column(ForeignKey("regions.code", deferrable=True), nullable=False) 

736 

737 

738class RegionLived(Base): 

739 __tablename__ = "regions_lived" 

740 __table_args__ = (UniqueConstraint("user_id", "region_code"),) 

741 

742 id = Column(BigInteger, primary_key=True) 

743 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

744 region_code = Column(ForeignKey("regions.code", deferrable=True), nullable=False) 

745 

746 

747class FriendStatus(enum.Enum): 

748 pending = enum.auto() 

749 accepted = enum.auto() 

750 rejected = enum.auto() 

751 cancelled = enum.auto() 

752 

753 

754class FriendRelationship(Base): 

755 """ 

756 Friendship relations between users 

757 

758 TODO: make this better with sqlalchemy self-referential stuff 

759 TODO: constraint on only one row per user pair where accepted or pending 

760 """ 

761 

762 __tablename__ = "friend_relationships" 

763 

764 id = Column(BigInteger, primary_key=True) 

765 

766 from_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

767 to_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

768 

769 status = Column(Enum(FriendStatus), nullable=False, default=FriendStatus.pending) 

770 

771 # timezones should always be UTC 

772 time_sent = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

773 time_responded = Column(DateTime(timezone=True), nullable=True) 

774 

775 from_user = relationship("User", backref="friends_from", foreign_keys="FriendRelationship.from_user_id") 

776 to_user = relationship("User", backref="friends_to", foreign_keys="FriendRelationship.to_user_id") 

777 

778 

779class ContributeOption(enum.Enum): 

780 yes = enum.auto() 

781 maybe = enum.auto() 

782 no = enum.auto() 

783 

784 

785class ContributorForm(Base): 

786 """ 

787 Someone filled in the contributor form 

788 """ 

789 

790 __tablename__ = "contributor_forms" 

791 

792 id = Column(BigInteger, primary_key=True) 

793 

794 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

795 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

796 

797 ideas = Column(String, nullable=True) 

798 features = Column(String, nullable=True) 

799 experience = Column(String, nullable=True) 

800 contribute = Column(Enum(ContributeOption), nullable=True) 

801 contribute_ways = Column(ARRAY(String), nullable=False) 

802 expertise = Column(String, nullable=True) 

803 

804 user = relationship("User", backref="contributor_forms") 

805 

806 @hybrid_property 

807 def is_filled(self): 

808 """ 

809 Whether the form counts as having been filled 

810 """ 

811 return ( 

812 (self.ideas != None) 

813 | (self.features != None) 

814 | (self.experience != None) 

815 | (self.contribute != None) 

816 | (self.contribute_ways != []) 

817 | (self.expertise != None) 

818 ) 

819 

820 @property 

821 def should_notify(self): 

822 """ 

823 If this evaluates to true, we send an email to the recruitment team. 

824 

825 We currently send if expertise is listed, or if they list a way to help outside of a set list 

826 """ 

827 return (self.expertise != None) | (not set(self.contribute_ways).issubset({"community", "blog", "other"})) 

828 

829 

830class SignupFlow(Base): 

831 """ 

832 Signup flows/incomplete users 

833 

834 Coinciding fields have the same meaning as in User 

835 """ 

836 

837 __tablename__ = "signup_flows" 

838 

839 id = Column(BigInteger, primary_key=True) 

840 

841 # housekeeping 

842 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

843 flow_token = Column(String, nullable=False, unique=True) 

844 email_verified = Column(Boolean, nullable=False, default=False) 

845 email_sent = Column(Boolean, nullable=False, default=False) 

846 email_token = Column(String, nullable=True) 

847 email_token_expiry = Column(DateTime(timezone=True), nullable=True) 

848 

849 ## Basic 

850 name = Column(String, nullable=False) 

851 # TODO: unique across both tables 

852 email = Column(String, nullable=False, unique=True) 

853 # TODO: invitation, attribution 

854 

855 ## Account 

856 # TODO: unique across both tables 

857 username = Column(String, nullable=True, unique=True) 

858 hashed_password = Column(Binary, nullable=True) 

859 birthdate = Column(Date, nullable=True) # in the timezone of birthplace 

860 gender = Column(String, nullable=True) 

861 hosting_status = Column(Enum(HostingStatus), nullable=True) 

862 city = Column(String, nullable=True) 

863 geom = Column(Geometry(geometry_type="POINT", srid=4326), nullable=True) 

864 geom_radius = Column(Float, nullable=True) 

865 

866 accepted_tos = Column(Integer, nullable=True) 

867 accepted_community_guidelines = Column(Integer, nullable=False, server_default="0") 

868 

869 opt_out_of_newsletter = Column(Boolean, nullable=True) 

870 

871 ## Feedback 

872 filled_feedback = Column(Boolean, nullable=False, default=False) 

873 ideas = Column(String, nullable=True) 

874 features = Column(String, nullable=True) 

875 experience = Column(String, nullable=True) 

876 contribute = Column(Enum(ContributeOption), nullable=True) 

877 contribute_ways = Column(ARRAY(String), nullable=True) 

878 expertise = Column(String, nullable=True) 

879 

880 @hybrid_property 

881 def token_is_valid(self): 

882 return (self.email_token != None) & (self.email_token_expiry >= now()) 

883 

884 @hybrid_property 

885 def account_is_filled(self): 

886 return ( 

887 (self.username != None) 

888 & (self.birthdate != None) 

889 & (self.gender != None) 

890 & (self.hosting_status != None) 

891 & (self.city != None) 

892 & (self.geom != None) 

893 & (self.geom_radius != None) 

894 & (self.accepted_tos != None) 

895 & (self.opt_out_of_newsletter != None) 

896 ) 

897 

898 @hybrid_property 

899 def is_completed(self): 

900 return ( 

901 self.email_verified 

902 & self.account_is_filled 

903 & self.filled_feedback 

904 & (self.accepted_community_guidelines == GUIDELINES_VERSION) 

905 ) 

906 

907 

908class LoginToken(Base): 

909 """ 

910 A login token sent in an email to a user, allows them to sign in between the times defined by created and expiry 

911 """ 

912 

913 __tablename__ = "login_tokens" 

914 token = Column(String, primary_key=True) 

915 

916 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

917 

918 # timezones should always be UTC 

919 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

920 expiry = Column(DateTime(timezone=True), nullable=False) 

921 

922 user = relationship("User", backref="login_tokens") 

923 

924 @hybrid_property 

925 def is_valid(self): 

926 return (self.created <= now()) & (self.expiry >= now()) 

927 

928 def __repr__(self): 

929 return f"LoginToken(token={self.token}, user={self.user}, created={self.created}, expiry={self.expiry})" 

930 

931 

932class PasswordResetToken(Base): 

933 __tablename__ = "password_reset_tokens" 

934 token = Column(String, primary_key=True) 

935 

936 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

937 

938 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

939 expiry = Column(DateTime(timezone=True), nullable=False) 

940 

941 user = relationship("User", backref="password_reset_tokens") 

942 

943 @hybrid_property 

944 def is_valid(self): 

945 return (self.created <= now()) & (self.expiry >= now()) 

946 

947 def __repr__(self): 

948 return f"PasswordResetToken(token={self.token}, user={self.user}, created={self.created}, expiry={self.expiry})" 

949 

950 

951class AccountDeletionToken(Base): 

952 __tablename__ = "account_deletion_tokens" 

953 

954 token = Column(String, primary_key=True) 

955 

956 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

957 

958 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

959 expiry = Column(DateTime(timezone=True), nullable=False) 

960 

961 user = relationship("User", backref="account_deletion_tokens") 

962 

963 @hybrid_property 

964 def is_valid(self): 

965 return (self.created <= now()) & (self.expiry >= now()) 

966 

967 def __repr__(self): 

968 return f"AccountDeletionToken(token={self.token}, user_id={self.user_id}, created={self.created}, expiry={self.expiry})" 

969 

970 

971class UserActivity(Base): 

972 """ 

973 User activity: for each unique (user_id, period, ip_address, user_agent) tuple, keep track of number of api calls 

974 

975 Used for user "last active" as well as admin stuff 

976 """ 

977 

978 __tablename__ = "user_activity" 

979 

980 id = Column(BigInteger, primary_key=True) 

981 

982 user_id = Column(ForeignKey("users.id"), nullable=False) 

983 # the start of a period of time, e.g. 1 hour during which we bin activeness 

984 period = Column(DateTime(timezone=True), nullable=False) 

985 

986 # details of the browser, if available 

987 ip_address = Column(INET, nullable=True) 

988 user_agent = Column(String, nullable=True) 

989 

990 # count of api calls made with this ip, user_agent, and period 

991 api_calls = Column(Integer, nullable=False, default=0) 

992 

993 __table_args__ = ( 

994 # helps look up this tuple quickly 

995 Index( 

996 "ix_user_activity_user_id_period_ip_address_user_agent", 

997 user_id, 

998 period, 

999 ip_address, 

1000 user_agent, 

1001 unique=True, 

1002 ), 

1003 ) 

1004 

1005 

1006class UserSession(Base): 

1007 """ 

1008 API keys/session cookies for the app 

1009 

1010 There are two types of sessions: long-lived, and short-lived. Long-lived are 

1011 like when you choose "remember this browser": they will be valid for a long 

1012 time without the user interacting with the site. Short-lived sessions on the 

1013 other hand get invalidated quickly if the user does not interact with the 

1014 site. 

1015 

1016 Long-lived tokens are valid from `created` until `expiry`. 

1017 

1018 Short-lived tokens expire after 168 hours (7 days) if they are not used. 

1019 """ 

1020 

1021 __tablename__ = "sessions" 

1022 token = Column(String, primary_key=True) 

1023 

1024 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1025 

1026 # sessions are either "api keys" or "session cookies", otherwise identical 

1027 # an api key is put in the authorization header (e.g. "authorization: Bearer <token>") 

1028 # a session cookie is set in the "couchers-sesh" cookie (e.g. "cookie: couchers-sesh=<token>") 

1029 # when a session is created, it's fixed as one or the other for security reasons 

1030 # for api keys to be useful, they should be long lived and have a long expiry 

1031 is_api_key = Column(Boolean, nullable=False, server_default=text("false")) 

1032 

1033 # whether it's a long-lived or short-lived session 

1034 long_lived = Column(Boolean, nullable=False) 

1035 

1036 # the time at which the session was created 

1037 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1038 

1039 # the expiry of the session: the session *cannot* be refreshed past this 

1040 expiry = Column(DateTime(timezone=True), nullable=False, server_default=func.now() + text("interval '730 days'")) 

1041 

1042 # the time at which the token was invalidated, allows users to delete sessions 

1043 deleted = Column(DateTime(timezone=True), nullable=True, default=None) 

1044 

1045 # the last time this session was used 

1046 last_seen = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1047 

1048 # count of api calls made with this token/session (if we're updating last_seen, might as well update this too) 

1049 api_calls = Column(Integer, nullable=False, default=0) 

1050 

1051 # details of the browser, if available 

1052 # these are from the request creating the session, not used for anything else 

1053 ip_address = Column(String, nullable=True) 

1054 user_agent = Column(String, nullable=True) 

1055 

1056 user = relationship("User", backref="sessions") 

1057 

1058 @hybrid_property 

1059 def is_valid(self): 

1060 """ 

1061 It must have been created and not be expired or deleted. 

1062 

1063 Also, if it's a short lived token, it must have been used in the last 168 hours. 

1064 

1065 TODO: this probably won't run in python (instance level), only in sql (class level) 

1066 """ 

1067 return ( 

1068 (self.created <= func.now()) 

1069 & (self.expiry >= func.now()) 

1070 & (self.deleted == None) 

1071 & (self.long_lived | (func.now() - self.last_seen < text("interval '168 hours'"))) 

1072 ) 

1073 

1074 

1075class Conversation(Base): 

1076 """ 

1077 Conversation brings together the different types of message/conversation types 

1078 """ 

1079 

1080 __tablename__ = "conversations" 

1081 

1082 id = Column(BigInteger, primary_key=True) 

1083 # timezone should always be UTC 

1084 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1085 

1086 def __repr__(self): 

1087 return f"Conversation(id={self.id}, created={self.created})" 

1088 

1089 

1090class GroupChat(Base): 

1091 """ 

1092 Group chat 

1093 """ 

1094 

1095 __tablename__ = "group_chats" 

1096 

1097 conversation_id = Column("id", ForeignKey("conversations.id"), nullable=False, primary_key=True) 

1098 

1099 title = Column(String, nullable=True) 

1100 only_admins_invite = Column(Boolean, nullable=False, default=True) 

1101 creator_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1102 is_dm = Column(Boolean, nullable=False) 

1103 

1104 conversation = relationship("Conversation", backref="group_chat") 

1105 creator = relationship("User", backref="created_group_chats") 

1106 

1107 def __repr__(self): 

1108 return f"GroupChat(conversation={self.conversation}, title={self.title or 'None'}, only_admins_invite={self.only_admins_invite}, creator={self.creator}, is_dm={self.is_dm})" 

1109 

1110 

1111class GroupChatRole(enum.Enum): 

1112 admin = enum.auto() 

1113 participant = enum.auto() 

1114 

1115 

1116class GroupChatSubscription(Base): 

1117 """ 

1118 The recipient of a thread and information about when they joined/left/etc. 

1119 """ 

1120 

1121 __tablename__ = "group_chat_subscriptions" 

1122 id = Column(BigInteger, primary_key=True) 

1123 

1124 # TODO: DB constraint on only one user+group_chat combo at a given time 

1125 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1126 group_chat_id = Column(ForeignKey("group_chats.id"), nullable=False, index=True) 

1127 

1128 # timezones should always be UTC 

1129 joined = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1130 left = Column(DateTime(timezone=True), nullable=True) 

1131 

1132 role = Column(Enum(GroupChatRole), nullable=False) 

1133 

1134 last_seen_message_id = Column(BigInteger, nullable=False, default=0) 

1135 

1136 # when this chat is muted until, DATETIME_INFINITY for "forever" 

1137 muted_until = Column(DateTime(timezone=True), nullable=False, server_default=DATETIME_MINUS_INFINITY.isoformat()) 

1138 

1139 user = relationship("User", backref="group_chat_subscriptions") 

1140 group_chat = relationship("GroupChat", backref=backref("subscriptions", lazy="dynamic")) 

1141 

1142 def muted_display(self): 

1143 """ 

1144 Returns (muted, muted_until) display values: 

1145 1. If not muted, returns (False, None) 

1146 2. If muted forever, returns (True, None) 

1147 3. If muted until a given datetime returns (True, dt) 

1148 """ 

1149 if self.muted_until < now(): 

1150 return (False, None) 

1151 elif self.muted_until == DATETIME_INFINITY: 

1152 return (True, None) 

1153 else: 

1154 return (True, self.muted_until) 

1155 

1156 @hybrid_property 

1157 def is_muted(self): 

1158 return self.muted_until > func.now() 

1159 

1160 def __repr__(self): 

1161 return f"GroupChatSubscription(id={self.id}, user={self.user}, joined={self.joined}, left={self.left}, role={self.role}, group_chat={self.group_chat})" 

1162 

1163 

1164class MessageType(enum.Enum): 

1165 text = enum.auto() 

1166 # e.g. 

1167 # image = 

1168 # emoji = 

1169 # ... 

1170 chat_created = enum.auto() 

1171 chat_edited = enum.auto() 

1172 user_invited = enum.auto() 

1173 user_left = enum.auto() 

1174 user_made_admin = enum.auto() 

1175 user_removed_admin = enum.auto() # RemoveGroupChatAdmin: remove admin permission from a user in group chat 

1176 host_request_status_changed = enum.auto() 

1177 user_removed = enum.auto() # user is removed from group chat by amdin RemoveGroupChatUser 

1178 

1179 

1180class HostRequestStatus(enum.Enum): 

1181 pending = enum.auto() 

1182 accepted = enum.auto() 

1183 rejected = enum.auto() 

1184 confirmed = enum.auto() 

1185 cancelled = enum.auto() 

1186 

1187 

1188class Message(Base): 

1189 """ 

1190 A message. 

1191 

1192 If message_type = text, then the message is a normal text message, otherwise, it's a special control message. 

1193 """ 

1194 

1195 __tablename__ = "messages" 

1196 

1197 id = Column(BigInteger, primary_key=True) 

1198 

1199 # which conversation the message belongs in 

1200 conversation_id = Column(ForeignKey("conversations.id"), nullable=False, index=True) 

1201 

1202 # the user that sent the message/command 

1203 author_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1204 

1205 # the message type, "text" is a text message, otherwise a "control message" 

1206 message_type = Column(Enum(MessageType), nullable=False) 

1207 

1208 # the target if a control message and requires target, e.g. if inviting a user, the user invited is the target 

1209 target_id = Column(ForeignKey("users.id"), nullable=True, index=True) 

1210 

1211 # time sent, timezone should always be UTC 

1212 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1213 

1214 # the plain-text message text if not control 

1215 text = Column(String, nullable=True) 

1216 

1217 # the new host request status if the message type is host_request_status_changed 

1218 host_request_status_target = Column(Enum(HostRequestStatus), nullable=True) 

1219 

1220 conversation = relationship("Conversation", backref="messages", order_by="Message.time.desc()") 

1221 author = relationship("User", foreign_keys="Message.author_id") 

1222 target = relationship("User", foreign_keys="Message.target_id") 

1223 

1224 @property 

1225 def is_normal_message(self): 

1226 """ 

1227 There's only one normal type atm, text 

1228 """ 

1229 return self.message_type == MessageType.text 

1230 

1231 def __repr__(self): 

1232 return f"Message(id={self.id}, time={self.time}, text={self.text}, author={self.author}, conversation={self.conversation})" 

1233 

1234 

1235class ContentReport(Base): 

1236 """ 

1237 A piece of content reported to admins 

1238 """ 

1239 

1240 __tablename__ = "content_reports" 

1241 

1242 id = Column(BigInteger, primary_key=True) 

1243 

1244 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1245 

1246 # the user who reported or flagged the content 

1247 reporting_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1248 

1249 # reason, e.g. spam, inappropriate, etc 

1250 reason = Column(String, nullable=False) 

1251 # a short description 

1252 description = Column(String, nullable=False) 

1253 

1254 # a reference to the content, see //docs/content_ref.md 

1255 content_ref = Column(String, nullable=False) 

1256 # the author of the content (e.g. the user who wrote the comment itself) 

1257 author_user_id = Column(ForeignKey("users.id"), nullable=False) 

1258 

1259 # details of the browser, if available 

1260 user_agent = Column(String, nullable=False) 

1261 # the URL the user was on when reporting the content 

1262 page = Column(String, nullable=False) 

1263 

1264 # see comments above for reporting vs author 

1265 reporting_user = relationship("User", foreign_keys="ContentReport.reporting_user_id") 

1266 author_user = relationship("User", foreign_keys="ContentReport.author_user_id") 

1267 

1268 

1269class Email(Base): 

1270 """ 

1271 Table of all dispatched emails for debugging purposes, etc. 

1272 """ 

1273 

1274 __tablename__ = "emails" 

1275 

1276 id = Column(String, primary_key=True) 

1277 

1278 # timezone should always be UTC 

1279 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1280 

1281 sender_name = Column(String, nullable=False) 

1282 sender_email = Column(String, nullable=False) 

1283 

1284 recipient = Column(String, nullable=False) 

1285 subject = Column(String, nullable=False) 

1286 

1287 plain = Column(String, nullable=False) 

1288 html = Column(String, nullable=False) 

1289 

1290 list_unsubscribe_header = Column(String, nullable=True) 

1291 source_data = Column(String, nullable=True) 

1292 

1293 

1294class SMS(Base): 

1295 """ 

1296 Table of all sent SMSs for debugging purposes, etc. 

1297 """ 

1298 

1299 __tablename__ = "smss" 

1300 

1301 id = Column(BigInteger, primary_key=True) 

1302 

1303 # timezone should always be UTC 

1304 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1305 # AWS message id 

1306 message_id = Column(String, nullable=False) 

1307 

1308 # the SMS sender ID sent to AWS, name that the SMS appears to come from 

1309 sms_sender_id = Column(String, nullable=False) 

1310 number = Column(String, nullable=False) 

1311 message = Column(String, nullable=False) 

1312 

1313 

1314class HostRequest(Base): 

1315 """ 

1316 A request to stay with a host 

1317 """ 

1318 

1319 __tablename__ = "host_requests" 

1320 

1321 conversation_id = Column("id", ForeignKey("conversations.id"), nullable=False, primary_key=True) 

1322 surfer_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1323 host_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1324 

1325 # TODO: proper timezone handling 

1326 timezone = "Etc/UTC" 

1327 

1328 # dates in the timezone above 

1329 from_date = Column(Date, nullable=False) 

1330 to_date = Column(Date, nullable=False) 

1331 

1332 # timezone aware start and end times of the request, can be compared to now() 

1333 start_time = column_property(date_in_timezone(from_date, timezone)) 

1334 end_time = column_property(date_in_timezone(to_date, timezone) + text("interval '1 days'")) 

1335 start_time_to_write_reference = column_property(date_in_timezone(to_date, timezone)) 

1336 # notice 1 day for midnight at the *end of the day*, then 14 days to write a ref 

1337 end_time_to_write_reference = column_property(date_in_timezone(to_date, timezone) + text("interval '15 days'")) 

1338 

1339 status = Column(Enum(HostRequestStatus), nullable=False) 

1340 

1341 host_last_seen_message_id = Column(BigInteger, nullable=False, default=0) 

1342 surfer_last_seen_message_id = Column(BigInteger, nullable=False, default=0) 

1343 

1344 # number of reference reminders sent out 

1345 host_sent_reference_reminders = Column(BigInteger, nullable=False, server_default=text("0")) 

1346 surfer_sent_reference_reminders = Column(BigInteger, nullable=False, server_default=text("0")) 

1347 

1348 surfer = relationship("User", backref="host_requests_sent", foreign_keys="HostRequest.surfer_user_id") 

1349 host = relationship("User", backref="host_requests_received", foreign_keys="HostRequest.host_user_id") 

1350 conversation = relationship("Conversation") 

1351 

1352 @hybrid_property 

1353 def can_write_reference(self): 

1354 return ( 

1355 ((self.status == HostRequestStatus.confirmed) | (self.status == HostRequestStatus.accepted)) 

1356 & (now() >= self.start_time_to_write_reference) 

1357 & (now() <= self.end_time_to_write_reference) 

1358 ) 

1359 

1360 @can_write_reference.expression 

1361 def can_write_reference(cls): 

1362 return ( 

1363 ((cls.status == HostRequestStatus.confirmed) | (cls.status == HostRequestStatus.accepted)) 

1364 & (func.now() >= cls.start_time_to_write_reference) 

1365 & (func.now() <= cls.end_time_to_write_reference) 

1366 ) 

1367 

1368 def __repr__(self): 

1369 return f"HostRequest(id={self.conversation_id}, surfer_user_id={self.surfer_user_id}, host_user_id={self.host_user_id}...)" 

1370 

1371 

1372class ReferenceType(enum.Enum): 

1373 friend = enum.auto() 

1374 surfed = enum.auto() # The "from" user surfed with the "to" user 

1375 hosted = enum.auto() # The "from" user hosted the "to" user 

1376 

1377 

1378class Reference(Base): 

1379 """ 

1380 Reference from one user to another 

1381 """ 

1382 

1383 __tablename__ = "references" 

1384 

1385 id = Column(BigInteger, primary_key=True) 

1386 # timezone should always be UTC 

1387 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1388 

1389 from_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1390 to_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1391 

1392 reference_type = Column(Enum(ReferenceType), nullable=False) 

1393 

1394 host_request_id = Column(ForeignKey("host_requests.id"), nullable=True) 

1395 

1396 text = Column(String, nullable=False) # plain text 

1397 # text that's only visible to mods 

1398 private_text = Column(String, nullable=True) # plain text 

1399 

1400 rating = Column(Float, nullable=False) 

1401 was_appropriate = Column(Boolean, nullable=False) 

1402 

1403 from_user = relationship("User", backref="references_from", foreign_keys="Reference.from_user_id") 

1404 to_user = relationship("User", backref="references_to", foreign_keys="Reference.to_user_id") 

1405 

1406 host_request = relationship("HostRequest", backref="references") 

1407 

1408 __table_args__ = ( 

1409 # Rating must be between 0 and 1, inclusive 

1410 CheckConstraint( 

1411 "rating BETWEEN 0 AND 1", 

1412 name="rating_between_0_and_1", 

1413 ), 

1414 # Has host_request_id or it's a friend reference 

1415 CheckConstraint( 

1416 "(host_request_id IS NOT NULL) <> (reference_type = 'friend')", 

1417 name="host_request_id_xor_friend_reference", 

1418 ), 

1419 # Each user can leave at most one friend reference to another user 

1420 Index( 

1421 "ix_references_unique_friend_reference", 

1422 from_user_id, 

1423 to_user_id, 

1424 reference_type, 

1425 unique=True, 

1426 postgresql_where=(reference_type == ReferenceType.friend), 

1427 ), 

1428 # Each user can leave at most one reference to another user for each stay 

1429 Index( 

1430 "ix_references_unique_per_host_request", 

1431 from_user_id, 

1432 to_user_id, 

1433 host_request_id, 

1434 unique=True, 

1435 postgresql_where=(host_request_id != None), 

1436 ), 

1437 ) 

1438 

1439 @property 

1440 def should_report(self): 

1441 """ 

1442 If this evaluates to true, we send a report to the moderation team. 

1443 """ 

1444 return self.rating <= 0.4 or not self.was_appropriate or self.private_text 

1445 

1446 

1447class InitiatedUpload(Base): 

1448 """ 

1449 Started downloads, not necessarily complete yet. 

1450 """ 

1451 

1452 __tablename__ = "initiated_uploads" 

1453 

1454 key = Column(String, primary_key=True) 

1455 

1456 # timezones should always be UTC 

1457 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1458 expiry = Column(DateTime(timezone=True), nullable=False) 

1459 

1460 initiator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1461 

1462 initiator_user = relationship("User") 

1463 

1464 @hybrid_property 

1465 def is_valid(self): 

1466 return (self.created <= func.now()) & (self.expiry >= func.now()) 

1467 

1468 

1469class Upload(Base): 

1470 """ 

1471 Completed uploads. 

1472 """ 

1473 

1474 __tablename__ = "uploads" 

1475 key = Column(String, primary_key=True) 

1476 

1477 filename = Column(String, nullable=False) 

1478 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1479 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1480 

1481 # photo credit, etc 

1482 credit = Column(String, nullable=True) 

1483 

1484 creator_user = relationship("User", backref="uploads", foreign_keys="Upload.creator_user_id") 

1485 

1486 def _url(self, size): 

1487 return urls.media_url(filename=self.filename, size=size) 

1488 

1489 @property 

1490 def thumbnail_url(self): 

1491 return self._url("thumbnail") 

1492 

1493 @property 

1494 def full_url(self): 

1495 return self._url("full") 

1496 

1497 

1498communities_seq = Sequence("communities_seq") 

1499 

1500 

1501class Node(Base): 

1502 """ 

1503 Node, i.e. geographical subdivision of the world 

1504 

1505 Administered by the official cluster 

1506 """ 

1507 

1508 __tablename__ = "nodes" 

1509 

1510 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

1511 

1512 # name and description come from official cluster 

1513 parent_node_id = Column(ForeignKey("nodes.id"), nullable=True, index=True) 

1514 geom = deferred(Column(Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=False)) 

1515 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1516 

1517 parent_node = relationship("Node", backref="child_nodes", remote_side="Node.id") 

1518 

1519 contained_users = relationship( 

1520 "User", 

1521 primaryjoin="func.ST_Contains(foreign(Node.geom), User.geom).as_comparison(1, 2)", 

1522 viewonly=True, 

1523 uselist=True, 

1524 ) 

1525 

1526 contained_user_ids = association_proxy("contained_users", "id") 

1527 

1528 

1529class Cluster(Base): 

1530 """ 

1531 Cluster, administered grouping of content 

1532 """ 

1533 

1534 __tablename__ = "clusters" 

1535 

1536 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

1537 parent_node_id = Column(ForeignKey("nodes.id"), nullable=False, index=True) 

1538 name = Column(String, nullable=False) 

1539 # short description 

1540 description = Column(String, nullable=False) 

1541 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1542 

1543 is_official_cluster = Column(Boolean, nullable=False, default=False) 

1544 

1545 slug = column_property(func.slugify(name)) 

1546 

1547 official_cluster_for_node = relationship( 

1548 "Node", 

1549 primaryjoin="and_(Cluster.parent_node_id == Node.id, Cluster.is_official_cluster)", 

1550 backref=backref("official_cluster", uselist=False), 

1551 uselist=False, 

1552 viewonly=True, 

1553 ) 

1554 

1555 parent_node = relationship( 

1556 "Node", backref="child_clusters", remote_side="Node.id", foreign_keys="Cluster.parent_node_id" 

1557 ) 

1558 

1559 nodes = relationship("Cluster", backref="clusters", secondary="node_cluster_associations", viewonly=True) 

1560 # all pages 

1561 pages = relationship( 

1562 "Page", backref="clusters", secondary="cluster_page_associations", lazy="dynamic", viewonly=True 

1563 ) 

1564 events = relationship("Event", backref="clusters", secondary="cluster_event_associations", viewonly=True) 

1565 discussions = relationship( 

1566 "Discussion", backref="clusters", secondary="cluster_discussion_associations", viewonly=True 

1567 ) 

1568 

1569 # includes also admins 

1570 members = relationship( 

1571 "User", 

1572 lazy="dynamic", 

1573 backref="cluster_memberships", 

1574 secondary="cluster_subscriptions", 

1575 primaryjoin="Cluster.id == ClusterSubscription.cluster_id", 

1576 secondaryjoin="User.id == ClusterSubscription.user_id", 

1577 viewonly=True, 

1578 ) 

1579 

1580 admins = relationship( 

1581 "User", 

1582 lazy="dynamic", 

1583 backref="cluster_adminships", 

1584 secondary="cluster_subscriptions", 

1585 primaryjoin="Cluster.id == ClusterSubscription.cluster_id", 

1586 secondaryjoin="and_(User.id == ClusterSubscription.user_id, ClusterSubscription.role == 'admin')", 

1587 viewonly=True, 

1588 ) 

1589 

1590 main_page = relationship( 

1591 "Page", 

1592 primaryjoin="and_(Cluster.id == Page.owner_cluster_id, Page.type == 'main_page')", 

1593 viewonly=True, 

1594 uselist=False, 

1595 ) 

1596 

1597 @property 

1598 def is_leaf(self) -> bool: 

1599 """Whether the cluster is a leaf node in the cluster hierarchy.""" 

1600 return len(self.parent_node.child_nodes) == 0 

1601 

1602 __table_args__ = ( 

1603 # Each node can have at most one official cluster 

1604 Index( 

1605 "ix_clusters_owner_parent_node_id_is_official_cluster", 

1606 parent_node_id, 

1607 is_official_cluster, 

1608 unique=True, 

1609 postgresql_where=is_official_cluster, 

1610 ), 

1611 ) 

1612 

1613 

1614class NodeClusterAssociation(Base): 

1615 """ 

1616 NodeClusterAssociation, grouping of nodes 

1617 """ 

1618 

1619 __tablename__ = "node_cluster_associations" 

1620 __table_args__ = (UniqueConstraint("node_id", "cluster_id"),) 

1621 

1622 id = Column(BigInteger, primary_key=True) 

1623 

1624 node_id = Column(ForeignKey("nodes.id"), nullable=False, index=True) 

1625 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

1626 

1627 node = relationship("Node", backref="node_cluster_associations") 

1628 cluster = relationship("Cluster", backref="node_cluster_associations") 

1629 

1630 

1631class ClusterRole(enum.Enum): 

1632 member = enum.auto() 

1633 admin = enum.auto() 

1634 

1635 

1636class ClusterSubscription(Base): 

1637 """ 

1638 ClusterSubscription of a user 

1639 """ 

1640 

1641 __tablename__ = "cluster_subscriptions" 

1642 __table_args__ = (UniqueConstraint("user_id", "cluster_id"),) 

1643 

1644 id = Column(BigInteger, primary_key=True) 

1645 

1646 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1647 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

1648 role = Column(Enum(ClusterRole), nullable=False) 

1649 

1650 user = relationship("User", backref="cluster_subscriptions") 

1651 cluster = relationship("Cluster", backref="cluster_subscriptions") 

1652 

1653 

1654class ClusterPageAssociation(Base): 

1655 """ 

1656 pages related to clusters 

1657 """ 

1658 

1659 __tablename__ = "cluster_page_associations" 

1660 __table_args__ = (UniqueConstraint("page_id", "cluster_id"),) 

1661 

1662 id = Column(BigInteger, primary_key=True) 

1663 

1664 page_id = Column(ForeignKey("pages.id"), nullable=False, index=True) 

1665 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

1666 

1667 page = relationship("Page", backref="cluster_page_associations") 

1668 cluster = relationship("Cluster", backref="cluster_page_associations") 

1669 

1670 

1671class PageType(enum.Enum): 

1672 main_page = enum.auto() 

1673 place = enum.auto() 

1674 guide = enum.auto() 

1675 

1676 

1677class Page(Base): 

1678 """ 

1679 similar to a wiki page about a community, POI or guide 

1680 """ 

1681 

1682 __tablename__ = "pages" 

1683 

1684 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

1685 

1686 parent_node_id = Column(ForeignKey("nodes.id"), nullable=False, index=True) 

1687 type = Column(Enum(PageType), nullable=False) 

1688 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1689 owner_user_id = Column(ForeignKey("users.id"), nullable=True, index=True) 

1690 owner_cluster_id = Column(ForeignKey("clusters.id"), nullable=True, index=True) 

1691 

1692 thread_id = Column(ForeignKey("threads.id"), nullable=False, unique=True) 

1693 

1694 parent_node = relationship("Node", backref="child_pages", remote_side="Node.id", foreign_keys="Page.parent_node_id") 

1695 

1696 thread = relationship("Thread", backref="page", uselist=False) 

1697 creator_user = relationship("User", backref="created_pages", foreign_keys="Page.creator_user_id") 

1698 owner_user = relationship("User", backref="owned_pages", foreign_keys="Page.owner_user_id") 

1699 owner_cluster = relationship( 

1700 "Cluster", backref=backref("owned_pages", lazy="dynamic"), uselist=False, foreign_keys="Page.owner_cluster_id" 

1701 ) 

1702 

1703 editors = relationship("User", secondary="page_versions", viewonly=True) 

1704 

1705 __table_args__ = ( 

1706 # Only one of owner_user and owner_cluster should be set 

1707 CheckConstraint( 

1708 "(owner_user_id IS NULL) <> (owner_cluster_id IS NULL)", 

1709 name="one_owner", 

1710 ), 

1711 # Only clusters can own main pages 

1712 CheckConstraint( 

1713 "NOT (owner_cluster_id IS NULL AND type = 'main_page')", 

1714 name="main_page_owned_by_cluster", 

1715 ), 

1716 # Each cluster can have at most one main page 

1717 Index( 

1718 "ix_pages_owner_cluster_id_type", 

1719 owner_cluster_id, 

1720 type, 

1721 unique=True, 

1722 postgresql_where=(type == PageType.main_page), 

1723 ), 

1724 ) 

1725 

1726 def __repr__(self): 

1727 return f"Page({self.id=})" 

1728 

1729 

1730class PageVersion(Base): 

1731 """ 

1732 version of page content 

1733 """ 

1734 

1735 __tablename__ = "page_versions" 

1736 

1737 id = Column(BigInteger, primary_key=True) 

1738 

1739 page_id = Column(ForeignKey("pages.id"), nullable=False, index=True) 

1740 editor_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1741 title = Column(String, nullable=False) 

1742 content = Column(String, nullable=False) # CommonMark without images 

1743 photo_key = Column(ForeignKey("uploads.key"), nullable=True) 

1744 geom = Column(Geometry(geometry_type="POINT", srid=4326), nullable=True) 

1745 # the human-readable address 

1746 address = Column(String, nullable=True) 

1747 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1748 

1749 slug = column_property(func.slugify(title)) 

1750 

1751 page = relationship("Page", backref="versions", order_by="PageVersion.id") 

1752 editor_user = relationship("User", backref="edited_pages") 

1753 photo = relationship("Upload") 

1754 

1755 __table_args__ = ( 

1756 # Geom and address must either both be null or both be set 

1757 CheckConstraint( 

1758 "(geom IS NULL) = (address IS NULL)", 

1759 name="geom_iff_address", 

1760 ), 

1761 ) 

1762 

1763 @property 

1764 def coordinates(self): 

1765 # returns (lat, lng) or None 

1766 return get_coordinates(self.geom) 

1767 

1768 def __repr__(self): 

1769 return f"PageVersion({self.id=}, {self.page_id=})" 

1770 

1771 

1772class ClusterEventAssociation(Base): 

1773 """ 

1774 events related to clusters 

1775 """ 

1776 

1777 __tablename__ = "cluster_event_associations" 

1778 __table_args__ = (UniqueConstraint("event_id", "cluster_id"),) 

1779 

1780 id = Column(BigInteger, primary_key=True) 

1781 

1782 event_id = Column(ForeignKey("events.id"), nullable=False, index=True) 

1783 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

1784 

1785 event = relationship("Event", backref="cluster_event_associations") 

1786 cluster = relationship("Cluster", backref="cluster_event_associations") 

1787 

1788 

1789class Event(Base): 

1790 """ 

1791 An event is compose of two parts: 

1792 

1793 * An event template (Event) 

1794 * An occurrence (EventOccurrence) 

1795 

1796 One-off events will have one of each; repeating events will have one Event, 

1797 multiple EventOccurrences, one for each time the event happens. 

1798 """ 

1799 

1800 __tablename__ = "events" 

1801 

1802 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

1803 parent_node_id = Column(ForeignKey("nodes.id"), nullable=False, index=True) 

1804 

1805 title = Column(String, nullable=False) 

1806 

1807 slug = column_property(func.slugify(title)) 

1808 

1809 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1810 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1811 owner_user_id = Column(ForeignKey("users.id"), nullable=True, index=True) 

1812 owner_cluster_id = Column(ForeignKey("clusters.id"), nullable=True, index=True) 

1813 thread_id = Column(ForeignKey("threads.id"), nullable=False, unique=True) 

1814 

1815 parent_node = relationship( 

1816 "Node", backref="child_events", remote_side="Node.id", foreign_keys="Event.parent_node_id" 

1817 ) 

1818 thread = relationship("Thread", backref="event", uselist=False) 

1819 subscribers = relationship( 

1820 "User", backref="subscribed_events", secondary="event_subscriptions", lazy="dynamic", viewonly=True 

1821 ) 

1822 organizers = relationship( 

1823 "User", backref="organized_events", secondary="event_organizers", lazy="dynamic", viewonly=True 

1824 ) 

1825 creator_user = relationship("User", backref="created_events", foreign_keys="Event.creator_user_id") 

1826 owner_user = relationship("User", backref="owned_events", foreign_keys="Event.owner_user_id") 

1827 owner_cluster = relationship( 

1828 "Cluster", 

1829 backref=backref("owned_events", lazy="dynamic"), 

1830 uselist=False, 

1831 foreign_keys="Event.owner_cluster_id", 

1832 ) 

1833 

1834 __table_args__ = ( 

1835 # Only one of owner_user and owner_cluster should be set 

1836 CheckConstraint( 

1837 "(owner_user_id IS NULL) <> (owner_cluster_id IS NULL)", 

1838 name="one_owner", 

1839 ), 

1840 ) 

1841 

1842 

1843class EventOccurrence(Base): 

1844 __tablename__ = "event_occurrences" 

1845 

1846 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

1847 event_id = Column(ForeignKey("events.id"), nullable=False, index=True) 

1848 

1849 # the user that created this particular occurrence of a repeating event (same as event.creator_user_id if single event) 

1850 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1851 content = Column(String, nullable=False) # CommonMark without images 

1852 photo_key = Column(ForeignKey("uploads.key"), nullable=True) 

1853 

1854 is_cancelled = Column(Boolean, nullable=False, default=False, server_default=text("false")) 

1855 is_deleted = Column(Boolean, nullable=False, default=False, server_default=text("false")) 

1856 

1857 # a null geom is an online-only event 

1858 geom = Column(Geometry(geometry_type="POINT", srid=4326), nullable=True) 

1859 # physical address, iff geom is not null 

1860 address = Column(String, nullable=True) 

1861 # videoconferencing link, etc, must be specified if no geom, otherwise optional 

1862 link = Column(String, nullable=True) 

1863 

1864 timezone = "Etc/UTC" 

1865 

1866 # time during which the event takes place; this is a range type (instead of separate start+end times) which 

1867 # simplifies database constraints, etc 

1868 during = Column(TSTZRANGE, nullable=False) 

1869 

1870 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1871 last_edited = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1872 

1873 creator_user = relationship( 

1874 "User", backref="created_event_occurrences", foreign_keys="EventOccurrence.creator_user_id" 

1875 ) 

1876 event = relationship( 

1877 "Event", 

1878 backref=backref("occurrences", lazy="dynamic"), 

1879 remote_side="Event.id", 

1880 foreign_keys="EventOccurrence.event_id", 

1881 ) 

1882 

1883 photo = relationship("Upload") 

1884 

1885 __table_args__ = ( 

1886 # Geom and address go together 

1887 CheckConstraint( 

1888 # geom and address are either both null or neither of them are null 

1889 "(geom IS NULL) = (address IS NULL)", 

1890 name="geom_iff_address", 

1891 ), 

1892 # Online-only events need a link, note that online events may also have a link 

1893 CheckConstraint( 

1894 # exactly oen of geom or link is non-null 

1895 "(geom IS NULL) <> (link IS NULL)", 

1896 name="link_or_geom", 

1897 ), 

1898 # Can't have overlapping occurrences in the same Event 

1899 ExcludeConstraint(("event_id", "="), ("during", "&&"), name="event_occurrences_event_id_during_excl"), 

1900 ) 

1901 

1902 @property 

1903 def coordinates(self): 

1904 # returns (lat, lng) or None 

1905 return get_coordinates(self.geom) 

1906 

1907 @hybrid_property 

1908 def start_time(self): 

1909 return self.during.lower 

1910 

1911 @start_time.expression 

1912 def start_time(cls): 

1913 return func.lower(cls.during) 

1914 

1915 @hybrid_property 

1916 def end_time(self): 

1917 return self.during.upper 

1918 

1919 @end_time.expression 

1920 def end_time(cls): 

1921 return func.upper(cls.during) 

1922 

1923 

1924class EventSubscription(Base): 

1925 """ 

1926 Users' subscriptions to events 

1927 """ 

1928 

1929 __tablename__ = "event_subscriptions" 

1930 __table_args__ = (UniqueConstraint("event_id", "user_id"),) 

1931 

1932 id = Column(BigInteger, primary_key=True) 

1933 

1934 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1935 event_id = Column(ForeignKey("events.id"), nullable=False, index=True) 

1936 joined = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1937 

1938 user = relationship("User") 

1939 event = relationship("Event") 

1940 

1941 

1942class EventOrganizer(Base): 

1943 """ 

1944 Organizers for events 

1945 """ 

1946 

1947 __tablename__ = "event_organizers" 

1948 __table_args__ = (UniqueConstraint("event_id", "user_id"),) 

1949 

1950 id = Column(BigInteger, primary_key=True) 

1951 

1952 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1953 event_id = Column(ForeignKey("events.id"), nullable=False, index=True) 

1954 joined = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1955 

1956 user = relationship("User") 

1957 event = relationship("Event") 

1958 

1959 

1960class AttendeeStatus(enum.Enum): 

1961 going = enum.auto() 

1962 maybe = enum.auto() 

1963 

1964 

1965class EventOccurrenceAttendee(Base): 

1966 """ 

1967 Attendees for events 

1968 """ 

1969 

1970 __tablename__ = "event_occurrence_attendees" 

1971 __table_args__ = (UniqueConstraint("occurrence_id", "user_id"),) 

1972 

1973 id = Column(BigInteger, primary_key=True) 

1974 

1975 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1976 occurrence_id = Column(ForeignKey("event_occurrences.id"), nullable=False, index=True) 

1977 responded = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1978 attendee_status = Column(Enum(AttendeeStatus), nullable=False) 

1979 

1980 user = relationship("User") 

1981 occurrence = relationship("EventOccurrence", backref=backref("attendances", lazy="dynamic")) 

1982 

1983 

1984class EventCommunityInviteRequest(Base): 

1985 """ 

1986 Requests to send out invitation notifications/emails to the community for a given event occurrence 

1987 """ 

1988 

1989 __tablename__ = "event_community_invite_requests" 

1990 

1991 id = Column(BigInteger, primary_key=True) 

1992 

1993 occurrence_id = Column(ForeignKey("event_occurrences.id"), nullable=False, index=True) 

1994 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

1995 

1996 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

1997 

1998 decided = Column(DateTime(timezone=True), nullable=True) 

1999 decided_by_user_id = Column(ForeignKey("users.id"), nullable=True) 

2000 approved = Column(Boolean, nullable=True) 

2001 

2002 occurrence = relationship("EventOccurrence", backref=backref("community_invite_requests", lazy="dynamic")) 

2003 user = relationship("User", foreign_keys="EventCommunityInviteRequest.user_id") 

2004 

2005 __table_args__ = ( 

2006 # each user can only request once 

2007 UniqueConstraint("occurrence_id", "user_id"), 

2008 # each event can only have one notification sent out 

2009 Index( 

2010 "ix_event_community_invite_requests_unique", 

2011 occurrence_id, 

2012 unique=True, 

2013 postgresql_where=and_(approved.is_not(None), approved == True), 

2014 ), 

2015 # decided and approved ought to be null simultaneously 

2016 CheckConstraint( 

2017 "((decided IS NULL) AND (decided_by_user_id IS NULL) AND (approved IS NULL)) OR \ 

2018 ((decided IS NOT NULL) AND (decided_by_user_id IS NOT NULL) AND (approved IS NOT NULL))", 

2019 name="decided_approved", 

2020 ), 

2021 ) 

2022 

2023 

2024class ClusterDiscussionAssociation(Base): 

2025 """ 

2026 discussions related to clusters 

2027 """ 

2028 

2029 __tablename__ = "cluster_discussion_associations" 

2030 __table_args__ = (UniqueConstraint("discussion_id", "cluster_id"),) 

2031 

2032 id = Column(BigInteger, primary_key=True) 

2033 

2034 discussion_id = Column(ForeignKey("discussions.id"), nullable=False, index=True) 

2035 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

2036 

2037 discussion = relationship("Discussion", backref="cluster_discussion_associations") 

2038 cluster = relationship("Cluster", backref="cluster_discussion_associations") 

2039 

2040 

2041class Discussion(Base): 

2042 """ 

2043 forum board 

2044 """ 

2045 

2046 __tablename__ = "discussions" 

2047 

2048 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value()) 

2049 

2050 title = Column(String, nullable=False) 

2051 content = Column(String, nullable=False) 

2052 thread_id = Column(ForeignKey("threads.id"), nullable=False, unique=True) 

2053 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2054 

2055 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

2056 owner_cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True) 

2057 

2058 slug = column_property(func.slugify(title)) 

2059 

2060 thread = relationship("Thread", backref="discussion", uselist=False) 

2061 

2062 subscribers = relationship("User", backref="discussions", secondary="discussion_subscriptions", viewonly=True) 

2063 

2064 creator_user = relationship("User", backref="created_discussions", foreign_keys="Discussion.creator_user_id") 

2065 owner_cluster = relationship("Cluster", backref=backref("owned_discussions", lazy="dynamic"), uselist=False) 

2066 

2067 

2068class DiscussionSubscription(Base): 

2069 """ 

2070 users subscriptions to discussions 

2071 """ 

2072 

2073 __tablename__ = "discussion_subscriptions" 

2074 __table_args__ = (UniqueConstraint("discussion_id", "user_id"),) 

2075 

2076 id = Column(BigInteger, primary_key=True) 

2077 

2078 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

2079 discussion_id = Column(ForeignKey("discussions.id"), nullable=False, index=True) 

2080 joined = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2081 left = Column(DateTime(timezone=True), nullable=True) 

2082 

2083 user = relationship("User", backref="discussion_subscriptions") 

2084 discussion = relationship("Discussion", backref="discussion_subscriptions") 

2085 

2086 

2087class Thread(Base): 

2088 """ 

2089 Thread 

2090 """ 

2091 

2092 __tablename__ = "threads" 

2093 

2094 id = Column(BigInteger, primary_key=True) 

2095 

2096 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2097 deleted = Column(DateTime(timezone=True), nullable=True) 

2098 

2099 

2100class Comment(Base): 

2101 """ 

2102 Comment 

2103 """ 

2104 

2105 __tablename__ = "comments" 

2106 

2107 id = Column(BigInteger, primary_key=True) 

2108 

2109 thread_id = Column(ForeignKey("threads.id"), nullable=False, index=True) 

2110 author_user_id = Column(ForeignKey("users.id"), nullable=False) 

2111 content = Column(String, nullable=False) # CommonMark without images 

2112 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2113 deleted = Column(DateTime(timezone=True), nullable=True) 

2114 

2115 thread = relationship("Thread", backref="comments") 

2116 

2117 

2118class Reply(Base): 

2119 """ 

2120 Reply 

2121 """ 

2122 

2123 __tablename__ = "replies" 

2124 

2125 id = Column(BigInteger, primary_key=True) 

2126 

2127 comment_id = Column(ForeignKey("comments.id"), nullable=False, index=True) 

2128 author_user_id = Column(ForeignKey("users.id"), nullable=False) 

2129 content = Column(String, nullable=False) # CommonMark without images 

2130 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2131 deleted = Column(DateTime(timezone=True), nullable=True) 

2132 

2133 comment = relationship("Comment", backref="replies") 

2134 

2135 

2136class BackgroundJobState(enum.Enum): 

2137 # job is fresh, waiting to be picked off the queue 

2138 pending = enum.auto() 

2139 # job complete 

2140 completed = enum.auto() 

2141 # error occured, will be retried 

2142 error = enum.auto() 

2143 # failed too many times, not retrying anymore 

2144 failed = enum.auto() 

2145 

2146 

2147class BackgroundJob(Base): 

2148 """ 

2149 This table implements a queue of background jobs. 

2150 """ 

2151 

2152 __tablename__ = "background_jobs" 

2153 

2154 id = Column(BigInteger, primary_key=True) 

2155 

2156 # used to discern which function should be triggered to service it 

2157 job_type = Column(String, nullable=False) 

2158 state = Column(Enum(BackgroundJobState), nullable=False, default=BackgroundJobState.pending) 

2159 

2160 # time queued 

2161 queued = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2162 

2163 # time at which we may next attempt it, for implementing exponential backoff 

2164 next_attempt_after = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2165 

2166 # used to count number of retries for failed jobs 

2167 try_count = Column(Integer, nullable=False, default=0) 

2168 

2169 max_tries = Column(Integer, nullable=False, default=5) 

2170 

2171 # protobuf encoded job payload 

2172 payload = Column(Binary, nullable=False) 

2173 

2174 # if the job failed, we write that info here 

2175 failure_info = Column(String, nullable=True) 

2176 

2177 __table_args__ = ( 

2178 # used in looking up background jobs to attempt 

2179 # create index on background_jobs(next_attempt_after, (max_tries - try_count)) where state = 'pending' OR state = 'error'; 

2180 Index( 

2181 "ix_background_jobs_lookup", 

2182 next_attempt_after, 

2183 (max_tries - try_count), 

2184 postgresql_where=((state == BackgroundJobState.pending) | (state == BackgroundJobState.error)), 

2185 ), 

2186 ) 

2187 

2188 @hybrid_property 

2189 def ready_for_retry(self): 

2190 return ( 

2191 (self.next_attempt_after <= func.now()) 

2192 & (self.try_count < self.max_tries) 

2193 & ((self.state == BackgroundJobState.pending) | (self.state == BackgroundJobState.error)) 

2194 ) 

2195 

2196 def __repr__(self): 

2197 return f"BackgroundJob(id={self.id}, job_type={self.job_type}, state={self.state}, next_attempt_after={self.next_attempt_after}, try_count={self.try_count}, failure_info={self.failure_info})" 

2198 

2199 

2200class NotificationDeliveryType(enum.Enum): 

2201 # send push notification to mobile/web 

2202 push = enum.auto() 

2203 # send individual email immediately 

2204 email = enum.auto() 

2205 # send in digest 

2206 digest = enum.auto() 

2207 

2208 

2209dt = NotificationDeliveryType 

2210nd = notification_data_pb2 

2211 

2212dt_sec = [dt.email, dt.push] 

2213dt_all = [dt.email, dt.push, dt.digest] 

2214 

2215 

2216class NotificationTopicAction(enum.Enum): 

2217 def __init__(self, topic_action, defaults, user_editable, data_type): 

2218 self.topic, self.action = topic_action.split(":") 

2219 self.defaults = defaults 

2220 # for now user editable == not a security notification 

2221 self.user_editable = user_editable 

2222 

2223 self.data_type = data_type 

2224 

2225 def unpack(self): 

2226 return self.topic, self.action 

2227 

2228 @property 

2229 def display(self): 

2230 return f"{self.topic}:{self.action}" 

2231 

2232 def __str__(self): 

2233 return self.display 

2234 

2235 # topic, action, default delivery types 

2236 friend_request__create = ("friend_request:create", dt_all, True, nd.FriendRequestCreate) 

2237 friend_request__accept = ("friend_request:accept", dt_all, True, nd.FriendRequestAccept) 

2238 

2239 # host requests 

2240 host_request__create = ("host_request:create", dt_all, True, nd.HostRequestCreate) 

2241 host_request__accept = ("host_request:accept", dt_all, True, nd.HostRequestAccept) 

2242 host_request__reject = ("host_request:reject", dt_all, True, nd.HostRequestReject) 

2243 host_request__confirm = ("host_request:confirm", dt_all, True, nd.HostRequestConfirm) 

2244 host_request__cancel = ("host_request:cancel", dt_all, True, nd.HostRequestCancel) 

2245 host_request__message = ("host_request:message", [dt.push, dt.digest], True, nd.HostRequestMessage) 

2246 host_request__missed_messages = ("host_request:missed_messages", [dt.email], True, nd.HostRequestMissedMessages) 

2247 

2248 # you receive a friend ref 

2249 reference__receive_friend = ("reference:receive_friend", dt_all, True, nd.ReferenceReceiveFriend) 

2250 # you receive a reference from ... the host 

2251 reference__receive_hosted = ("reference:receive_hosted", dt_all, True, nd.ReferenceReceiveHostRequest) 

2252 # ... the surfer 

2253 reference__receive_surfed = ("reference:receive_surfed", dt_all, True, nd.ReferenceReceiveHostRequest) 

2254 

2255 # you hosted 

2256 reference__reminder_hosted = ("reference:reminder_hosted", dt_all, True, nd.ReferenceReminder) 

2257 # you surfed 

2258 reference__reminder_surfed = ("reference:reminder_surfed", dt_all, True, nd.ReferenceReminder) 

2259 

2260 badge__add = ("badge:add", [dt.push, dt.digest], True, nd.BadgeAdd) 

2261 badge__remove = ("badge:remove", [dt.push, dt.digest], True, nd.BadgeRemove) 

2262 

2263 # group chats 

2264 chat__message = ("chat:message", [dt.push, dt.digest], True, nd.ChatMessage) 

2265 chat__missed_messages = ("chat:missed_messages", [dt.email], True, nd.ChatMissedMessages) 

2266 

2267 # events 

2268 # approved by mods 

2269 event__create_approved = ("event:create_approved", dt_all, True, nd.EventCreate) 

2270 # any user creates any event, default to no notifications 

2271 event__create_any = ("event:create_any", [], True, nd.EventCreate) 

2272 event__update = ("event:update", dt_all, True, nd.EventUpdate) 

2273 event__cancel = ("event:cancel", dt_all, True, nd.EventCancel) 

2274 event__delete = ("event:delete", dt_all, True, nd.EventDelete) 

2275 event__invite_organizer = ("event:invite_organizer", dt_all, True, nd.EventInviteOrganizer) 

2276 

2277 # account settings 

2278 password__change = ("password:change", dt_sec, False, empty_pb2.Empty) 

2279 email_address__change = ("email_address:change", dt_sec, False, nd.EmailAddressChange) 

2280 email_address__verify = ("email_address:verify", dt_sec, False, empty_pb2.Empty) 

2281 phone_number__change = ("phone_number:change", dt_sec, False, nd.PhoneNumberChange) 

2282 phone_number__verify = ("phone_number:verify", dt_sec, False, nd.PhoneNumberVerify) 

2283 # reset password 

2284 password_reset__start = ("password_reset:start", dt_sec, False, nd.PasswordResetStart) 

2285 password_reset__complete = ("password_reset:complete", dt_sec, False, empty_pb2.Empty) 

2286 

2287 # account deletion 

2288 account_deletion__start = ("account_deletion:start", dt_sec, False, nd.AccountDeletionStart) 

2289 # no more pushing to do 

2290 account_deletion__complete = ("account_deletion:complete", dt_sec, False, nd.AccountDeletionComplete) 

2291 # undeleted 

2292 account_deletion__recovered = ("account_deletion:recovered", dt_sec, False, empty_pb2.Empty) 

2293 

2294 # admin actions 

2295 gender__change = ("gender:change", dt_sec, False, nd.GenderChange) 

2296 birthdate__change = ("birthdate:change", dt_sec, False, nd.BirthdateChange) 

2297 api_key__create = ("api_key:create", dt_sec, False, nd.ApiKeyCreate) 

2298 

2299 donation__received = ("donation:received", dt_sec, True, nd.DonationReceived) 

2300 

2301 onboarding__reminder = ("onboarding:reminder", dt_sec, True, empty_pb2.Empty) 

2302 

2303 modnote__create = ("modnote:create", dt_sec, False, empty_pb2.Empty) 

2304 

2305 verification__sv_fail = ("verification:sv_fail", dt_sec, False, nd.VerificationSVFail) 

2306 verification__sv_success = ("verification:sv_success", dt_sec, False, empty_pb2.Empty) 

2307 

2308 

2309class NotificationPreference(Base): 

2310 __tablename__ = "notification_preferences" 

2311 

2312 id = Column(BigInteger, primary_key=True) 

2313 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

2314 

2315 topic_action = Column(Enum(NotificationTopicAction), nullable=False) 

2316 delivery_type = Column(Enum(NotificationDeliveryType), nullable=False) 

2317 deliver = Column(Boolean, nullable=False) 

2318 

2319 user = relationship("User", foreign_keys="NotificationPreference.user_id") 

2320 

2321 __table_args__ = (UniqueConstraint("user_id", "topic_action", "delivery_type"),) 

2322 

2323 

2324class Notification(Base): 

2325 """ 

2326 Table for accumulating notifications until it is time to send email digest 

2327 """ 

2328 

2329 __tablename__ = "notifications" 

2330 

2331 id = Column(BigInteger, primary_key=True) 

2332 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2333 

2334 # recipient user id 

2335 user_id = Column(ForeignKey("users.id"), nullable=False) 

2336 

2337 topic_action = Column(Enum(NotificationTopicAction), nullable=False) 

2338 key = Column(String, nullable=False) 

2339 

2340 data = Column(Binary, nullable=False) 

2341 

2342 user = relationship("User", foreign_keys="Notification.user_id") 

2343 

2344 __table_args__ = ( 

2345 # used in looking up which notifications need delivery 

2346 Index( 

2347 "ix_notifications_created", 

2348 created, 

2349 ), 

2350 ) 

2351 

2352 @property 

2353 def topic(self): 

2354 return self.topic_action.topic 

2355 

2356 @property 

2357 def action(self): 

2358 return self.topic_action.action 

2359 

2360 

2361class NotificationDelivery(Base): 

2362 __tablename__ = "notification_deliveries" 

2363 

2364 id = Column(BigInteger, primary_key=True) 

2365 notification_id = Column(ForeignKey("notifications.id"), nullable=False, index=True) 

2366 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2367 delivered = Column(DateTime(timezone=True), nullable=True) 

2368 read = Column(DateTime(timezone=True), nullable=True) 

2369 # todo: enum of "phone, web, digest" 

2370 delivery_type = Column(Enum(NotificationDeliveryType), nullable=False) 

2371 # todo: device id 

2372 # todo: receipt id, etc 

2373 notification = relationship("Notification", foreign_keys="NotificationDelivery.notification_id") 

2374 

2375 __table_args__ = ( 

2376 UniqueConstraint("notification_id", "delivery_type"), 

2377 # used in looking up which notifications need delivery 

2378 Index( 

2379 "ix_notification_deliveries_delivery_type", 

2380 delivery_type, 

2381 postgresql_where=(delivered != None), 

2382 ), 

2383 Index( 

2384 "ix_notification_deliveries_dt_ni_dnull", 

2385 delivery_type, 

2386 notification_id, 

2387 delivered == None, 

2388 ), 

2389 ) 

2390 

2391 

2392class PushNotificationSubscription(Base): 

2393 __tablename__ = "push_notification_subscriptions" 

2394 

2395 id = Column(BigInteger, primary_key=True) 

2396 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2397 

2398 # which user this is connected to 

2399 user_id = Column(ForeignKey("users.id"), nullable=False, index=True) 

2400 

2401 # these come from https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription 

2402 # the endpoint 

2403 endpoint = Column(String, nullable=False) 

2404 # the "auth" key 

2405 auth_key = Column(Binary, nullable=False) 

2406 # the "p256dh" key 

2407 p256dh_key = Column(Binary, nullable=False) 

2408 

2409 full_subscription_info = Column(String, nullable=False) 

2410 

2411 # the browse user-agent, so we can tell the user what browser notifications are going to 

2412 user_agent = Column(String, nullable=True) 

2413 

2414 # when it was disabled 

2415 disabled_at = Column(DateTime(timezone=True), nullable=False, server_default=DATETIME_INFINITY.isoformat()) 

2416 

2417 user = relationship("User") 

2418 

2419 

2420class PushNotificationDeliveryAttempt(Base): 

2421 __tablename__ = "push_notification_delivery_attempt" 

2422 

2423 id = Column(BigInteger, primary_key=True) 

2424 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2425 

2426 push_notification_subscription_id = Column( 

2427 ForeignKey("push_notification_subscriptions.id"), nullable=False, index=True 

2428 ) 

2429 

2430 success = Column(Boolean, nullable=False) 

2431 # the HTTP status code, 201 is success 

2432 status_code = Column(Integer, nullable=False) 

2433 

2434 # can be null if it was a success 

2435 response = Column(String, nullable=True) 

2436 

2437 push_notification_subscription = relationship("PushNotificationSubscription") 

2438 

2439 

2440class Language(Base): 

2441 """ 

2442 Table of allowed languages (a subset of ISO639-3) 

2443 """ 

2444 

2445 __tablename__ = "languages" 

2446 

2447 # ISO639-3 language code, in lowercase, e.g. fin, eng 

2448 code = Column(String(3), primary_key=True) 

2449 

2450 # the english name 

2451 name = Column(String, nullable=False, unique=True) 

2452 

2453 

2454class Region(Base): 

2455 """ 

2456 Table of regions 

2457 """ 

2458 

2459 __tablename__ = "regions" 

2460 

2461 # iso 3166-1 alpha3 code in uppercase, e.g. FIN, USA 

2462 code = Column(String(3), primary_key=True) 

2463 

2464 # the name, e.g. Finland, United States 

2465 # this is the display name in English, should be the "common name", not "Republic of Finland" 

2466 name = Column(String, nullable=False, unique=True) 

2467 

2468 

2469class UserBlock(Base): 

2470 """ 

2471 Table of blocked users 

2472 """ 

2473 

2474 __tablename__ = "user_blocks" 

2475 __table_args__ = (UniqueConstraint("blocking_user_id", "blocked_user_id"),) 

2476 

2477 id = Column(BigInteger, primary_key=True) 

2478 

2479 blocking_user_id = Column(ForeignKey("users.id"), nullable=False) 

2480 blocked_user_id = Column(ForeignKey("users.id"), nullable=False) 

2481 time_blocked = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2482 

2483 blocking_user = relationship("User", foreign_keys="UserBlock.blocking_user_id") 

2484 blocked_user = relationship("User", foreign_keys="UserBlock.blocked_user_id") 

2485 

2486 

2487class APICall(Base): 

2488 """ 

2489 API call logs 

2490 """ 

2491 

2492 __tablename__ = "api_calls" 

2493 __table_args__ = {"schema": "logging"} 

2494 

2495 id = Column(BigInteger, primary_key=True) 

2496 

2497 # whether the call was made using an api key or session cookies 

2498 is_api_key = Column(Boolean, nullable=False, server_default=text("false")) 

2499 

2500 # backend version (normally e.g. develop-31469e3), allows us to figure out which proto definitions were used 

2501 # note that `default` is a python side default, not hardcoded into DB schema 

2502 version = Column(String, nullable=False, default=config["VERSION"]) 

2503 

2504 # approximate time of the call 

2505 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2506 

2507 # the method call name, e.g. "/org.couchers.api.core.API/ListFriends" 

2508 method = Column(String, nullable=False) 

2509 

2510 # gRPC status code name, e.g. FAILED_PRECONDITION, None if success 

2511 status_code = Column(String, nullable=True) 

2512 

2513 # handler duration (excluding serialization, etc) 

2514 duration = Column(Float, nullable=False) 

2515 

2516 # user_id of caller, None means not logged in 

2517 user_id = Column(BigInteger, nullable=True) 

2518 

2519 # sanitized request bytes 

2520 request = Column(Binary, nullable=True) 

2521 

2522 # sanitized response bytes 

2523 response = Column(Binary, nullable=True) 

2524 

2525 # whether response bytes have been truncated 

2526 response_truncated = Column(Boolean, nullable=False, server_default=text("false")) 

2527 

2528 # the exception traceback, if any 

2529 traceback = Column(String, nullable=True) 

2530 

2531 # human readable perf report 

2532 perf_report = Column(String, nullable=True) 

2533 

2534 # details of the browser, if available 

2535 ip_address = Column(String, nullable=True) 

2536 user_agent = Column(String, nullable=True) 

2537 

2538 

2539class AccountDeletionReason(Base): 

2540 __tablename__ = "account_deletion_reason" 

2541 

2542 id = Column(BigInteger, primary_key=True) 

2543 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now()) 

2544 user_id = Column(ForeignKey("users.id"), nullable=False) 

2545 reason = Column(String, nullable=True) 

2546 

2547 user = relationship("User")