def migrate_state_store(): conn = op.get_bind() session: orm.Session = orm.sessionmaker(bind=conn)() try: with open("mx-state.json") as file: data = json.load(file) except FileNotFoundError: return if not data: return registrations = data.get("registrations", []) mxtg_config_path = context.get_x_argument(as_dictionary=True).get( "config", "config.yaml") mxtg_config = Config(mxtg_config_path, None, None) mxtg_config.load() username_template = mxtg_config.get("bridge.username_template", "telegram_{userid}") hs_domain = mxtg_config["homeserver.domain"] localpart = username_template.format(userid="(.+)") mxid_regex = re.compile("@{}:{}".format(localpart, hs_domain)) for user in registrations: match = mxid_regex.match(user) if not match: continue puppet = session.query(Puppet).get(match.group(1)) if not puppet: continue puppet.matrix_registered = True session.merge(puppet) session.commit() user_profiles = [ UserProfile(room_id=room, user_id=user, membership=member.get("membership", "leave"), displayname=member.get("displayname", None), avatar_url=member.get("avatar_url", None)) for room, members in data.get("members", {}).items() for user, member in members.items() ] session.add_all(user_profiles) session.commit() room_state = [ RoomState(room_id=room, power_levels=json.dumps(levels)) for room, levels in data.get("power_levels", {}).items() ] session.add_all(room_state) session.commit()
sys.path.insert(0, dirname(dirname(abspath(__file__)))) from mautrix.util.db import Base import mautrix_telegram.db from mautrix_telegram.config import Config from alchemysession import AlchemySessionContainer # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config mxtg_config_path = context.get_x_argument(as_dictionary=True).get( "config", "config.yaml") mxtg_config = Config(mxtg_config_path, None, None) mxtg_config.load() config.set_main_option("sqlalchemy.url", mxtg_config["appservice.database"].replace("%", "%%")) AlchemySessionContainer.create_table_classes(None, "telethon_", Base) # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = Base.metadata
from mautrix_telegram.db import Portal, Message, Puppet, BotChat from .models import ChatLink, TgUser, MatrixUser, Message as TMMessage, Base as TelematrixBase parser = argparse.ArgumentParser( description="mautrix-telegram telematrix import script", prog="python -m mautrix_telegram.scripts.telematrix_import") parser.add_argument("-c", "--config", type=str, default="config.yaml", metavar="<path>", help="the path to your mautrix-telegram config file") parser.add_argument("-b", "--bot-id", type=int, required=True, metavar="<id>", help="the telegram user ID of your relay bot") parser.add_argument("-t", "--telematrix-database", type=str, default="sqlite:///database.db", metavar="<url>", help="your telematrix database URL") args = parser.parse_args() config = Config(args.config, None, None) config.load() mxtg_db_engine = sql.create_engine(config.get("appservice.database", "sqlite:///mautrix-telegram.db")) mxtg = orm.sessionmaker(bind=mxtg_db_engine)() Base.metadata.bind = mxtg_db_engine telematrix_db_engine = sql.create_engine(args.telematrix_database) telematrix = orm.sessionmaker(bind=telematrix_db_engine)() TelematrixBase.metadata.bind = telematrix_db_engine chat_links = telematrix.query(ChatLink).all() tg_users = telematrix.query(TgUser).all() mx_users = telematrix.query(MatrixUser).all() messages = telematrix.query(TMMessage).all() telematrix.close()