Polyjuice Util v0.2.2 Polyjuice.Util.RoomVersion View Source
Functions related to room versions.
Link to this section Summary
Functions
Determine if an event is allowed to be sent to a room, given the room state.
Compute the content hash for an event.
Compute the reference 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
The state of a room.
A map from {event_type, state_key}
to state event.
Link to this section Functions
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
Compute the content hash for an event.
Compute the reference hash for an event.
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}
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"
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}
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