Skip to content

Commit

Permalink
delete from remote post
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Nov 21, 2023
1 parent 7eeabdb commit 476a8bc
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 19 deletions.
3 changes: 3 additions & 0 deletions journal/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def ap_object(self):

@classmethod
def update_by_ap_object(cls, owner, item, obj, post_id, visibility):
p = cls.objects.filter(owner=owner, item=item).first()
if p and p.edited_time >= datetime.fromisoformat(obj["updated"]):
return p # incoming ap object is older than what we have, no update needed
content = obj.get("content", "").strip() if obj else ""
if not content:
cls.objects.filter(owner=owner, item=item).delete()
Expand Down
12 changes: 9 additions & 3 deletions journal/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,15 @@ def clear_post_ids(self):
PiecePost.objects.filter(piece=self).delete()

@cached_property
def latest_post(self):
# local post id is ordered by their created time
def latest_post_id(self):
# post id is ordered by their created time
pp = PiecePost.objects.filter(piece=self).order_by("-post_id").first()
return Takahe.get_post(pp.post_id) if pp else None # type: ignore
return pp.post_id if pp else None

@cached_property
def latest_post(self):
pk = self.latest_post_id
return Takahe.get_post(pk) if pk else None

@cached_property
def all_post_ids(self):
Expand All @@ -212,6 +217,7 @@ def all_post_ids(self):


class PiecePost(models.Model):
post_id: int
piece = models.ForeignKey(Piece, on_delete=models.CASCADE)
post = models.ForeignKey(
"takahe.Post", db_constraint=False, db_index=True, on_delete=models.CASCADE
Expand Down
3 changes: 3 additions & 0 deletions journal/models/rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def ap_object(self):

@classmethod
def update_by_ap_object(cls, owner, item, obj, post_id, visibility):
p = cls.objects.filter(owner=owner, item=item).first()
if p and p.edited_time >= datetime.fromisoformat(obj["updated"]):
return p # incoming ap object is older than what we have, no update needed
value = obj.get("value", 0) if obj else 0
if not value:
cls.objects.filter(owner=owner, item=item).delete()
Expand Down
3 changes: 3 additions & 0 deletions journal/models/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def ap_object(self):

@classmethod
def update_by_ap_object(cls, owner, item, obj, post_id, visibility):
p = cls.objects.filter(owner=owner, item=item).first()
if p and p.edited_time >= datetime.fromisoformat(obj["updated"]):
return p # incoming ap object is older than what we have, no update needed
content = (
obj["content"]
if obj.get("mediaType") == "text/markdown"
Expand Down
10 changes: 5 additions & 5 deletions journal/models/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ def ap_object(self):
def update_by_ap_object(
cls, owner: APIdentity, item: Identity, obj: dict, post_id: int, visibility: int
):
# TODO check timestamp? (update may come in with inconsistent sequence)
if not obj:
cls.objects.filter(owner=owner, item=item).delete()
return
p = cls.objects.filter(owner=owner, item=item).first()
if p and p.edited_time >= datetime.fromisoformat(obj["updated"]):
return p # incoming ap object is older than what we have, no update needed
shelf = owner.shelf_manager.get_shelf(obj["status"])
if not shelf:
logger.warning(f"unable to locate shelf for {owner}, {obj}")
Expand Down Expand Up @@ -147,7 +146,8 @@ def log_and_delete(self):
self.delete()

def link_post_id(self, post_id: int):
self.ensure_log_entry().link_post_id(post_id)
if self.local:
self.ensure_log_entry().link_post_id(post_id)
return super().link_post_id(post_id)


Expand Down
2 changes: 1 addition & 1 deletion journal/templatetags/user_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def like_piece_action(context, piece):
action = {}
if user and user.is_authenticated and piece and piece.latest_post:
action = {
"taken": Takahe.post_liked_by(piece.latest_post.pk, user),
"taken": Takahe.post_liked_by(piece.latest_post.pk, user.identity.pk),
"url": reverse("journal:like", args=[piece.uuid]),
}
return action
Expand Down
2 changes: 1 addition & 1 deletion neodb-takahe
10 changes: 4 additions & 6 deletions social/templates/activity/mark_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
{% load duration %}
{% wish_item_action activity.action_object.item as action %}
<span class="action">
{% if activity.action_object.mark.comment_text %}
<span>
{% liked_piece activity.action_object.mark.comment as liked %}
{% include 'like_stats.html' with liked=liked piece=activity.action_object.mark.comment %}
</span>
{% endif %}
<span>
{% liked_piece activity.action_object as liked %}
{% include 'like_stats.html' with liked=liked piece=activity.action_object %}
</span>
{% comment %}
<span>
<a><i class="fa-solid fa-circle-play"></i></a>
Expand Down
2 changes: 1 addition & 1 deletion social/templates/feed_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<a href="{{ activity.owner.url }}" class="nickname">{{ activity.owner.display_name }}</a>
</span>
<span>
<a href="{{ activity.owner.url }}" class="handler">@{{ activity.owner.handler }}</a>
<a href="{{ activity.owner.url }}" class="handler">{{ activity.owner.handler }}</a>
</span>
</div>
{% with "activity/"|add:activity.template|add:".html" as template %}
Expand Down
17 changes: 15 additions & 2 deletions takahe/ap_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ def _get_or_create_item(item_obj):
if typ in ["TVEpisode", "PodcastEpisode"]:
# TODO support episode item
# match and fetch parent item first
logger.debug(f"{typ}:{url} not supported yet")
return None
site = SiteManager.get_site_by_url(url)
if not site:
logger.warning(f"Site not found for {url}")
return None
site.get_resource_ready()
item = site.get_item()
if not item:
logger.warning(f"Item not fetched for {url}")
return item


Expand Down Expand Up @@ -100,12 +104,21 @@ def post_fetched(pk, obj):
logger.warning(f"Post {post} has no local item matched or created")
return
for p in pieces:
cls = _supported_ap_journal_types[p["type"]]
cls = _supported_ap_journal_types.get(p["type"])
if not cls:
logger.warning(f'Unknown link type {p["type"]}')
continue
cls.update_by_ap_object(owner, item, p, pk, _get_visibility(post.visibility))


def post_deleted(pk, obj):
Piece.objects.filter(posts__id=pk, local=False).delete()
for piece in Piece.objects.filter(posts__id=pk, local=False):
# delete piece if the deleted post is the most recent one for the piece
if piece.latest_post_id == pk:
logger.debug(f"Deleting remote piece {piece}")
piece.delete()
else:
logger.debug(f"Matched remote piece {piece} has newer posts, not deleting")


def identity_fetched(pk):
Expand Down

0 comments on commit 476a8bc

Please sign in to comment.