Add support for private message #1 #70
1 changed files with 76 additions and 0 deletions
|
@ -345,6 +345,46 @@ def get_channel_settings(channel_id):
|
||||||
|
|
||||||
return channel_settings
|
return channel_settings
|
||||||
|
|
||||||
|
def create_room(channel, invite):
|
||||||
|
channel_settings = {}
|
||||||
|
for item in channel.raw:
|
||||||
|
if item.type == 'io.pnut.core.chat-settings':
|
||||||
|
channel_settings = item.value
|
||||||
|
|
||||||
|
# Matrix sdk doesn't include all details in a single call
|
||||||
|
room = {'room_alias_name': app.config['MATRIX_PNUT_PREFIX'] + channel.id}
|
||||||
|
logger.debug(invite)
|
||||||
|
logger.debug(room)
|
||||||
|
room['invite'] = [invite]
|
||||||
|
if 'name' in channel_settings:
|
||||||
|
room['name'] = channel_settings['name']
|
||||||
|
if 'description' in channel_settings:
|
||||||
|
room['topic'] = channel_settings['description']
|
||||||
|
if channel.acl.read.any_user:
|
||||||
|
room['preset'] = 'public_chat'
|
||||||
|
room['visibility'] = 'public'
|
||||||
|
else:
|
||||||
|
room['preset'] = 'private_chat'
|
||||||
|
room['visibility'] = 'private'
|
||||||
|
|
||||||
|
url = app.config['MATRIX_HOST'] + '/_matrix/client/api/v1/createRoom?access_token='
|
||||||
|
url += app.config['MATRIX_AS_TOKEN']
|
||||||
|
headers = {"Content-Type":"application/json"}
|
||||||
|
r = requests.post(url, headers=headers, data=json.dumps(room))
|
||||||
|
|
||||||
|
if r.status_code == 200:
|
||||||
|
#pnutpy.api.subscribe_channel(channel.id)
|
||||||
|
rdata = r.json()
|
||||||
|
rr = Rooms(
|
||||||
|
room_id=rdata['room_id'],
|
||||||
|
pnut_chan=channel.id,
|
||||||
|
portal=True
|
||||||
|
)
|
||||||
|
db_session.add(rr)
|
||||||
|
db_session.commit()
|
||||||
|
logger.debug(r.status_code)
|
||||||
|
logger.debug(r)
|
||||||
|
|
||||||
def on_admin_event(event):
|
def on_admin_event(event):
|
||||||
matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'],
|
matrix_api = MatrixHttpApi(app.config['MATRIX_HOST'],
|
||||||
token=app.config['MATRIX_AS_TOKEN'])
|
token=app.config['MATRIX_AS_TOKEN'])
|
||||||
|
@ -643,6 +683,13 @@ def on_control_message(event):
|
||||||
elif msg[0] == '!status':
|
elif msg[0] == '!status':
|
||||||
matrix_api.send_message(event['room_id'], cmd_user_status(event['sender']))
|
matrix_api.send_message(event['room_id'], cmd_user_status(event['sender']))
|
||||||
|
|
||||||
|
elif msg[0] == '!join':
|
||||||
|
if len(msg) > 1:
|
||||||
|
matrix_api.send_message(event['room_id'], cmd_user_join(event['sender'], msg[1]))
|
||||||
|
else:
|
||||||
|
matrix_api.send_message(event['room_id'], cmd_user_join(event['sender']))
|
||||||
|
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
errmsg = "- on_direct_message -"
|
errmsg = "- on_direct_message -"
|
||||||
logger.exception(errmsg)
|
logger.exception(errmsg)
|
||||||
|
@ -656,6 +703,7 @@ def cmd_user_help(cmd=None):
|
||||||
reply += "!save <token>\t- Save your pnut.io auth token\n"
|
reply += "!save <token>\t- Save your pnut.io auth token\n"
|
||||||
reply += "!drop\t\t\t- Drop your pnut.io auth token\n"
|
reply += "!drop\t\t\t- Drop your pnut.io auth token\n"
|
||||||
reply += "!status\t\t\t- Status of your pnut.io auth token\n"
|
reply += "!status\t\t\t- Status of your pnut.io auth token\n"
|
||||||
|
reply += "!join <channel #>\t- Join a private channel on pnut.io\n"
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
@ -732,3 +780,31 @@ def cmd_user_status(sender=None):
|
||||||
reply = "Error! There was a problem checking your account."
|
reply = "Error! There was a problem checking your account."
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
def cmd_user_join(sender=None, channel_id=None):
|
||||||
|
if channel_id is None:
|
||||||
|
reply = "You must provide a channel id number with this command.\n"
|
||||||
|
reply += "!join <channel #>"
|
||||||
|
return reply
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = Users.query.filter(Users.matrix_id == sender).one_or_none()
|
||||||
|
if user is None:
|
||||||
|
reply = "You are currently not authorized on pnut.io"
|
||||||
|
else:
|
||||||
|
pnutpy.api.add_authorization_token(user.pnut_user_token)
|
||||||
|
channel, meta = pnutpy.api.get_channel(channel_id, include_raw=1)
|
||||||
|
create_room(channel, user.matrix_id)
|
||||||
|
reply = "is working?"
|
||||||
|
|
||||||
|
except pnutpy.errors.PnutAuthAPIException as e:
|
||||||
|
reply = "You are currently not authorized on pnut.io"
|
||||||
|
|
||||||
|
except pnutpy.errors.PnutPermissionDenied:
|
||||||
|
reply = "You are not authorized for this channel"
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
logging.exception('!join')
|
||||||
|
reply = "Error! There was a problem joining the channel."
|
||||||
|
|
||||||
|
return reply
|
||||||
|
|
Loading…
Reference in a new issue