Merge pull request #2524 from chdorner/feature/tag-support
Initial hashtag support
This commit is contained in:
commit
12af5992a3
20 changed files with 635 additions and 13 deletions
|
@ -3,7 +3,7 @@ import inspect
|
|||
import sys
|
||||
|
||||
from .base_activity import ActivityEncoder, Signature, naive_parse
|
||||
from .base_activity import Link, Mention
|
||||
from .base_activity import Link, Mention, Hashtag
|
||||
from .base_activity import ActivitySerializerError, resolve_remote_id
|
||||
from .image import Document, Image
|
||||
from .note import Note, GeneratedNote, Article, Comment, Quotation
|
||||
|
|
|
@ -186,7 +186,7 @@ class ActivityObject:
|
|||
|
||||
# add many to many fields, which have to be set post-save
|
||||
for field in instance.many_to_many_fields:
|
||||
# mention books/users, for example
|
||||
# mention books/users/hashtags, for example
|
||||
field.set_field_from_activity(
|
||||
instance,
|
||||
self,
|
||||
|
@ -426,3 +426,10 @@ class Mention(Link):
|
|||
"""a subtype of Link for mentioning an actor"""
|
||||
|
||||
type: str = "Mention"
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Hashtag(Link):
|
||||
"""a subtype of Link for mentioning a hashtag"""
|
||||
|
||||
type: str = "Hashtag"
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
""" note serializer and children thereof """
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List
|
||||
from django.apps import apps
|
||||
import re
|
||||
|
||||
from .base_activity import ActivityObject, Link
|
||||
from django.apps import apps
|
||||
from django.db import IntegrityError, transaction
|
||||
|
||||
from .base_activity import ActivityObject, ActivitySerializerError, Link
|
||||
from .image import Document
|
||||
|
||||
|
||||
|
@ -38,6 +41,47 @@ class Note(ActivityObject):
|
|||
updated: str = None
|
||||
type: str = "Note"
|
||||
|
||||
# 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(
|
||||
rf'(<a href=")[^"]*(" data-mention="hashtag">{hashtag.name}</a>)',
|
||||
rf"\1{hashtag.remote_id}\2",
|
||||
instance.content,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Article(Note):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue