Ejemplo n.º 1
0
    def _analyze_chat_admin_logs(
            self,
            *,
            db_chat: 'tg_models.Chat',
            db_tg_admin_account: 'tg_models.TelegramAccount',
            client: 'ClientProxy',
    ) -> BaseResponse:
        try:
            raw_admin_logs = client(
                'get_admin_log',
                db_chat.chat_id,
            )
        except tg_errors.ChatAdminRequired or tg_errors.ChatWriteForbidden:
            self.db.telegram.update_chat_analyzers_status(
                db_chat=db_chat,
                enabled=False,
                only_admin_based_analyzers=True,
            )
        except Exception as e:
            logger.exception(e)
        else:
            for raw_admin_log in raw_admin_logs:
                self.db.telegram.create_admin_log(
                    raw_admin_log=raw_admin_log,
                    db_chat=db_chat,
                    logged_by=db_tg_admin_account,
                )

        return BaseResponse().done()
 def create_chat_permissions(self, **kwargs) -> Optional["ChatPermissions"]:
     try:
         return self.create(**kwargs)
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
Ejemplo n.º 3
0
 def update_dialog(self, **kwargs) -> bool:
     try:
         return bool(self.update(**kwargs))
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return False
    def create_event(self, **kwargs) -> Optional['AdminLogEvent']:
        try:
            return self.create(**kwargs)
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 5
0
    def update_membership(self, **kwargs) -> bool:
        try:
            return bool(self.update(**kwargs))
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 6
0
    def create_restriction(self, **kwargs) -> Optional['Restriction']:
        try:
            return self.create(**kwargs)
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
    def log_message_views_task(self, *args, **kwargs) -> BaseResponse:
        db_chats: QuerySet = self.db.telegram.get_chats_filter_by_analyzer(
            message_view_analyzer=True, )
        if db_chats.exists():
            for db_chat in db_chats:
                db_telegram_accounts = self.get_telegram_accounts(db_chat)
                if db_telegram_accounts is None or not len(
                        db_telegram_accounts):
                    # return BaseResponse().done(message='No Telegram Account is available now.')
                    continue

                db_telegram_account = db_telegram_accounts[0]

                client = self.get_client(
                    session_name=db_telegram_account.session_name)
                last_raw_message: 'types.Message' = self.get_last_valid_message(
                    client, db_chat.chat_id)
                if last_raw_message is not None:
                    last_message_id = last_raw_message.message_id
                    now = arrow.utcnow().timestamp()
                    start = int((-10000) / 100)
                    if start < 1:
                        start = 1

                    for i in range(start, int(last_message_id / 100) + 1):
                        try:
                            raw_message_views = client(
                                'get_messages_views',
                                chat_id=db_chat.chat_id,
                                message_ids=list(range(i, i + 100)),
                            )
                        except tg_errors.ChannelPrivate as e:
                            pass
                        except tg_errors.RPCError as e:
                            logger.exception(e)
                        except Exception as e:
                            logger.exception(e)
                        else:
                            for raw_message_view in raw_message_views:
                                db_message = self.db.telegram.get_message_by_message_id(
                                    db_chat=db_chat,
                                    message_id=raw_message_view.message_id)
                                db_message_view = self.db.telegram.get_updated_message_view(
                                    raw_message_view=raw_message_view,
                                    db_chat=db_chat,
                                    db_message=db_message,
                                    logger_account=db_telegram_account,
                                    date_ts=now,
                                )
                    self.db.telegram.update_analyzer_metadata(
                        analyzer=db_chat.message_view_analyzer,
                        timestamp=now,
                    )
        else:
            return BaseResponse().done(message='No analyzer is enabled.')

        return BaseResponse().done()
    def create_admin_rights(self, **kwargs) -> Optional['ChatAdminRights']:
        try:
            return self.create(**kwargs)
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
 def update_or_create_group(self, *, defaults: dict,
                            **kwargs) -> Optional["Group"]:
     try:
         return self.update_or_create(defaults=defaults, **kwargs)[0]
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
Ejemplo n.º 10
0
    def create_user(self, **kwargs) -> Optional["User"]:
        try:
            return self.create(**kwargs)
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 11
0
 def get_by_user_id(self, *, user_id: int) -> "User":
     try:
         instance = self.get(user_id=user_id)
     except User.DoesNotExist:
         instance = None
     except Exception as e:
         logger.exception(e)
         instance = None
     return instance
 def get_by_id(self, *, id: str) -> Optional["Message"]:
     try:
         return self.get(id=id)
     except Message.DoesNotExist:
         pass
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
    def update_or_create_adminship(self, *, defaults: dict,
                                   **kwargs) -> Optional['AdminShip']:
        try:
            return self.update_or_create(defaults=defaults, **kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 14
0
    def update_or_create_account(self, *, defaults: dict,
                                 **kwargs) -> Optional['TelegramAccount']:
        try:
            return self.update_or_create(defaults=defaults, **kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 15
0
 def user_deleted_account(self, *, user_id: int, delete_ts: int) -> bool:
     try:
         return bool(
             self.filter_by_user_id(user_id=user_id).update(
                 user_deleted_ts=delete_ts))
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return False
Ejemplo n.º 16
0
 def get_group_by_id(self, *, group_id: int) -> Optional["Group"]:
     try:
         return self.get(id=group_id)
     except Group.DoesNotExist as e:
         return None
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
    def update_or_create_action(
            self, **kwargs) -> Optional['AdminLogEventActionEditMessage']:
        try:
            return self.update_or_create(**kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 18
0
    def update_or_create_chat_member(self, *, defaults: dict,
                                     **kwargs) -> Optional['ChatMember']:

        try:
            return self.update_or_create(defaults=defaults, **kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)
        return None
    def update_or_create_event(
            self, *, defaults: dict,
            **kwargs) -> Tuple[Optional['AdminLogEvent'], bool]:
        try:
            return self.update_or_create(defaults=defaults, **kwargs)
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None, False
Ejemplo n.º 20
0
    def create_channel(self, **kwargs) -> Optional['TelegramChannel']:
        try:
            return self.create(
                **kwargs
            )
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 21
0
    def update_or_create_action(self, **kwargs) -> Optional['AdminLogEventActionDefaultBannedRights']:
        try:
            return self.update_or_create(
                **kwargs
            )[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
    def update_or_create_analyzer(
            self, *, defaults: dict,
            **kwargs) -> Optional['AdminLogAnalyzerMetaData']:
        try:
            return self.update_or_create(defaults=defaults, **kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 23
0
    def update_or_create_action(
            self,
            **kwargs) -> Optional['AdminLogEventActionTogglePreHistoryHidden']:
        try:
            return self.update_or_create(**kwargs)[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
    def update_or_create_shared_media(self, *, defaults: dict, **kwargs) -> Optional["ChatSharedMedia"]:
        try:
            return self.update_or_create(
                defaults=defaults,
                **kwargs
            )[0]
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
Ejemplo n.º 25
0
 def get_dialog(self, *, id: str) -> Optional["Dialog"]:
     if not id:
         return None
     try:
         return self.get(id=id)
     except Dialog.DoesNotExist as e:
         pass
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
Ejemplo n.º 26
0
 def get_post_by_id(self, *, post_id: str) -> Optional['Post']:
     try:
         return self.get(id=post_id)
     except Post.DoesNotExist as e:
         pass
     except Post.MultipleObjectsReturned as e:
         pass
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
     return None
Ejemplo n.º 27
0
 def clear_restrictions(self,
                        *,
                        db_user: "******" = None,
                        db_chat: "tg_models.Chat" = None,
                        db_message: "tg_models.Message" = None):
     try:
         self.filter(user=db_user, chat=db_chat,
                     message=db_message).delete()
     except DatabaseError as e:
         logger.exception(e)
     except Exception as e:
         logger.exception(e)
    def get_by_id(self, *, chat_id: int,
                  event_id: int) -> Optional['AdminLogEvent']:
        try:
            return self.get(id=f"{chat_id}:{event_id}")
        except AdminLogEvent.DoesNotExist as e:
            pass
        except DatabaseError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)

        return None
 def get_by_message_id(self, *, message_id: int) -> Optional["Message"]:
     try:
         return self.order_by().filter(message_id=message_id).order_by('-edit_date_ts')[0]
     except Message.DoesNotExist:
         pass
     except DatabaseError as e:
         logger.exception(e)
     except IndexError as e:
         pass
     except Exception as e:
         logger.exception(e)
     return None
Ejemplo n.º 30
0
 def get_user_by_username(
     self,
     *,
     username: str,
 ) -> "SiteUser":
     site_user = None
     try:
         site_user = self.get_queryset().get(username=username.lower())
     except SiteUser.DoesNotExist:
         pass
     except Exception as e:
         logger.exception(e)
     return site_user