Example #1
0
    def create_ban_even_if_not_on_this_node(self, activity: Activity) -> None:
        """
        since bans can be created through the rest api we need to create the ban even though the user might not be on
        this node, since one reason could be that he's not even connected. So make sure the ban is created first.
        """
        banned_id = activity.object.id
        target_type = activity.target.object_type

        if target_type == 'room':
            target_id = activity.target.id
        elif target_type == 'channel':
            target_id = activity.target.id
        else:
            target_type = 'global'
            target_id = ''

        reason = None
        if hasattr(activity.object, 'content'):
            reason = activity.object.content

        try:
            ban_duration = activity.object.summary
            ban_timestamp = utils.ban_duration_to_timestamp(ban_duration)
            activity.object.updated = utils.ban_duration_to_datetime(ban_duration)\
                .strftime(ConfigKeys.DEFAULT_DATE_FORMAT)
            banner_id = activity.actor.id

            # don't duplicate the ban notification to external queue
            if environ.env.node == 'rest':
                self.send_ban_event_to_external_queue(activity, target_type)

            if target_type == 'global':
                logger.info('banning user %s globally for %s' %
                            (banned_id, ban_duration))
                self.env.db.ban_user_global(banned_id, ban_timestamp,
                                            ban_duration, reason, banner_id)
            elif target_type == 'channel':
                logger.info('banning user %s in channel %s for %s' %
                            (banned_id, target_id, ban_duration))
                self.env.db.ban_user_channel(banned_id, ban_timestamp,
                                             ban_duration, target_id, reason,
                                             banner_id)
            else:
                logger.info('banning user %s in room %s for %s' %
                            (banned_id, target_id, ban_duration))
                self.env.db.ban_user_room(banned_id, ban_timestamp,
                                          ban_duration, target_id, reason,
                                          banner_id)
        except KeyError as ke:
            logger.error('could not ban: %s' % str(ke))
            logger.exception(traceback.format_exc())
Example #2
0
 def test_ban_duration_days(self):
     expected = self.get_now_plus(days=5)
     timestamp = utils.ban_duration_to_timestamp('5d')
     self.assertEqual(expected, timestamp)
Example #3
0
 def test_ban_duration_hours(self):
     expected = self.get_now_plus(hours=12)
     timestamp = utils.ban_duration_to_timestamp('12h')
     self.assertEqual(expected, timestamp)
Example #4
0
 def test_ban_duration_minutes(self):
     expected = self.get_now_plus(minutes=15)
     timestamp = utils.ban_duration_to_timestamp('15m')
     self.assertEqual(expected, timestamp)
Example #5
0
 def test_ban_duration_seconds(self):
     expected = self.get_now_plus(seconds=50)
     timestamp = utils.ban_duration_to_timestamp('50s')
     self.assertEqual(expected, timestamp)