From 9e8a6bd123249606021ad1761071e8822172ed9d Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Thu, 25 May 2017 12:04:02 -0700 Subject: [PATCH] duplicate matrixroom table with new constraint --- appservice.py | 12 ++++++--- migrations/versions/744f11d26259_.py | 37 ++++++++++++++++++++++++++++ models.py | 15 +++++++++++ pnut-bridge.py | 4 +-- 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 migrations/versions/744f11d26259_.py diff --git a/appservice.py b/appservice.py index 7f23c37..cf967f2 100644 --- a/appservice.py +++ b/appservice.py @@ -30,7 +30,7 @@ def on_receive_events(transaction): embed = None - chan = MatrixRoom.query.filter_by(room_id=event['room_id']).first() + chan = MatrixRoom2.query.filter_by(room_id=event['room_id']).first() if chan: chan_id = chan.pnut_chan else: @@ -170,8 +170,14 @@ def on_receive_events(transaction): @app.route("/rooms/") def query_alias(alias): alias_localpart = alias.split(":")[0][1:] + channel_id = int(alias_localpart.split('_')[1]) + + # prevent room from being created if channel is already plumbed + chroom = MatrixRoom2.query.filter_by(pnut_chan=channel_id).first() + if chroom: + abort(404) + try: - channel_id = int(alias_localpart.split('_')[1]) r = requests.get('https://api.pnut.io/v0/channels/' + str(channel_id) + '?include_raw=1') if r.status_code == 200: cdata = json.loads(r.text)['data'] @@ -208,7 +214,7 @@ def query_alias(alias): if resp.status_code == 200: room_id = json.loads(resp.text)['room_id'] - mro = MatrixRoom(room_id, channel_id, cdata['acl']['write']['any_user']) + mro = MatrixRoom2(room_id, channel_id, cdata['acl']['write']['any_user']) db.session.add(mro) db.session.commit() diff --git a/migrations/versions/744f11d26259_.py b/migrations/versions/744f11d26259_.py new file mode 100644 index 0000000..0870bd4 --- /dev/null +++ b/migrations/versions/744f11d26259_.py @@ -0,0 +1,37 @@ +"""empty message + +Revision ID: 744f11d26259 +Revises: f878073e1b4a +Create Date: 2017-05-25 09:51:32.238059 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '744f11d26259' +down_revision = 'f878073e1b4a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('matrix_room2', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('room_id', sa.Text(), nullable=True), + sa.Column('pnut_chan', sa.Text(), nullable=True), + sa.Column('pnut_since', sa.Text(), nullable=True), + sa.Column('pnut_write', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('pnut_chan'), + sa.UniqueConstraint('room_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('matrix_room2') + # ### end Alembic commands ### diff --git a/models.py b/models.py index 55c46ff..75d06df 100644 --- a/models.py +++ b/models.py @@ -33,6 +33,21 @@ class MatrixRoom(db.Model): def __repr__(self): return '' % self.room_id +class MatrixRoom2(db.Model): + id = db.Column(db.Integer, primary_key=True) + room_id = db.Column(db.Text, unique=True) + pnut_chan = db.Column(db.Text, unique=True) + pnut_since = db.Column(db.Text) + pnut_write = db.Column(db.Boolean, default=True) + + def __init__(self, room_id, pnut_chan, pnut_write=True): + self.room_id = room_id + self.pnut_chan = pnut_chan + self.pnut_write = pnut_write + + def __repr__(self): + return '' % self.room_id + class MatrixMsgEvents(db.Model): id = db.Column(db.Integer, primary_key=True) event_id = db.Column(db.Text) diff --git a/pnut-bridge.py b/pnut-bridge.py index 71fe894..51f7af5 100644 --- a/pnut-bridge.py +++ b/pnut-bridge.py @@ -172,10 +172,10 @@ class ChannelMonitor(threading.Thread): def run(self): logging.info("-- Starting channel monitor --") app.app_context().push() - rooms = MatrixRoom.query.all() + rooms = MatrixRoom2.query.all() self.txId = int(rooms[0].pnut_since) while not _shutdown.isSet(): - rooms = MatrixRoom.query.all() + rooms = MatrixRoom2.query.all() for r in rooms: self.poll_channel(r) time.sleep(15)