Polyjuice Util v0.2.1 Polyjuice.Util.RoomVersion View Source

Functions related to room versions.

Link to this section Summary

Types

The state of a room.

Functions

Determine if an event is allowed to be sent to a room, given the room state.

Compute the content hash for an event.

Get the required power level for sending an event.

Get the room version from an m.room.create event.

Get the user's power level, given the room state.

Calculate the redacted version of an event for the given room version.

Link to this section Types

Link to this type

state()

View Source
state() :: %{optional({String.t(), String.t()}) => map()}

The state of a room.

A map from {event_type, state_key} to state event.

Link to this section Functions

Link to this function

authorized?(room_version, event, state, auth_events \\ nil)

View Source
authorized?(
  room_version :: String.t(),
  event :: map(),
  state :: state(),
  auth_events :: [map()] | nil
) :: boolean()

Determine if an event is allowed to be sent to a room, given the room state.

Example:

iex> Polyjuice.Util.RoomVersion.authorized?(
...>   "1",
...>   %{
...>     "sender" => "@u:domain",
...>     "type" => "m.room.create",
...>     "room_id" => "!foo:domain",
...>     "content" => %{
...>       "creator" => "@u:domain",
...>     }
...>   },
...>   %{}
...> )
true
Link to this function

compute_content_hash(room_version, event)

View Source
compute_content_hash(room_version :: String.t(), event :: map()) ::
  {:ok, binary()} | :error

Compute the content hash for an event.

Link to this function

get_required_pl(room_version, event_type, is_state, state)

View Source
get_required_pl(
  room_version :: String.t(),
  event_type :: String.t() | [String.t()],
  is_state :: boolean(),
  state :: state()
) :: {:ok, integer()} | :error

Get the required power level for sending an event.

event_type is either a string indicating one of the keys at the top level of the m.room.power_levels event (ban, invite, kick, redact), or a list indicating the path within the m.room.power_levels event to check (e.g. ["events", "m.room.topic"] or ["notifications", "room"]).

Examples:

iex> Polyjuice.Util.RoomVersion.get_required_pl(
...>   "1",
...>   ["events", "m.room.message"],
...>   false,
...>   %{
...>     {"m.room.power_levels", ""} => %{
...>       "type" => "m.room.power_levels",
...>       "state_key" => "",
...>       "sender" => "@alice:example.org",
...>       "content" => %{
...>         "events" => %{
...>           "m.room.topic" => 75
...>         }
...>       }
...>     }
...>   }
...> )
{:ok, 0}

iex> Polyjuice.Util.RoomVersion.get_required_pl(
...>   "1",
...>   ["events", "m.room.name"],
...>   true,
...>   %{
...>     {"m.room.power_levels", ""} => %{
...>       "type" => "m.room.power_levels",
...>       "state_key" => "",
...>       "sender" => "@alice:example.org",
...>       "content" => %{
...>         "events" => %{
...>           "m.room.topic" => 75
...>         }
...>       }
...>     }
...>   }
...> )
{:ok, 50}

iex> Polyjuice.Util.RoomVersion.get_required_pl(
...>   "1",
...>   ["events", "m.room.topic"],
...>   true,
...>   %{
...>     {"m.room.power_levels", ""} => %{
...>       "type" => "m.room.power_levels",
...>       "state_key" => "",
...>       "sender" => "@alice:example.org",
...>       "content" => %{
...>         "events" => %{
...>           "m.room.topic" => 75
...>         }
...>       }
...>     }
...>   }
...> )
{:ok, 75}
Link to this function

get_room_version(event)

View Source
get_room_version(event :: map()) :: String.t()

Get the room version from an m.room.create event.

Examples:

iex> Polyjuice.Util.RoomVersion.get_room_version(%{
...>   "type" => "m.room.create",
...>   "sender" => "@alice:example.org",
...>   "content" => %{
...>     "room_version" => "2"
...>   }
...> })
"2"
Link to this function

get_user_pl(room_version, user, state)

View Source
get_user_pl(room_version :: String.t(), user :: String.t(), state :: state()) ::
  {:ok, integer()} | :error

Get the user's power level, given the room state.

Examples:

iex> Polyjuice.Util.RoomVersion.get_user_pl(
...>   "1",
...>   "@alice:example.org",
...>   %{
...>     {"m.room.power_levels", ""} => %{
...>       "type" => "m.room.power_levels",
...>       "state_key" => "",
...>       "sender" => "@alice:example.org",
...>       "content" => %{
...>         "users" => %{
...>           "@alice:example.org" => 75
...>         }
...>       }
...>     }
...>   }
...> )
{:ok, 75}

iex> Polyjuice.Util.RoomVersion.get_user_pl(
...>   "1",
...>   "@bob:example.org",
...>   %{
...>     {"m.room.power_levels", ""} => %{
...>       "type" => "m.room.power_levels",
...>       "state_key" => "",
...>       "sender" => "@alice:example.org",
...>       "content" => %{
...>         "users" => %{
...>           "@alice:example.org" => 75
...>         }
...>       }
...>     }
...>   }
...> )
{:ok, 0}
Link to this function

redact(room_version, event)

View Source
redact(room_version :: String.t(), event :: map()) :: {:ok, map()} | :error

Calculate the redacted version of an event for the given room version.

Examples:

iex> Polyjuice.Util.RoomVersion.redact(
...>   "1",
...>   %{
...>     "content" => %{
...>       "body" => "Here is the message content"
...>     },
...>     "event_id" => "$0:domain",
...>     "origin" => "domain",
...>     "origin_server_ts" => 1000000,
...>     "type" => "m.room.message",
...>     "room_id" => "!r:domain",
...>     "sender" => "@u:domain",
...>     "signatures" => %{},
...>     "unsigned" => %{
...>       "age_ts" => 1000000
...>     }
...>   }
...> )
{
  :ok,
  %{
    "content" => %{},
    "event_id" => "$0:domain",
    "origin" => "domain",
    "origin_server_ts" => 1000000,
    "type" => "m.room.message",
    "room_id" => "!r:domain",
    "sender" => "@u:domain",
    "signatures" => %{}
  }
}

iex> Polyjuice.Util.RoomVersion.redact(
...>   "unknown room version",
...>   %{}
...> )
:error