__init__.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from enum import Enum
  17. from twisted.internet import defer
  18. from synapse.storage.state import StateFilter
  19. MYPY = False
  20. if MYPY:
  21. import synapse.server
  22. logger = logging.getLogger(__name__)
  23. class RegistrationBehaviour(Enum):
  24. """
  25. Enum to define whether a registration request should allowed, denied, or shadow-banned.
  26. """
  27. ALLOW = "allow"
  28. SHADOW_BAN = "shadow_ban"
  29. DENY = "deny"
  30. class SpamCheckerApi:
  31. """A proxy object that gets passed to spam checkers so they can get
  32. access to rooms and other relevant information.
  33. """
  34. def __init__(self, hs: "synapse.server.HomeServer"):
  35. self.hs = hs
  36. self._store = hs.get_datastore()
  37. @defer.inlineCallbacks
  38. def get_state_events_in_room(self, room_id: str, types: tuple) -> defer.Deferred:
  39. """Gets state events for the given room.
  40. Args:
  41. room_id: The room ID to get state events in.
  42. types: The event type and state key (using None
  43. to represent 'any') of the room state to acquire.
  44. Returns:
  45. twisted.internet.defer.Deferred[list(synapse.events.FrozenEvent)]:
  46. The filtered state events in the room.
  47. """
  48. state_ids = yield defer.ensureDeferred(
  49. self._store.get_filtered_current_state_ids(
  50. room_id=room_id, state_filter=StateFilter.from_types(types)
  51. )
  52. )
  53. state = yield defer.ensureDeferred(self._store.get_events(state_ids.values()))
  54. return state.values()