Skip to content

Commit

Permalink
Rename ConversationTag to Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Saluev committed Dec 26, 2023
1 parent 55c6a04 commit f9cd60c
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 95 deletions.
2 changes: 1 addition & 1 deletion docs/development/core_entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ implementing your own Suppgram frontend, you may need to extend these classes wi
show_if_no_docstring: true
heading_level: 3

::: suppgram.entities.ConversationTag
::: suppgram.entities.Tag
handler: python
options:
show_root_heading: true
Expand Down
10 changes: 5 additions & 5 deletions suppgram/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Workplace,
Message,
CustomerDiff,
ConversationTag,
Tag,
ConversationTagEvent,
Customer,
TagEvent,
Expand Down Expand Up @@ -70,11 +70,11 @@ async def identify_workplace(self, identification: WorkplaceIdentification) -> W
pass

@abc.abstractmethod
async def create_tag(self, name: str, created_by: Agent) -> ConversationTag:
async def create_tag(self, name: str, created_by: Agent) -> Tag:
pass

@abc.abstractmethod
async def get_all_tags(self) -> List[ConversationTag]:
async def get_all_tags(self) -> List[Tag]:
pass

@abc.abstractmethod
Expand Down Expand Up @@ -105,11 +105,11 @@ async def get_customer_conversations(self, customer: Customer) -> List[Conversat
pass

@abc.abstractmethod
async def add_tag_to_conversation(self, conversation: Conversation, tag: ConversationTag):
async def add_tag_to_conversation(self, conversation: Conversation, tag: Tag):
pass

@abc.abstractmethod
async def remove_tag_from_conversation(self, conversation: Conversation, tag: ConversationTag):
async def remove_tag_from_conversation(self, conversation: Conversation, tag: Tag):
pass

@abc.abstractmethod
Expand Down
10 changes: 5 additions & 5 deletions suppgram/backends/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
SetNone,
MessageKind,
CustomerDiff,
ConversationTag,
Tag,
ConversationTagEvent,
Customer,
TagEvent,
Expand Down Expand Up @@ -94,15 +94,15 @@ async def identify_customer_conversation(
async def identify_workplace(self, identification: WorkplaceIdentification) -> Workplace:
return await self._storage.get_or_create_workplace(identification)

async def create_tag(self, name: str, created_by: Agent) -> ConversationTag:
async def create_tag(self, name: str, created_by: Agent) -> Tag:
if created_by.deactivated:
raise AgentDeactivated(created_by.identification)

tag = await self._storage.create_tag(name=name, created_by=created_by)
await self.on_tag_created.trigger(TagEvent(tag=tag))
return tag

async def get_all_tags(self) -> List[ConversationTag]:
async def get_all_tags(self) -> List[Tag]:
return await self._storage.find_all_tags()

async def identify_agent_conversation(
Expand Down Expand Up @@ -192,14 +192,14 @@ async def get_conversations(
async def get_customer_conversations(self, customer: Customer) -> List[Conversation]:
return await self._storage.find_customer_conversations(customer, with_messages=True)

async def add_tag_to_conversation(self, conversation: Conversation, tag: ConversationTag):
async def add_tag_to_conversation(self, conversation: Conversation, tag: Tag):
await self._storage.update_conversation(conversation.id, ConversationDiff(added_tags=[tag]))
conversation = await self.get_conversation(conversation.id)
await self.on_conversation_tag_added.trigger(
ConversationTagEvent(conversation=conversation, tag=tag)
)

async def remove_tag_from_conversation(self, conversation: Conversation, tag: ConversationTag):
async def remove_tag_from_conversation(self, conversation: Conversation, tag: Tag):
await self._storage.update_conversation(
conversation.id, ConversationDiff(removed_tags=[tag])
)
Expand Down
12 changes: 6 additions & 6 deletions suppgram/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ConversationState(str, Enum):


@dataclass(frozen=True)
class ConversationTag:
class Tag:
"""
Describes tag that can be used to label a [conversation][suppgram.entities.Conversation].
Expand Down Expand Up @@ -271,7 +271,7 @@ class Conversation:
id: Any
state: ConversationState
customer: Customer
tags: List[ConversationTag]
tags: List[Tag]
assigned_agent: Optional[Agent] = None
assigned_workplace: Optional[Workplace] = None
messages: List[Message] = field(default_factory=list)
Expand All @@ -295,8 +295,8 @@ class ConversationDiff:

state: Optional[ConversationState] = None
assigned_workplace_id: Union[Optional[Any], _SetNone] = None
added_tags: Optional[List[ConversationTag]] = None
removed_tags: Optional[List[ConversationTag]] = None
added_tags: Optional[List[Tag]] = None
removed_tags: Optional[List[Tag]] = None
customer_rating: Optional[int] = None

@property
Expand All @@ -314,7 +314,7 @@ class ConversationEvent:
@dataclass(frozen=True)
class ConversationTagEvent:
conversation: Conversation
tag: ConversationTag
tag: Tag


@dataclass(frozen=True)
Expand All @@ -339,4 +339,4 @@ class NewMessageForAgentEvent:

@dataclass(frozen=True)
class TagEvent:
tag: ConversationTag
tag: Tag
14 changes: 5 additions & 9 deletions suppgram/frontends/telegram/manager_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
NewUnassignedMessageFromCustomerEvent,
ConversationEvent,
ConversationState,
ConversationTag,
Tag,
ConversationTagEvent,
TagEvent,
)
Expand Down Expand Up @@ -185,7 +185,7 @@ async def _send_new_conversation_notification(
self,
group: TelegramChat,
conversation: Conversation,
all_tags: List[ConversationTag],
all_tags: List[Tag],
):
message = await self._send_placeholder_message(
group,
Expand Down Expand Up @@ -275,7 +275,7 @@ async def _update_new_conversation_notification(
self,
message: TelegramMessage,
conversation: Conversation,
all_tags: List[ConversationTag],
all_tags: List[Tag],
keyboard_only: bool = False,
):
present_tag_ids = {tag.id for tag in conversation.tags}
Expand Down Expand Up @@ -327,19 +327,15 @@ def _make_assign_to_me_button(self, conversation: Conversation) -> InlineKeyboar
),
)

def _make_add_tag_button(
self, conversation: Conversation, tag: ConversationTag
) -> InlineKeyboardButton:
def _make_add_tag_button(self, conversation: Conversation, tag: Tag) -> InlineKeyboardButton:
return InlineKeyboardButton(
text=self._texts.compose_add_tag_button_text(tag),
callback_data=encode_callback_data(
{"a": CallbackActionKind.ADD_CONVERSATION_TAG, "c": conversation.id, "t": tag.id}
),
)

def _make_remove_tag_button(
self, conversation: Conversation, tag: ConversationTag
) -> InlineKeyboardButton:
def _make_remove_tag_button(self, conversation: Conversation, tag: Tag) -> InlineKeyboardButton:
return InlineKeyboardButton(
text=self._texts.compose_remove_tag_button_text(tag),
callback_data=encode_callback_data(
Expand Down
6 changes: 3 additions & 3 deletions suppgram/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Message,
ConversationDiff,
CustomerDiff,
ConversationTag,
Tag,
)


Expand Down Expand Up @@ -70,11 +70,11 @@ async def get_or_create_workplace(self, identification: WorkplaceIdentification)
pass

@abc.abstractmethod
async def create_tag(self, name: str, created_by: Agent) -> ConversationTag:
async def create_tag(self, name: str, created_by: Agent) -> Tag:
pass

@abc.abstractmethod
async def find_all_tags(self) -> List[ConversationTag]:
async def find_all_tags(self) -> List[Tag]:
pass

@abc.abstractmethod
Expand Down
10 changes: 5 additions & 5 deletions suppgram/storages/inmemory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Customer,
WorkplaceIdentification,
ConversationDiff,
ConversationTag,
Tag,
Workplace,
AgentIdentification,
AgentDiff,
Expand All @@ -37,7 +37,7 @@ def __init__(self) -> None:
self.customers: List[Customer] = []
self.agents: List[Agent] = []
self.workplaces: List[Workplace] = []
self.tags: List[ConversationTag] = []
self.tags: List[Tag] = []
self.conversations: List[Conversation] = []

async def create_or_update_customer(
Expand Down Expand Up @@ -100,16 +100,16 @@ async def get_or_create_workplace(self, identification: WorkplaceIdentification)
self.workplaces.append(workplace)
return workplace

async def create_tag(self, name: str, created_by: Agent) -> ConversationTag:
async def create_tag(self, name: str, created_by: Agent) -> Tag:
if any(t.name == name for t in self.tags):
raise TagAlreadyExists(name)
tag = ConversationTag(
tag = Tag(
id=name, name=name, created_at_utc=datetime.now(timezone.utc), created_by=created_by
)
self.tags.append(tag)
return tag

async def find_all_tags(self) -> List[ConversationTag]:
async def find_all_tags(self) -> List[Tag]:
return [*self.tags]

async def get_or_create_conversation(self, customer: Customer) -> Conversation:
Expand Down
16 changes: 7 additions & 9 deletions suppgram/storages/mongodb/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
AgentDiff,
WorkplaceIdentification,
Workplace,
ConversationTag,
Tag,
CustomerIdentification,
CustomerDiff,
Customer,
Expand Down Expand Up @@ -68,7 +68,7 @@ def __init__(
customer_collection_name: str = "suppgram_customers",
agent_collection_name: str = "suppgram_agents",
conversation_collection_name: str = "suppgram_conversations",
conversation_tag_collection_name: str = "suppgram_conversation_tags",
tag_collection_name: str = "suppgram_tags",
codec_options: CodecOptions = CodecOptions(
tz_aware=True, tzinfo=timezone.utc, uuid_representation=UuidRepresentation.STANDARD
),
Expand All @@ -79,7 +79,7 @@ def __init__(
customer_collection_name: name of MongoDB collection to store customers in
agent_collection_name: name of MongoDB collection to store agents and workplaces in
conversation_collection_name: name of MongoDB collection to store conversations and messages in
conversation_tag_collection_name: name of MongoDB collection to store conversation tags in
tag_collection_name: name of MongoDB collection to store tags in
"""
self.customer_collection = database.get_collection(customer_collection_name, codec_options)
self.agent_collection = database.get_collection(agent_collection_name, codec_options)
Expand All @@ -88,9 +88,7 @@ def __init__(
conversation_collection_name, codec_options
)
# Messages are stored within conversations.
self.conversation_tag_collection = database.get_collection(
conversation_tag_collection_name, codec_options
)
self.tag_collection = database.get_collection(tag_collection_name, codec_options)

def make_customer_filter(self, identification: CustomerIdentification) -> Document:
if identification.id is not None:
Expand Down Expand Up @@ -255,9 +253,9 @@ def convert_to_tag_document(self, name: str, created_by: Agent) -> Document:
def extract_agent_ids(self, tag_docs: List[Document]) -> Set[ObjectId]:
return {tag_doc["created_by"] for tag_doc in tag_docs}

def convert_to_tag(self, doc: Document, agents: Mapping[Any, Agent]) -> ConversationTag:
def convert_to_tag(self, doc: Document, agents: Mapping[Any, Agent]) -> Tag:
agent = agents[str(doc["created_by"])]
return ConversationTag(
return Tag(
id=doc["_id"],
name=doc["_id"],
created_at_utc=doc["created_at_utc"],
Expand Down Expand Up @@ -348,7 +346,7 @@ def convert_to_conversation(
doc: Document,
customers: Mapping[Any, Customer],
workplaces: Mapping[Any, Workplace],
tags: Mapping[Any, ConversationTag],
tags: Mapping[Any, Tag],
) -> Conversation:
assigned_workplace_id = doc.get("assigned_workplace_id")
assigned_workplace = (
Expand Down
10 changes: 5 additions & 5 deletions suppgram/storages/mongodb/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WorkplaceIdentification,
ConversationDiff,
Customer,
ConversationTag,
Tag,
Agent,
Workplace,
AgentIdentification,
Expand Down Expand Up @@ -127,16 +127,16 @@ async def get_or_create_workplace(self, identification: WorkplaceIdentification)
)
return self._collections.convert_to_workplace(identification, agent_doc)

async def create_tag(self, name: str, created_by: Agent) -> ConversationTag:
async def create_tag(self, name: str, created_by: Agent) -> Tag:
try:
doc = self._collections.convert_to_tag_document(name, created_by)
await self._collections.conversation_tag_collection.insert_one(doc)
await self._collections.tag_collection.insert_one(doc)
return self._collections.convert_to_tag(doc, {created_by.id: created_by})
except DuplicateKeyError as exc:
raise TagAlreadyExists(name) from exc

async def find_all_tags(self) -> List[ConversationTag]:
docs = await self._collections.conversation_tag_collection.find({}).to_list(None)
async def find_all_tags(self) -> List[Tag]:
docs = await self._collections.tag_collection.find({}).to_list(None)
agent_ids = self._collections.extract_agent_ids(docs)
filter_ = self._collections.make_agents_filter(agent_ids)
agents = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Initial revision
Revision ID: 78877fb69405
Revision ID: 48f9a7635310
Revises:
Create Date: 2023-12-27 00:06:23.214873
Create Date: 2023-12-27 01:35:29.099926
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "78877fb69405"
revision = "48f9a7635310"
down_revision = None
branch_labels = None
depends_on = None
Expand Down Expand Up @@ -50,7 +50,7 @@ def upgrade() -> None:
sa.PrimaryKeyConstraint("telegram_chat_id"),
)
op.create_table(
"suppgram_conversation_tags",
"suppgram_tags",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
Expand Down Expand Up @@ -114,16 +114,16 @@ def upgrade() -> None:
op.create_table(
"suppgram_conversation_tag_associations",
sa.Column("conversation_id", sa.Integer(), nullable=False),
sa.Column("conversation_tag_id", sa.Integer(), nullable=False),
sa.Column("tag_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["conversation_id"],
["suppgram_conversations.id"],
),
sa.ForeignKeyConstraint(
["conversation_tag_id"],
["suppgram_conversation_tags.id"],
["tag_id"],
["suppgram_tags.id"],
),
sa.PrimaryKeyConstraint("conversation_id", "conversation_tag_id"),
sa.PrimaryKeyConstraint("conversation_id", "tag_id"),
)
op.create_table(
"suppgram_telegram_messages",
Expand Down Expand Up @@ -174,7 +174,7 @@ def downgrade() -> None:
op.drop_table("suppgram_conversation_messages")
op.drop_table("suppgram_conversations")
op.drop_table("suppgram_workplaces")
op.drop_table("suppgram_conversation_tags")
op.drop_table("suppgram_tags")
op.drop_table("suppgram_telegram_chats")
op.drop_table("suppgram_customers")
op.drop_table("suppgram_agents")
Expand Down
Loading

0 comments on commit f9cd60c

Please sign in to comment.