user_has_power_level.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from __future__ import annotations
  17. from mautrix.appservice import IntentAPI
  18. from mautrix.errors import MatrixRequestError
  19. from mautrix.types import EventType, RoomID
  20. from .. import user as u
  21. async def user_has_power_level(
  22. room_id: RoomID, intent: IntentAPI, sender: u.User, event: str
  23. ) -> bool:
  24. if sender.is_admin:
  25. return True
  26. # Make sure the state store contains the power levels.
  27. try:
  28. await intent.get_power_levels(room_id)
  29. except MatrixRequestError:
  30. return False
  31. event_type = EventType.find(f"net.maunium.signal.{event}", t_class=EventType.Class.STATE)
  32. return await intent.state_store.has_power_level(room_id, sender.mxid, event_type)