Skip to content

Commit

Permalink
fix undo boost
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Jul 6, 2024
1 parent 6568b23 commit b9f2173
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
9 changes: 5 additions & 4 deletions journal/views/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def post_boost(request: AuthedHttpRequest, post_id: int):
post = Takahe.get_post(post_id)
if not post:
raise BadRequest(_("Invalid parameter"))
if request.user.mastodon and request.user.preference.mastodon_repost_mode == 1:
request.user.mastodon.boost_later(post.object_uri)
else:
Takahe.boost_post(post_id, request.user.identity.pk)
boost = Takahe.boost_post(post_id, request.user.identity.pk)
print(boost.state if boost else "x")
if boost and boost.state == "new":
if request.user.mastodon and request.user.preference.mastodon_repost_mode == 1:
request.user.mastodon.boost_later(post.object_uri)
return render(request, "action_boost_post.html", {"post": post})


Expand Down
2 changes: 2 additions & 0 deletions takahe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,8 @@ class Types(models.TextChoices):
# state = StateField(PostInteractionStates)
state = models.CharField(max_length=100, default="new")
state_changed = models.DateTimeField(auto_now_add=True)
state_next_attempt = models.DateTimeField(blank=True, null=True)
state_locked_until = models.DateTimeField(null=True, blank=True, db_index=True)

# The canonical object ID
object_uri = models.CharField(max_length=500, blank=True, null=True, unique=True)
Expand Down
17 changes: 9 additions & 8 deletions takahe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def post_collection(collection: "Collection"):
return post

@staticmethod
def interact_post(post_pk: int, identity_pk: int, type: str):
def interact_post(post_pk: int, identity_pk: int, type: str, flip=False):
post = Post.objects.filter(pk=post_pk).first()
if not post:
logger.warning(f"Cannot find post {post_pk}")
Expand All @@ -626,14 +626,15 @@ def interact_post(post_pk: int, identity_pk: int, type: str):
if not identity:
logger.warning(f"Cannot find identity {identity_pk}")
return
interaction = PostInteraction.objects.get_or_create(
interaction, created = PostInteraction.objects.get_or_create(
type=type,
identity_id=identity_pk,
post=post,
)[0]
if interaction.state not in ["new", "fanned_out"]:
interaction.state = "new"
interaction.save()
)
if flip and not created:
Takahe.update_state(interaction, "undone")
elif interaction.state not in ["new", "fanned_out"]:
Takahe.update_state(interaction, "new")
post.calculate_stats()
return interaction

Expand All @@ -660,7 +661,7 @@ def reply_post(

@staticmethod
def boost_post(post_pk: int, identity_pk: int):
return Takahe.interact_post(post_pk, identity_pk, "boost")
return Takahe.interact_post(post_pk, identity_pk, "boost", flip=True)

@staticmethod
def post_boosted_by(post_pk: int, identity_pk: int) -> bool:
Expand Down Expand Up @@ -747,7 +748,7 @@ def txt2html(txt: str) -> str:
return FediverseHtmlParser(linebreaks_filter(txt)).html

@staticmethod
def update_state(obj: Post | Relay | Identity, state: str):
def update_state(obj: Post | PostInteraction | Relay | Identity, state: str):
obj.state = state
obj.state_changed = timezone.now()
obj.state_next_attempt = None
Expand Down

0 comments on commit b9f2173

Please sign in to comment.