-
Notifications
You must be signed in to change notification settings - Fork 4
feat: refactor item metadata database #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f8cf59
feat: refactor MongoDB ODM
aturret 0ef42e8
feat: add cache system
aturret 907357c
feat: add tgbot force refresh caching settings
aturret 79182b3
doc: update CLAUDE.md
aturret d170784
feat: add unit tests
aturret 3ce3757
feat: update env and docker templates
aturret 7e9d1f0
fix: fix issues raised by coder rabbit
aturret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,14 @@ | ||
| from typing import Optional, Union, List | ||
|
|
||
| from motor.motor_asyncio import AsyncIOMotorClient | ||
| from beanie import init_beanie, Document, Indexed | ||
|
|
||
| from fastfetchbot_shared.database.mongodb import ( | ||
| init_mongodb, | ||
| close_mongodb, | ||
| save_instances, | ||
| ) | ||
| from src.config import settings | ||
| from src.models.database_model import document_list | ||
| from fastfetchbot_shared.utils.logger import logger | ||
|
|
||
|
|
||
| async def startup() -> None: | ||
| client = AsyncIOMotorClient(settings.MONGODB_URL) | ||
| await init_beanie(database=client["telegram_bot"], document_models=document_list) | ||
| await init_mongodb(settings.MONGODB_URL) | ||
|
|
||
|
|
||
| async def shutdown() -> None: | ||
| pass | ||
|
|
||
|
|
||
| async def save_instances(instances: Union[Document, List[Document]], *args) -> None: | ||
| if instances is None: | ||
| raise TypeError("instances must be a Model or a list of Model") | ||
|
|
||
| if isinstance(instances, Document): | ||
| instance_type = type(instances) | ||
| await instance_type.insert(instances) | ||
| elif isinstance(instances, list): | ||
| instance_type = type(instances[0]) | ||
| await instance_type.insert_many(instances) | ||
| else: | ||
| raise TypeError("instances must be a Model or a list of Model") | ||
|
|
||
| for arg in args: | ||
| if not isinstance(arg, Document): | ||
| raise TypeError("args must be a Model") | ||
| instance_type = type(arg) | ||
| await instance_type.insert_one(arg) | ||
| await close_mongodb() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,6 @@ | ||
| from typing import Optional, Any | ||
| from datetime import datetime | ||
| from fastfetchbot_shared.database.mongodb.models.metadata import ( | ||
| Metadata, | ||
| document_list, | ||
| ) | ||
|
|
||
| from pydantic import BaseModel, Field | ||
| from beanie import Document, Indexed, Insert, after_event, before_event | ||
|
|
||
| from fastfetchbot_shared.models.metadata_item import MediaFile, MessageType | ||
| from fastfetchbot_shared.utils.logger import logger | ||
| from fastfetchbot_shared.utils.parse import get_html_text_length | ||
|
|
||
|
|
||
| class Metadata(Document): | ||
| title: str = Field(default="untitled") | ||
| message_type: MessageType = MessageType.SHORT | ||
| url: str | ||
| author: Optional[str] = None | ||
| author_url: Optional[str] = None | ||
| text: Optional[str] = None | ||
| text_length: Optional[int] = Field(ge=0) | ||
| content: Optional[str] = None | ||
| content_length: Optional[int] = Field(ge=0) | ||
| category: Optional[str] = None | ||
| source: Optional[str] = None | ||
| media_files: Optional[list[MediaFile]] = None | ||
| telegraph_url: Optional[str] = None | ||
| timestamp: datetime = Field(default_factory=datetime.utcnow) | ||
| scrape_status: bool = False | ||
|
|
||
| @before_event(Insert) | ||
| def get_text_length(self): | ||
| self.text_length = get_html_text_length(self.text) | ||
| self.content_length = get_html_text_length(self.content) | ||
|
|
||
| # | ||
| @staticmethod | ||
| def from_dict(obj: Any) -> "Metadata": | ||
| assert isinstance(obj, dict) | ||
| return Metadata(**obj) | ||
|
|
||
|
|
||
| document_list = [Metadata] | ||
| __all__ = ["Metadata", "document_list"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Do MongoDB connection string usernames and passwords need percent-encoding for reserved URI characters such as @, :, /, and %?💡 Result:
Yes, MongoDB connection string usernames and passwords need percent-encoding for reserved URI characters such as @ (%40), : (%3A), / (%2F), and % (%25). Official MongoDB documentation explicitly states that if the username or password includes characters like $ : / ? # [ ] @, they must be converted using percent encoding. Examples include D1fficultP%40ssw0rd for a password containing @, and %2Ftmp%2Fmongodb-27017.sock for a UNIX socket path with /. This applies to both mongodb:// and mongodb+srv:// formats, and is confirmed across multiple MongoDB docs versions and community examples.
Citations:
🏁 Script executed:
Repository: aturret/FastFetchBot
Length of output: 679
🏁 Script executed:
Repository: aturret/FastFetchBot
Length of output: 1217
🏁 Script executed:
Repository: aturret/FastFetchBot
Length of output: 3748
Percent-encode MongoDB credentials in the connection string.
Raw interpolation of
MONGODB_USERNAMEandMONGODB_PASSWORDinto the connection string will fail if credentials contain reserved URI characters like@,:,/, or%. MongoDB URIs require these characters to be percent-encoded. The URL is passed directly toAsyncIOMotorClient()at line 14 ofpackages/shared/fastfetchbot_shared/database/mongodb/connection.py, so invalid URIs will cause connection failures.Suggested fix
Add the import:
Update the URL construction:
🤖 Prompt for AI Agents