'For a room loops through all events with media and quarantines
the associated media'

def quarantine_media_ids_in_room(self, room_id, quarantined_by):

    def _get_media_ids_in_room(txn):
        mxc_re = re.compile('^mxc://([^/]+)/([^/#?]+)')
        next_token = (self.get_current_events_token() + 1)
        total_media_quarantined = 0
        while next_token:
            sql = '\n                                                                                SELECT    stream_ordering,    content    FROM    events\n                                                                                WHERE    room_id    =    ?\n                                                                                                AND    stream_ordering    <    ?\n                                                                                                AND    contains_url    =    ?    AND    outlier    =    ?\n                                                                                ORDER    BY    stream_ordering    DESC\n                                                                                LIMIT    ?\n                                                                '
            txn.execute(sql, (room_id, next_token, True, False, 100))
            next_token = None
            local_media_mxcs = []
            remote_media_mxcs = []
            for (stream_ordering, content_json) in txn:
                next_token = stream_ordering
                content = json.loads(content_json)
                content_url = content.get('url')
                thumbnail_url = content.get('info', {}).get('thumbnail_url')
                for url in (content_url, thumbnail_url):
                    if (not url):
                        continue
                    matches = mxc_re.match(url)
                    if matches:
                        hostname = matches.group(1)
                        media_id = matches.group(2)
                        if (hostname == self.hostname):
                            local_media_mxcs.append(media_id)
                        else:
                            remote_media_mxcs.append((hostname, media_id))
            txn.executemany('\n                                                                                UPDATE    local_media_repository\n                                                                                SET    quarantined_by    =    ?\n                                                                                WHERE    media_id    =    ?\n                                                                ', ((quarantined_by, media_id) for media_id in local_media_mxcs))
            txn.executemany('\n                                                                                                UPDATE    remote_media_cache\n                                                                                                SET    quarantined_by    =    ?\n                                                                                                WHERE    media_origin    AND    media_id    =    ?\n                                                                                ', ((quarantined_by, origin, media_id) for (origin, media_id) in remote_media_mxcs))
            total_media_quarantined += len(local_media_mxcs)
            total_media_quarantined += len(remote_media_mxcs)
        return total_media_quarantined
    return self.runInteraction('get_media_ids_in_room', _get_media_ids_in_room)
