def test_dump_msg_entities(self): """Show that entities are correctly parsed and stored""" message = types.Message( id=1, to_id=types.PeerUser(321), date=datetime.now(), message='No entities' ) dumper = Dumper(self.dumper_config) fmt = BaseFormatter(dumper.conn) # Test with no entities await dumper.dump_message(message, 123, None, None) dumper.commit() assert not next(fmt.get_messages_from_context(123, order='DESC')).formatting # Test with many entities text, entities = markdown.parse( 'Testing message with __italic__, **bold**, inline ' '[links](https://example.com) and [mentions](@hi), ' 'as well as `code` and ``pre`` blocks.' ) entities[3] = types.MessageEntityMentionName( entities[3].offset, entities[3].length, 123 ) message.id = 2 message.date -= timedelta(days=1) message.message = text message.entities = entities await dumper.dump_message(message, 123, None, None) dumper.commit() msg = next(fmt.get_messages_from_context(123, order='ASC')) assert utils.decode_msg_entities(msg.formatting) == message.entities
def decode_msg_entities(string): """ Reverses the transformation made by ``utils.encode_msg_entities``. """ if not string: return None parsed = [] for part in string.split(';'): split = part.split(',') kind, offset, length = split[0], int(split[1]), int(split[2]) if kind in TEXT_TO_ENTITY: if kind == 'texturl': parsed.append( types.MessageEntityTextUrl(offset, length, split[-1])) elif kind == 'mentionname': parsed.append( types.MessageEntityMentionName(offset, length, int(split[-1]))) else: parsed.append(TEXT_TO_ENTITY[kind](offset, length)) return parsed