-
-
Notifications
You must be signed in to change notification settings - Fork 69
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 1 | New Feature: Rebloom - New #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d9ed15b
48db6f4
397bca1
320d5c6
df2ce91
532e6b3
62f68ec
64efa89
05ad727
db8601c
02f3358
e0d739b
fadd3e1
7280198
d0f4bb3
2207ba9
79bebde
6282c0d
8e9fdf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ class Bloom: | |
| sender: User | ||
| content: str | ||
| sent_timestamp: datetime.datetime | ||
| original_bloom: Optional["Bloom"] = None | ||
| rebloom_count: int = 0 | ||
|
|
||
|
|
||
| def add_bloom(*, sender: User, content: str) -> Bloom: | ||
|
|
@@ -36,6 +38,86 @@ def add_bloom(*, sender: User, content: str) -> Bloom: | |
| dict(hashtag=hashtag, bloom_id=bloom_id), | ||
| ) | ||
|
|
||
| def rebloom( | ||
| *, sender: User, original_bloom_id: int | ||
| ) -> Optional[Bloom]: | ||
| now = datetime.datetime.now(tz=datetime.UTC) | ||
| bloom_id = int(now.timestamp() * 1000000) | ||
|
|
||
| with db_cursor() as cur: | ||
| cur.execute( | ||
| """ | ||
| SELECT COALESCE(original_bloom_id, id) | ||
| FROM blooms | ||
| WHERE id = %(original_bloom_id)s | ||
| """, | ||
| {"original_bloom_id": original_bloom_id}, | ||
| ) | ||
|
|
||
| row = cur.fetchone() | ||
|
|
||
| if row is None: | ||
| return None | ||
|
|
||
| root_bloom_id = row[0] | ||
|
|
||
| cur.execute( | ||
| """ | ||
| INSERT INTO blooms ( | ||
| id, | ||
| sender_id, | ||
| content, | ||
| send_timestamp, | ||
| original_bloom_id | ||
| ) | ||
| SELECT | ||
| %(bloom_id)s, | ||
| %(sender_id)s, | ||
| content, | ||
| %(timestamp)s, | ||
| %(root_bloom_id)s | ||
| FROM blooms | ||
| WHERE id = %(original_bloom_id)s | ||
| RETURNING id, sender_id, content, send_timestamp, original_bloom_id | ||
| """, | ||
| { | ||
| "bloom_id": bloom_id, | ||
| "sender_id": sender.id, | ||
| "timestamp": now, | ||
| "original_bloom_id": original_bloom_id, | ||
| "root_bloom_id": root_bloom_id, | ||
| }, | ||
| ) | ||
|
|
||
| row = cur.fetchone() | ||
|
|
||
| if row is None: | ||
| return None | ||
|
|
||
| cur.execute( | ||
| """ | ||
| INSERT INTO reblooms ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you deleted this table? |
||
| rebloomer_id, | ||
| bloom_id, | ||
| rebloom_timestamp | ||
| ) | ||
| VALUES ( | ||
| %(rebloomer_id)s, | ||
| %(bloom_id)s, | ||
| %(timestamp)s | ||
| ) | ||
| """, | ||
| { | ||
| "rebloomer_id": sender.id, | ||
| "bloom_id": root_bloom_id, | ||
| "timestamp": now, | ||
| }, | ||
| ) | ||
|
|
||
| return get_bloom(bloom_id) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def get_blooms_for_user( | ||
| username: str, *, before: Optional[int] = None, limit: Optional[int] = None | ||
|
|
@@ -44,60 +126,129 @@ def get_blooms_for_user( | |
| kwargs = { | ||
| "sender_username": username, | ||
| } | ||
|
|
||
| if before is not None: | ||
| before_clause = "AND send_timestamp < %(before_limit)s" | ||
| before_clause = "AND blooms.send_timestamp < %(before_limit)s" | ||
| kwargs["before_limit"] = before | ||
| else: | ||
| before_clause = "" | ||
|
|
||
| limit_clause = make_limit_clause(limit, kwargs) | ||
|
|
||
| cur.execute( | ||
| f"""SELECT | ||
| blooms.id, users.username, content, send_timestamp | ||
| FROM | ||
| blooms INNER JOIN users ON users.id = blooms.sender_id | ||
| WHERE | ||
| username = %(sender_username)s | ||
| {before_clause} | ||
| ORDER BY send_timestamp DESC | ||
| f""" | ||
| SELECT | ||
| blooms.id, | ||
| users.username, | ||
| blooms.content, | ||
| blooms.send_timestamp, | ||
| blooms.original_bloom_id, | ||
| ( | ||
| SELECT COUNT(*) | ||
| FROM reblooms | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This table no longer exists? |
||
| WHERE reblooms.bloom_id = COALESCE( | ||
| blooms.original_bloom_id, | ||
| blooms.id | ||
| ) | ||
| ) AS rebloom_count | ||
| FROM blooms | ||
| INNER JOIN users | ||
| ON users.id = blooms.sender_id | ||
| WHERE users.username = %(sender_username)s | ||
| {before_clause} | ||
| ORDER BY blooms.send_timestamp DESC | ||
| {limit_clause} | ||
| """, | ||
| kwargs, | ||
| ) | ||
|
|
||
| rows = cur.fetchall() | ||
| blooms = [] | ||
| blooms_list = [] | ||
|
|
||
| for row in rows: | ||
| bloom_id, sender_username, content, timestamp = row | ||
| blooms.append( | ||
| ( | ||
| bloom_id, | ||
| sender_username, | ||
| content, | ||
| timestamp, | ||
| original_bloom_id, | ||
| rebloom_count, | ||
| ) = row | ||
|
|
||
| original_bloom = ( | ||
| get_bloom(original_bloom_id) | ||
| if original_bloom_id is not None | ||
| else None | ||
| ) | ||
|
|
||
| blooms_list.append( | ||
| Bloom( | ||
| id=bloom_id, | ||
| sender=sender_username, | ||
| content=content, | ||
| sent_timestamp=timestamp, | ||
| original_bloom=original_bloom, | ||
| rebloom_count=rebloom_count, | ||
| ) | ||
| ) | ||
| return blooms | ||
|
|
||
| return blooms_list | ||
|
|
||
|
|
||
| def get_bloom(bloom_id: int) -> Optional[Bloom]: | ||
| with db_cursor() as cur: | ||
| cur.execute( | ||
| "SELECT blooms.id, users.username, content, send_timestamp FROM blooms INNER JOIN users ON users.id = blooms.sender_id WHERE blooms.id = %s", | ||
| """ | ||
| SELECT | ||
| blooms.id, | ||
| users.username, | ||
| blooms.content, | ||
| blooms.send_timestamp, | ||
| blooms.original_bloom_id, | ||
| ( | ||
| SELECT COUNT(*) | ||
| FROM reblooms | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
| WHERE reblooms.bloom_id = COALESCE( | ||
| blooms.original_bloom_id, | ||
| blooms.id | ||
| ) | ||
| ) AS rebloom_count | ||
| FROM blooms | ||
| INNER JOIN users ON users.id = blooms.sender_id | ||
| WHERE blooms.id = %s | ||
| """, | ||
| (bloom_id,), | ||
| ) | ||
|
|
||
| row = cur.fetchone() | ||
|
|
||
| if row is None: | ||
| return None | ||
| bloom_id, sender_username, content, timestamp = row | ||
|
|
||
| ( | ||
| bloom_id, | ||
| sender_username, | ||
| content, | ||
| timestamp, | ||
| original_bloom_id, | ||
| rebloom_count, | ||
| ) = row | ||
|
|
||
| original_bloom = ( | ||
| get_bloom(original_bloom_id) | ||
| if original_bloom_id is not None | ||
| else None | ||
| ) | ||
|
|
||
| return Bloom( | ||
| id=bloom_id, | ||
| sender=sender_username, | ||
| content=content, | ||
| sent_timestamp=timestamp, | ||
| original_bloom=original_bloom, | ||
| rebloom_count=rebloom_count, | ||
| ) | ||
|
|
||
|
|
||
| def get_blooms_with_hashtag( | ||
| hashtag_without_leading_hash: str, *, limit: int = None | ||
| ) -> List[Bloom]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this
COALESCE? When would theoriginal_bloom_idnot be present as anidfor some row?