Index hashtags and mentions

This commit is contained in:
Thomas Sileo
2019-09-01 20:58:51 +02:00
parent 36bc93cfda
commit 52bc600832
5 changed files with 66 additions and 16 deletions

View File

@@ -128,9 +128,20 @@ def save(box: Box, activity: ap.BaseActivity) -> None:
actor_id = activity.get_actor().id
# Set some "type"-related neta
extra = {}
if box == Box.OUTBOX and activity.has_type(ap.Follow):
extra: Dict[str, Any] = {}
if box == Box.OUTBOX and activity.has_type(ap.ActivityType.FOLLOW):
extra[MetaKey.FOLLOW_STATUS.value] = FollowStatus.WAITING.value
elif activity.has_type(ap.ActivityType.CREATE):
mentions = []
obj = activity.get_object()
for m in obj.get_mentions():
mentions.append(m.href)
hashtags = []
for h in obj.get_hashtags():
hashtags.append(h.name[1:]) # Strip the #
extra.update(
{MetaKey.MENTIONS.value: mentions, MetaKey.HASHTAGS.value: hashtags}
)
DB.activities.insert_one(
{

View File

@@ -26,6 +26,8 @@ def create_indexes():
DB.activities.create_index([("remote_id", pymongo.ASCENDING)])
DB.activities.create_index([("meta.actor_id", pymongo.ASCENDING)])
DB.activities.create_index([("meta.object_id", pymongo.ASCENDING)])
DB.activities.create_index([("meta.mentions", pymongo.ASCENDING)])
DB.activities.create_index([("meta.hashtags", pymongo.ASCENDING)])
DB.activities.create_index([("meta.thread_root_parent", pymongo.ASCENDING)])
DB.activities.create_index(
[

View File

@@ -46,6 +46,9 @@ class MetaKey(Enum):
OBJECT_ACTOR_HASH = "object_actor_hash"
PUBLIC = "public"
HASHTAGS = "hashtags"
MENTIONS = "mentions"
FOLLOW_STATUS = "follow_status"
THREAD_ROOT_PARENT = "thread_root_parent"
@@ -121,6 +124,14 @@ def is_public() -> _SubQuery:
return flag(MetaKey.PUBLIC, True)
def by_visibility(vis: ap.Visibility) -> _SubQuery:
return flag(MetaKey.VISIBILITY, vis.name)
def by_hashtag(ht: str) -> _SubQuery:
return flag(MetaKey.HASHTAGS, ht)
def inc(mk: MetaKey, val: int) -> _SubQuery:
return {"$inc": flag(mk, val)}

View File

@@ -19,6 +19,7 @@ from core.meta import by_remote_id
from core.meta import by_type
from core.meta import in_inbox
from core.meta import in_outbox
from core.meta import not_deleted
from core.meta import not_undo
from core.meta import upsert
from utils.migrations import Migration
@@ -293,3 +294,27 @@ class _20190901_FollowFollowBackMigrationFix(Migration):
except Exception:
logger.exception(f"failed to process activity {data!r}")
class _20190901_MetaHashtagsAndMentions(Migration):
def migrate(self) -> None:
for data in find_activities(
{**by_type(ap.ActivityType.CREATE), **not_deleted()}
):
try:
activity = ap.parse_activity(data["activity"])
mentions = []
obj = activity.get_object()
for m in obj.get_mentions():
mentions.append(m.href)
hashtags = []
for h in obj.get_hashtags():
hashtags.append(h.name[1:]) # Strip the #
update_one_activity(
by_remote_id(data["remote_id"]),
upsert({MetaKey.MENTIONS: mentions, MetaKey.HASHTAGS: hashtags}),
)
except Exception:
logger.exception(f"failed to process activity {data!r}")