Adds thread id to status model

This commit is contained in:
Mouse Reeve 2021-10-01 14:12:03 -07:00
parent 7d03bfd2f6
commit b8fd909fe3
2 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,53 @@
# Generated by Django 3.2.5 on 2021-10-01 20:12
from django.db import migrations, models
def set_thread_id(app_registry, schema_editor):
"""set thread ids"""
db_alias = schema_editor.connection.alias
# set the thread id on parent nodes
model = app_registry.get_model("bookwyrm", "Status")
model.objects.using(db_alias).filter(reply_parent__isnull=True).update(
thread_id=models.F("id")
)
queryset = model.objects.using(db_alias).filter(
reply_parent__isnull=False,
reply_parent__thread_id__isnull=False,
thread_id__isnull=True,
)
iters = 0
while queryset.exists():
queryset.update(
thread_id=models.Subquery(
model.objects.filter(id=models.OuterRef("reply_parent")).values_list(
"thread_id"
)[:1]
)
)
print(iters)
iters += 1
if iters > 50:
print("exceeded query depth")
break
def reverse(*_):
"""do nothing"""
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0103_remove_connector_local"),
]
operations = [
migrations.AddField(
model_name="status",
name="thread_id",
field=models.IntegerField(blank=True, null=True),
),
migrations.RunPython(set_thread_id, reverse),
]

View file

@ -57,6 +57,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
on_delete=models.PROTECT,
activitypub_field="inReplyTo",
)
thread_id = models.IntegerField(blank=True, null=True)
objects = InheritanceManager()
activity_serializer = activitypub.Note
@ -68,6 +69,13 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
ordering = ("-published_date",)
def save(self, *args, **kwargs):
"""save and notify"""
if self.reply_parent:
self.thread_id = self.reply_parent.thread_id or self.reply_parent.id
super().save(*args, **kwargs)
def delete(self, *args, **kwargs): # pylint: disable=unused-argument
""" "delete" a status"""
if hasattr(self, "boosted_status"):