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

simplify Mark.update() and improve incoming mark update/deletion #386

Merged
merged 8 commits into from
Nov 21, 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
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
python-version: ['3.11']
steps:
- uses: actions/checkout@v3
with:
submodules: 'true'
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down
1 change: 1 addition & 0 deletions catalog/sites/fedi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FediverseInstance(AbstractSite):
}
supported_types = {
"Book": Edition,
"Edition": Edition,
"Movie": Movie,
"TVShow": TVShow,
"TVSeason": TVSeason,
Expand Down
8 changes: 4 additions & 4 deletions catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ def embed(request, item_path, item_uuid):


def retrieve(request, item_path, item_uuid):
if request.method != "GET":
raise BadRequest()
# item = get_object_or_404(Item, uid=get_uuid_or_404(item_uuid))
item = Item.get_by_url(item_uuid)
if item is None:
raise Http404()
item_url = f"/{item_path}/{item_uuid}"
if item.url != item_url:
return redirect(item.url)
if request.headers.get("Accept", "").endswith("json"):
return redirect(item.api_url)
skipcheck = request.GET.get("skipcheck", False) and request.user.is_authenticated
if not skipcheck and item.merged_to_item:
return redirect(item.merged_to_item.url)
if not skipcheck and item.is_deleted:
raise Http404()
if request.headers.get("Accept", "").endswith("json"):
return redirect(item.api_url)
if request.method != "GET":
raise BadRequest()
focus_item = None
if request.GET.get("focus"):
focus_item = get_object_or_404(
Expand Down
13 changes: 4 additions & 9 deletions journal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from catalog.common.models import *
from common.api import *
from mastodon.api import boost_toot
from mastodon.api import boost_toot_later

from .models import Mark, Review, ShelfType, TagManager, q_item_in_category

Expand Down Expand Up @@ -195,21 +195,16 @@ def review_item(request, item_uuid: str, review: ReviewInSchema):
item = Item.get_by_url(item_uuid)
if not item:
return 404, {"message": "Item not found"}
r, p = Review.update_item_review(
r, post = Review.update_item_review(
item,
request.user,
review.title,
review.body,
review.visibility,
created_time=review.created_time,
)
if p and review.post_to_fediverse and request.user.mastodon_username:
boost_toot(
request.user.mastodon_site,
request.user.mastodon_token,
p.url,
)

if post and review.post_to_fediverse:
boost_toot_later(request.user, post.url)
return 200, {"message": "OK"}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.7 on 2023-11-20 06:52

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("takahe", "0001_initial"),
("journal", "0017_alter_piece_options_and_more"),
]

operations = [
migrations.RemoveField(
model_name="shelflogentry",
name="metadata",
),
migrations.CreateModel(
name="ShelfLogEntryPost",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"log_entry",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="journal.shelflogentry",
),
),
(
"post",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.CASCADE,
to="takahe.post",
),
),
],
),
migrations.AddField(
model_name="shelflogentry",
name="posts",
field=models.ManyToManyField(
related_name="log_entries",
through="journal.ShelfLogEntryPost",
to="takahe.post",
),
),
migrations.AddConstraint(
model_name="shelflogentrypost",
constraint=models.UniqueConstraint(
fields=("log_entry", "post"), name="unique_log_entry_post"
),
),
]
63 changes: 63 additions & 0 deletions journal/migrations/0019_alter_collection_edited_time_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 4.2.7 on 2023-11-21 00:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("journal", "0018_shelflogentrypost_shelflogentry_posts_and_more"),
]

operations = [
migrations.AlterField(
model_name="collection",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="collectionmember",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="comment",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="like",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="rating",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="review",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="shelf",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="shelfmember",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="tag",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name="tagmember",
name="edited_time",
field=models.DateTimeField(auto_now=True),
),
]
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
20 changes: 12 additions & 8 deletions journal/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,19 @@ def ap_object(self):
def link_post_id(self, post_id: int):
PiecePost.objects.get_or_create(piece=self, post_id=post_id)

def link_post(self, post: "Post"):
return self.link_post_id(post.pk)
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 All @@ -229,9 +235,7 @@ class Content(Piece):
default=0
) # 0: Public / 1: Follower only / 2: Self only
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(
default=timezone.now
) # auto_now=True FIXME revert this after migration
edited_time = models.DateTimeField(auto_now=True)
metadata = models.JSONField(default=dict)
item = models.ForeignKey(Item, on_delete=models.PROTECT)
remote_id = models.CharField(max_length=200, null=True, default=None)
Expand Down
4 changes: 2 additions & 2 deletions journal/models/itemlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class List(Piece):
default=0
) # 0: Public / 1: Follower only / 2: Self only
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
metadata = models.JSONField(default=dict)

class Meta:
Expand Down Expand Up @@ -148,7 +148,7 @@ class ListMember(Piece):
default=0
) # 0: Public / 1: Follower only / 2: Self only
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
metadata = models.JSONField(default=dict)
item = models.ForeignKey(Item, on_delete=models.PROTECT)
position = models.PositiveIntegerField()
Expand Down
2 changes: 1 addition & 1 deletion journal/models/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Like(Piece): # TODO remove
default=0
) # 0: Public / 1: Follower only / 2: Self only
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
target = models.ForeignKey(Piece, on_delete=models.CASCADE, related_name="likes")

@staticmethod
Expand Down
Loading