From 4ab2c6f40523fa774d866072b065d8d717e6a363 Mon Sep 17 00:00:00 2001 From: drew2a Date: Tue, 30 May 2023 16:18:11 +0200 Subject: [PATCH] Add `CONTENT_ITEM` relation --- .../components/knowledge/db/knowledge_db.py | 1 + .../knowledge/db/tests/test_knowledge_db.py | 24 ++++++++++--------- .../rules/knowledge_rules_processor.py | 5 ++-- .../metadata_store/restapi/search_endpoint.py | 2 +- src/tribler/gui/dialogs/editmetadatadialog.py | 8 ++++++- .../gui/qt_resources/edit_metadata_dialog.ui | 2 +- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/tribler/core/components/knowledge/db/knowledge_db.py b/src/tribler/core/components/knowledge/db/knowledge_db.py index b7adbeced68..b6497147af6 100644 --- a/src/tribler/core/components/knowledge/db/knowledge_db.py +++ b/src/tribler/core/components/knowledge/db/knowledge_db.py @@ -51,6 +51,7 @@ class ResourceType(IntEnum): # this is a section for extra types TAG = 101 TORRENT = 102 + CONTENT_ITEM = 103 @dataclass diff --git a/src/tribler/core/components/knowledge/db/tests/test_knowledge_db.py b/src/tribler/core/components/knowledge/db/tests/test_knowledge_db.py index 471ce15d215..bc1b2fbd663 100644 --- a/src/tribler/core/components/knowledge/db/tests/test_knowledge_db.py +++ b/src/tribler/core/components/knowledge/db/tests/test_knowledge_db.py @@ -539,24 +539,26 @@ def test_get_subjects(self): self.db, { 'infohash1': [ - Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True), + Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True), Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True), ], 'infohash2': [ - Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True), + Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True), Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True), ], 'infohash3': [ - Resource(predicate=ResourceType.TITLE, name='debian', auto_generated=True), + Resource(predicate=ResourceType.CONTENT_ITEM, name='debian', auto_generated=True), Resource(predicate=ResourceType.TAG, name='linux', auto_generated=True), ], } ) - actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TITLE, obj='missed') + actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.CONTENT_ITEM, + obj='missed') assert actual == [] - actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TITLE, obj='ubuntu') + actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.CONTENT_ITEM, + obj='ubuntu') assert actual == ['infohash1', 'infohash2'] actual = self.db.get_subjects(subject_type=ResourceType.TORRENT, predicate=ResourceType.TAG, obj='linux') @@ -568,11 +570,11 @@ def test_get_statements(self): self.db, { 'infohash1': [ - Resource(predicate=ResourceType.TITLE, name='ubuntu', auto_generated=True), + Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu', auto_generated=True), Resource(predicate=ResourceType.TYPE, name='linux', auto_generated=True), ], 'infohash2': [ - Resource(predicate=ResourceType.TITLE, name='debian', auto_generated=True), + Resource(predicate=ResourceType.CONTENT_ITEM, name='debian', auto_generated=True), Resource(predicate=ResourceType.TYPE, name='linux', auto_generated=True), ], 'INFOHASH1': [ @@ -582,7 +584,7 @@ def test_get_statements(self): ) expected = [ - SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.TITLE, + SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.CONTENT_ITEM, object='ubuntu'), SimpleStatement(subject_type=ResourceType.TORRENT, subject='infohash1', predicate=ResourceType.TYPE, object='linux') @@ -600,11 +602,11 @@ def test_various_queries(self): self.db, { 'infohash1': [ - Resource(predicate=ResourceType.TITLE, name='ubuntu'), + Resource(predicate=ResourceType.CONTENT_ITEM, name='ubuntu'), Resource(predicate=ResourceType.TYPE, name='linux'), ], 'infohash2': [ - Resource(predicate=ResourceType.TITLE, name='debian'), + Resource(predicate=ResourceType.CONTENT_ITEM, name='debian'), Resource(predicate=ResourceType.TYPE, name='linux'), ], 'infohash3': [ @@ -632,7 +634,7 @@ def _subjects(subject_type=None, obj='', predicate=None): assert _subjects(obj='linux') == {'infohash1', 'infohash2', 'infohash3'} assert _subjects(predicate=ResourceType.TAG, obj='linux') == {'infohash3'} - assert _subjects(predicate=ResourceType.TITLE) == {'infohash1', 'infohash2'} + assert _subjects(predicate=ResourceType.CONTENT_ITEM) == {'infohash1', 'infohash2'} @db_session def test_non_existent_misc(self): diff --git a/src/tribler/core/components/knowledge/rules/knowledge_rules_processor.py b/src/tribler/core/components/knowledge/rules/knowledge_rules_processor.py index 6c8b55288cb..5f6f018fad1 100644 --- a/src/tribler/core/components/knowledge/rules/knowledge_rules_processor.py +++ b/src/tribler/core/components/knowledge/rules/knowledge_rules_processor.py @@ -1,7 +1,6 @@ import logging import queue import time -from asyncio import Event from dataclasses import dataclass from typing import Optional, Set @@ -187,8 +186,8 @@ async def process_torrent_title(self, infohash: Optional[bytes] = None, title: O objects=tags) if content_items := set(extract_only_valid_tags(title, rules=content_items_rules)): - self.save_statements(subject_type=ResourceType.TORRENT, subject=infohash_str, predicate=ResourceType.TITLE, - objects=content_items) + self.save_statements(subject_type=ResourceType.TORRENT, subject=infohash_str, + predicate=ResourceType.CONTENT_ITEM, objects=content_items) return len(tags) + len(content_items) diff --git a/src/tribler/core/components/metadata_store/restapi/search_endpoint.py b/src/tribler/core/components/metadata_store/restapi/search_endpoint.py index dfbaef20d45..a1bbf3dc670 100644 --- a/src/tribler/core/components/metadata_store/restapi/search_endpoint.py +++ b/src/tribler/core/components/metadata_store/restapi/search_endpoint.py @@ -50,7 +50,7 @@ def build_snippets(self, search_results: List[Dict]) -> List[Dict]: with db_session: content_items: List[str] = self.knowledge_db.get_objects(subject_type=ResourceType.TORRENT, subject=search_result["infohash"], - predicate=ResourceType.TITLE) + predicate=ResourceType.CONTENT_ITEM) if content_items: for content_id in content_items: content_to_torrents[content_id].append(search_result) diff --git a/src/tribler/gui/dialogs/editmetadatadialog.py b/src/tribler/gui/dialogs/editmetadatadialog.py index b809606c251..ce098bc1c0e 100644 --- a/src/tribler/gui/dialogs/editmetadatadialog.py +++ b/src/tribler/gui/dialogs/editmetadatadialog.py @@ -5,6 +5,7 @@ from PyQt5.QtCore import QModelIndex, QPoint, Qt, pyqtSignal from PyQt5.QtWidgets import QComboBox, QSizePolicy, QWidget +from tribler.core.components.knowledge.community.knowledge_validator import is_valid_resource from tribler.core.components.knowledge.db.knowledge_db import ResourceType from tribler.core.components.knowledge.knowledge_constants import MAX_RESOURCE_LENGTH, MIN_RESOURCE_LENGTH from tribler.core.utilities.path_util import Path @@ -14,7 +15,12 @@ from tribler.gui.utilities import connect, get_objects_with_predicate, get_ui_file_path, tr from tribler.gui.widgets.tagbutton import TagButton -METADATA_TABLE_PREDICATES = [ResourceType.TITLE, ResourceType.DESCRIPTION, ResourceType.DATE, ResourceType.LANGUAGE] +METADATA_TABLE_PREDICATES = [ + ResourceType.CONTENT_ITEM, + ResourceType.DESCRIPTION, + ResourceType.DATE, + ResourceType.LANGUAGE +] class EditMetadataDialog(DialogContainer): diff --git a/src/tribler/gui/qt_resources/edit_metadata_dialog.ui b/src/tribler/gui/qt_resources/edit_metadata_dialog.ui index ab593852982..9fc177b6689 100644 --- a/src/tribler/gui/qt_resources/edit_metadata_dialog.ui +++ b/src/tribler/gui/qt_resources/edit_metadata_dialog.ui @@ -276,7 +276,7 @@ margin: 0px; - Title + Content Item