Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and borisevich-a-v committed Dec 25, 2024
1 parent 0cfdae3 commit 30a0977
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
76 changes: 43 additions & 33 deletions alembic/versions/5449fbd7e244_init_v2.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,66 @@
"""init_v2
Revision ID: 5449fbd7e244
Revises:
Revises:
Create Date: 2024-12-25 14:23:56.622802
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from aggregator.models import NOT_SPECIFIED_CHANNEL_TYPE
from alembic import op

# revision identifiers, used by Alembic.
revision: str = '5449fbd7e244'
revision: str = "5449fbd7e244"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_table('channel_type',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('type_', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('type_')
)
op.create_table('channel',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('type_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(), nullable=True),
sa.ForeignKeyConstraint(['type_id'], ['channel_type.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('message',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('message_id', sa.Integer(), nullable=False),
sa.Column('grouped_id', sa.BigInteger(), nullable=True),
sa.Column('sent', sa.DateTime(), nullable=True),
sa.Column('channel_id', sa.BigInteger(), nullable=False),
sa.Column('original_channel_id', sa.BigInteger(), nullable=False),
sa.Column('original_message_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['channel_id'], ['channel.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('original_channel_id', 'original_message_id', name='source_message_uniq')
)
op.execute("INSERT INTO public.channel_type (id, type_)"
f"VALUES (0, '{NOT_SPECIFIED_CHANNEL_TYPE}')")
op.create_table(
"channel_type",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("type_", sa.String(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("type_"),
)
op.create_table(
"channel",
sa.Column("id", sa.BigInteger(), nullable=False),
sa.Column("type_id", sa.Integer(), nullable=True),
sa.Column("name", sa.String(), nullable=True),
sa.ForeignKeyConstraint(
["type_id"],
["channel_type.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"message",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("message_id", sa.Integer(), nullable=False),
sa.Column("grouped_id", sa.BigInteger(), nullable=True),
sa.Column("sent", sa.DateTime(), nullable=True),
sa.Column("channel_id", sa.BigInteger(), nullable=False),
sa.Column("original_channel_id", sa.BigInteger(), nullable=False),
sa.Column("original_message_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["channel_id"],
["channel.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("original_channel_id", "original_message_id", name="source_message_uniq"),
)
op.execute("INSERT INTO public.channel_type (id, type_)" f"VALUES (0, '{NOT_SPECIFIED_CHANNEL_TYPE}')")


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('message')
op.drop_table('channel')
op.drop_table('channel_type')
op.drop_table("message")
op.drop_table("channel")
op.drop_table("channel_type")
# ### end Alembic commands ###
4 changes: 3 additions & 1 deletion src/aggregator/posts_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def __init__(self, session_maker) -> None:
logger.info("Post storage is initializing...")
self.session_maker = session_maker

def post(self, message_id: MESSAGE_ID, grouped_id: int , event_peer_id, original_channel_id, original_message_id) -> None:
def post(
self, message_id: MESSAGE_ID, grouped_id: int, event_peer_id, original_channel_id, original_message_id
) -> None:
with self.session_maker() as session:
orm_message = MessageModel(
message_id=message_id,
Expand Down
10 changes: 4 additions & 6 deletions src/aggregator/telegram_agent/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from aggregator.posts_storage import PostStorage




def create_telegram_agent(post_storage: PostStorage) -> TelegramClient:
logger.info("Creating telegram agent")
client = TelegramClient(StringSession(CLIENT_SESSION), TELEGRAM_API_ID, TELEGRAM_API_HASH)
Expand Down Expand Up @@ -50,19 +48,19 @@ async def public_channel_listener(event) -> None:
logger.info("New messages {} will be forwarded into the aggregation channel", messages)
fwd_event = await event.forward_to(AGGREGATOR_CHANNEL)

first_message = fwd_event if is_single_message else fwd_event[0]
fwd_messages = [fwd_event] if is_single_message else fwd_event
first_message = fwd_event if is_single_message else fwd_event[0]
fwd_messages = [fwd_event] if is_single_message else fwd_event

original_channel_id = get_peer_id(first_message.fwd_from.from_id)
forwarded_from_channel_id = get_peer_id(messages[0].peer_id)

for fwd_msg, msg in zip(fwd_messages, messages):
for fwd_msg, msg in zip(fwd_messages, messages):
post_storage.post(
fwd_msg.id,
msg.grouped_id,
forwarded_from_channel_id,
original_channel_id,
fwd_msg.fwd_from.channel_post
fwd_msg.fwd_from.channel_post,
)
logger.debug("Messages was successfully processed")

Expand Down

0 comments on commit 30a0977

Please sign in to comment.