mirror of
https://mau.dev/maunium/synapse.git
synced 2024-12-17 21:53:51 +01:00
WIP impl commiting to get feedback
This commit is contained in:
parent
c055c91655
commit
df1e4f259f
1 changed files with 74 additions and 6 deletions
|
@ -40,7 +40,8 @@ class ResourceLimitsServerNotices(object):
|
||||||
|
|
||||||
self._notified_of_blocking = set()
|
self._notified_of_blocking = set()
|
||||||
self._resouce_limited = False
|
self._resouce_limited = False
|
||||||
|
self._message_handler = hs.get_message_handler()
|
||||||
|
self._state = hs.get_state_handler()
|
||||||
# Config checks?
|
# Config checks?
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
@ -57,6 +58,72 @@ class ResourceLimitsServerNotices(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._limit_usage_by_mau is True:
|
if self._limit_usage_by_mau is True:
|
||||||
|
room_id = yield self._server_notices_manager.get_notice_room_for_user(user_id)
|
||||||
|
|
||||||
|
|
||||||
|
# Alternate impl - currently inlcuded because I'm not sure I am on
|
||||||
|
# the right track and want to share WIP
|
||||||
|
|
||||||
|
# logger.info("GET STATE EVENTS")
|
||||||
|
# currently_blocked = False
|
||||||
|
# events = []
|
||||||
|
# try:
|
||||||
|
# events = yield self._message_handler.get_state_events(user_id, room_id, types=[(EventTypes.Pinned, None)])
|
||||||
|
# except AuthError as e:
|
||||||
|
# # The user has yet to join the server notices room
|
||||||
|
# pass
|
||||||
|
#
|
||||||
|
# pinned_event_refs = []
|
||||||
|
# for e in events:
|
||||||
|
# logger.info('events %s' % e)
|
||||||
|
# logger.info(type(e))
|
||||||
|
# for key, event_ids in e['content'].items():
|
||||||
|
# logger.info('Key Event %s %s' % (key, event_ids))
|
||||||
|
# if key == 'pinned':
|
||||||
|
# pinned_event_refs = event_ids
|
||||||
|
#
|
||||||
|
# logger.info('pinned_event_refs %s' % pinned_event_refs)
|
||||||
|
#
|
||||||
|
# events = yield self._store.get_events(pinned_event_refs)
|
||||||
|
# logger.info(events)
|
||||||
|
# for event_id, event in events.items():
|
||||||
|
# logger.info("event_id, event event.type %s %s %s" % (event_id, event, event.type))
|
||||||
|
# if event.type == 'm.server_notice.usage_limit_reached':
|
||||||
|
# currently_blocked = True
|
||||||
|
#
|
||||||
|
# logger.info('Currently Blocked is %r' % currently_blocked)
|
||||||
|
|
||||||
|
#for e in events:
|
||||||
|
# logger.info(e)
|
||||||
|
currently_blocked = False
|
||||||
|
logger.info("GET CURRENT STATE")
|
||||||
|
pinned_state_event = yield self._state.get_current_state(room_id, event_type=EventTypes.Pinned)
|
||||||
|
logger.info(events)
|
||||||
|
logger.info(events.get('content'))
|
||||||
|
|
||||||
|
referenced_events = []
|
||||||
|
if pinned_state_event is not None:
|
||||||
|
content = pinned_state_event.get('content')
|
||||||
|
if content is not None:
|
||||||
|
referenced_events = content.get('pinned')
|
||||||
|
|
||||||
|
events = yield self._store.get_events(referenced_events)
|
||||||
|
logger.info(events)
|
||||||
|
for event_id, event in events.items():
|
||||||
|
logger.info("event_id, event event.type %s %s %s" % (event_id, event, event.type))
|
||||||
|
if event.type == 'm.server_notice.usage_limit_reached':
|
||||||
|
currently_blocked = True
|
||||||
|
|
||||||
|
logger.info("currently_blocked is %r" % currently_blocked)
|
||||||
|
|
||||||
|
#event = yield self._store.get_event(events.event_id)
|
||||||
|
#logger.info(event)
|
||||||
|
|
||||||
|
#logger.info("GET CURRENT STATE IDs")
|
||||||
|
#events = yield self._state.get_current_state_ids(room_id)
|
||||||
|
#for k,v in events.items():
|
||||||
|
# logger.info('%s %s' % (k,v))
|
||||||
|
|
||||||
timestamp = yield self._store.user_last_seen_monthly_active(user_id)
|
timestamp = yield self._store.user_last_seen_monthly_active(user_id)
|
||||||
if timestamp is None:
|
if timestamp is None:
|
||||||
# This user will be blocked from receiving the notice anyway.
|
# This user will be blocked from receiving the notice anyway.
|
||||||
|
@ -69,7 +136,8 @@ class ResourceLimitsServerNotices(object):
|
||||||
yield self.auth.check_auth_blocking()
|
yield self.auth.check_auth_blocking()
|
||||||
self._resouce_limited = False
|
self._resouce_limited = False
|
||||||
# Need to start removing notices
|
# Need to start removing notices
|
||||||
if user_id in self._notified_of_blocking:
|
# if user_id in self._notified_of_blocking:
|
||||||
|
if currently_blocked:
|
||||||
# Send message to remove warning
|
# Send message to remove warning
|
||||||
# send state event here
|
# send state event here
|
||||||
# How do I do this? if drop the id, how to refer to it?
|
# How do I do this? if drop the id, how to refer to it?
|
||||||
|
@ -79,14 +147,14 @@ class ResourceLimitsServerNotices(object):
|
||||||
yield self._server_notices_manager.send_notice(
|
yield self._server_notices_manager.send_notice(
|
||||||
user_id, content, EventTypes.Pinned, '',
|
user_id, content, EventTypes.Pinned, '',
|
||||||
)
|
)
|
||||||
|
logger.info('deactivate block')
|
||||||
self._notified_of_blocking.remove(user_id)
|
|
||||||
|
|
||||||
except AuthError as e:
|
except AuthError as e:
|
||||||
# Need to start notifying of blocking
|
# Need to start notifying of blocking
|
||||||
try:
|
try:
|
||||||
self._resouce_limited = True
|
self._resouce_limited = True
|
||||||
if user_id not in self._notified_of_blocking:
|
#if user_id not in self._notified_of_blocking:
|
||||||
|
if not currently_blocked:
|
||||||
# TODO use admin email contained in error once PR lands
|
# TODO use admin email contained in error once PR lands
|
||||||
content = {
|
content = {
|
||||||
'body': e.msg,
|
'body': e.msg,
|
||||||
|
@ -103,10 +171,10 @@ class ResourceLimitsServerNotices(object):
|
||||||
event.event_id,
|
event.event_id,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
logger.info("active block")
|
||||||
yield self._server_notices_manager.send_notice(
|
yield self._server_notices_manager.send_notice(
|
||||||
user_id, content, EventTypes.Pinned, '',
|
user_id, content, EventTypes.Pinned, '',
|
||||||
)
|
)
|
||||||
|
|
||||||
self._notified_of_blocking.add(user_id)
|
|
||||||
except SynapseError as e:
|
except SynapseError as e:
|
||||||
logger.error("Error sending server notice about resource limits: %s", e)
|
logger.error("Error sending server notice about resource limits: %s", e)
|
||||||
|
|
Loading…
Reference in a new issue