...
 
Commits (4)
......@@ -201,8 +201,14 @@ def raw_from_event(event):
value['url'] = url
value['title'] = event['content']['body']
if 'info' in event['content']:
value['width'] = event['content']['info']['w']
value['height'] = event['content']['info']['h']
if 'w' in event['content']['info']:
value['width'] = event['content']['info']['w']
else:
value['width'] = 200
if 'h' in event['content']['info']:
value['height'] = event['content']['info']['h']
else:
value['height'] = 200
if 'thumbnail_info' in event['content']['info']:
value['thumbnail_url'] = app.config['MATRIX_HOST'] + '/_matrix/media/r0/download/' + event['content']['info']['thumbnail_url'][6:]
value['thumbnail_width'] = event['content']['info']['thumbnail_info']['w']
......@@ -245,9 +251,13 @@ def delete_message(event, user):
logger.debug("- can't find the event to remove -")
return
r, meta = pnutpy.api.delete_message(e.pnut_chan_id, e.pnut_msg_id)
e.deleted = True
db_session.commit()
try:
r, meta = pnutpy.api.delete_message(e.pnut_chan_id, e.pnut_msg_id)
e.deleted = True
db_session.commit()
except pnutpy.errors.PnutPermissionDenied as e:
pass
def get_displayname(userid):
url = "http://localhost:8008/_matrix/client/r0/profile/" + userid
......
......@@ -18,6 +18,7 @@ from appservice import app
logger = logging.getLogger()
_shutdown = threading.Event()
_reconnect = threading.Event()
def new_message(msg):
logger.debug("channel: " + msg.channel_id)
......@@ -263,33 +264,49 @@ def on_error(ws, error):
def on_close(ws):
logger.debug("on_close: ### CLOSED ###")
if not _shutdown.set():
time.sleep(10)
t.start()
else:
time.sleep(2)
def on_open(ws):
def run(*args):
while not _shutdown.isSet():
while not _shutdown.isSet() and not _reconnect.isSet():
time.sleep(3)
ws.send(".")
try:
ws.send(".")
except websocket._exceptions.WebSocketConnectionClosedException:
logger.debug('websocket closed exception caught...')
_reconnect.set()
time.sleep(1)
ws.close()
logger.debug("*** terminate ***")
logger.debug("*** terminate thread ***")
t = threading.Thread(target=run)
t.start()
def wsthreader(threadfunc):
def wrapper():
while not _shutdown.isSet():
_reconnect.clear()
logger.debug('threadfunc start...')
running = threadfunc()
logger.debug('threadfunc end...')
if running:
time.sleep(5)
else:
_shutdown.set()
logger.debug('*** thread stopped ***')
return wrapper
if __name__ == '__main__':
# websocket.enableTrace(True)
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
with open("config.yaml", "rb") as config_file:
config = yaml.load(config_file)
ws_url = 'wss://stream.pnut.io/v0/app?access_token='
ws_url = 'wss://stream.pnut.io/v0/app?access_token='
ws_url += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY']
ws_url += '&include_raw=1'
matrix_url = config['MATRIX_HOST'] + '/_matrix/client/r0'
......@@ -301,9 +318,8 @@ if __name__ == '__main__':
ws = websocket.WebSocketApp(ws_url, on_message=on_message,
on_error=on_error, on_close=on_close)
ws.on_open = on_open
t = threading.Thread(target=ws.run_forever)
t.daemon = True
t.start()
wst = threading.Thread(target=wsthreader(ws.run_forever))
wst.start()
# setup the matrix app service
if config['MATRIX_ADMIN_ROOM']:
......@@ -312,6 +328,7 @@ if __name__ == '__main__':
app.config.update(config)
app.run(port=config['LISTEN_PORT'])
_shutdown.set()
logger.info('!! shutdown initiated !!')
_shutdown.set()
ws.close()
time.sleep(2)