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
« prev ^ index » next coverage.py v7.5.0, created at 2024-11-21 04:21 +0000
1import enum
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
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
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)
56Base = declarative_base(metadata=meta)
59class HostingStatus(enum.Enum):
60 can_host = enum.auto()
61 maybe = enum.auto()
62 cant_host = enum.auto()
65class MeetupStatus(enum.Enum):
66 wants_to_meetup = enum.auto()
67 open_to_meetup = enum.auto()
68 does_not_want_to_meetup = enum.auto()
71class SmokingLocation(enum.Enum):
72 yes = enum.auto()
73 window = enum.auto()
74 outside = enum.auto()
75 no = enum.auto()
78class SleepingArrangement(enum.Enum):
79 private = enum.auto()
80 common = enum.auto()
81 shared_room = enum.auto()
82 shared_space = enum.auto()
85class ParkingDetails(enum.Enum):
86 free_onsite = enum.auto()
87 free_offsite = enum.auto()
88 paid_onsite = enum.auto()
89 paid_offsite = enum.auto()
92class TimezoneArea(Base):
93 __tablename__ = "timezone_areas"
94 id = Column(BigInteger, primary_key=True)
96 tzid = Column(String)
97 geom = Column(Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=False)
99 __table_args__ = (
100 Index(
101 "ix_timezone_areas_geom_tzid",
102 geom,
103 tzid,
104 postgresql_using="gist",
105 ),
106 )
109class User(Base):
110 """
111 Basic user and profile details
112 """
114 __tablename__ = "users"
116 id = Column(BigInteger, primary_key=True)
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"))
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)
136 regions_visited = relationship("Region", secondary="regions_visited", order_by="Region.name")
137 regions_lived = relationship("Region", secondary="regions_lived", order_by="Region.name")
139 timezone = column_property(
140 sa_select(TimezoneArea.tzid).where(func.ST_Contains(TimezoneArea.geom, geom)).limit(1).scalar_subquery(),
141 deferred=True,
142 )
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())
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"))
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
158 # name as on official docs for verification, etc. not needed until verification
159 full_name = Column(String, nullable=True)
161 avatar_key = Column(ForeignKey("uploads.key"), nullable=True)
163 hosting_status = Column(Enum(HostingStatus), nullable=False)
164 meetup_status = Column(Enum(MeetupStatus), nullable=False, server_default="open_to_meetup")
166 # community standing score
167 community_standing = Column(Float, nullable=True)
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
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"))
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)
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
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)
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")
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)
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")
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)"))
232 # for changing their email
233 new_email = Column(String, nullable=True)
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)
239 recommendation_score = Column(Float, nullable=False, server_default="0")
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 # '-----------------' '-------------------' '-----------------------'
264 # randomly generated Luhn 6-digit string
265 phone_verification_token = Column(String(6), nullable=True, server_default=text("NULL"))
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"))
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)
278 has_passport_sex_gender_exception = Column(Boolean, nullable=False, server_default=text("false"))
280 # checking for phone verification
281 has_donated = Column(Boolean, nullable=False, server_default=text("false"))
283 # whether this user has all emails turned off
284 do_not_email = Column(Boolean, nullable=False, server_default=text("false"))
286 avatar = relationship("Upload", foreign_keys="User.avatar_key")
288 admin_note = Column(String, nullable=False, server_default=text("''"))
290 age = column_property(func.date_part("year", func.age(birthdate)))
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 )
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
346 @has_completed_profile.expression
347 def has_completed_profile(cls):
348 return (cls.avatar_key != None) & (func.character_length(cls.about_me) >= 150)
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 )
359 @hybrid_property
360 def is_missing_location(self):
361 return (self.geom == None) | (self.geom_radius == None)
363 @hybrid_property
364 def is_visible(self):
365 return ~(self.is_banned | self.is_deleted)
367 @property
368 def coordinates(self):
369 return get_coordinates(self.geom)
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)
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)
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 )
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 )
398 @hybrid_property
399 def phone_code_expired(self):
400 return now() - self.phone_verification_sent > SMS_CODE_LIFETIME
402 def __repr__(self):
403 return f"User(id={self.id}, email={self.email}, username={self.username})"
406class UserBadge(Base):
407 """
408 A badge on a user's profile
409 """
411 __tablename__ = "user_badges"
412 __table_args__ = (UniqueConstraint("user_id", "badge_id"),)
414 id = Column(BigInteger, primary_key=True)
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)
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())
423 user = relationship("User", backref="badges")
426class StrongVerificationAttemptStatus(enum.Enum):
427 ## full data states
428 # completed, this now provides verification for a user
429 succeeded = enum.auto()
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()
441 ## minimal data states
442 # the data, except minimal deduplication data, was deleted
443 deleted = enum.auto()
446class PassportSex(enum.Enum):
447 """
448 We don't care about sex, we use gender on the platform. But passports apparently do.
449 """
451 male = enum.auto()
452 female = enum.auto()
453 unspecified = enum.auto()
456class StrongVerificationAttempt(Base):
457 """
458 An attempt to perform strong verification
459 """
461 __tablename__ = "strong_verification_attempts"
463 # our verification id
464 id = Column(BigInteger, primary_key=True)
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)
469 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
470 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
472 status = Column(
473 Enum(StrongVerificationAttemptStatus),
474 nullable=False,
475 default=StrongVerificationAttemptStatus.in_progress_waiting_on_user_to_open_app,
476 )
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)
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)
492 iris_token = Column(String, nullable=False, unique=True)
493 iris_session_id = Column(BigInteger, nullable=False, unique=True)
495 passport_expiry_datetime = column_property(date_in_timezone(passport_expiry_date, "Etc/UTC"))
497 user = relationship("User")
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())
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 )
512 @hybrid_property
513 def is_visible(self):
514 return self.status != StrongVerificationAttemptStatus.deleted
516 @hybrid_method
517 def matches_birthdate(self, user):
518 return self.is_valid & (self.passport_date_of_birth == user.birthdate)
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 )
529 @hybrid_method
530 def has_strong_verification(self, user):
531 return self.is_valid & self.matches_birthdate(user) & self.matches_gender(user)
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 )
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.
593 The user has to read and "acknowledge" the note before continuing onto the platform, and being un-jailed.
594 """
596 __tablename__ = "mod_notes"
597 id = Column(BigInteger, primary_key=True)
599 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
601 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
602 acknowledged = Column(DateTime(timezone=True), nullable=True)
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)
609 note_content = Column(String, nullable=False) # CommonMark without images
611 user = relationship("User", backref=backref("mod_notes", lazy="dynamic"), foreign_keys="ModNote.user_id")
613 def __repr__(self):
614 return f"ModeNote(id={self.id}, user={self.user}, created={self.created}, ack'd={self.acknowledged})"
616 @hybrid_property
617 def is_pending(self):
618 return self.acknowledged == None
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 )
630class StrongVerificationCallbackEvent(Base):
631 __tablename__ = "strong_verification_callback_events"
633 id = Column(BigInteger, primary_key=True)
634 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
636 verification_attempt_id = Column(ForeignKey("strong_verification_attempts.id"), nullable=False, index=True)
638 iris_status = Column(String, nullable=False)
641class DonationType(enum.Enum):
642 one_time = enum.auto()
643 recurring = enum.auto()
646class DonationInitiation(Base):
647 """
648 Whenever someone initiaties a donation through the platform
649 """
651 __tablename__ = "donation_initiations"
652 id = Column(BigInteger, primary_key=True)
654 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
655 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
657 amount = Column(Integer, nullable=False)
658 stripe_checkout_session_id = Column(String, nullable=False)
660 donation_type = Column(Enum(DonationType), nullable=False)
662 user = relationship("User", backref="donation_initiations")
665class Invoice(Base):
666 """
667 Successful donations, both one off and recurring
669 Triggered by `payment_intent.succeeded` webhook
670 """
672 __tablename__ = "invoices"
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)
678 amount = Column(Float, nullable=False)
680 stripe_payment_intent_id = Column(String, nullable=False, unique=True)
681 stripe_receipt_url = Column(String, nullable=False)
683 user = relationship("User", backref="invoices")
686class LanguageFluency(enum.Enum):
687 # note that the numbering is important here, these are ordinal
688 beginner = 1
689 conversational = 2
690 fluent = 3
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 )
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)
705 user = relationship("User", backref="language_abilities")
706 language = relationship("Language")
709class RegionVisited(Base):
710 __tablename__ = "regions_visited"
711 __table_args__ = (UniqueConstraint("user_id", "region_code"),)
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)
718class RegionLived(Base):
719 __tablename__ = "regions_lived"
720 __table_args__ = (UniqueConstraint("user_id", "region_code"),)
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)
727class FriendStatus(enum.Enum):
728 pending = enum.auto()
729 accepted = enum.auto()
730 rejected = enum.auto()
731 cancelled = enum.auto()
734class FriendRelationship(Base):
735 """
736 Friendship relations between users
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 """
742 __tablename__ = "friend_relationships"
744 id = Column(BigInteger, primary_key=True)
746 from_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
747 to_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
749 status = Column(Enum(FriendStatus), nullable=False, default=FriendStatus.pending)
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)
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")
759class ContributeOption(enum.Enum):
760 yes = enum.auto()
761 maybe = enum.auto()
762 no = enum.auto()
765class ContributorForm(Base):
766 """
767 Someone filled in the contributor form
768 """
770 __tablename__ = "contributor_forms"
772 id = Column(BigInteger, primary_key=True)
774 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
775 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
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)
784 user = relationship("User", backref="contributor_forms")
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 )
800 @property
801 def should_notify(self):
802 """
803 If this evaluates to true, we send an email to the recruitment team.
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"}))
810class SignupFlow(Base):
811 """
812 Signup flows/incomplete users
814 Coinciding fields have the same meaning as in User
815 """
817 __tablename__ = "signup_flows"
819 id = Column(BigInteger, primary_key=True)
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)
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
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)
846 accepted_tos = Column(Integer, nullable=True)
847 accepted_community_guidelines = Column(Integer, nullable=False, server_default="0")
849 opt_out_of_newsletter = Column(Boolean, nullable=True)
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)
860 @hybrid_property
861 def token_is_valid(self):
862 return (self.email_token != None) & (self.email_token_expiry >= now())
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 )
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 )
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 """
893 __tablename__ = "login_tokens"
894 token = Column(String, primary_key=True)
896 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
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)
902 user = relationship("User", backref="login_tokens")
904 @hybrid_property
905 def is_valid(self):
906 return (self.created <= now()) & (self.expiry >= now())
908 def __repr__(self):
909 return f"LoginToken(token={self.token}, user={self.user}, created={self.created}, expiry={self.expiry})"
912class PasswordResetToken(Base):
913 __tablename__ = "password_reset_tokens"
914 token = Column(String, primary_key=True)
916 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
918 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
919 expiry = Column(DateTime(timezone=True), nullable=False)
921 user = relationship("User", backref="password_reset_tokens")
923 @hybrid_property
924 def is_valid(self):
925 return (self.created <= now()) & (self.expiry >= now())
927 def __repr__(self):
928 return f"PasswordResetToken(token={self.token}, user={self.user}, created={self.created}, expiry={self.expiry})"
931class AccountDeletionToken(Base):
932 __tablename__ = "account_deletion_tokens"
934 token = Column(String, primary_key=True)
936 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
938 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
939 expiry = Column(DateTime(timezone=True), nullable=False)
941 user = relationship("User", backref="account_deletion_tokens")
943 @hybrid_property
944 def is_valid(self):
945 return (self.created <= now()) & (self.expiry >= now())
947 def __repr__(self):
948 return f"AccountDeletionToken(token={self.token}, user_id={self.user_id}, created={self.created}, expiry={self.expiry})"
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
955 Used for user "last active" as well as admin stuff
956 """
958 __tablename__ = "user_activity"
960 id = Column(BigInteger, primary_key=True)
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)
966 # details of the browser, if available
967 ip_address = Column(INET, nullable=True)
968 user_agent = Column(String, nullable=True)
970 # count of api calls made with this ip, user_agent, and period
971 api_calls = Column(Integer, nullable=False, default=0)
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 )
986class UserSession(Base):
987 """
988 API keys/session cookies for the app
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.
996 Long-lived tokens are valid from `created` until `expiry`.
998 Short-lived tokens expire after 168 hours (7 days) if they are not used.
999 """
1001 __tablename__ = "sessions"
1002 token = Column(String, primary_key=True)
1004 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
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"))
1013 # whether it's a long-lived or short-lived session
1014 long_lived = Column(Boolean, nullable=False)
1016 # the time at which the session was created
1017 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
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'"))
1022 # the time at which the token was invalidated, allows users to delete sessions
1023 deleted = Column(DateTime(timezone=True), nullable=True, default=None)
1025 # the last time this session was used
1026 last_seen = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
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)
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)
1036 user = relationship("User", backref="sessions")
1038 @hybrid_property
1039 def is_valid(self):
1040 """
1041 It must have been created and not be expired or deleted.
1043 Also, if it's a short lived token, it must have been used in the last 168 hours.
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 )
1055class Conversation(Base):
1056 """
1057 Conversation brings together the different types of message/conversation types
1058 """
1060 __tablename__ = "conversations"
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())
1066 def __repr__(self):
1067 return f"Conversation(id={self.id}, created={self.created})"
1070class GroupChat(Base):
1071 """
1072 Group chat
1073 """
1075 __tablename__ = "group_chats"
1077 conversation_id = Column("id", ForeignKey("conversations.id"), nullable=False, primary_key=True)
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)
1084 conversation = relationship("Conversation", backref="group_chat")
1085 creator = relationship("User", backref="created_group_chats")
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})"
1091class GroupChatRole(enum.Enum):
1092 admin = enum.auto()
1093 participant = enum.auto()
1096class GroupChatSubscription(Base):
1097 """
1098 The recipient of a thread and information about when they joined/left/etc.
1099 """
1101 __tablename__ = "group_chat_subscriptions"
1102 id = Column(BigInteger, primary_key=True)
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)
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)
1112 role = Column(Enum(GroupChatRole), nullable=False)
1114 last_seen_message_id = Column(BigInteger, nullable=False, default=0)
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())
1119 user = relationship("User", backref="group_chat_subscriptions")
1120 group_chat = relationship("GroupChat", backref=backref("subscriptions", lazy="dynamic"))
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)
1136 @hybrid_property
1137 def is_muted(self):
1138 return self.muted_until > func.now()
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})"
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
1160class HostRequestStatus(enum.Enum):
1161 pending = enum.auto()
1162 accepted = enum.auto()
1163 rejected = enum.auto()
1164 confirmed = enum.auto()
1165 cancelled = enum.auto()
1168class Message(Base):
1169 """
1170 A message.
1172 If message_type = text, then the message is a normal text message, otherwise, it's a special control message.
1173 """
1175 __tablename__ = "messages"
1177 id = Column(BigInteger, primary_key=True)
1179 # which conversation the message belongs in
1180 conversation_id = Column(ForeignKey("conversations.id"), nullable=False, index=True)
1182 # the user that sent the message/command
1183 author_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1185 # the message type, "text" is a text message, otherwise a "control message"
1186 message_type = Column(Enum(MessageType), nullable=False)
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)
1191 # time sent, timezone should always be UTC
1192 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
1194 # the plain-text message text if not control
1195 text = Column(String, nullable=True)
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)
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")
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
1211 def __repr__(self):
1212 return f"Message(id={self.id}, time={self.time}, text={self.text}, author={self.author}, conversation={self.conversation})"
1215class ContentReport(Base):
1216 """
1217 A piece of content reported to admins
1218 """
1220 __tablename__ = "content_reports"
1222 id = Column(BigInteger, primary_key=True)
1224 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
1226 # the user who reported or flagged the content
1227 reporting_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1229 # reason, e.g. spam, inappropriate, etc
1230 reason = Column(String, nullable=False)
1231 # a short description
1232 description = Column(String, nullable=False)
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)
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)
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")
1249class Email(Base):
1250 """
1251 Table of all dispatched emails for debugging purposes, etc.
1252 """
1254 __tablename__ = "emails"
1256 id = Column(String, primary_key=True)
1258 # timezone should always be UTC
1259 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
1261 sender_name = Column(String, nullable=False)
1262 sender_email = Column(String, nullable=False)
1264 recipient = Column(String, nullable=False)
1265 subject = Column(String, nullable=False)
1267 plain = Column(String, nullable=False)
1268 html = Column(String, nullable=False)
1270 list_unsubscribe_header = Column(String, nullable=True)
1271 source_data = Column(String, nullable=True)
1274class SMS(Base):
1275 """
1276 Table of all sent SMSs for debugging purposes, etc.
1277 """
1279 __tablename__ = "smss"
1281 id = Column(BigInteger, primary_key=True)
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)
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)
1294class HostRequest(Base):
1295 """
1296 A request to stay with a host
1297 """
1299 __tablename__ = "host_requests"
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)
1305 # TODO: proper timezone handling
1306 timezone = "Etc/UTC"
1308 # dates in the timezone above
1309 from_date = Column(Date, nullable=False)
1310 to_date = Column(Date, nullable=False)
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'"))
1319 status = Column(Enum(HostRequestStatus), nullable=False)
1321 host_last_seen_message_id = Column(BigInteger, nullable=False, default=0)
1322 surfer_last_seen_message_id = Column(BigInteger, nullable=False, default=0)
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"))
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")
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 )
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 )
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}...)"
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
1358class Reference(Base):
1359 """
1360 Reference from one user to another
1361 """
1363 __tablename__ = "references"
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())
1369 from_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1370 to_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1372 reference_type = Column(Enum(ReferenceType), nullable=False)
1374 host_request_id = Column(ForeignKey("host_requests.id"), nullable=True)
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
1380 rating = Column(Float, nullable=False)
1381 was_appropriate = Column(Boolean, nullable=False)
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")
1386 host_request = relationship("HostRequest", backref="references")
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 )
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
1427class InitiatedUpload(Base):
1428 """
1429 Started downloads, not necessarily complete yet.
1430 """
1432 __tablename__ = "initiated_uploads"
1434 key = Column(String, primary_key=True)
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)
1440 initiator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1442 initiator_user = relationship("User")
1444 @hybrid_property
1445 def is_valid(self):
1446 return (self.created <= func.now()) & (self.expiry >= func.now())
1449class Upload(Base):
1450 """
1451 Completed uploads.
1452 """
1454 __tablename__ = "uploads"
1455 key = Column(String, primary_key=True)
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)
1461 # photo credit, etc
1462 credit = Column(String, nullable=True)
1464 creator_user = relationship("User", backref="uploads", foreign_keys="Upload.creator_user_id")
1466 def _url(self, size):
1467 return urls.media_url(filename=self.filename, size=size)
1469 @property
1470 def thumbnail_url(self):
1471 return self._url("thumbnail")
1473 @property
1474 def full_url(self):
1475 return self._url("full")
1478communities_seq = Sequence("communities_seq")
1481class Node(Base):
1482 """
1483 Node, i.e. geographical subdivision of the world
1485 Administered by the official cluster
1486 """
1488 __tablename__ = "nodes"
1490 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value())
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())
1497 parent_node = relationship("Node", backref="child_nodes", remote_side="Node.id")
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 )
1506 contained_user_ids = association_proxy("contained_users", "id")
1509class Cluster(Base):
1510 """
1511 Cluster, administered grouping of content
1512 """
1514 __tablename__ = "clusters"
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())
1523 is_official_cluster = Column(Boolean, nullable=False, default=False)
1525 slug = column_property(func.slugify(name))
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 )
1535 parent_node = relationship(
1536 "Node", backref="child_clusters", remote_side="Node.id", foreign_keys="Cluster.parent_node_id"
1537 )
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 )
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 )
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 )
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 )
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
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 )
1594class NodeClusterAssociation(Base):
1595 """
1596 NodeClusterAssociation, grouping of nodes
1597 """
1599 __tablename__ = "node_cluster_associations"
1600 __table_args__ = (UniqueConstraint("node_id", "cluster_id"),)
1602 id = Column(BigInteger, primary_key=True)
1604 node_id = Column(ForeignKey("nodes.id"), nullable=False, index=True)
1605 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True)
1607 node = relationship("Node", backref="node_cluster_associations")
1608 cluster = relationship("Cluster", backref="node_cluster_associations")
1611class ClusterRole(enum.Enum):
1612 member = enum.auto()
1613 admin = enum.auto()
1616class ClusterSubscription(Base):
1617 """
1618 ClusterSubscription of a user
1619 """
1621 __tablename__ = "cluster_subscriptions"
1622 __table_args__ = (UniqueConstraint("user_id", "cluster_id"),)
1624 id = Column(BigInteger, primary_key=True)
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)
1630 user = relationship("User", backref="cluster_subscriptions")
1631 cluster = relationship("Cluster", backref="cluster_subscriptions")
1634class ClusterPageAssociation(Base):
1635 """
1636 pages related to clusters
1637 """
1639 __tablename__ = "cluster_page_associations"
1640 __table_args__ = (UniqueConstraint("page_id", "cluster_id"),)
1642 id = Column(BigInteger, primary_key=True)
1644 page_id = Column(ForeignKey("pages.id"), nullable=False, index=True)
1645 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True)
1647 page = relationship("Page", backref="cluster_page_associations")
1648 cluster = relationship("Cluster", backref="cluster_page_associations")
1651class PageType(enum.Enum):
1652 main_page = enum.auto()
1653 place = enum.auto()
1654 guide = enum.auto()
1657class Page(Base):
1658 """
1659 similar to a wiki page about a community, POI or guide
1660 """
1662 __tablename__ = "pages"
1664 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value())
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)
1672 thread_id = Column(ForeignKey("threads.id"), nullable=False, unique=True)
1674 parent_node = relationship("Node", backref="child_pages", remote_side="Node.id", foreign_keys="Page.parent_node_id")
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 )
1683 editors = relationship("User", secondary="page_versions", viewonly=True)
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 )
1706 def __repr__(self):
1707 return f"Page({self.id=})"
1710class PageVersion(Base):
1711 """
1712 version of page content
1713 """
1715 __tablename__ = "page_versions"
1717 id = Column(BigInteger, primary_key=True)
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())
1729 slug = column_property(func.slugify(title))
1731 page = relationship("Page", backref="versions", order_by="PageVersion.id")
1732 editor_user = relationship("User", backref="edited_pages")
1733 photo = relationship("Upload")
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 )
1743 @property
1744 def coordinates(self):
1745 # returns (lat, lng) or None
1746 return get_coordinates(self.geom)
1748 def __repr__(self):
1749 return f"PageVersion({self.id=}, {self.page_id=})"
1752class ClusterEventAssociation(Base):
1753 """
1754 events related to clusters
1755 """
1757 __tablename__ = "cluster_event_associations"
1758 __table_args__ = (UniqueConstraint("event_id", "cluster_id"),)
1760 id = Column(BigInteger, primary_key=True)
1762 event_id = Column(ForeignKey("events.id"), nullable=False, index=True)
1763 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True)
1765 event = relationship("Event", backref="cluster_event_associations")
1766 cluster = relationship("Cluster", backref="cluster_event_associations")
1769class Event(Base):
1770 """
1771 An event is compose of two parts:
1773 * An event template (Event)
1774 * An occurrence (EventOccurrence)
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 """
1780 __tablename__ = "events"
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)
1785 title = Column(String, nullable=False)
1787 slug = column_property(func.slugify(title))
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)
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 )
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 )
1823class EventOccurrence(Base):
1824 __tablename__ = "event_occurrences"
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)
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)
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"))
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)
1844 timezone = "Etc/UTC"
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)
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())
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 )
1863 photo = relationship("Upload")
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 )
1882 @property
1883 def coordinates(self):
1884 # returns (lat, lng) or None
1885 return get_coordinates(self.geom)
1887 @hybrid_property
1888 def start_time(self):
1889 return self.during.lower
1891 @start_time.expression
1892 def start_time(cls):
1893 return func.lower(cls.during)
1895 @hybrid_property
1896 def end_time(self):
1897 return self.during.upper
1899 @end_time.expression
1900 def end_time(cls):
1901 return func.upper(cls.during)
1904class EventSubscription(Base):
1905 """
1906 Users' subscriptions to events
1907 """
1909 __tablename__ = "event_subscriptions"
1910 __table_args__ = (UniqueConstraint("event_id", "user_id"),)
1912 id = Column(BigInteger, primary_key=True)
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())
1918 user = relationship("User")
1919 event = relationship("Event")
1922class EventOrganizer(Base):
1923 """
1924 Organizers for events
1925 """
1927 __tablename__ = "event_organizers"
1928 __table_args__ = (UniqueConstraint("event_id", "user_id"),)
1930 id = Column(BigInteger, primary_key=True)
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())
1936 user = relationship("User")
1937 event = relationship("Event")
1940class AttendeeStatus(enum.Enum):
1941 going = enum.auto()
1942 maybe = enum.auto()
1945class EventOccurrenceAttendee(Base):
1946 """
1947 Attendees for events
1948 """
1950 __tablename__ = "event_occurrence_attendees"
1951 __table_args__ = (UniqueConstraint("occurrence_id", "user_id"),)
1953 id = Column(BigInteger, primary_key=True)
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)
1960 user = relationship("User")
1961 occurrence = relationship("EventOccurrence", backref=backref("attendances", lazy="dynamic"))
1964class EventCommunityInviteRequest(Base):
1965 """
1966 Requests to send out invitation notifications/emails to the community for a given event occurrence
1967 """
1969 __tablename__ = "event_community_invite_requests"
1971 id = Column(BigInteger, primary_key=True)
1973 occurrence_id = Column(ForeignKey("event_occurrences.id"), nullable=False, index=True)
1974 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
1976 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
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)
1982 occurrence = relationship("EventOccurrence", backref=backref("community_invite_requests", lazy="dynamic"))
1983 user = relationship("User", foreign_keys="EventCommunityInviteRequest.user_id")
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 )
2004class ClusterDiscussionAssociation(Base):
2005 """
2006 discussions related to clusters
2007 """
2009 __tablename__ = "cluster_discussion_associations"
2010 __table_args__ = (UniqueConstraint("discussion_id", "cluster_id"),)
2012 id = Column(BigInteger, primary_key=True)
2014 discussion_id = Column(ForeignKey("discussions.id"), nullable=False, index=True)
2015 cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True)
2017 discussion = relationship("Discussion", backref="cluster_discussion_associations")
2018 cluster = relationship("Cluster", backref="cluster_discussion_associations")
2021class Discussion(Base):
2022 """
2023 forum board
2024 """
2026 __tablename__ = "discussions"
2028 id = Column(BigInteger, communities_seq, primary_key=True, server_default=communities_seq.next_value())
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())
2035 creator_user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
2036 owner_cluster_id = Column(ForeignKey("clusters.id"), nullable=False, index=True)
2038 slug = column_property(func.slugify(title))
2040 thread = relationship("Thread", backref="discussion", uselist=False)
2042 subscribers = relationship("User", backref="discussions", secondary="discussion_subscriptions", viewonly=True)
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)
2048class DiscussionSubscription(Base):
2049 """
2050 users subscriptions to discussions
2051 """
2053 __tablename__ = "discussion_subscriptions"
2054 __table_args__ = (UniqueConstraint("discussion_id", "user_id"),)
2056 id = Column(BigInteger, primary_key=True)
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)
2063 user = relationship("User", backref="discussion_subscriptions")
2064 discussion = relationship("Discussion", backref="discussion_subscriptions")
2067class Thread(Base):
2068 """
2069 Thread
2070 """
2072 __tablename__ = "threads"
2074 id = Column(BigInteger, primary_key=True)
2076 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
2077 deleted = Column(DateTime(timezone=True), nullable=True)
2080class Comment(Base):
2081 """
2082 Comment
2083 """
2085 __tablename__ = "comments"
2087 id = Column(BigInteger, primary_key=True)
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)
2095 thread = relationship("Thread", backref="comments")
2098class Reply(Base):
2099 """
2100 Reply
2101 """
2103 __tablename__ = "replies"
2105 id = Column(BigInteger, primary_key=True)
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)
2113 comment = relationship("Comment", backref="replies")
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()
2127class BackgroundJob(Base):
2128 """
2129 This table implements a queue of background jobs.
2130 """
2132 __tablename__ = "background_jobs"
2134 id = Column(BigInteger, primary_key=True)
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)
2140 # time queued
2141 queued = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
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())
2146 # used to count number of retries for failed jobs
2147 try_count = Column(Integer, nullable=False, default=0)
2149 max_tries = Column(Integer, nullable=False, default=5)
2151 # protobuf encoded job payload
2152 payload = Column(Binary, nullable=False)
2154 # if the job failed, we write that info here
2155 failure_info = Column(String, nullable=True)
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 )
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 )
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})"
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()
2189dt = NotificationDeliveryType
2190nd = notification_data_pb2
2192dt_sec = [dt.email, dt.push]
2193dt_all = [dt.email, dt.push, dt.digest]
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
2203 self.data_type = data_type
2205 def unpack(self):
2206 return self.topic, self.action
2208 @property
2209 def display(self):
2210 return f"{self.topic}:{self.action}"
2212 def __str__(self):
2213 return self.display
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)
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)
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)
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)
2240 badge__add = ("badge:add", [dt.push, dt.digest], True, nd.BadgeAdd)
2241 badge__remove = ("badge:remove", [dt.push, dt.digest], True, nd.BadgeRemove)
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)
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)
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)
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)
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)
2279 donation__received = ("donation:received", dt_sec, True, nd.DonationReceived)
2281 onboarding__reminder = ("onboarding:reminder", dt_sec, True, empty_pb2.Empty)
2283 modnote__create = ("modnote:create", dt_sec, False, empty_pb2.Empty)
2286class NotificationPreference(Base):
2287 __tablename__ = "notification_preferences"
2289 id = Column(BigInteger, primary_key=True)
2290 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
2292 topic_action = Column(Enum(NotificationTopicAction), nullable=False)
2293 delivery_type = Column(Enum(NotificationDeliveryType), nullable=False)
2294 deliver = Column(Boolean, nullable=False)
2296 user = relationship("User", foreign_keys="NotificationPreference.user_id")
2298 __table_args__ = (UniqueConstraint("user_id", "topic_action", "delivery_type"),)
2301class Notification(Base):
2302 """
2303 Table for accumulating notifications until it is time to send email digest
2304 """
2306 __tablename__ = "notifications"
2308 id = Column(BigInteger, primary_key=True)
2309 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
2311 # recipient user id
2312 user_id = Column(ForeignKey("users.id"), nullable=False)
2314 topic_action = Column(Enum(NotificationTopicAction), nullable=False)
2315 key = Column(String, nullable=False)
2317 data = Column(Binary, nullable=False)
2319 user = relationship("User", foreign_keys="Notification.user_id")
2321 __table_args__ = (
2322 # used in looking up which notifications need delivery
2323 Index(
2324 "ix_notifications_created",
2325 created,
2326 ),
2327 )
2329 @property
2330 def topic(self):
2331 return self.topic_action.topic
2333 @property
2334 def action(self):
2335 return self.topic_action.action
2338class NotificationDelivery(Base):
2339 __tablename__ = "notification_deliveries"
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")
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 )
2369class PushNotificationSubscription(Base):
2370 __tablename__ = "push_notification_subscriptions"
2372 id = Column(BigInteger, primary_key=True)
2373 created = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
2375 # which user this is connected to
2376 user_id = Column(ForeignKey("users.id"), nullable=False, index=True)
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)
2386 full_subscription_info = Column(String, nullable=False)
2388 # the browse user-agent, so we can tell the user what browser notifications are going to
2389 user_agent = Column(String, nullable=True)
2391 # when it was disabled
2392 disabled_at = Column(DateTime(timezone=True), nullable=False, server_default=DATETIME_INFINITY.isoformat())
2394 user = relationship("User")
2397class PushNotificationDeliveryAttempt(Base):
2398 __tablename__ = "push_notification_delivery_attempt"
2400 id = Column(BigInteger, primary_key=True)
2401 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
2403 push_notification_subscription_id = Column(
2404 ForeignKey("push_notification_subscriptions.id"), nullable=False, index=True
2405 )
2407 success = Column(Boolean, nullable=False)
2408 # the HTTP status code, 201 is success
2409 status_code = Column(Integer, nullable=False)
2411 # can be null if it was a success
2412 response = Column(String, nullable=True)
2414 push_notification_subscription = relationship("PushNotificationSubscription")
2417class Language(Base):
2418 """
2419 Table of allowed languages (a subset of ISO639-3)
2420 """
2422 __tablename__ = "languages"
2424 # ISO639-3 language code, in lowercase, e.g. fin, eng
2425 code = Column(String(3), primary_key=True)
2427 # the english name
2428 name = Column(String, nullable=False, unique=True)
2431class Region(Base):
2432 """
2433 Table of regions
2434 """
2436 __tablename__ = "regions"
2438 # iso 3166-1 alpha3 code in uppercase, e.g. FIN, USA
2439 code = Column(String(3), primary_key=True)
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)
2446class UserBlock(Base):
2447 """
2448 Table of blocked users
2449 """
2451 __tablename__ = "user_blocks"
2452 __table_args__ = (UniqueConstraint("blocking_user_id", "blocked_user_id"),)
2454 id = Column(BigInteger, primary_key=True)
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())
2460 blocking_user = relationship("User", foreign_keys="UserBlock.blocking_user_id")
2461 blocked_user = relationship("User", foreign_keys="UserBlock.blocked_user_id")
2464class APICall(Base):
2465 """
2466 API call logs
2467 """
2469 __tablename__ = "api_calls"
2470 __table_args__ = {"schema": "logging"}
2472 id = Column(BigInteger, primary_key=True)
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"))
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"])
2481 # approximate time of the call
2482 time = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
2484 # the method call name, e.g. "/org.couchers.api.core.API/ListFriends"
2485 method = Column(String, nullable=False)
2487 # gRPC status code name, e.g. FAILED_PRECONDITION, None if success
2488 status_code = Column(String, nullable=True)
2490 # handler duration (excluding serialization, etc)
2491 duration = Column(Float, nullable=False)
2493 # user_id of caller, None means not logged in
2494 user_id = Column(BigInteger, nullable=True)
2496 # sanitized request bytes
2497 request = Column(Binary, nullable=True)
2499 # sanitized response bytes
2500 response = Column(Binary, nullable=True)
2502 # whether response bytes have been truncated
2503 response_truncated = Column(Boolean, nullable=False, server_default=text("false"))
2505 # the exception traceback, if any
2506 traceback = Column(String, nullable=True)
2508 # human readable perf report
2509 perf_report = Column(String, nullable=True)
2511 # details of the browser, if available
2512 ip_address = Column(String, nullable=True)
2513 user_agent = Column(String, nullable=True)
2516class AccountDeletionReason(Base):
2517 __tablename__ = "account_deletion_reason"
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)
2524 user = relationship("User")