Skip to content
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

Feat/last interaction 684 #700

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/bot/services/external_site_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ async def toggle_has_mailing(self, site_user_id: int, field: HasMailingField) ->
await self._repository.set_has_mailing_my_tasks(site_user, not site_user.has_mailing_my_tasks)
case HasMailingField.procharity:
await self._repository.set_has_mailing_procharity(site_user, not site_user.has_mailing_procharity)

async def update_last_interaction(self, site_user: ExternalSiteUser) -> None:
"""Обновляет last_interaction для site_user и соответствующего user."""
if not site_user:
return
await self._repository.update_last_interaction(site_user)
user = await self._user_repository.get_by_external_id(site_user.id)
if user:
await self._user_repository.update_last_interaction(user)
2 changes: 2 additions & 0 deletions src/bot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ async def decorated_handler(
parse_mode=ParseMode.HTML,
reply_markup=keyboard,
)
if ext_site_user:
await ext_site_user_service.update_last_interaction(ext_site_user)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно добавить сохранение времени взаимодействия для пользователя Телеграм при неудачной авторизации.

Сейчас при наличии связи с пользователем сайта оба столбца обновляются вместе внутри ext_site_user_service.update_last_interaction. Можно вынести оттуда обновление столбца для user, а здесь добавить поиск user по telegram_id и обновление столбца last_interaction, если user найден.


return decorated_handler

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add user.last_interaction

Revision ID: f4524481b9dc
Revises: b155a83b9476
Create Date: 2024-09-22 16:01:34.651499

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "f4524481b9dc"
down_revision = "b155a83b9476"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("external_site_users", sa.Column("last_interaction", sa.DateTime(), nullable=True))
op.add_column("users", sa.Column("last_interaction", sa.DateTime(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("users", "last_interaction")
op.drop_column("external_site_users", "last_interaction")
# ### end Alembic commands ###
2 changes: 2 additions & 0 deletions src/core/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class User(Base):

external_id: Mapped[int | None] = mapped_column(ForeignKey("external_site_users.id"), nullable=True)
external_user: Mapped["ExternalSiteUser"] = relationship(back_populates="user")
last_interaction: Mapped[datetime | None] = mapped_column(nullable=True)

@property
def is_volunteer(self) -> bool:
Expand Down Expand Up @@ -116,6 +117,7 @@ class ExternalSiteUser(ArchivableBase):
has_mailing_profile: Mapped[bool] = mapped_column(nullable=True)
has_mailing_my_tasks: Mapped[bool] = mapped_column(nullable=True)
has_mailing_procharity: Mapped[bool] = mapped_column(nullable=True)
last_interaction: Mapped[datetime | None] = mapped_column(nullable=True)

def __repr__(self):
return f"<SiteUser {self.id}>"
Expand Down
7 changes: 6 additions & 1 deletion src/core/db/repository/external_site_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import select
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession

from src.core.db.models import ExternalSiteUser, Task, TaskResponseVolunteer
Expand Down Expand Up @@ -100,3 +100,8 @@ async def set_has_mailing_procharity(self, site_user: ExternalSiteUser, has_mail
"""Изменяет настройку уведомлений о ProCharity."""
site_user.has_mailing_procharity = has_mailing_procharity
await self.update(site_user.id, site_user)

async def update_last_interaction(self, site_user: ExternalSiteUser) -> None:
"""Обновляет статус ExternalSiteUser.last_interaction текущим временем."""
site_user.last_interaction = func.now()
await self.update(site_user.id, site_user)
5 changes: 5 additions & 0 deletions src/core/db/repository/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ async def get_filtered_objects_by_page(
)
objects = await self._session.scalars(statement.limit(limit).offset(offset).order_by(desc(column_name)))
return objects.all()

async def update_last_interaction(self, user: User) -> None:
"""Обновляет статус User.last_interaction текущим временем."""
user.last_interaction = func.now()
await self.update(user.id, user)