support for mentions
This commit is contained in:
parent
715dedb4f7
commit
7199f23324
1 changed files with 51 additions and 11 deletions
|
@ -8,6 +8,7 @@ from typing import Type, Tuple
|
||||||
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
||||||
from maubot import Plugin, MessageEvent
|
from maubot import Plugin, MessageEvent
|
||||||
from maubot.handlers import command
|
from maubot.handlers import command
|
||||||
|
import mautrix.util
|
||||||
from mautrix.types import (
|
from mautrix.types import (
|
||||||
EventType,
|
EventType,
|
||||||
MemberStateEventContent,
|
MemberStateEventContent,
|
||||||
|
@ -69,7 +70,7 @@ class Ubottu(Plugin):
|
||||||
# Fetch the state of the room, focusing on power levels
|
# Fetch the state of the room, focusing on power levels
|
||||||
levels = await self.get_power_levels(evt.room_id)
|
levels = await self.get_power_levels(evt.room_id)
|
||||||
for user_id, level in levels.users.items():
|
for user_id, level in levels.users.items():
|
||||||
if level > 50:
|
if level >= 50:
|
||||||
high_level_user_ids.append(user_id)
|
high_level_user_ids.append(user_id)
|
||||||
else:
|
else:
|
||||||
self.log.info("No power levels found in {evt.room_id}")
|
self.log.info("No power levels found in {evt.room_id}")
|
||||||
|
@ -142,6 +143,7 @@ class Ubottu(Plugin):
|
||||||
name = data['name']
|
name = data['name']
|
||||||
value = data['value']
|
value = data['value']
|
||||||
ftype = data['ftype']
|
ftype = data['ftype']
|
||||||
|
user_ids = data['user_ids']
|
||||||
if ftype == 'ALIAS':
|
if ftype == 'ALIAS':
|
||||||
command_name = value
|
command_name = value
|
||||||
url = api_url + command_name + '/?format=json'
|
url = api_url + command_name + '/?format=json'
|
||||||
|
@ -149,19 +151,57 @@ class Ubottu(Plugin):
|
||||||
if resp and resp.status == 200:
|
if resp and resp.status == 200:
|
||||||
data = await resp.json()
|
data = await resp.json()
|
||||||
value = data['value']
|
value = data['value']
|
||||||
if "{moderators}" in value:
|
|
||||||
moderators = await self.get_room_mods_and_admins(evt)
|
content = {}
|
||||||
if isinstance(moderators, list) and len(moderators) > 0:
|
content['m.mentions'] = {}
|
||||||
|
moderators = []
|
||||||
|
mentions = 0
|
||||||
m_str = ''
|
m_str = ''
|
||||||
|
m_str_html = ''
|
||||||
|
user_ids = []
|
||||||
|
value = data['value']
|
||||||
|
formatted_value = value
|
||||||
|
|
||||||
|
if '.mentions' in value:
|
||||||
|
value = value.replace('.mentions', '')
|
||||||
|
mentions = 1
|
||||||
|
|
||||||
|
#find matrix user ids in message
|
||||||
|
user_id_re = '@[\w.-]+:[a-zA-Z\d.-]+\.[a-zA-Z]{2,}'
|
||||||
|
user_ids = re.findall(user_id_re, value)
|
||||||
|
if len(user_ids) > 0:
|
||||||
|
for user_id in user_ids:
|
||||||
|
formatted_value = formatted_value.replace(user_id, '<a href="https://matrix.to/#/' + user_id + '">' + user_id + '</a>')
|
||||||
|
|
||||||
|
|
||||||
|
#find room moderators and add to mentions if needed
|
||||||
|
if '{moderators}' in value:
|
||||||
|
moderators = await self.get_room_mods_and_admins(evt)
|
||||||
|
if mentions == 1:
|
||||||
|
value = value.replace('{moderators}', '')
|
||||||
|
|
||||||
|
formatted_value = value
|
||||||
|
|
||||||
|
if mentions == 0:
|
||||||
for m in moderators:
|
for m in moderators:
|
||||||
m_str = m_str + 'https://matrix.to/#/' + m + ' '
|
m_str_html = "<a href='https://matrix.to/#/'>" + m + '</a> ' + m_str_html
|
||||||
value = value.replace("{moderators}", m_str)
|
m_str = m_str + ' ' + m
|
||||||
else:
|
value = value.replace('{moderators}', m_str)
|
||||||
return False
|
formatted_value = formatted_value.replace('{moderators}', m_str_html)
|
||||||
|
|
||||||
|
value = re.sub(' +', ' ', value)
|
||||||
|
|
||||||
|
content['m.mentions']['user_ids'] = list(set(list(moderators) + list(data['user_ids'])))
|
||||||
|
self.log.info(content['m.mentions']['user_ids'])
|
||||||
|
content['formatted_body'] = mautrix.util.markdown.render(formatted_value, allow_html=True)
|
||||||
|
content['msgtype'] = "m.text"
|
||||||
|
content['format'] = 'org.matrix.custom.html'
|
||||||
|
content['body'] = mautrix.util.markdown.render(value, allow_html=True)
|
||||||
if to_user:
|
if to_user:
|
||||||
await evt.respond(to_user + ': ' + value)
|
content['body'] = to_user + ': ' + content['body']
|
||||||
else:
|
content['formatted_body'] = to_user + content['formatted_body']
|
||||||
await evt.respond(value)
|
|
||||||
|
await evt.respond(content)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue