Skip to content
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

Fixes related to bsky.social migration #5

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/data_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _run(name, operations_callback, stream_stop_event=None):
if state:
params = models.ComAtprotoSyncSubscribeRepos.Params(cursor=state.cursor)

client = FirehoseSubscribeReposClient(params)
client = FirehoseSubscribeReposClient(params,base_uri='wss://bsky.network/xrpc')

if not state:
SubscriptionState.create(service=name, cursor=0)
Expand All @@ -93,6 +93,8 @@ def on_message_handler(message: 'MessageFrame') -> None:
commit = parse_subscribe_repos_message(message)
if not isinstance(commit, models.ComAtprotoSyncSubscribeRepos.Commit):
return
if not commit.blocks:
return

# update stored state every ~20 events
if commit.seq % 20 == 0:
Expand Down
22 changes: 21 additions & 1 deletion server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


db = peewee.SqliteDatabase('feed_database.db')
db_version = 2


class BaseModel(peewee.Model):
Expand All @@ -23,7 +24,26 @@ class SubscriptionState(BaseModel):
service = peewee.CharField(unique=True)
cursor = peewee.IntegerField()

class DbMetadata(BaseModel):
version = peewee.IntegerField()


if db.is_closed():
db.connect()
db.create_tables([Post, SubscriptionState])
db.create_tables([Post, SubscriptionState, DbMetadata])

# DB migration
current_version = 1 if DbMetadata.select().count() == 0 else DbMetadata.select().first().version

if current_version != db_version:
with db.atomic():
# V2
# Drop cursors stored from the old bsky.social PDS
if current_version == 1:
SubscriptionState.delete().execute()

# Update version in DB
if DbMetadata.select().count() == 0:
DbMetadata.insert({DbMetadata.version:db_version}).execute()
else:
DbMetadata.update({DbMetadata.version:db_version}).execute()