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

1064 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-11-21 04:21 +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 hometown = Column(String, nullable=True) 

135 

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

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

138 

139 timezone = column_property( 

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

141 deferred=True, 

142 ) 

143 

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

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

146 

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

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

149 # same as above for host requests 

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

151 

152 # display name 

153 name = Column(String, nullable=False) 

154 gender = Column(String, nullable=False) 

155 pronouns = Column(String, nullable=True) 

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

157 

158 # name as on official docs for verification, etc. not needed until verification 

159 full_name = Column(String, nullable=True) 

160 

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

162 

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

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

165 

166 # community standing score 

167 community_standing = Column(Float, nullable=True) 

168 

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

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

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

172 my_travels = Column(String, nullable=True) # CommonMark without images 

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

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

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

176 

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

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

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

180 

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

182 # accidental or they changed their mind 

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

184 undelete_token = Column(String, nullable=True) 

185 # validity of the undelete token 

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

187 

188 # hosting preferences 

189 max_guests = Column(Integer, nullable=True) 

190 last_minute = Column(Boolean, nullable=True) 

191 has_pets = Column(Boolean, nullable=True) 

192 accepts_pets = Column(Boolean, nullable=True) 

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

194 has_kids = Column(Boolean, nullable=True) 

195 accepts_kids = Column(Boolean, nullable=True) 

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

197 has_housemates = Column(Boolean, nullable=True) 

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

199 wheelchair_accessible = Column(Boolean, nullable=True) 

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

201 smokes_at_home = Column(Boolean, nullable=True) 

202 drinking_allowed = Column(Boolean, nullable=True) 

203 drinks_at_home = Column(Boolean, nullable=True) 

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

205 

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

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

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

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

210 parking = Column(Boolean, nullable=True) 

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

212 camping_ok = Column(Boolean, nullable=True) 

213 

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

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

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

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

218 

219 # number of onboarding emails sent 

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

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

222 

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

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

225 # opted out of the newsletter 

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

227 

228 # set to null to receive no digests 

229 digest_frequency = Column(Interval, nullable=True) 

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

231 

232 # for changing their email 

233 new_email = Column(String, nullable=True) 

234 

235 new_email_token = Column(String, nullable=True) 

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

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

238 

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

240 

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

242 # ,-------------------, 

243 # | Start | 

244 # | phone = None | someone else 

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

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

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

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

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

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

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

252 # '-----------------' +-------- | ------+----+--------------------+ '-----------------------' 

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

254 # | | ^ V | | 

255 # ,-----------------, | | ,-------------------, | ,-----------------------, 

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

257 # | phone = xx | | phone = xx | | | phone = xx | 

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

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

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

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

262 # '-----------------' '-------------------' '-----------------------' 

263 

264 # randomly generated Luhn 6-digit string 

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

266 

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

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

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

270 

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

272 # e.g. cus_JjoXHttuZopv0t 

273 # for new US entity 

274 stripe_customer_id = Column(String, nullable=True) 

275 # for old AU entity 

276 stripe_customer_id_old = Column(String, nullable=True) 

277 

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

279 

280 # checking for phone verification 

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

282 

283 # whether this user has all emails turned off 

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

285 

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

287 

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

289 

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

291 

292 __table_args__ = ( 

293 # Verified phone numbers should be unique 

294 Index( 

295 "ix_users_unique_phone", 

296 phone, 

297 unique=True, 

298 postgresql_where=phone_verification_verified != None, 

299 ), 

300 Index( 

301 "ix_users_active", 

302 id, 

303 postgresql_where=~is_banned & ~is_deleted, 

304 ), 

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

306 Index( 

307 "ix_users_geom_active", 

308 geom, 

309 id, 

310 username, 

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

312 ), 

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

314 CheckConstraint( 

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

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

317 name="check_new_email_token_state", 

318 ), 

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

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

321 CheckConstraint( 

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

323 name="phone_verified_conditions", 

324 ), 

325 # Email must match our regex 

326 CheckConstraint( 

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

328 name="valid_email", 

329 ), 

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

331 CheckConstraint( 

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

333 name="undelete_nullity", 

334 ), 

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

336 CheckConstraint( 

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

338 name="do_not_email_inactive", 

339 ), 

340 ) 

341 

342 @hybrid_property 

343 def has_completed_profile(self): 

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

345 

346 @has_completed_profile.expression 

347 def has_completed_profile(cls): 

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

349 

350 @hybrid_property 

351 def is_jailed(self): 

352 return ( 

353 (self.accepted_tos < TOS_VERSION) 

354 | (self.accepted_community_guidelines < GUIDELINES_VERSION) 

355 | self.is_missing_location 

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

357 ) 

358 

359 @hybrid_property 

360 def is_missing_location(self): 

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

362 

363 @hybrid_property 

364 def is_visible(self): 

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

366 

367 @property 

368 def coordinates(self): 

369 return get_coordinates(self.geom) 

370 

371 @property 

372 def display_joined(self): 

373 """ 

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

375 """ 

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

377 

378 @property 

379 def display_last_active(self): 

380 """ 

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

382 """ 

383 return last_active_coarsen(self.last_active) 

384 

385 @hybrid_property 

386 def phone_is_verified(self): 

387 return ( 

388 self.phone_verification_verified is not None 

389 and now() - self.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

390 ) 

391 

392 @phone_is_verified.expression 

393 def phone_is_verified(cls): 

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

395 now() - cls.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

396 ) 

397 

398 @hybrid_property 

399 def phone_code_expired(self): 

400 return now() - self.phone_verification_sent > SMS_CODE_LIFETIME 

401 

402 def __repr__(self): 

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

404 

405 

406class UserBadge(Base): 

407 """ 

408 A badge on a user's profile 

409 """ 

410 

411 __tablename__ = "user_badges" 

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

413 

414 id = Column(BigInteger, primary_key=True) 

415 

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

417 # corresponds to "id" in badges.json 

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

419 

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

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

422 

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

424 

425 

426class StrongVerificationAttemptStatus(enum.Enum): 

427 ## full data states 

428 # completed, this now provides verification for a user 

429 succeeded = enum.auto() 

430 

431 ## no data states 

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

433 in_progress_waiting_on_user_to_open_app = enum.auto() 

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

435 in_progress_waiting_on_user_in_app = enum.auto() 

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

437 in_progress_waiting_on_backend = enum.auto() 

438 # failed at iris end, no data 

439 failed = enum.auto() 

440 

441 ## minimal data states 

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

443 deleted = enum.auto() 

444 

445 

446class PassportSex(enum.Enum): 

447 """ 

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

449 """ 

450 

451 male = enum.auto() 

452 female = enum.auto() 

453 unspecified = enum.auto() 

454 

455 

456class StrongVerificationAttempt(Base): 

457 """ 

458 An attempt to perform strong verification 

459 """ 

460 

461 __tablename__ = "strong_verification_attempts" 

462 

463 # our verification id 

464 id = Column(BigInteger, primary_key=True) 

465 

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

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

468 

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

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

471 

472 status = Column( 

473 Enum(StrongVerificationAttemptStatus), 

474 nullable=False, 

475 default=StrongVerificationAttemptStatus.in_progress_waiting_on_user_to_open_app, 

476 ) 

477 

478 ## full data 

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

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

481 passport_encrypted_data = Column(Binary, nullable=True) 

482 passport_date_of_birth = Column(Date, nullable=True) 

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

484 

485 ## minimal data: this will not be deleted 

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

487 passport_expiry_date = Column(Date, nullable=True) 

488 passport_nationality = Column(String, nullable=True) 

489 # last three characters of the passport number 

490 passport_last_three_document_chars = Column(String, nullable=True) 

491 

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

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

494 

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

496 

497 user = relationship("User") 

498 

499 @hybrid_property 

500 def is_valid(self): 

501 """ 

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

503 """ 

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

505 

506 @is_valid.expression 

507 def is_valid(cls): 

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

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

510 ) 

511 

512 @hybrid_property 

513 def is_visible(self): 

514 return self.status != StrongVerificationAttemptStatus.deleted 

515 

516 @hybrid_method 

517 def matches_birthdate(self, user): 

518 return self.is_valid & (self.passport_date_of_birth == user.birthdate) 

519 

520 @hybrid_method 

521 def matches_gender(self, user): 

522 return self.is_valid & ( 

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

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

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

526 | (user.has_passport_sex_gender_exception == True) 

527 ) 

528 

529 @hybrid_method 

530 def has_strong_verification(self, user): 

531 return self.is_valid & self.matches_birthdate(user) & self.matches_gender(user) 

532 

533 __table_args__ = ( 

534 # used to look up verification status for a user 

535 Index( 

536 "ix_strong_verification_attempts_current", 

537 user_id, 

538 passport_expiry_date, 

539 postgresql_where=status == StrongVerificationAttemptStatus.succeeded, 

540 ), 

541 # each passport can be verified only once 

542 Index( 

543 "ix_strong_verification_attempts_unique_succeeded", 

544 passport_expiry_date, 

545 passport_nationality, 

546 passport_last_three_document_chars, 

547 unique=True, 

548 postgresql_where=( 

549 (status == StrongVerificationAttemptStatus.succeeded) 

550 | (status == StrongVerificationAttemptStatus.deleted) 

551 ), 

552 ), 

553 # full data check 

554 CheckConstraint( 

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

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

557 name="full_data_status", 

558 ), 

559 # minimal data check 

560 CheckConstraint( 

561 "(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 \ 

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

563 name="minimal_data_status", 

564 ), 

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

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

567 CheckConstraint( 

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

569 name="full_data_implies_minimal_data", 

570 ), 

571 # succeeded implies full data 

572 CheckConstraint( 

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

574 name="succeeded_implies_full_data", 

575 ), 

576 # in_progress/failed implies no_data 

577 CheckConstraint( 

578 "(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)", 

579 name="in_progress_failed_iris_implies_no_data", 

580 ), 

581 # deleted implies minimal data 

582 CheckConstraint( 

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

584 name="deleted_implies_minimal_data", 

585 ), 

586 ) 

587 

588 

589class ModNote(Base): 

590 """ 

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

592 

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

594 """ 

595 

596 __tablename__ = "mod_notes" 

597 id = Column(BigInteger, primary_key=True) 

598 

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

600 

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

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

603 

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

605 internal_id = Column(String, nullable=False) 

606 # the admin that left this note 

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

608 

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

610 

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

612 

613 def __repr__(self): 

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

615 

616 @hybrid_property 

617 def is_pending(self): 

618 return self.acknowledged == None 

619 

620 __table_args__ = ( 

621 # used to look up pending notes 

622 Index( 

623 "ix_mod_notes_unacknowledged", 

624 user_id, 

625 postgresql_where=acknowledged == None, 

626 ), 

627 ) 

628 

629 

630class StrongVerificationCallbackEvent(Base): 

631 __tablename__ = "strong_verification_callback_events" 

632 

633 id = Column(BigInteger, primary_key=True) 

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

635 

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

637 

638 iris_status = Column(String, nullable=False) 

639 

640 

641class DonationType(enum.Enum): 

642 one_time = enum.auto() 

643 recurring = enum.auto() 

644 

645 

646class DonationInitiation(Base): 

647 """ 

648 Whenever someone initiaties a donation through the platform 

649 """ 

650 

651 __tablename__ = "donation_initiations" 

652 id = Column(BigInteger, primary_key=True) 

653 

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

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

656 

657 amount = Column(Integer, nullable=False) 

658 stripe_checkout_session_id = Column(String, nullable=False) 

659 

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

661 

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

663 

664 

665class Invoice(Base): 

666 """ 

667 Successful donations, both one off and recurring 

668 

669 Triggered by `payment_intent.succeeded` webhook 

670 """ 

671 

672 __tablename__ = "invoices" 

673 

674 id = Column(BigInteger, primary_key=True) 

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

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

677 

678 amount = Column(Float, nullable=False) 

679 

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

681 stripe_receipt_url = Column(String, nullable=False) 

682 

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

684 

685 

686class LanguageFluency(enum.Enum): 

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

688 beginner = 1 

689 conversational = 2 

690 fluent = 3 

691 

692 

693class LanguageAbility(Base): 

694 __tablename__ = "language_abilities" 

695 __table_args__ = ( 

696 # Users can only have one language ability per language 

697 UniqueConstraint("user_id", "language_code"), 

698 ) 

699 

700 id = Column(BigInteger, primary_key=True) 

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

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

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

704 

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

706 language = relationship("Language") 

707 

708 

709class RegionVisited(Base): 

710 __tablename__ = "regions_visited" 

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

712 

713 id = Column(BigInteger, primary_key=True) 

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

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

716 

717 

718class RegionLived(Base): 

719 __tablename__ = "regions_lived" 

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

721 

722 id = Column(BigInteger, primary_key=True) 

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

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

725 

726 

727class FriendStatus(enum.Enum): 

728 pending = enum.auto() 

729 accepted = enum.auto() 

730 rejected = enum.auto() 

731 cancelled = enum.auto() 

732 

733 

734class FriendRelationship(Base): 

735 """ 

736 Friendship relations between users 

737 

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

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

740 """ 

741 

742 __tablename__ = "friend_relationships" 

743 

744 id = Column(BigInteger, primary_key=True) 

745 

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

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

748 

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

750 

751 # timezones should always be UTC 

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

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

754 

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

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

757 

758 

759class ContributeOption(enum.Enum): 

760 yes = enum.auto() 

761 maybe = enum.auto() 

762 no = enum.auto() 

763 

764 

765class ContributorForm(Base): 

766 """ 

767 Someone filled in the contributor form 

768 """ 

769 

770 __tablename__ = "contributor_forms" 

771 

772 id = Column(BigInteger, primary_key=True) 

773 

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

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

776 

777 ideas = Column(String, nullable=True) 

778 features = Column(String, nullable=True) 

779 experience = Column(String, nullable=True) 

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

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

782 expertise = Column(String, nullable=True) 

783 

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

785 

786 @hybrid_property 

787 def is_filled(self): 

788 """ 

789 Whether the form counts as having been filled 

790 """ 

791 return ( 

792 (self.ideas != None) 

793 | (self.features != None) 

794 | (self.experience != None) 

795 | (self.contribute != None) 

796 | (self.contribute_ways != []) 

797 | (self.expertise != None) 

798 ) 

799 

800 @property 

801 def should_notify(self): 

802 """ 

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

804 

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

806 """ 

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

808 

809 

810class SignupFlow(Base): 

811 """ 

812 Signup flows/incomplete users 

813 

814 Coinciding fields have the same meaning as in User 

815 """ 

816 

817 __tablename__ = "signup_flows" 

818 

819 id = Column(BigInteger, primary_key=True) 

820 

821 # housekeeping 

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

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

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

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

826 email_token = Column(String, nullable=True) 

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

828 

829 ## Basic 

830 name = Column(String, nullable=False) 

831 # TODO: unique across both tables 

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

833 # TODO: invitation, attribution 

834 

835 ## Account 

836 # TODO: unique across both tables 

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

838 hashed_password = Column(Binary, nullable=True) 

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

840 gender = Column(String, nullable=True) 

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

842 city = Column(String, nullable=True) 

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

844 geom_radius = Column(Float, nullable=True) 

845 

846 accepted_tos = Column(Integer, nullable=True) 

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

848 

849 opt_out_of_newsletter = Column(Boolean, nullable=True) 

850 

851 ## Feedback 

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

853 ideas = Column(String, nullable=True) 

854 features = Column(String, nullable=True) 

855 experience = Column(String, nullable=True) 

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

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

858 expertise = Column(String, nullable=True) 

859 

860 @hybrid_property 

861 def token_is_valid(self): 

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

863 

864 @hybrid_property 

865 def account_is_filled(self): 

866 return ( 

867 (self.username != None) 

868 & (self.birthdate != None) 

869 & (self.gender != None) 

870 & (self.hosting_status != None) 

871 & (self.city != None) 

872 & (self.geom != None) 

873 & (self.geom_radius != None) 

874 & (self.accepted_tos != None) 

875 & (self.opt_out_of_newsletter != None) 

876 ) 

877 

878 @hybrid_property 

879 def is_completed(self): 

880 return ( 

881 self.email_verified 

882 & self.account_is_filled 

883 & self.filled_feedback 

884 & (self.accepted_community_guidelines == GUIDELINES_VERSION) 

885 ) 

886 

887 

888class LoginToken(Base): 

889 """ 

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

891 """ 

892 

893 __tablename__ = "login_tokens" 

894 token = Column(String, primary_key=True) 

895 

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

897 

898 # timezones should always be UTC 

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

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

901 

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

903 

904 @hybrid_property 

905 def is_valid(self): 

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

907 

908 def __repr__(self): 

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

910 

911 

912class PasswordResetToken(Base): 

913 __tablename__ = "password_reset_tokens" 

914 token = Column(String, primary_key=True) 

915 

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

917 

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

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

920 

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

922 

923 @hybrid_property 

924 def is_valid(self): 

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

926 

927 def __repr__(self): 

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

929 

930 

931class AccountDeletionToken(Base): 

932 __tablename__ = "account_deletion_tokens" 

933 

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="account_deletion_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"AccountDeletionToken(token={self.token}, user_id={self.user_id}, created={self.created}, expiry={self.expiry})" 

949 

950 

951class UserActivity(Base): 

952 """ 

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

954 

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

956 """ 

957 

958 __tablename__ = "user_activity" 

959 

960 id = Column(BigInteger, primary_key=True) 

961 

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

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

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

965 

966 # details of the browser, if available 

967 ip_address = Column(INET, nullable=True) 

968 user_agent = Column(String, nullable=True) 

969 

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

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

972 

973 __table_args__ = ( 

974 # helps look up this tuple quickly 

975 Index( 

976 "ix_user_activity_user_id_period_ip_address_user_agent", 

977 user_id, 

978 period, 

979 ip_address, 

980 user_agent, 

981 unique=True, 

982 ), 

983 ) 

984 

985 

986class UserSession(Base): 

987 """ 

988 API keys/session cookies for the app 

989 

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

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

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

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

994 site. 

995 

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

997 

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

999 """ 

1000 

1001 __tablename__ = "sessions" 

1002 token = Column(String, primary_key=True) 

1003 

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

1005 

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

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

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

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

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

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

1012 

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

1014 long_lived = Column(Boolean, nullable=False) 

1015 

1016 # the time at which the session was created 

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

1018 

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

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

1021 

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

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

1024 

1025 # the last time this session was used 

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

1027 

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

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

1030 

1031 # details of the browser, if available 

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

1033 ip_address = Column(String, nullable=True) 

1034 user_agent = Column(String, nullable=True) 

1035 

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

1037 

1038 @hybrid_property 

1039 def is_valid(self): 

1040 """ 

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

1042 

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

1044 

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

1046 """ 

1047 return ( 

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

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

1050 & (self.deleted == None) 

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

1052 ) 

1053 

1054 

1055class Conversation(Base): 

1056 """ 

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

1058 """ 

1059 

1060 __tablename__ = "conversations" 

1061 

1062 id = Column(BigInteger, primary_key=True) 

1063 # timezone should always be UTC 

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

1065 

1066 def __repr__(self): 

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

1068 

1069 

1070class GroupChat(Base): 

1071 """ 

1072 Group chat 

1073 """ 

1074 

1075 __tablename__ = "group_chats" 

1076 

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

1078 

1079 title = Column(String, nullable=True) 

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

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

1082 is_dm = Column(Boolean, nullable=False) 

1083 

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

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

1086 

1087 def __repr__(self): 

1088 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})" 

1089 

1090 

1091class GroupChatRole(enum.Enum): 

1092 admin = enum.auto() 

1093 participant = enum.auto() 

1094 

1095 

1096class GroupChatSubscription(Base): 

1097 """ 

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

1099 """ 

1100 

1101 __tablename__ = "group_chat_subscriptions" 

1102 id = Column(BigInteger, primary_key=True) 

1103 

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

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

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

1107 

1108 # timezones should always be UTC 

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

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

1111 

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

1113 

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

1115 

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

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

1118 

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

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

1121 

1122 def muted_display(self): 

1123 """ 

1124 Returns (muted, muted_until) display values: 

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

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

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

1128 """ 

1129 if self.muted_until < now(): 

1130 return (False, None) 

1131 elif self.muted_until == DATETIME_INFINITY: 

1132 return (True, None) 

1133 else: 

1134 return (True, self.muted_until) 

1135 

1136 @hybrid_property 

1137 def is_muted(self): 

1138 return self.muted_until > func.now() 

1139 

1140 def __repr__(self): 

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

1142 

1143 

1144class MessageType(enum.Enum): 

1145 text = enum.auto() 

1146 # e.g. 

1147 # image = 

1148 # emoji = 

1149 # ... 

1150 chat_created = enum.auto() 

1151 chat_edited = enum.auto() 

1152 user_invited = enum.auto() 

1153 user_left = enum.auto() 

1154 user_made_admin = enum.auto() 

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

1156 host_request_status_changed = enum.auto() 

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

1158 

1159 

1160class HostRequestStatus(enum.Enum): 

1161 pending = enum.auto() 

1162 accepted = enum.auto() 

1163 rejected = enum.auto() 

1164 confirmed = enum.auto() 

1165 cancelled = enum.auto() 

1166 

1167 

1168class Message(Base): 

1169 """ 

1170 A message. 

1171 

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

1173 """ 

1174 

1175 __tablename__ = "messages" 

1176 

1177 id = Column(BigInteger, primary_key=True) 

1178 

1179 # which conversation the message belongs in 

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

1181 

1182 # the user that sent the message/command 

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

1184 

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

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

1187 

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

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

1190 

1191 # time sent, timezone should always be UTC 

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

1193 

1194 # the plain-text message text if not control 

1195 text = Column(String, nullable=True) 

1196 

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

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

1199 

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

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

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

1203 

1204 @property 

1205 def is_normal_message(self): 

1206 """ 

1207 There's only one normal type atm, text 

1208 """ 

1209 return self.message_type == MessageType.text 

1210 

1211 def __repr__(self): 

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

1213 

1214 

1215class ContentReport(Base): 

1216 """ 

1217 A piece of content reported to admins 

1218 """ 

1219 

1220 __tablename__ = "content_reports" 

1221 

1222 id = Column(BigInteger, primary_key=True) 

1223 

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

1225 

1226 # the user who reported or flagged the content 

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

1228 

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

1230 reason = Column(String, nullable=False) 

1231 # a short description 

1232 description = Column(String, nullable=False) 

1233 

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

1235 content_ref = Column(String, nullable=False) 

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

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

1238 

1239 # details of the browser, if available 

1240 user_agent = Column(String, nullable=False) 

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

1242 page = Column(String, nullable=False) 

1243 

1244 # see comments above for reporting vs author 

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

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

1247 

1248 

1249class Email(Base): 

1250 """ 

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

1252 """ 

1253 

1254 __tablename__ = "emails" 

1255 

1256 id = Column(String, primary_key=True) 

1257 

1258 # timezone should always be UTC 

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

1260 

1261 sender_name = Column(String, nullable=False) 

1262 sender_email = Column(String, nullable=False) 

1263 

1264 recipient = Column(String, nullable=False) 

1265 subject = Column(String, nullable=False) 

1266 

1267 plain = Column(String, nullable=False) 

1268 html = Column(String, nullable=False) 

1269 

1270 list_unsubscribe_header = Column(String, nullable=True) 

1271 source_data = Column(String, nullable=True) 

1272 

1273 

1274class SMS(Base): 

1275 """ 

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

1277 """ 

1278 

1279 __tablename__ = "smss" 

1280 

1281 id = Column(BigInteger, primary_key=True) 

1282 

1283 # timezone should always be UTC 

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

1285 # AWS message id 

1286 message_id = Column(String, nullable=False) 

1287 

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

1289 sms_sender_id = Column(String, nullable=False) 

1290 number = Column(String, nullable=False) 

1291 message = Column(String, nullable=False) 

1292 

1293 

1294class HostRequest(Base): 

1295 """ 

1296 A request to stay with a host 

1297 """ 

1298 

1299 __tablename__ = "host_requests" 

1300 

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

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

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

1304 

1305 # TODO: proper timezone handling 

1306 timezone = "Etc/UTC" 

1307 

1308 # dates in the timezone above 

1309 from_date = Column(Date, nullable=False) 

1310 to_date = Column(Date, nullable=False) 

1311 

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

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

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

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

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

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

1318 

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

1320 

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

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

1323 

1324 # number of reference reminders sent out 

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

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

1327 

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

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

1330 conversation = relationship("Conversation") 

1331 

1332 @hybrid_property 

1333 def can_write_reference(self): 

1334 return ( 

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

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

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

1338 ) 

1339 

1340 @can_write_reference.expression 

1341 def can_write_reference(cls): 

1342 return ( 

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

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

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

1346 ) 

1347 

1348 def __repr__(self): 

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

1350 

1351 

1352class ReferenceType(enum.Enum): 

1353 friend = enum.auto() 

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

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

1356 

1357 

1358class Reference(Base): 

1359 """ 

1360 Reference from one user to another 

1361 """ 

1362 

1363 __tablename__ = "references" 

1364 

1365 id = Column(BigInteger, primary_key=True) 

1366 # timezone should always be UTC 

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

1368 

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

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

1371 

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

1373 

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

1375 

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

1377 # text that's only visible to mods 

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

1379 

1380 rating = Column(Float, nullable=False) 

1381 was_appropriate = Column(Boolean, nullable=False) 

1382 

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

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

1385 

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

1387 

1388 __table_args__ = ( 

1389 # Rating must be between 0 and 1, inclusive 

1390 CheckConstraint( 

1391 "rating BETWEEN 0 AND 1", 

1392 name="rating_between_0_and_1", 

1393 ), 

1394 # Has host_request_id or it's a friend reference 

1395 CheckConstraint( 

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

1397 name="host_request_id_xor_friend_reference", 

1398 ), 

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

1400 Index( 

1401 "ix_references_unique_friend_reference", 

1402 from_user_id, 

1403 to_user_id, 

1404 reference_type, 

1405 unique=True, 

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

1407 ), 

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

1409 Index( 

1410 "ix_references_unique_per_host_request", 

1411 from_user_id, 

1412 to_user_id, 

1413 host_request_id, 

1414 unique=True, 

1415 postgresql_where=(host_request_id != None), 

1416 ), 

1417 ) 

1418 

1419 @property 

1420 def should_report(self): 

1421 """ 

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

1423 """ 

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

1425 

1426 

1427class InitiatedUpload(Base): 

1428 """ 

1429 Started downloads, not necessarily complete yet. 

1430 """ 

1431 

1432 __tablename__ = "initiated_uploads" 

1433 

1434 key = Column(String, primary_key=True) 

1435 

1436 # timezones should always be UTC 

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

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

1439 

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

1441 

1442 initiator_user = relationship("User") 

1443 

1444 @hybrid_property 

1445 def is_valid(self): 

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

1447 

1448 

1449class Upload(Base): 

1450 """ 

1451 Completed uploads. 

1452 """ 

1453 

1454 __tablename__ = "uploads" 

1455 key = Column(String, primary_key=True) 

1456 

1457 filename = Column(String, nullable=False) 

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

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

1460 

1461 # photo credit, etc 

1462 credit = Column(String, nullable=True) 

1463 

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

1465 

1466 def _url(self, size): 

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

1468 

1469 @property 

1470 def thumbnail_url(self): 

1471 return self._url("thumbnail") 

1472 

1473 @property 

1474 def full_url(self): 

1475 return self._url("full") 

1476 

1477 

1478communities_seq = Sequence("communities_seq") 

1479 

1480 

1481class Node(Base): 

1482 """ 

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

1484 

1485 Administered by the official cluster 

1486 """ 

1487 

1488 __tablename__ = "nodes" 

1489 

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

1491 

1492 # name and description come from official cluster 

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

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

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

1496 

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

1498 

1499 contained_users = relationship( 

1500 "User", 

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

1502 viewonly=True, 

1503 uselist=True, 

1504 ) 

1505 

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

1507 

1508 

1509class Cluster(Base): 

1510 """ 

1511 Cluster, administered grouping of content 

1512 """ 

1513 

1514 __tablename__ = "clusters" 

1515 

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

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

1518 name = Column(String, nullable=False) 

1519 # short description 

1520 description = Column(String, nullable=False) 

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

1522 

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

1524 

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

1526 

1527 official_cluster_for_node = relationship( 

1528 "Node", 

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

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

1531 uselist=False, 

1532 viewonly=True, 

1533 ) 

1534 

1535 parent_node = relationship( 

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

1537 ) 

1538 

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

1540 # all pages 

1541 pages = relationship( 

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

1543 ) 

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

1545 discussions = relationship( 

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

1547 ) 

1548 

1549 # includes also admins 

1550 members = relationship( 

1551 "User", 

1552 lazy="dynamic", 

1553 backref="cluster_memberships", 

1554 secondary="cluster_subscriptions", 

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

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

1557 viewonly=True, 

1558 ) 

1559 

1560 admins = relationship( 

1561 "User", 

1562 lazy="dynamic", 

1563 backref="cluster_adminships", 

1564 secondary="cluster_subscriptions", 

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

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

1567 viewonly=True, 

1568 ) 

1569 

1570 main_page = relationship( 

1571 "Page", 

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

1573 viewonly=True, 

1574 uselist=False, 

1575 ) 

1576 

1577 @property 

1578 def is_leaf(self) -> bool: 

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

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

1581 

1582 __table_args__ = ( 

1583 # Each node can have at most one official cluster 

1584 Index( 

1585 "ix_clusters_owner_parent_node_id_is_official_cluster", 

1586 parent_node_id, 

1587 is_official_cluster, 

1588 unique=True, 

1589 postgresql_where=is_official_cluster, 

1590 ), 

1591 ) 

1592 

1593 

1594class NodeClusterAssociation(Base): 

1595 """ 

1596 NodeClusterAssociation, grouping of nodes 

1597 """ 

1598 

1599 __tablename__ = "node_cluster_associations" 

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

1601 

1602 id = Column(BigInteger, primary_key=True) 

1603 

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

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

1606 

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

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

1609 

1610 

1611class ClusterRole(enum.Enum): 

1612 member = enum.auto() 

1613 admin = enum.auto() 

1614 

1615 

1616class ClusterSubscription(Base): 

1617 """ 

1618 ClusterSubscription of a user 

1619 """ 

1620 

1621 __tablename__ = "cluster_subscriptions" 

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

1623 

1624 id = Column(BigInteger, primary_key=True) 

1625 

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

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

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

1629 

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

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

1632 

1633 

1634class ClusterPageAssociation(Base): 

1635 """ 

1636 pages related to clusters 

1637 """ 

1638 

1639 __tablename__ = "cluster_page_associations" 

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

1641 

1642 id = Column(BigInteger, primary_key=True) 

1643 

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

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

1646 

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

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

1649 

1650 

1651class PageType(enum.Enum): 

1652 main_page = enum.auto() 

1653 place = enum.auto() 

1654 guide = enum.auto() 

1655 

1656 

1657class Page(Base): 

1658 """ 

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

1660 """ 

1661 

1662 __tablename__ = "pages" 

1663 

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

1665 

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

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

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

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

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

1671 

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

1673 

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

1675 

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

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

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

1679 owner_cluster = relationship( 

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

1681 ) 

1682 

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

1684 

1685 __table_args__ = ( 

1686 # Only one of owner_user and owner_cluster should be set 

1687 CheckConstraint( 

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

1689 name="one_owner", 

1690 ), 

1691 # Only clusters can own main pages 

1692 CheckConstraint( 

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

1694 name="main_page_owned_by_cluster", 

1695 ), 

1696 # Each cluster can have at most one main page 

1697 Index( 

1698 "ix_pages_owner_cluster_id_type", 

1699 owner_cluster_id, 

1700 type, 

1701 unique=True, 

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

1703 ), 

1704 ) 

1705 

1706 def __repr__(self): 

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

1708 

1709 

1710class PageVersion(Base): 

1711 """ 

1712 version of page content 

1713 """ 

1714 

1715 __tablename__ = "page_versions" 

1716 

1717 id = Column(BigInteger, primary_key=True) 

1718 

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

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

1721 title = Column(String, nullable=False) 

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

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

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

1725 # the human-readable address 

1726 address = Column(String, nullable=True) 

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

1728 

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

1730 

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

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

1733 photo = relationship("Upload") 

1734 

1735 __table_args__ = ( 

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

1737 CheckConstraint( 

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

1739 name="geom_iff_address", 

1740 ), 

1741 ) 

1742 

1743 @property 

1744 def coordinates(self): 

1745 # returns (lat, lng) or None 

1746 return get_coordinates(self.geom) 

1747 

1748 def __repr__(self): 

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

1750 

1751 

1752class ClusterEventAssociation(Base): 

1753 """ 

1754 events related to clusters 

1755 """ 

1756 

1757 __tablename__ = "cluster_event_associations" 

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

1759 

1760 id = Column(BigInteger, primary_key=True) 

1761 

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

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

1764 

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

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

1767 

1768 

1769class Event(Base): 

1770 """ 

1771 An event is compose of two parts: 

1772 

1773 * An event template (Event) 

1774 * An occurrence (EventOccurrence) 

1775 

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

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

1778 """ 

1779 

1780 __tablename__ = "events" 

1781 

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

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

1784 

1785 title = Column(String, nullable=False) 

1786 

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

1788 

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

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

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

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

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

1794 

1795 parent_node = relationship( 

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

1797 ) 

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

1799 subscribers = relationship( 

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

1801 ) 

1802 organizers = relationship( 

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

1804 ) 

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

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

1807 owner_cluster = relationship( 

1808 "Cluster", 

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

1810 uselist=False, 

1811 foreign_keys="Event.owner_cluster_id", 

1812 ) 

1813 

1814 __table_args__ = ( 

1815 # Only one of owner_user and owner_cluster should be set 

1816 CheckConstraint( 

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

1818 name="one_owner", 

1819 ), 

1820 ) 

1821 

1822 

1823class EventOccurrence(Base): 

1824 __tablename__ = "event_occurrences" 

1825 

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

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

1828 

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

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

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

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

1833 

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

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

1836 

1837 # a null geom is an online-only event 

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

1839 # physical address, iff geom is not null 

1840 address = Column(String, nullable=True) 

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

1842 link = Column(String, nullable=True) 

1843 

1844 timezone = "Etc/UTC" 

1845 

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

1847 # simplifies database constraints, etc 

1848 during = Column(TSTZRANGE, nullable=False) 

1849 

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

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

1852 

1853 creator_user = relationship( 

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

1855 ) 

1856 event = relationship( 

1857 "Event", 

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

1859 remote_side="Event.id", 

1860 foreign_keys="EventOccurrence.event_id", 

1861 ) 

1862 

1863 photo = relationship("Upload") 

1864 

1865 __table_args__ = ( 

1866 # Geom and address go together 

1867 CheckConstraint( 

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

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

1870 name="geom_iff_address", 

1871 ), 

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

1873 CheckConstraint( 

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

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

1876 name="link_or_geom", 

1877 ), 

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

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

1880 ) 

1881 

1882 @property 

1883 def coordinates(self): 

1884 # returns (lat, lng) or None 

1885 return get_coordinates(self.geom) 

1886 

1887 @hybrid_property 

1888 def start_time(self): 

1889 return self.during.lower 

1890 

1891 @start_time.expression 

1892 def start_time(cls): 

1893 return func.lower(cls.during) 

1894 

1895 @hybrid_property 

1896 def end_time(self): 

1897 return self.during.upper 

1898 

1899 @end_time.expression 

1900 def end_time(cls): 

1901 return func.upper(cls.during) 

1902 

1903 

1904class EventSubscription(Base): 

1905 """ 

1906 Users' subscriptions to events 

1907 """ 

1908 

1909 __tablename__ = "event_subscriptions" 

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

1911 

1912 id = Column(BigInteger, primary_key=True) 

1913 

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

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

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

1917 

1918 user = relationship("User") 

1919 event = relationship("Event") 

1920 

1921 

1922class EventOrganizer(Base): 

1923 """ 

1924 Organizers for events 

1925 """ 

1926 

1927 __tablename__ = "event_organizers" 

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

1929 

1930 id = Column(BigInteger, primary_key=True) 

1931 

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

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

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

1935 

1936 user = relationship("User") 

1937 event = relationship("Event") 

1938 

1939 

1940class AttendeeStatus(enum.Enum): 

1941 going = enum.auto() 

1942 maybe = enum.auto() 

1943 

1944 

1945class EventOccurrenceAttendee(Base): 

1946 """ 

1947 Attendees for events 

1948 """ 

1949 

1950 __tablename__ = "event_occurrence_attendees" 

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

1952 

1953 id = Column(BigInteger, primary_key=True) 

1954 

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

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

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

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

1959 

1960 user = relationship("User") 

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

1962 

1963 

1964class EventCommunityInviteRequest(Base): 

1965 """ 

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

1967 """ 

1968 

1969 __tablename__ = "event_community_invite_requests" 

1970 

1971 id = Column(BigInteger, primary_key=True) 

1972 

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

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

1975 

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

1977 

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

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

1980 approved = Column(Boolean, nullable=True) 

1981 

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

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

1984 

1985 __table_args__ = ( 

1986 # each user can only request once 

1987 UniqueConstraint("occurrence_id", "user_id"), 

1988 # each event can only have one notification sent out 

1989 Index( 

1990 "ix_event_community_invite_requests_unique", 

1991 occurrence_id, 

1992 unique=True, 

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

1994 ), 

1995 # decided and approved ought to be null simultaneously 

1996 CheckConstraint( 

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

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

1999 name="decided_approved", 

2000 ), 

2001 ) 

2002 

2003 

2004class ClusterDiscussionAssociation(Base): 

2005 """ 

2006 discussions related to clusters 

2007 """ 

2008 

2009 __tablename__ = "cluster_discussion_associations" 

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

2011 

2012 id = Column(BigInteger, primary_key=True) 

2013 

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

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

2016 

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

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

2019 

2020 

2021class Discussion(Base): 

2022 """ 

2023 forum board 

2024 """ 

2025 

2026 __tablename__ = "discussions" 

2027 

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

2029 

2030 title = Column(String, nullable=False) 

2031 content = Column(String, nullable=False) 

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

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

2034 

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

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

2037 

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

2039 

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

2041 

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

2043 

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

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

2046 

2047 

2048class DiscussionSubscription(Base): 

2049 """ 

2050 users subscriptions to discussions 

2051 """ 

2052 

2053 __tablename__ = "discussion_subscriptions" 

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

2055 

2056 id = Column(BigInteger, primary_key=True) 

2057 

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

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

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

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

2062 

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

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

2065 

2066 

2067class Thread(Base): 

2068 """ 

2069 Thread 

2070 """ 

2071 

2072 __tablename__ = "threads" 

2073 

2074 id = Column(BigInteger, primary_key=True) 

2075 

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

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

2078 

2079 

2080class Comment(Base): 

2081 """ 

2082 Comment 

2083 """ 

2084 

2085 __tablename__ = "comments" 

2086 

2087 id = Column(BigInteger, primary_key=True) 

2088 

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

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

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

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

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

2094 

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

2096 

2097 

2098class Reply(Base): 

2099 """ 

2100 Reply 

2101 """ 

2102 

2103 __tablename__ = "replies" 

2104 

2105 id = Column(BigInteger, primary_key=True) 

2106 

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

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

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

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

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

2112 

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

2114 

2115 

2116class BackgroundJobState(enum.Enum): 

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

2118 pending = enum.auto() 

2119 # job complete 

2120 completed = enum.auto() 

2121 # error occured, will be retried 

2122 error = enum.auto() 

2123 # failed too many times, not retrying anymore 

2124 failed = enum.auto() 

2125 

2126 

2127class BackgroundJob(Base): 

2128 """ 

2129 This table implements a queue of background jobs. 

2130 """ 

2131 

2132 __tablename__ = "background_jobs" 

2133 

2134 id = Column(BigInteger, primary_key=True) 

2135 

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

2137 job_type = Column(String, nullable=False) 

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

2139 

2140 # time queued 

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

2142 

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

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

2145 

2146 # used to count number of retries for failed jobs 

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

2148 

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

2150 

2151 # protobuf encoded job payload 

2152 payload = Column(Binary, nullable=False) 

2153 

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

2155 failure_info = Column(String, nullable=True) 

2156 

2157 __table_args__ = ( 

2158 # used in looking up background jobs to attempt 

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

2160 Index( 

2161 "ix_background_jobs_lookup", 

2162 next_attempt_after, 

2163 (max_tries - try_count), 

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

2165 ), 

2166 ) 

2167 

2168 @hybrid_property 

2169 def ready_for_retry(self): 

2170 return ( 

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

2172 & (self.try_count < self.max_tries) 

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

2174 ) 

2175 

2176 def __repr__(self): 

2177 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})" 

2178 

2179 

2180class NotificationDeliveryType(enum.Enum): 

2181 # send push notification to mobile/web 

2182 push = enum.auto() 

2183 # send individual email immediately 

2184 email = enum.auto() 

2185 # send in digest 

2186 digest = enum.auto() 

2187 

2188 

2189dt = NotificationDeliveryType 

2190nd = notification_data_pb2 

2191 

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

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

2194 

2195 

2196class NotificationTopicAction(enum.Enum): 

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

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

2199 self.defaults = defaults 

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

2201 self.user_editable = user_editable 

2202 

2203 self.data_type = data_type 

2204 

2205 def unpack(self): 

2206 return self.topic, self.action 

2207 

2208 @property 

2209 def display(self): 

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

2211 

2212 def __str__(self): 

2213 return self.display 

2214 

2215 # topic, action, default delivery types 

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

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

2218 

2219 # host requests 

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

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

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

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

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

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

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

2227 

2228 # you receive a friend ref 

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

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

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

2232 # ... the surfer 

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

2234 

2235 # you hosted 

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

2237 # you surfed 

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

2239 

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

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

2242 

2243 # group chats 

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

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

2246 

2247 # events 

2248 # approved by mods 

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

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

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

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

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

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

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

2256 

2257 # account settings 

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

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

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

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

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

2263 # reset password 

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

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

2266 

2267 # account deletion 

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

2269 # no more pushing to do 

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

2271 # undeleted 

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

2273 

2274 # admin actions 

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

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

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

2278 

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

2280 

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

2282 

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

2284 

2285 

2286class NotificationPreference(Base): 

2287 __tablename__ = "notification_preferences" 

2288 

2289 id = Column(BigInteger, primary_key=True) 

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

2291 

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

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

2294 deliver = Column(Boolean, nullable=False) 

2295 

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

2297 

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

2299 

2300 

2301class Notification(Base): 

2302 """ 

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

2304 """ 

2305 

2306 __tablename__ = "notifications" 

2307 

2308 id = Column(BigInteger, primary_key=True) 

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

2310 

2311 # recipient user id 

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

2313 

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

2315 key = Column(String, nullable=False) 

2316 

2317 data = Column(Binary, nullable=False) 

2318 

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

2320 

2321 __table_args__ = ( 

2322 # used in looking up which notifications need delivery 

2323 Index( 

2324 "ix_notifications_created", 

2325 created, 

2326 ), 

2327 ) 

2328 

2329 @property 

2330 def topic(self): 

2331 return self.topic_action.topic 

2332 

2333 @property 

2334 def action(self): 

2335 return self.topic_action.action 

2336 

2337 

2338class NotificationDelivery(Base): 

2339 __tablename__ = "notification_deliveries" 

2340 

2341 id = Column(BigInteger, primary_key=True) 

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

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

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

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

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

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

2348 # todo: device id 

2349 # todo: receipt id, etc 

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

2351 

2352 __table_args__ = ( 

2353 UniqueConstraint("notification_id", "delivery_type"), 

2354 # used in looking up which notifications need delivery 

2355 Index( 

2356 "ix_notification_deliveries_delivery_type", 

2357 delivery_type, 

2358 postgresql_where=(delivered != None), 

2359 ), 

2360 Index( 

2361 "ix_notification_deliveries_dt_ni_dnull", 

2362 delivery_type, 

2363 notification_id, 

2364 delivered == None, 

2365 ), 

2366 ) 

2367 

2368 

2369class PushNotificationSubscription(Base): 

2370 __tablename__ = "push_notification_subscriptions" 

2371 

2372 id = Column(BigInteger, primary_key=True) 

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

2374 

2375 # which user this is connected to 

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

2377 

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

2379 # the endpoint 

2380 endpoint = Column(String, nullable=False) 

2381 # the "auth" key 

2382 auth_key = Column(Binary, nullable=False) 

2383 # the "p256dh" key 

2384 p256dh_key = Column(Binary, nullable=False) 

2385 

2386 full_subscription_info = Column(String, nullable=False) 

2387 

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

2389 user_agent = Column(String, nullable=True) 

2390 

2391 # when it was disabled 

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

2393 

2394 user = relationship("User") 

2395 

2396 

2397class PushNotificationDeliveryAttempt(Base): 

2398 __tablename__ = "push_notification_delivery_attempt" 

2399 

2400 id = Column(BigInteger, primary_key=True) 

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

2402 

2403 push_notification_subscription_id = Column( 

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

2405 ) 

2406 

2407 success = Column(Boolean, nullable=False) 

2408 # the HTTP status code, 201 is success 

2409 status_code = Column(Integer, nullable=False) 

2410 

2411 # can be null if it was a success 

2412 response = Column(String, nullable=True) 

2413 

2414 push_notification_subscription = relationship("PushNotificationSubscription") 

2415 

2416 

2417class Language(Base): 

2418 """ 

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

2420 """ 

2421 

2422 __tablename__ = "languages" 

2423 

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

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

2426 

2427 # the english name 

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

2429 

2430 

2431class Region(Base): 

2432 """ 

2433 Table of regions 

2434 """ 

2435 

2436 __tablename__ = "regions" 

2437 

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

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

2440 

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

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

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

2444 

2445 

2446class UserBlock(Base): 

2447 """ 

2448 Table of blocked users 

2449 """ 

2450 

2451 __tablename__ = "user_blocks" 

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

2453 

2454 id = Column(BigInteger, primary_key=True) 

2455 

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

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

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

2459 

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

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

2462 

2463 

2464class APICall(Base): 

2465 """ 

2466 API call logs 

2467 """ 

2468 

2469 __tablename__ = "api_calls" 

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

2471 

2472 id = Column(BigInteger, primary_key=True) 

2473 

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

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

2476 

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

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

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

2480 

2481 # approximate time of the call 

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

2483 

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

2485 method = Column(String, nullable=False) 

2486 

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

2488 status_code = Column(String, nullable=True) 

2489 

2490 # handler duration (excluding serialization, etc) 

2491 duration = Column(Float, nullable=False) 

2492 

2493 # user_id of caller, None means not logged in 

2494 user_id = Column(BigInteger, nullable=True) 

2495 

2496 # sanitized request bytes 

2497 request = Column(Binary, nullable=True) 

2498 

2499 # sanitized response bytes 

2500 response = Column(Binary, nullable=True) 

2501 

2502 # whether response bytes have been truncated 

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

2504 

2505 # the exception traceback, if any 

2506 traceback = Column(String, nullable=True) 

2507 

2508 # human readable perf report 

2509 perf_report = Column(String, nullable=True) 

2510 

2511 # details of the browser, if available 

2512 ip_address = Column(String, nullable=True) 

2513 user_agent = Column(String, nullable=True) 

2514 

2515 

2516class AccountDeletionReason(Base): 

2517 __tablename__ = "account_deletion_reason" 

2518 

2519 id = Column(BigInteger, primary_key=True) 

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

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

2522 reason = Column(String, nullable=True) 

2523 

2524 user = relationship("User")