|
| 1 | +from operator import or_ |
| 2 | +from typing import List, Optional |
| 3 | +from ..business_objects import general |
| 4 | +from ..session import session |
| 5 | +from ..models import ConversationShare, CognitionConversation |
| 6 | +from submodules.model.util import sql_alchemy_to_dict |
| 7 | + |
| 8 | + |
| 9 | +def get(share_id: str, user_id: str) -> Optional[ConversationShare]: |
| 10 | + return ( |
| 11 | + session.query(ConversationShare) |
| 12 | + .filter(ConversationShare.id == share_id) |
| 13 | + .filter( |
| 14 | + or_( |
| 15 | + ConversationShare.shared_by == user_id, |
| 16 | + ConversationShare.shared_with == user_id, |
| 17 | + ) |
| 18 | + ) |
| 19 | + .first() |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def get_all_by_conversation(conversation_id: str) -> List[ConversationShare]: |
| 24 | + return ( |
| 25 | + session.query(ConversationShare) |
| 26 | + .filter(ConversationShare.conversation_id == conversation_id) |
| 27 | + .all() |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def update_by_conversation( |
| 32 | + conversation_id: str, |
| 33 | + user_id: str, |
| 34 | + shared_with: List[str], |
| 35 | + can_copy: Optional[bool] = None, |
| 36 | + with_commit: bool = True, |
| 37 | +) -> List[ConversationShare]: |
| 38 | + existing_shares = ( |
| 39 | + session.query(ConversationShare) |
| 40 | + .filter(ConversationShare.conversation_id == conversation_id) |
| 41 | + .filter(ConversationShare.shared_by == user_id) |
| 42 | + .all() |
| 43 | + ) |
| 44 | + |
| 45 | + existing_shared_with = {share.shared_with: share for share in existing_shares} |
| 46 | + shared_with_set = set(shared_with) |
| 47 | + |
| 48 | + for share in existing_shares: |
| 49 | + if share.shared_with not in shared_with_set: |
| 50 | + general.delete(share, with_commit=False) |
| 51 | + |
| 52 | + for sharing_user_id in shared_with: |
| 53 | + if sharing_user_id not in existing_shared_with: |
| 54 | + share = ConversationShare( |
| 55 | + conversation_id=conversation_id, |
| 56 | + shared_with=sharing_user_id, |
| 57 | + shared_by=user_id, |
| 58 | + can_copy=can_copy if can_copy is not None else False, |
| 59 | + ) |
| 60 | + general.add(share, with_commit=False) |
| 61 | + else: |
| 62 | + if can_copy is not None: |
| 63 | + existing_shared_with[sharing_user_id].can_copy = can_copy |
| 64 | + |
| 65 | + general.flush_or_commit(with_commit) |
| 66 | + |
| 67 | + updated_shares = ( |
| 68 | + session.query(ConversationShare) |
| 69 | + .filter(ConversationShare.conversation_id == conversation_id) |
| 70 | + .filter(ConversationShare.shared_by == user_id) |
| 71 | + .all() |
| 72 | + ) |
| 73 | + return updated_shares |
| 74 | + |
| 75 | + |
| 76 | +def get_all_shared_by_or_for_user( |
| 77 | + project_id: str, user_id: str, with_header: bool = True |
| 78 | +) -> List[ConversationShare]: |
| 79 | + conversation_shares = ( |
| 80 | + session.query(ConversationShare, CognitionConversation.header) |
| 81 | + .join( |
| 82 | + CognitionConversation, |
| 83 | + ConversationShare.conversation_id == CognitionConversation.id, |
| 84 | + ) |
| 85 | + .filter(CognitionConversation.project_id == project_id) |
| 86 | + .filter( |
| 87 | + or_( |
| 88 | + ConversationShare.shared_by == user_id, |
| 89 | + ConversationShare.shared_with == user_id, |
| 90 | + ) |
| 91 | + ) |
| 92 | + .all() |
| 93 | + ) |
| 94 | + conversation_shares_dict = [] |
| 95 | + for share_obj, header in conversation_shares: |
| 96 | + share_dict = sql_alchemy_to_dict(share_obj) |
| 97 | + share_dict["conversation_header"] = header |
| 98 | + conversation_shares_dict.append(share_dict) |
| 99 | + |
| 100 | + return conversation_shares_dict |
| 101 | + |
| 102 | + |
| 103 | +def get_all_shared_by_user(user_id: str) -> List[ConversationShare]: |
| 104 | + return ( |
| 105 | + session.query(ConversationShare) |
| 106 | + .filter(ConversationShare.shared_by == user_id) |
| 107 | + .all() |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def create( |
| 112 | + conversation_id: str, |
| 113 | + shared_with: str, |
| 114 | + shared_by: str, |
| 115 | + can_copy: bool = False, |
| 116 | + with_commit: bool = True, |
| 117 | +) -> ConversationShare: |
| 118 | + share = ConversationShare( |
| 119 | + conversation_id=conversation_id, |
| 120 | + shared_with=shared_with, |
| 121 | + shared_by=shared_by, |
| 122 | + can_copy=can_copy, |
| 123 | + ) |
| 124 | + general.add(share, with_commit) |
| 125 | + return share |
| 126 | + |
| 127 | + |
| 128 | +def create_many( |
| 129 | + conversation_id: str, |
| 130 | + shared_with_user_ids: List[str], |
| 131 | + shared_by: str, |
| 132 | + can_copy: bool = False, |
| 133 | + with_commit: bool = True, |
| 134 | +) -> List[ConversationShare]: |
| 135 | + |
| 136 | + shares = [ |
| 137 | + ConversationShare( |
| 138 | + conversation_id=conversation_id, |
| 139 | + shared_with=user_id, |
| 140 | + shared_by=shared_by, |
| 141 | + can_copy=can_copy, |
| 142 | + ) |
| 143 | + for user_id in shared_with_user_ids |
| 144 | + ] |
| 145 | + general.add_all(shares, with_commit=True) |
| 146 | + return shares |
| 147 | + |
| 148 | + |
| 149 | +def update( |
| 150 | + share_id: str, |
| 151 | + user_id: str, |
| 152 | + can_copy: Optional[bool] = None, |
| 153 | + with_commit: bool = True, |
| 154 | +) -> Optional[ConversationShare]: |
| 155 | + share_entity = get(share_id) |
| 156 | + if share_entity is None: |
| 157 | + return None |
| 158 | + |
| 159 | + if str(share_entity.shared_by) != user_id: |
| 160 | + raise ValueError("You are not allowed to update this sharing context.") |
| 161 | + |
| 162 | + if can_copy is not None: |
| 163 | + share_entity.can_copy = can_copy |
| 164 | + |
| 165 | + general.flush_or_commit(with_commit) |
| 166 | + return share_entity |
| 167 | + |
| 168 | + |
| 169 | +def delete_shared_with( |
| 170 | + conversation_share_id: str, user_id: str, with_commit: bool = True |
| 171 | +) -> None: |
| 172 | + session.query(ConversationShare).filter( |
| 173 | + ConversationShare.id == conversation_share_id |
| 174 | + ).filter(ConversationShare.shared_with == user_id).delete() |
| 175 | + general.flush_or_commit(with_commit) |
| 176 | + |
| 177 | + |
| 178 | +def delete_shared_by_by_conversation_id( |
| 179 | + conversation_id: str, user_id: str, with_commit: bool = True |
| 180 | +) -> None: |
| 181 | + session.query(ConversationShare).filter( |
| 182 | + ConversationShare.conversation_id == conversation_id |
| 183 | + ).filter(ConversationShare.shared_by == user_id).delete() |
| 184 | + general.flush_or_commit(with_commit) |
0 commit comments