File size: 16,871 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Database models for authentication, user management, and social features
"""
from datetime import datetime, timezone
from sqlalchemy import CheckConstraint, Column, Integer, String, DateTime, Boolean, Text, Float, ForeignKey, Identity, Index, UniqueConstraint, func, text
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class User(Base):
    """User account model"""
    __tablename__ = "user"

    user_id = Column(Integer, primary_key=True, index=True)
    email = Column(String(255), unique=True, index=True, nullable=False)
    username = Column(String(100), unique=True, index=True, nullable=True)
    full_name = Column(String(255), nullable=True)
    # OAuth avatar URLs (esp. Google's lh3.googleusercontent.com) routinely
    # exceed 500 chars — use unbounded Text to avoid StringDataRightTruncation.
    avatar_url = Column(Text, nullable=True)
    
    # OAuth provider info
    oauth_provider = Column(String(50), nullable=True)  # 'huggingface', 'google', 'facebook', 'github'
    oauth_id = Column(String(255), nullable=True)  # Provider-specific user ID
    
    # Authentication
    hashed_password = Column(String(255), nullable=True)  # For email/password (optional)
    is_active = Column(Boolean, default=True)
    is_verified = Column(Boolean, default=False)
    # Operator/admin flag — gates the Admin area (Lighthouse report + batch jobs)
    # surfaced via the profile menu and the /admin route. See migration
    # packages/hosting/scripts/neon/migrations/110_add_user_is_admin.sql
    is_admin = Column(Boolean, nullable=False, default=False, server_default="false")
    
    # Location preferences
    state = Column(String(100), nullable=True)  # US State
    county = Column(String(100), nullable=True)  # County
    city = Column(String(100), nullable=True)  # City
    school_board = Column(String(255), nullable=True)  # School board/district
    
    # Profile completion
    profile_completed = Column(Boolean, default=False)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    last_login = Column(DateTime, nullable=True)
    
    # User preferences (JSON stored as text)
    preferences = Column(Text, nullable=True)
    
    def __repr__(self):
        return f"<User {self.email}>"


class OAuthState(Base):
    """Temporary storage for OAuth state tokens (CSRF protection)"""
    __tablename__ = "contact_oauth_state"
    
    id = Column(Integer, primary_key=True, index=True)
    state_token = Column(String(255), unique=True, index=True, nullable=False)
    provider = Column(String(50), nullable=False)
    redirect_uri = Column(String(500), nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    expires_at = Column(DateTime, nullable=False)
    
    def __repr__(self):
        return f"<OAuthState {self.provider} - {self.state_token[:8]}...>"


class ContactSubmission(Base):
    """One contact-form submission, kept privately for follow-up (#448).

    Grain: one row per accepted ``POST /api/contact/submit``. App-owned like
    ``user``: created by ``Base.metadata.create_all``, not dbt, and never copied by
    the local-to-Neon serving sync (see ``RUNTIME_OWNED`` in
    ``hosting.neon.sync_public_to_neon``). The public GitHub issue carries only
    ``contact_submission_id``; the sender's email lives here and nowhere public.
    No foreign keys. Retention: see
    ``web_docs/docs/development/contact-form-privacy.md``.
    """

    __tablename__ = "contact_submission"

    contact_submission_id = Column(Integer, Identity(), primary_key=True)
    created_at = Column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        server_default=func.now(),
        nullable=False,
    )
    sender_name = Column(Text, nullable=False)
    sender_email = Column(Text, nullable=True)  # nullable so retention can clear it
    subject = Column(Text, nullable=False)
    github_issue_number = Column(Integer, nullable=True)
    github_issue_url = Column(Text, nullable=True)

    def __repr__(self):
        return f"<ContactSubmission {self.contact_submission_id}>"


# ============================================================================
# SOCIAL FEATURES MODELS
# ============================================================================

# NOTE: the integer-keyed `organization` ORM entity was folded into the MDM
# golden record `mdm_organization` (a dbt mart, PK master_org_id — not an ORM
# table). Organizations are now followed by their text master_org_id via
# SocialFollow.target_uid; see api/routes/social.py and migration 093.


# NOTE: the integer-keyed `cause` ORM entity was retired in favour of the dbt tag
# taxonomy (a mart, PK tag_id text -- not an ORM table). Causes/topics are now
# followed by their text tag_id via SocialFollow.target_uid, exactly like
# organizations; see api/routes/social.py and migration 099.


# NOTE: the `Official` ORM entity (table contact_official) was retired by
# migration 052, which dropped the empty table. Officials data lives in per-state
# parquet gold files (data/gold/states/<ST>/contact_official.parquet), read directly
# by api/routes/search.py — never as a queryable table. The follow-an-official
# feature that depended on this model was orphaned (the frontend never called its
# routes) and was removed alongside it; see api/routes/social.py.


# ============================================================================
# FOLLOW RELATIONSHIPS (Many-to-Many)
# ============================================================================

class SocialFollow(Base):
    """A user following another entity (user, organization, or tag).

    Consolidates the former user_follows / contact_official_follows /
    organization_follows / cause_follows tables into one polymorphic table.
    ``follower_id`` is always the acting user; ``target_type`` + the matching
    target key identify what is being followed.

    Two target-key columns coexist because targets are keyed differently:
      - integer-keyed targets ('user') use ``target_id``.
      - text-keyed targets use ``target_uid``: 'organization' =
        mdm_organization.master_org_id, 'tag' = tag.tag_id.
    Exactly one of the two is set per row; uniqueness is enforced by the two
    partial indexes below.
    """
    __tablename__ = "social_follows"
    __table_args__ = (
        Index('uq_social_follow_intid', 'follower_id', 'target_type', 'target_id',
              unique=True, postgresql_where=text('target_uid IS NULL')),
        Index('uq_social_follow_uid', 'follower_id', 'target_type', 'target_uid',
              unique=True, postgresql_where=text('target_uid IS NOT NULL')),
    )

    # Allowed target_type values. target_id keys user; target_uid keys
    # organization (mdm_organization.master_org_id) and tag (tag.tag_id).
    TARGET_TYPES = ("user", "organization", "tag")
    UID_TARGET_TYPES = ("organization", "tag")

    id = Column(Integer, primary_key=True, index=True)
    follower_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), nullable=False, index=True)
    target_type = Column(String, nullable=False, index=True)
    target_id = Column(Integer, nullable=True, index=True)
    target_uid = Column(String, nullable=True, index=True)
    created_at = Column(DateTime, default=datetime.utcnow)

    def __repr__(self):
        key = self.target_uid if self.target_uid is not None else self.target_id
        return f"<SocialFollow user:{self.follower_id} -> {self.target_type}:{key}>"


# ============================================================================
# FEED PERSONALIZATION MODELS
# ============================================================================
# Operational user-preference tables for the feed-setup screen. Like User /
# SocialFollow these are ORM-managed public-schema tables (NOT dbt marts),
# auto-created by Base.metadata.create_all in api/database.py at startup. All
# three cascade-delete with their owning user.


class UserLocation(Base):
    """A location a user follows in their feed, at a chosen `shared_level`.

    `name` is the human label (e.g. "Tuscaloosa, AL"); the resolved geo columns
    are filled in from the place typeahead selection (all nullable). One row per
    location; `is_primary` marks the one synced into User.city/county/state.
    """
    __tablename__ = "user_locations"

    id = Column(Integer, primary_key=True, autoincrement=True, index=True)
    user_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), nullable=False, index=True)
    name = Column(String(255), nullable=False)  # e.g. "Tuscaloosa, AL"
    # One of street | district | city | county | state
    shared_level = Column(String(20), nullable=False, default='city')
    is_primary = Column(Boolean, default=False)

    # Resolved geo (all nullable — filled from the typeahead selection)
    state_code = Column(String(2), nullable=True)
    state = Column(String(100), nullable=True)
    county = Column(String(100), nullable=True)
    place_fips = Column(String(10), nullable=True)
    county_fips = Column(String(10), nullable=True)
    latitude = Column(Float, nullable=True)
    longitude = Column(Float, nullable=True)
    jurisdiction_id = Column(String(100), nullable=True)

    created_at = Column(DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<UserLocation user:{self.user_id} {self.name} ({self.shared_level})>"


class UserLensPref(Base):
    """A topical lens a user has enabled for their feed (composite PK)."""
    __tablename__ = "user_lens_prefs"

    user_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), primary_key=True)
    lens_slug = Column(String(50), primary_key=True)
    created_at = Column(DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<UserLensPref user:{self.user_id} {self.lens_slug}>"


class UserSignalPref(Base):
    """A story signal a user has enabled for their feed (composite PK)."""
    __tablename__ = "user_signal_prefs"

    user_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), primary_key=True)
    signal_slug = Column(String(50), primary_key=True)
    created_at = Column(DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<UserSignalPref user:{self.user_id} {self.signal_slug}>"


# ============================================================================
# GEOSPATIAL FEATURES
# ============================================================================

class ProximityAlert(Base):
    """Geospatial alert for a user, monitoring a specific radius."""
    __tablename__ = "proximity_alerts"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), nullable=False, index=True)
    alert_name = Column(String(255), nullable=False)
    target_radius_meters = Column(Float, nullable=False)
    is_active = Column(Boolean, default=True)
    
    latitude = Column(Float, nullable=False)
    longitude = Column(Float, nullable=False)
    
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
    
    def __repr__(self):
        return f"<ProximityAlert user:{self.user_id} radius:{self.target_radius_meters}m>"


# ============================================================================
# DATA CENTER ACCOUNTABILITY
# ============================================================================

class JobApplicationLink(Base):
    """A job-application link for a data center project, kept by admins (DC-1.3, #248).

    Grain: one row per application URL. App-owned like ``user``: created by
    ``Base.metadata.create_all``, not dbt. The place is carried as text
    (state_code / state / city) rather than a FK to ``jurisdictions``, because dbt
    drops and rebuilds that relation. ``last_verified_at`` is set only by a check
    that reached a live page or by an admin confirming it by hand; a link is stale
    when it is null or more than 30 days old.
    """

    __tablename__ = "job_application_link"
    __table_args__ = (
        CheckConstraint(
            "last_check_status IN ('live', 'dead', 'error')",
            name="ck_job_application_link_check_status",
        ),
        CheckConstraint("length(state_code) = 2", name="ck_job_application_link_state_code"),
        Index("ix_job_application_link_place", "state_code", "city"),
    )

    job_application_link_id = Column(Integer, primary_key=True)
    project_name = Column(Text, nullable=False)
    employer = Column(Text, nullable=True)
    state_code = Column(String(2), nullable=False)
    state = Column(Text, nullable=True)
    city = Column(Text, nullable=True)
    url = Column(Text, nullable=False, unique=True)
    source_note = Column(Text, nullable=True)
    last_verified_at = Column(DateTime(timezone=True), nullable=True)
    last_checked_at = Column(DateTime(timezone=True), nullable=True)
    last_check_status = Column(String(5), nullable=True)
    last_http_status = Column(Integer, nullable=True)
    created_by_user_id = Column(
        Integer, ForeignKey("user.user_id", ondelete="SET NULL"), nullable=True, index=True
    )
    created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), nullable=False)
    updated_at = Column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc),
        nullable=False,
    )

    def __repr__(self):
        return f"<JobApplicationLink {self.job_application_link_id} {self.url}>"


# ============================================================================
# PROFILE CLAIMS (#1062)
# ============================================================================

def _utcnow() -> datetime:
    return datetime.now(timezone.utc)


class PersonClaim(Base):
    """A signed-in user's claim on an official's profile (#1062).

    Grain: one row per (person_id, user_id). ``person_id`` is the official's
    ``person_government.person_id`` (the OCD membership id) carried as text, not a
    FK, because dbt drops and rebuilds that relation. App-owned like ``user``:
    created by ``Base.metadata.create_all``. ``status`` is 'verified' only when the
    user's verified sign-in email hashes to the official's ``email_sha256``
    (``verified_via='official_email'``); otherwise 'pending' for manual review.
    """

    __tablename__ = "person_claim"
    __table_args__ = (
        UniqueConstraint("person_id", "user_id", name="uq_person_claim_person_user"),
        CheckConstraint(
            "status IN ('pending', 'verified', 'rejected')", name="ck_person_claim_status"
        ),
    )

    person_claim_id = Column(Integer, primary_key=True)
    person_id = Column(Text, nullable=False, index=True)
    user_id = Column(
        Integer, ForeignKey("user.user_id", ondelete="CASCADE"), nullable=False, index=True
    )
    status = Column(String(10), nullable=False, default="pending")
    verified_via = Column(String(32), nullable=True)
    note = Column(Text, nullable=True)
    created_at = Column(DateTime(timezone=True), default=_utcnow, nullable=False)
    updated_at = Column(DateTime(timezone=True), default=_utcnow, onupdate=_utcnow, nullable=False)

    def __repr__(self):
        return f"<PersonClaim {self.person_claim_id} {self.person_id} {self.status}>"


class PersonProfileEdit(Base):
    """A proposed profile edit or feedback note from a claimant (#1062).

    Grain: one row per submission. Nothing is published automatically: every row
    starts 'pending' until the owner approves it.
    """

    __tablename__ = "person_profile_edit"
    __table_args__ = (
        CheckConstraint(
            "field IN ('title', 'biography', 'photo_url', 'feedback')",
            name="ck_person_profile_edit_field",
        ),
        CheckConstraint(
            "status IN ('pending', 'approved', 'rejected')",
            name="ck_person_profile_edit_status",
        ),
    )

    person_profile_edit_id = Column(Integer, primary_key=True)
    person_claim_id = Column(
        Integer,
        ForeignKey("person_claim.person_claim_id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    field = Column(String(16), nullable=False)
    new_value = Column(Text, nullable=False)
    status = Column(String(10), nullable=False, default="pending")
    created_at = Column(DateTime(timezone=True), default=_utcnow, nullable=False)

    def __repr__(self):
        return f"<PersonProfileEdit {self.person_profile_edit_id} {self.field} {self.status}>"