social media crossposting tool. 3rd time's the charm
mastodon misskey crossposting bluesky

dedup post insertion code

zenfyr.dev 7e3232c7 7ddafd7b

verified
+4 -29
bluesky.py
···
if post.get('reply'):
parent_ref = json.dumps(post['reply']['parent'], sort_keys=True)
-
root_id = None
-
parent_id = None
-
if parent_ref:
-
parent_post = database.find_post(self.db, parent_ref, self.user_id, self.service)
-
if not parent_post:
-
LOGGER.info("Skipping '%s' as parent post was not found in db!", post_ref)
-
return
-
-
root_id = parent_post['id']
-
parent_id = root_id
-
if parent_post['root_id']:
-
root_id = parent_post['root_id']
+
success = database.try_insert_post(self.db, post_ref, parent_ref, self.user_id, self.service)
+
if not success:
+
LOGGER.info("Skipping '%s' as parent post was not found in db!", post_ref)
+
return
LOGGER.info("Crossposting '%s'...", post_ref)
-
if root_id and parent_id:
-
database.insert_reply(
-
self.db,
-
post_ref,
-
self.user_id,
-
self.service,
-
parent_id,
-
root_id
-
)
-
else:
-
database.insert_post(
-
self.db,
-
post_ref,
-
self.user_id,
-
self.service
-
)
-
cross_post = BlueskyPost(self.pds, self.user_id, post)
for output in outputs:
output.accept_post(cross_post)
+27
database.py
···
with self.lock:
self.conn.close()
+
def try_insert_post(
+
db: DataBaseWorker,
+
post_id: str,
+
in_reply: str | None,
+
input_user: str,
+
input_service: str) -> bool:
+
root_id = None
+
parent_id = None
+
+
if in_reply:
+
parent_post = find_post(db, in_reply, input_user, input_service)
+
if not parent_post:
+
return False
+
+
root_id = parent_post['id']
+
parent_id = root_id
+
if parent_post['root_id']:
+
root_id = parent_post['root_id']
+
+
if root_id and parent_id:
+
insert_reply(db,post_id, input_user, input_service, parent_id, root_id)
+
else:
+
insert_post(db, post_id, input_user, input_service)
+
+
return True
+
+
def find_mapped_thread(
db: DataBaseWorker,
parent_id: str,
+4 -29
mastodon.py
···
LOGGER.info("Skipping '%s'! '%s' visibility..", status['id'], status.get('visibility'))
return
-
root_id = None
-
parent_id = None
-
if in_reply:
-
parent_post = database.find_post(self.db, in_reply, self.user_id, self.service)
-
if not parent_post:
-
LOGGER.info("Skipping '%s' as parent post was not found in db!", status['id'])
-
return
-
-
root_id = parent_post['id']
-
parent_id = root_id
-
if parent_post['root_id']:
-
root_id = parent_post['root_id']
+
success = database.try_insert_post(self.db, status['id'], in_reply, self.user_id, self.service)
+
if not success:
+
LOGGER.info("Skipping '%s' as parent post was not found in db!", status['id'])
+
return
LOGGER.info("Crossposting '%s'...", status['id'])
-
if root_id and parent_id:
-
database.insert_reply(
-
self.db,
-
status['id'],
-
self.user_id,
-
self.service,
-
parent_id,
-
root_id
-
)
-
else:
-
database.insert_post(
-
self.db,
-
status['id'],
-
self.user_id,
-
self.service
-
)
-
cross_post = MastodonPost(status)
for output in outputs:
output.accept_post(cross_post)
+4 -29
misskey.py
···
LOGGER.info("Skipping '%s'! '%s' visibility..", note['id'], note.get('visibility'))
return
-
root_id = None
-
parent_id = None
-
if reply_id:
-
parent_post = database.find_post(self.db, reply_id, self.user_id, self.service)
-
if not parent_post:
-
LOGGER.info("Skipping '%s' as parent post was not found in db!", note['id'])
-
return
-
-
root_id = parent_post['id']
-
parent_id = root_id
-
if parent_post['root_id']:
-
root_id = parent_post['root_id']
+
success = database.try_insert_post(self.db, note['id'], reply_id, self.user_id, self.service)
+
if not success:
+
LOGGER.info("Skipping '%s' as parent note was not found in db!", note['id'])
+
return
LOGGER.info("Crossposting '%s'...", note['id'])
-
if root_id and parent_id:
-
database.insert_reply(
-
self.db,
-
note['id'],
-
self.user_id,
-
self.service,
-
parent_id,
-
root_id
-
)
-
else:
-
database.insert_post(
-
self.db,
-
note['id'],
-
self.user_id,
-
self.service
-
)
-
cross_post = MisskeyPost(note)
for output in outputs:
output.accept_post(cross_post)