forked from mautrix/telegram
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add active user tracking and optional bridge blocking
- Loading branch information
Showing
8 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""add account activity | ||
Revision ID: 97404229e75e | ||
Revises: bfc0a39bfe02 | ||
Create Date: 2021-09-07 10:38:41.655301 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '97404229e75e' | ||
down_revision = 'bfc0a39bfe02' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user_activity', | ||
sa.Column('puppet_id', sa.BigInteger(), nullable=False), | ||
sa.Column('first_activity_ts', sa.Integer(), nullable=False), | ||
sa.Column('last_activity_ts', sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint('puppet_id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('user_activity') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# mautrix-telegram - A Matrix-Telegram puppeting bridge | ||
# Copyright (C) 2019 Tulir Asokan | ||
# Copyright (C) 2021 Tadeusz Sośnierz | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
from typing import Optional, Iterable | ||
|
||
from sqlalchemy import Column, Integer, BigInteger | ||
|
||
from mautrix.util.db import Base | ||
from mautrix.util.logging import TraceLogger | ||
|
||
from ..types import TelegramID | ||
|
||
import logging | ||
import datetime | ||
import time | ||
|
||
UPPER_ACTIVITY_LIMIT_MS = 60 * 1000 * 5 # 5 minutes | ||
ONE_DAY_MS = 24 * 60 * 60 * 1000 | ||
|
||
|
||
class UserActivity(Base): | ||
__tablename__ = "user_activity" | ||
|
||
log: TraceLogger = logging.getLogger("mau.user_activity") | ||
|
||
puppet_id: TelegramID = Column(BigInteger, primary_key=True) | ||
first_activity_ts: Optional[int] = Column(Integer) | ||
last_activity_ts: Optional[int] = Column(Integer) | ||
|
||
def update(self, activity_ts: int) -> None: | ||
if self.last_activity_ts > activity_ts: | ||
return | ||
|
||
self.last_activity_ts = activity_ts | ||
|
||
self.edit(last_activity_ts=self.last_activity_ts) | ||
|
||
@classmethod | ||
def update_for_puppet(cls, puppet: 'Puppet', activity_dt: datetime) -> None: | ||
activity_ts = int(activity_dt.timestamp() * 1000) | ||
|
||
if (time.time() * 1000) - activity_ts > UPPER_ACTIVITY_LIMIT_MS: | ||
return | ||
|
||
cls.log.debug(f"Updating activity time for {puppet.id} to {activity_ts}") | ||
obj = cls._select_one_or_none(cls.c.puppet_id == puppet.id) | ||
if obj: | ||
obj.update(activity_ts) | ||
else: | ||
obj = UserActivity( | ||
puppet_id=puppet.id, | ||
first_activity_ts=activity_ts, | ||
last_activity_ts=activity_ts, | ||
) | ||
obj.insert() | ||
|
||
@classmethod | ||
def get_active_count(cls, min_activity_days: int, max_activity_days: Optional[int]) -> int: | ||
current_ms = time.time() / 1000 | ||
active_count = 0 | ||
for user in cls._select_all(): | ||
activity_days = (user.last_activity_ts - user.first_activity_ts / 1000) / ONE_DAY_MS | ||
# If maxActivityTime is not set, they are always active | ||
is_active = max_activity_days is None or (current_ms - user.last_activity_ts) <= (max_activity_days * ONE_DAY_MS) | ||
if is_active and activity_days > min_activity_days: | ||
active_count += 1 | ||
return active_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters