Coverage for functions \ flipdare \ search \ factory \ friend_search_factory.py: 94%
32 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
1#!/usr/bin/env python
2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved.
3#
4# This file is part of Flipdare's proprietary software and contains
5# confidential and copyrighted material. Unauthorised copying,
6# modification, distribution, or use of this file is strictly
7# prohibited without prior written permission from Flipdare Pty Ltd.
8#
9# This software includes third-party components licensed under MIT,
10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details.
11#
14from typing import Any, override
15from flipdare.core.tokenizer import Tokenizer
16from flipdare.firestore.context.friend_context import FriendContext
17from flipdare.generated.shared.search.friend_document_type import FriendDocumentType
18from flipdare.generated.shared.search.search_obj_type import SearchObjType
19from flipdare.search.doc._search_document import SearchDocument
20from flipdare.search.doc.friend_document import FriendDocument
21from flipdare.search.factory._search_document_factory import SearchDocumentFactory
22from flipdare.util.time_util import TypesenseTime
24__all__ = ["FriendSearchFactory"]
27class FriendSearchFactory(SearchDocumentFactory):
29 def __init__(self, friend_context: FriendContext, tokenizer: Tokenizer | None = None) -> None:
30 if not friend_context.valid:
31 raise ValueError(
32 f"FriendContext must be valid to create FriendModelTag.\n{friend_context.error_str}",
33 )
34 self.friend_context = friend_context
35 super().__init__(tokenizer=tokenizer)
37 @property
38 @override
39 def obj_type(self) -> SearchObjType:
40 return SearchObjType.FRIEND
42 @override
43 def get_documents(self) -> list[SearchDocument[Any]] | None:
44 # for friends we create a bi-directional entry
45 # where
46 # obj_id = user id
47 # friend_values = user.safe_name
48 # search_type = SearchType.USER
49 friend_context = self.friend_context
50 from_uid = friend_context.from_user.doc_id
51 to_uid = friend_context.to_user.doc_id
52 from_keywords = friend_context.from_user.model.searchable_names
53 to_keywords = friend_context.to_user.model.searchable_names
55 created_at = TypesenseTime.from_firestore(friend_context.friend.created_at_db)
56 updated_at = TypesenseTime.from_firestore(friend_context.friend.updated_at_db)
58 forward = FriendDocument.create(
59 uid=from_uid,
60 friend_type=FriendDocumentType.FRIEND,
61 keywords=from_keywords,
62 friend_uid=to_uid,
63 friend_keywords=to_keywords,
64 created_at=created_at,
65 updated_at=updated_at,
66 )
68 reverse = FriendDocument.create(
69 uid=to_uid,
70 friend_type=FriendDocumentType.FRIEND,
71 keywords=to_keywords,
72 friend_uid=from_uid,
73 friend_keywords=from_keywords,
74 created_at=created_at,
75 updated_at=updated_at,
76 )
78 return [forward, reverse]