duplicate matrixroom table with new constraint

This commit is contained in:
Morgan McMillian 2017-05-25 12:04:02 -07:00
parent 50dff7a634
commit 9e8a6bd123
4 changed files with 63 additions and 5 deletions

View file

@ -30,7 +30,7 @@ def on_receive_events(transaction):
embed = None 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: if chan:
chan_id = chan.pnut_chan chan_id = chan.pnut_chan
else: else:
@ -170,8 +170,14 @@ def on_receive_events(transaction):
@app.route("/rooms/<alias>") @app.route("/rooms/<alias>")
def query_alias(alias): def query_alias(alias):
alias_localpart = alias.split(":")[0][1:] alias_localpart = alias.split(":")[0][1:]
try:
channel_id = int(alias_localpart.split('_')[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:
r = requests.get('https://api.pnut.io/v0/channels/' + str(channel_id) + '?include_raw=1') r = requests.get('https://api.pnut.io/v0/channels/' + str(channel_id) + '?include_raw=1')
if r.status_code == 200: if r.status_code == 200:
cdata = json.loads(r.text)['data'] cdata = json.loads(r.text)['data']
@ -208,7 +214,7 @@ def query_alias(alias):
if resp.status_code == 200: if resp.status_code == 200:
room_id = json.loads(resp.text)['room_id'] 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.add(mro)
db.session.commit() db.session.commit()

View file

@ -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 ###

View file

@ -33,6 +33,21 @@ class MatrixRoom(db.Model):
def __repr__(self): def __repr__(self):
return '<MatrixRoom %r>' % self.room_id return '<MatrixRoom %r>' % 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 '<MatrixRoom %r>' % self.room_id
class MatrixMsgEvents(db.Model): class MatrixMsgEvents(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
event_id = db.Column(db.Text) event_id = db.Column(db.Text)

View file

@ -172,10 +172,10 @@ class ChannelMonitor(threading.Thread):
def run(self): def run(self):
logging.info("-- Starting channel monitor --") logging.info("-- Starting channel monitor --")
app.app_context().push() app.app_context().push()
rooms = MatrixRoom.query.all() rooms = MatrixRoom2.query.all()
self.txId = int(rooms[0].pnut_since) self.txId = int(rooms[0].pnut_since)
while not _shutdown.isSet(): while not _shutdown.isSet():
rooms = MatrixRoom.query.all() rooms = MatrixRoom2.query.all()
for r in rooms: for r in rooms:
self.poll_channel(r) self.poll_channel(r)
time.sleep(15) time.sleep(15)