2021-03-08 08:49:10 -08:00
|
|
|
""" note serializer and children thereof """
|
2020-09-17 13:02:52 -07:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from typing import Dict, List
|
2023-02-17 19:24:42 +01:00
|
|
|
import re
|
|
|
|
|
2021-02-16 09:35:00 -08:00
|
|
|
from django.apps import apps
|
2023-02-17 19:24:42 +01:00
|
|
|
from django.db import IntegrityError, transaction
|
2020-09-17 13:02:52 -07:00
|
|
|
|
2023-02-17 19:24:42 +01:00
|
|
|
from .base_activity import ActivityObject, ActivitySerializerError, Link
|
2021-04-15 16:35:04 -07:00
|
|
|
from .image import Document
|
2020-09-17 13:02:52 -07:00
|
|
|
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2020-10-08 12:32:45 -07:00
|
|
|
@dataclass(init=False)
|
|
|
|
class Tombstone(ActivityObject):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""the placeholder for a deleted status"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
|
|
|
type: str = "Tombstone"
|
2020-10-08 12:32:45 -07:00
|
|
|
|
2021-03-08 09:54:02 -08:00
|
|
|
def to_model(self, *args, **kwargs): # pylint: disable=unused-argument
|
2021-04-26 09:15:42 -07:00
|
|
|
"""this should never really get serialized, just searched for"""
|
2021-03-08 09:54:02 -08:00
|
|
|
model = apps.get_model("bookwyrm.Status")
|
2021-02-16 09:35:00 -08:00
|
|
|
return model.find_existing_by_remote_id(self.id)
|
|
|
|
|
2020-10-08 12:32:45 -07:00
|
|
|
|
2021-06-18 14:29:24 -07:00
|
|
|
# pylint: disable=invalid-name
|
2020-09-17 13:02:52 -07:00
|
|
|
@dataclass(init=False)
|
|
|
|
class Note(ActivityObject):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""Note activity"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2020-09-17 13:02:52 -07:00
|
|
|
published: str
|
|
|
|
attributedTo: str
|
2021-03-08 08:49:10 -08:00
|
|
|
content: str = ""
|
2020-11-30 14:24:31 -08:00
|
|
|
to: List[str] = field(default_factory=lambda: [])
|
|
|
|
cc: List[str] = field(default_factory=lambda: [])
|
|
|
|
replies: Dict = field(default_factory=lambda: {})
|
2021-08-28 11:16:39 -07:00
|
|
|
inReplyTo: str = None
|
|
|
|
summary: str = None
|
2020-11-25 10:44:49 -08:00
|
|
|
tag: List[Link] = field(default_factory=lambda: [])
|
2021-04-15 16:35:04 -07:00
|
|
|
attachment: List[Document] = field(default_factory=lambda: [])
|
2020-09-17 13:02:52 -07:00
|
|
|
sensitive: bool = False
|
2021-10-15 08:56:07 -07:00
|
|
|
updated: str = None
|
2021-03-08 08:49:10 -08:00
|
|
|
type: str = "Note"
|
2020-09-17 13:02:52 -07:00
|
|
|
|
2023-02-17 19:24:42 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
def to_model(
|
|
|
|
self,
|
|
|
|
model=None,
|
|
|
|
instance=None,
|
|
|
|
allow_create=True,
|
|
|
|
save=True,
|
|
|
|
overwrite=True,
|
|
|
|
allow_external_connections=True,
|
|
|
|
):
|
|
|
|
instance = super().to_model(
|
|
|
|
model, instance, allow_create, save, overwrite, allow_external_connections
|
|
|
|
)
|
|
|
|
|
|
|
|
if instance is None:
|
|
|
|
return instance
|
|
|
|
|
|
|
|
# Replace links to hashtags in content with local URLs
|
|
|
|
changed_content = False
|
|
|
|
for hashtag in instance.mention_hashtags.all():
|
|
|
|
updated_content = re.sub(
|
2023-02-18 00:18:32 +01:00
|
|
|
rf'(<a href=")[^"]*(" data-mention="hashtag">{hashtag.name}</a>)',
|
2023-02-17 19:24:42 +01:00
|
|
|
rf"\1{hashtag.remote_id}\2",
|
|
|
|
instance.content,
|
2023-02-21 17:17:53 +01:00
|
|
|
flags=re.IGNORECASE,
|
2023-02-17 19:24:42 +01:00
|
|
|
)
|
|
|
|
if instance.content != updated_content:
|
|
|
|
instance.content = updated_content
|
|
|
|
changed_content = True
|
|
|
|
|
|
|
|
if not save or not changed_content:
|
|
|
|
return instance
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
try:
|
|
|
|
instance.save(broadcast=False, update_fields=["content"])
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise ActivitySerializerError(e)
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
2020-09-17 13:02:52 -07:00
|
|
|
|
|
|
|
@dataclass(init=False)
|
|
|
|
class Article(Note):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""what's an article except a note with more fields"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2020-09-17 13:02:52 -07:00
|
|
|
name: str
|
2021-03-08 08:49:10 -08:00
|
|
|
type: str = "Article"
|
2020-09-17 13:02:52 -07:00
|
|
|
|
|
|
|
|
2020-09-28 17:26:15 -07:00
|
|
|
@dataclass(init=False)
|
|
|
|
class GeneratedNote(Note):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""just a re-typed note"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
|
|
|
type: str = "GeneratedNote"
|
2020-09-28 17:26:15 -07:00
|
|
|
|
|
|
|
|
2021-06-18 14:29:24 -07:00
|
|
|
# pylint: disable=invalid-name
|
2020-09-17 13:02:52 -07:00
|
|
|
@dataclass(init=False)
|
|
|
|
class Comment(Note):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""like a note but with a book"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2020-09-17 13:02:52 -07:00
|
|
|
inReplyToBook: str
|
2021-08-16 13:59:15 -07:00
|
|
|
readingStatus: str = None
|
|
|
|
progress: int = None
|
|
|
|
progressMode: str = None
|
2021-03-08 08:49:10 -08:00
|
|
|
type: str = "Comment"
|
2020-09-17 13:02:52 -07:00
|
|
|
|
|
|
|
|
2021-01-01 11:05:49 -08:00
|
|
|
@dataclass(init=False)
|
|
|
|
class Quotation(Comment):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""a quote and commentary on a book"""
|
2021-03-08 09:48:25 -08:00
|
|
|
|
2021-01-01 11:05:49 -08:00
|
|
|
quote: str
|
2021-09-05 16:00:40 -07:00
|
|
|
position: int = None
|
|
|
|
positionMode: str = None
|
2021-03-08 09:48:25 -08:00
|
|
|
type: str = "Quotation"
|
2021-01-01 11:05:49 -08:00
|
|
|
|
|
|
|
|
2020-09-17 13:02:52 -07:00
|
|
|
@dataclass(init=False)
|
|
|
|
class Review(Comment):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""a full book review"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2020-12-17 13:21:21 -08:00
|
|
|
name: str = None
|
2020-12-16 20:10:50 -08:00
|
|
|
rating: int = None
|
2021-03-08 08:49:10 -08:00
|
|
|
type: str = "Review"
|
2020-09-17 13:02:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(init=False)
|
2021-01-01 11:05:49 -08:00
|
|
|
class Rating(Comment):
|
2021-04-26 09:15:42 -07:00
|
|
|
"""just a star rating"""
|
2021-03-08 08:49:10 -08:00
|
|
|
|
2021-02-25 10:17:24 -08:00
|
|
|
rating: int
|
2021-01-01 11:05:49 -08:00
|
|
|
content: str = None
|
2021-04-29 15:16:51 -07:00
|
|
|
name: str = None # not used, but the model inherits from Review
|
2021-03-08 09:48:25 -08:00
|
|
|
type: str = "Rating"
|