Merge branch 'pnut_v1' into 'main'
pnut api v1 app stream See merge request thrrgilag/pnut-matrix!3
This commit is contained in:
commit
06c8055fb2
1 changed files with 51 additions and 43 deletions
|
@ -58,7 +58,7 @@ def new_message(msg):
|
|||
logger.debug('-set_display-')
|
||||
|
||||
avatar = Avatars.query.filter(Avatars.pnut_user == msg.user.username).one_or_none()
|
||||
if avatar is None or avatar.avatar != msg.user.content.avatar_image.link:
|
||||
if avatar is None or avatar.avatar != msg.user.content.avatar_image.url:
|
||||
set_matrix_avatar(msg.user)
|
||||
logger.debug('-set_avatar-')
|
||||
|
||||
|
@ -76,8 +76,8 @@ def new_message(msg):
|
|||
for link in msg.content.entities.links:
|
||||
if 'title' in link:
|
||||
lnktext += link.title + "\n"
|
||||
if 'link' in link:
|
||||
lnktext += link.link + "\n"
|
||||
if 'url' in link:
|
||||
lnktext += link.url + "\n"
|
||||
|
||||
if len(lnktext) > 0:
|
||||
text += "\n" + lnktext
|
||||
|
@ -93,7 +93,7 @@ def new_message(msg):
|
|||
db_session.add(event)
|
||||
db_session.commit()
|
||||
|
||||
if len(msg.raw) > 0:
|
||||
if 'raw' in msg:
|
||||
logger.debug('-handle media uploads-')
|
||||
new_media(room.room_id, msg)
|
||||
|
||||
|
@ -104,36 +104,45 @@ def new_media(room_id, msg):
|
|||
identity=matrix_id)
|
||||
ts = int(msg.created_at.strftime('%s')) * 1000
|
||||
|
||||
for item in msg.raw:
|
||||
if item.type == 'io.pnut.core.oembed' and 'url' in item.value:
|
||||
if 'io.pnut.core.oembed' in msg.raw:
|
||||
|
||||
dl = requests.get(item.value.url, stream=True)
|
||||
for oembed in msg.raw['io.pnut.core.oembed']:
|
||||
info = {}
|
||||
|
||||
if oembed.type == 'photo':
|
||||
msgtype = 'm.image'
|
||||
dl_url = oembed.url
|
||||
info['h'] = oembed.height
|
||||
info['w'] = oembed.width
|
||||
elif oembed.type == 'audio':
|
||||
logger.debug("* recieved audio attachment")
|
||||
continue
|
||||
elif oembed.type == 'video':
|
||||
logger.debug("* recieved video attachment")
|
||||
continue
|
||||
elif oembed.type == 'html5video':
|
||||
logger.debug("* recieved html5 video attachment")
|
||||
continue
|
||||
elif oembed.type == 'rich':
|
||||
logger.debug("* recieved video attachment")
|
||||
continue
|
||||
else:
|
||||
logger.debug("* recieved unknown attachment")
|
||||
continue
|
||||
|
||||
dl = requests.get(dl_url, stream=True)
|
||||
dl.raise_for_status()
|
||||
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
||||
mtype = m.id_buffer(dl.content)
|
||||
info = {'mimetype': mtype}
|
||||
|
||||
info['mimetype'] = m.id_buffer(dl.content)
|
||||
info['size'] = dl.content
|
||||
ul = matrix_api.media_upload(dl.content, mtype)
|
||||
|
||||
if item.value.type == 'photo':
|
||||
msgtype = 'm.image'
|
||||
info['h'] = item.value.height
|
||||
info['w'] = item.value.width
|
||||
info['size'] = len(dl.content)
|
||||
elif item.value.type == 'video' or item.value.type == 'html5video':
|
||||
msgtype = 'm.video'
|
||||
info['h'] = item.value.height
|
||||
info['w'] = item.value.width
|
||||
info['size'] = len(dl.content)
|
||||
elif item.value.type == 'audio':
|
||||
msgtype = 'm.audio'
|
||||
info['duration'] = int(item.value.duration) * 1000
|
||||
info['size'] = len(dl.content)
|
||||
if 'title' in oembed:
|
||||
title = oembed.title
|
||||
else:
|
||||
msgtype = 'm.file'
|
||||
info['size'] = len(dl.content)
|
||||
title = ""
|
||||
|
||||
r = matrix_api.send_content(room_id, ul['content_uri'], item.value.title, msgtype, extra_information=info, timestamp=ts)
|
||||
r = matrix_api.send_content(room_id, ul['content_uri'], title, msgtype, extra_information=info, timestamp=ts)
|
||||
event = Events(
|
||||
event_id=r['event_id'],
|
||||
room_id=room_id,
|
||||
|
@ -189,7 +198,7 @@ def set_matrix_avatar(user):
|
|||
token=config['MATRIX_AS_TOKEN'],
|
||||
identity=matrix_id)
|
||||
|
||||
dl = requests.get(user.content.avatar_image.link, stream=True)
|
||||
dl = requests.get(user.content.avatar_image.url, stream=True)
|
||||
dl.raise_for_status()
|
||||
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
|
||||
mtype = m.id_buffer(dl.content)
|
||||
|
@ -199,10 +208,10 @@ def set_matrix_avatar(user):
|
|||
matrix_api.set_avatar_url(matrix_id, ul['content_uri'])
|
||||
avatar = Avatars.query.filter(Avatars.pnut_user == user.username).one_or_none()
|
||||
if avatar is None:
|
||||
avatar = Avatars(pnut_user=user.username, avatar=user.content.avatar_image.link)
|
||||
avatar = Avatars(pnut_user=user.username, avatar=user.content.avatar_image.url)
|
||||
db_session.add(avatar)
|
||||
else:
|
||||
avatar.avatar = user.content.avatar_image.link
|
||||
avatar.avatar = user.content.avatar_image.url
|
||||
db_session.commit()
|
||||
|
||||
except MatrixRequestError:
|
||||
|
@ -243,22 +252,23 @@ def on_message(ws, message):
|
|||
|
||||
if 'data' in msg:
|
||||
|
||||
if msg['meta']['type'] == "message":
|
||||
if 'channel_type' in msg['meta']:
|
||||
|
||||
# TODO: bypassed other channel types for now
|
||||
if msg['meta']['channel_type'] != 'io.pnut.core.chat':
|
||||
return
|
||||
|
||||
pmsg = pnutpy.models.Message.from_response_data(msg['data'])
|
||||
for d_item in msg['data']:
|
||||
pmsg = pnutpy.models.Message.from_response_data(d_item)
|
||||
|
||||
if 'is_deleted' in msg['meta']:
|
||||
if msg['meta']['is_deleted']:
|
||||
logger.debug("message: delete")
|
||||
delete_message(pmsg)
|
||||
if 'is_deleted' in msg['meta']:
|
||||
if msg['meta']['is_deleted']:
|
||||
logger.debug("message: delete")
|
||||
delete_message(pmsg)
|
||||
else:
|
||||
logger.debug("uh whut?")
|
||||
else:
|
||||
logger.debug("uh whut?")
|
||||
else:
|
||||
new_message(pmsg)
|
||||
new_message(pmsg)
|
||||
|
||||
def on_error(ws, error):
|
||||
logger.error("on_error: !!! ERROR !!!")
|
||||
|
@ -309,6 +319,7 @@ if __name__ == '__main__':
|
|||
'-d', action='store_true', dest='debug',
|
||||
help="debug logging"
|
||||
)
|
||||
# TODO: solve the database.py problem and enable this
|
||||
# a_parser.add_argument(
|
||||
# '-c', '--config', default="config.yaml",
|
||||
# help="configuration file"
|
||||
|
@ -321,15 +332,12 @@ if __name__ == '__main__':
|
|||
else:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# with open(args.config, 'rb') as config_file:
|
||||
# config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
||||
|
||||
configyaml = os.environ.get("CONFIG_FILE")
|
||||
|
||||
with open(configyaml, "rb") as config_file:
|
||||
config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
||||
|
||||
ws_url = 'wss://stream.pnut.io/v0/app?access_token='
|
||||
ws_url = 'wss://stream.pnut.io/v1/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'
|
||||
|
|
Loading…
Reference in a new issue