|
| 1 | +import datetime |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, Dict, List, Optional |
| 5 | + |
| 6 | +from data.connection import db_cursor |
| 7 | +from data.users import User |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class Bloom: |
| 12 | + id: int |
| 13 | + sender: User |
| 14 | + content: str |
| 15 | + sent_timestamp: datetime.datetime |
| 16 | + |
| 17 | + |
| 18 | +def add_bloom(*, sender: User, content: str) -> Bloom: |
| 19 | + hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")] |
| 20 | + |
| 21 | + now = datetime.datetime.now(tz=datetime.UTC) |
| 22 | + bloom_id = int(now.timestamp() * 1000000) |
| 23 | + with db_cursor() as cur: |
| 24 | + cur.execute( |
| 25 | + "INSERT INTO blooms (id, sender_id, content, send_timestamp) VALUES (%(bloom_id)s, %(sender_id)s, %(content)s, %(timestamp)s)", |
| 26 | + dict( |
| 27 | + bloom_id=bloom_id, |
| 28 | + sender_id=sender.id, |
| 29 | + content=content, |
| 30 | + timestamp=datetime.datetime.now(datetime.UTC), |
| 31 | + ), |
| 32 | + ) |
| 33 | + for hashtag in hashtags: |
| 34 | + cur.execute( |
| 35 | + "INSERT INTO hashtags (hashtag, bloom_id) VALUES (%(hashtag)s, %(bloom_id)s)", |
| 36 | + dict(hashtag=hashtag, bloom_id=bloom_id), |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def get_blooms_for_user( |
| 41 | + username: str, *, before: Optional[int] = None, limit: Optional[int] = None |
| 42 | +) -> List[Bloom]: |
| 43 | + with db_cursor() as cur: |
| 44 | + kwargs = { |
| 45 | + "sender_username": username, |
| 46 | + } |
| 47 | + if before is not None: |
| 48 | + before_clause = "AND send_timestamp < %(before_limit)s" |
| 49 | + kwargs["before_limit"] = before |
| 50 | + else: |
| 51 | + before_clause = "" |
| 52 | + |
| 53 | + limit_clause = make_limit_clause(limit, kwargs) |
| 54 | + |
| 55 | + cur.execute( |
| 56 | + f"""SELECT |
| 57 | + blooms.id, users.username, content, send_timestamp |
| 58 | + FROM |
| 59 | + blooms INNER JOIN users ON users.id = blooms.sender_id |
| 60 | + WHERE |
| 61 | + username = %(sender_username)s |
| 62 | + {before_clause} |
| 63 | + ORDER BY send_timestamp DESC |
| 64 | + {limit_clause} |
| 65 | + """, |
| 66 | + kwargs, |
| 67 | + ) |
| 68 | + rows = cur.fetchall() |
| 69 | + blooms = [] |
| 70 | + for row in rows: |
| 71 | + bloom_id, sender_username, content, timestamp = row |
| 72 | + blooms.append( |
| 73 | + Bloom( |
| 74 | + id=bloom_id, |
| 75 | + sender=sender_username, |
| 76 | + content=content, |
| 77 | + sent_timestamp=timestamp, |
| 78 | + ) |
| 79 | + ) |
| 80 | + return blooms |
| 81 | + |
| 82 | + |
| 83 | +def get_bloom(bloom_id: int) -> Optional[Bloom]: |
| 84 | + with db_cursor() as cur: |
| 85 | + cur.execute( |
| 86 | + "SELECT blooms.id, users.username, content, send_timestamp FROM blooms INNER JOIN users ON users.id = blooms.sender_id WHERE blooms.id = %s", |
| 87 | + (bloom_id,), |
| 88 | + ) |
| 89 | + row = cur.fetchone() |
| 90 | + if row is None: |
| 91 | + return None |
| 92 | + bloom_id, sender_username, content, timestamp = row |
| 93 | + return Bloom( |
| 94 | + id=bloom_id, |
| 95 | + sender=sender_username, |
| 96 | + content=content, |
| 97 | + sent_timestamp=timestamp, |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def get_blooms_with_hashtag( |
| 102 | + hashtag_without_leading_hash: str, *, limit: int = None |
| 103 | +) -> List[Bloom]: |
| 104 | + kwargs = { |
| 105 | + "hashtag_without_leading_hash": hashtag_without_leading_hash, |
| 106 | + } |
| 107 | + limit_clause = make_limit_clause(limit, kwargs) |
| 108 | + with db_cursor() as cur: |
| 109 | + cur.execute( |
| 110 | + f"""SELECT |
| 111 | + blooms.id, users.username, content, send_timestamp |
| 112 | + FROM |
| 113 | + blooms INNER JOIN hashtags ON blooms.id = hashtags.bloom_id INNER JOIN users ON blooms.sender_id = users.id |
| 114 | + WHERE |
| 115 | + hashtag = %(hashtag_without_leading_hash)s |
| 116 | + ORDER BY send_timestamp DESC |
| 117 | + {limit_clause} |
| 118 | + """, |
| 119 | + kwargs, |
| 120 | + ) |
| 121 | + rows = cur.fetchall() |
| 122 | + blooms = [] |
| 123 | + for row in rows: |
| 124 | + bloom_id, sender_username, content, timestamp = row |
| 125 | + blooms.append( |
| 126 | + Bloom( |
| 127 | + id=bloom_id, |
| 128 | + sender=sender_username, |
| 129 | + content=content, |
| 130 | + sent_timestamp=timestamp, |
| 131 | + ) |
| 132 | + ) |
| 133 | + return blooms |
| 134 | + |
| 135 | + |
| 136 | +def make_limit_clause(limit: Optional[int], kwargs: Dict[Any, Any]) -> str: |
| 137 | + if limit is not None: |
| 138 | + limit_clause = "LIMIT %(limit)s" |
| 139 | + kwargs["limit"] = limit |
| 140 | + else: |
| 141 | + limit_clause = "" |
| 142 | + return limit_clause |
0 commit comments