Use updated ModulesAPI method to resolve alias to room_id
Monkeypatch BulkPushRuleEvaluator to not spam notifications Update README
This commit is contained in:
parent
fa065cd52b
commit
b46fdd6752
|
@ -1,6 +1,7 @@
|
||||||
# ppfun-auth-module for matrix-synapse
|
# ppfun-auth-module for matrix-synapse
|
||||||
|
|
||||||
Allows pixelplanet users to login with their credentials on matrix.
|
Allows pixelplanet users to login with their credentials on matrix.
|
||||||
|
Required synapse version is 1.69+
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
@ -16,8 +17,11 @@ modules:
|
||||||
ppfunurl: "http://local.pixelplanet.url:port"
|
ppfunurl: "http://local.pixelplanet.url:port"
|
||||||
# if true, only mail-verified users can log in
|
# if true, only mail-verified users can log in
|
||||||
verified: true
|
verified: true
|
||||||
# rooms that will be automatically joined on login
|
# rooms that will be automatically joined on login, must be a list
|
||||||
autojoin_rooms: ['#pp_en:pixelplanet.fun', '#pp_int:pixelplanet.fun']
|
autojoin_rooms: ['#pp_en:pixelplanet.fun', '#pp_int:pixelplanet.fun']
|
||||||
|
# dont notify users in pp_ channels
|
||||||
|
# when true, it triggers a monkeypatch that blocks notifications in bridged channels in order to avoid flooding the synapse database with push_actions
|
||||||
|
block_notify: false
|
||||||
```
|
```
|
||||||
## References
|
## References
|
||||||
|
|
||||||
|
|
|
@ -10,23 +10,62 @@ import logging
|
||||||
import synapse
|
import synapse
|
||||||
from synapse import module_api
|
from synapse import module_api
|
||||||
from synapse.types import RoomAlias
|
from synapse.types import RoomAlias
|
||||||
|
from synapse.push.bulk_push_rule_evaluator import BulkPushRuleEvaluator
|
||||||
|
from synapse.api.constants import EventTypes, Membership
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def monkeyPatchPushRules(store):
|
||||||
|
"""Monkey patching BulkPushRuleEvaluator to not
|
||||||
|
send notifications if message is in pp_ channel,
|
||||||
|
otherwise event_push_actions will get filled up with unread
|
||||||
|
notification for pp_ users, creating and infinite growing database
|
||||||
|
"""
|
||||||
|
originalFunction = BulkPushRuleEvaluator.action_for_event_by_user
|
||||||
|
# cache for whether room is bridge room that gets notifications blocked or not
|
||||||
|
room_block_notif = {}
|
||||||
|
|
||||||
|
async def monkeyPatch(self, event, context):
|
||||||
|
# always notify on invites
|
||||||
|
if event.type != EventTypes.Member or event.membership != Membership.INVITE:
|
||||||
|
room_id = event.room_id
|
||||||
|
if room_id in room_block_notif:
|
||||||
|
if room_block_notif[room_id]:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
aliases = await store.get_aliases_for_room(room_id)
|
||||||
|
for alias in aliases:
|
||||||
|
if alias.startswith('#pp_'):
|
||||||
|
room_block_notif[room_id] = True
|
||||||
|
return
|
||||||
|
room_block_notif[room_id] = False
|
||||||
|
return await originalFunction(self, event, context)
|
||||||
|
|
||||||
|
BulkPushRuleEvaluator.action_for_event_by_user = monkeyPatch
|
||||||
|
logger.info('Monkey patched BulkPushRuleEvaluator')
|
||||||
|
|
||||||
|
|
||||||
class PPfunAuthProvider:
|
class PPfunAuthProvider:
|
||||||
def __init__(self, config: dict, api: module_api):
|
def __init__(self, config: dict, api: module_api):
|
||||||
self.api = api
|
self.api = api
|
||||||
|
|
||||||
if 'ppfunurl' not in config:
|
if 'ppfunurl' not in config:
|
||||||
raise Exception('Pixelplanet ppfunurl not configured')
|
raise Exception('Pixelplanet ppfunurl not configured')
|
||||||
self.ppfunurl = config["ppfunurl"]
|
self.ppfunurl = config["ppfunurl"]
|
||||||
|
|
||||||
if 'apisocketkey' not in config:
|
if 'apisocketkey' not in config:
|
||||||
raise Exception('Pixelplanet apisocketkey not configured')
|
raise Exception('Pixelplanet apisocketkey not configured')
|
||||||
self.apisocketkey = config["apisocketkey"]
|
self.apisocketkey = config["apisocketkey"]
|
||||||
|
|
||||||
if 'verified' in config:
|
if 'verified' in config:
|
||||||
self.check_verified = config['verified']
|
self.check_verified = config['verified']
|
||||||
else:
|
else:
|
||||||
self.check_verified = False
|
self.check_verified = False
|
||||||
|
|
||||||
|
if 'block_notify' in config and config['block_notify']:
|
||||||
|
monkeyPatchPushRules(api._store)
|
||||||
|
|
||||||
|
self.autojoin_rooms = []
|
||||||
if 'autojoin_rooms' in config:
|
if 'autojoin_rooms' in config:
|
||||||
self.parsed_rooms = False
|
self.parsed_rooms = False
|
||||||
self.autojoin_rooms = config["autojoin_rooms"]
|
self.autojoin_rooms = config["autojoin_rooms"]
|
||||||
|
@ -38,19 +77,21 @@ class PPfunAuthProvider:
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# resolve room_aliases to room_ids
|
|
||||||
async def translate_room_aliases_to_ids(
|
async def translate_room_aliases_to_ids(
|
||||||
self,
|
self,
|
||||||
room_aliases: List[str],
|
room_aliases: List[str],
|
||||||
) -> List[str]:
|
) -> List[str]:
|
||||||
|
"""Resole list of room_aliases to room_ids
|
||||||
|
"""
|
||||||
room_ids = []
|
room_ids = []
|
||||||
logger.info('Translating room_aliases to ids')
|
logger.info('Translating room_aliases to ids')
|
||||||
for alias in room_aliases:
|
for alias in room_aliases:
|
||||||
aliasObject = RoomAlias.from_string(alias)
|
try:
|
||||||
room = await self.api._store.get_association_from_room_alias(aliasObject)
|
(room_id, hosts) = await self.api.lookup_room_alias(alias)
|
||||||
if room is not None:
|
logger.info('Map alias %s to room %s', alias, room_id)
|
||||||
logger.info('Map alias %s to room %s', alias, room.room_id)
|
room_ids.append(room_id)
|
||||||
room_ids.append(room.room_id)
|
except:
|
||||||
|
logger.warning('Could not resolve room %s', alias)
|
||||||
return room_ids;
|
return room_ids;
|
||||||
|
|
||||||
async def check_credentials(
|
async def check_credentials(
|
||||||
|
@ -79,12 +120,13 @@ class PPfunAuthProvider:
|
||||||
logger.warning('Could not login via ppfun: %s', e)
|
logger.warning('Could not login via ppfun: %s', e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Make sure that email is set correctly when logging in
|
|
||||||
async def set_email(
|
async def set_email(
|
||||||
self,
|
self,
|
||||||
user_id: str,
|
user_id: str,
|
||||||
email: str
|
email: str
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
"""making sure that email is the same between ppfun and matrix user
|
||||||
|
"""
|
||||||
threepid_dict_list = await self.api.get_threepids_for_user(user_id)
|
threepid_dict_list = await self.api.get_threepids_for_user(user_id)
|
||||||
for threepid_dict in threepid_dict_list:
|
for threepid_dict in threepid_dict_list:
|
||||||
if threepid_dict['address'] == email:
|
if threepid_dict['address'] == email:
|
||||||
|
@ -105,6 +147,8 @@ class PPfunAuthProvider:
|
||||||
self,
|
self,
|
||||||
user_id: str,
|
user_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
if not self.autojoin_rooms:
|
||||||
|
return
|
||||||
if not self.parsed_rooms:
|
if not self.parsed_rooms:
|
||||||
self.autojoin_rooms = await self.translate_room_aliases_to_ids(self.autojoin_rooms)
|
self.autojoin_rooms = await self.translate_room_aliases_to_ids(self.autojoin_rooms)
|
||||||
self.parsed_rooms = True
|
self.parsed_rooms = True
|
||||||
|
|
Loading…
Reference in New Issue
Block a user