admin.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import abc
  15. import logging
  16. from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set
  17. from synapse.api.constants import Membership
  18. from synapse.events import EventBase
  19. from synapse.types import JsonDict, RoomStreamToken, StateMap, UserID
  20. from synapse.visibility import filter_events_for_client
  21. from ._base import BaseHandler
  22. if TYPE_CHECKING:
  23. from synapse.server import HomeServer
  24. logger = logging.getLogger(__name__)
  25. class AdminHandler(BaseHandler):
  26. def __init__(self, hs: "HomeServer"):
  27. super().__init__(hs)
  28. self.storage = hs.get_storage()
  29. self.state_store = self.storage.state
  30. async def get_whois(self, user: UserID) -> JsonDict:
  31. connections = []
  32. sessions = await self.store.get_user_ip_and_agents(user)
  33. for session in sessions:
  34. connections.append(
  35. {
  36. "ip": session["ip"],
  37. "last_seen": session["last_seen"],
  38. "user_agent": session["user_agent"],
  39. }
  40. )
  41. ret = {
  42. "user_id": user.to_string(),
  43. "devices": {"": {"sessions": [{"connections": connections}]}},
  44. }
  45. return ret
  46. async def get_user(self, user: UserID) -> Optional[JsonDict]:
  47. """Function to get user details"""
  48. ret = await self.store.get_user_by_id(user.to_string())
  49. if ret:
  50. profile = await self.store.get_profileinfo(user.localpart)
  51. threepids = await self.store.user_get_threepids(user.to_string())
  52. external_ids = [
  53. ({"auth_provider": auth_provider, "external_id": external_id})
  54. for auth_provider, external_id in await self.store.get_external_ids_by_user(
  55. user.to_string()
  56. )
  57. ]
  58. ret["displayname"] = profile.display_name
  59. ret["avatar_url"] = profile.avatar_url
  60. ret["threepids"] = threepids
  61. ret["external_ids"] = external_ids
  62. return ret
  63. async def export_user_data(self, user_id: str, writer: "ExfiltrationWriter") -> Any:
  64. """Write all data we have on the user to the given writer.
  65. Args:
  66. user_id: The user ID to fetch data of.
  67. writer: The writer to write to.
  68. Returns:
  69. Resolves when all data for a user has been written.
  70. The returned value is that returned by `writer.finished()`.
  71. """
  72. # Get all rooms the user is in or has been in
  73. rooms = await self.store.get_rooms_for_local_user_where_membership_is(
  74. user_id,
  75. membership_list=(
  76. Membership.JOIN,
  77. Membership.LEAVE,
  78. Membership.BAN,
  79. Membership.INVITE,
  80. ),
  81. )
  82. # We only try and fetch events for rooms the user has been in. If
  83. # they've been e.g. invited to a room without joining then we handle
  84. # those separately.
  85. rooms_user_has_been_in = await self.store.get_rooms_user_has_been_in(user_id)
  86. for index, room in enumerate(rooms):
  87. room_id = room.room_id
  88. logger.info(
  89. "[%s] Handling room %s, %d/%d", user_id, room_id, index + 1, len(rooms)
  90. )
  91. forgotten = await self.store.did_forget(user_id, room_id)
  92. if forgotten:
  93. logger.info("[%s] User forgot room %d, ignoring", user_id, room_id)
  94. continue
  95. if room_id not in rooms_user_has_been_in:
  96. # If we haven't been in the rooms then the filtering code below
  97. # won't return anything, so we need to handle these cases
  98. # explicitly.
  99. if room.membership == Membership.INVITE:
  100. event_id = room.event_id
  101. invite = await self.store.get_event(event_id, allow_none=True)
  102. if invite:
  103. invited_state = invite.unsigned["invite_room_state"]
  104. writer.write_invite(room_id, invite, invited_state)
  105. continue
  106. # We only want to bother fetching events up to the last time they
  107. # were joined. We estimate that point by looking at the
  108. # stream_ordering of the last membership if it wasn't a join.
  109. if room.membership == Membership.JOIN:
  110. stream_ordering = self.store.get_room_max_stream_ordering()
  111. else:
  112. stream_ordering = room.stream_ordering
  113. from_key = RoomStreamToken(0, 0)
  114. to_key = RoomStreamToken(None, stream_ordering)
  115. # Events that we've processed in this room
  116. written_events: Set[str] = set()
  117. # We need to track gaps in the events stream so that we can then
  118. # write out the state at those events. We do this by keeping track
  119. # of events whose prev events we haven't seen.
  120. # Map from event ID to prev events that haven't been processed,
  121. # dict[str, set[str]].
  122. event_to_unseen_prevs = {}
  123. # The reverse mapping to above, i.e. map from unseen event to events
  124. # that have the unseen event in their prev_events, i.e. the unseen
  125. # events "children".
  126. unseen_to_child_events: Dict[str, Set[str]] = {}
  127. # We fetch events in the room the user could see by fetching *all*
  128. # events that we have and then filtering, this isn't the most
  129. # efficient method perhaps but it does guarantee we get everything.
  130. while True:
  131. events, _ = await self.store.paginate_room_events(
  132. room_id, from_key, to_key, limit=100, direction="f"
  133. )
  134. if not events:
  135. break
  136. from_key = events[-1].internal_metadata.after
  137. events = await filter_events_for_client(self.storage, user_id, events)
  138. writer.write_events(room_id, events)
  139. # Update the extremity tracking dicts
  140. for event in events:
  141. # Check if we have any prev events that haven't been
  142. # processed yet, and add those to the appropriate dicts.
  143. unseen_events = set(event.prev_event_ids()) - written_events
  144. if unseen_events:
  145. event_to_unseen_prevs[event.event_id] = unseen_events
  146. for unseen in unseen_events:
  147. unseen_to_child_events.setdefault(unseen, set()).add(
  148. event.event_id
  149. )
  150. # Now check if this event is an unseen prev event, if so
  151. # then we remove this event from the appropriate dicts.
  152. for child_id in unseen_to_child_events.pop(event.event_id, []):
  153. event_to_unseen_prevs[child_id].discard(event.event_id)
  154. written_events.add(event.event_id)
  155. logger.info(
  156. "Written %d events in room %s", len(written_events), room_id
  157. )
  158. # Extremities are the events who have at least one unseen prev event.
  159. extremities = (
  160. event_id
  161. for event_id, unseen_prevs in event_to_unseen_prevs.items()
  162. if unseen_prevs
  163. )
  164. for event_id in extremities:
  165. if not event_to_unseen_prevs[event_id]:
  166. continue
  167. state = await self.state_store.get_state_for_event(event_id)
  168. writer.write_state(room_id, event_id, state)
  169. return writer.finished()
  170. class ExfiltrationWriter(metaclass=abc.ABCMeta):
  171. """Interface used to specify how to write exported data."""
  172. @abc.abstractmethod
  173. def write_events(self, room_id: str, events: List[EventBase]) -> None:
  174. """Write a batch of events for a room."""
  175. raise NotImplementedError()
  176. @abc.abstractmethod
  177. def write_state(
  178. self, room_id: str, event_id: str, state: StateMap[EventBase]
  179. ) -> None:
  180. """Write the state at the given event in the room.
  181. This only gets called for backward extremities rather than for each
  182. event.
  183. """
  184. raise NotImplementedError()
  185. @abc.abstractmethod
  186. def write_invite(
  187. self, room_id: str, event: EventBase, state: StateMap[dict]
  188. ) -> None:
  189. """Write an invite for the room, with associated invite state.
  190. Args:
  191. room_id: The room ID the invite is for.
  192. event: The invite event.
  193. state: A subset of the state at the invite, with a subset of the
  194. event keys (type, state_key content and sender).
  195. """
  196. raise NotImplementedError()
  197. @abc.abstractmethod
  198. def finished(self) -> Any:
  199. """Called when all data has successfully been exported and written.
  200. This functions return value is passed to the caller of
  201. `export_user_data`.
  202. """
  203. raise NotImplementedError()