diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2020-03-15 22:38:29 -0400 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2020-03-15 22:38:29 -0400 |
commit | fdfd1bf803f804e66bce690d875f6aac610490df (patch) | |
tree | fc70fdf361a3460de40869503f2bb54d3311c44d /lib | |
parent | mix format (diff) |
add endpoint for setting room state
Diffstat (limited to 'lib')
-rw-r--r-- | lib/polyjuice/client/endpoint/put_rooms_state.ex | 79 | ||||
-rw-r--r-- | lib/polyjuice/client/room.ex | 23 |
2 files changed, 102 insertions, 0 deletions
diff --git a/lib/polyjuice/client/endpoint/put_rooms_state.ex b/lib/polyjuice/client/endpoint/put_rooms_state.ex new file mode 100644 index 0000000..c5f6fa1 --- /dev/null +++ b/lib/polyjuice/client/endpoint/put_rooms_state.ex @@ -0,0 +1,79 @@ +# Copyright 2020 Hubert Chathi <hubert@uhoreg.ca> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Polyjuice.Client.Endpoint.PutRoomsState do + @moduledoc """ + Send a state event to a room. + + https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-rooms-roomid-state-eventtype-statekey + """ + + @type t :: %__MODULE__{ + room: String.t(), + event_type: String.t(), + state_key: String.t(), + content: map + } + + @enforce_keys [:room, :event_type, :state_key, :content] + defstruct [ + :room, + :event_type, + :state_key, + :content + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec( + %Polyjuice.Client.Endpoint.PutRoomsState{ + room: room, + event_type: event_type, + state_key: state_key, + content: content + }, + base_url + ) do + e = &URI.encode_www_form/1 + body = Poison.encode!(content) + + %Polyjuice.Client.Endpoint.HttpSpec{ + method: :put, + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + url: + URI.merge( + base_url, + "#{Polyjuice.Client.prefix_r0()}/rooms/#{e.(room)}/state/#{e.(event_type)}/#{ + e.(state_key) + }" + ) + |> to_string(), + body: body, + transform: &Polyjuice.Client.Endpoint.PutRoomsSend.transform/3 + } + end + end + + def transform(status_code, _resp_headers, body) do + case status_code do + 200 -> + {:ok, body |> Poison.decode!() |> Map.get("event_id")} + + _ -> + {:error, status_code, body} + end + end +end diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex index f93bfd6..0196571 100644 --- a/lib/polyjuice/client/room.ex +++ b/lib/polyjuice/client/room.ex @@ -106,6 +106,29 @@ defmodule Polyjuice.Client.Room do end @doc """ + Send a state event to a room. + """ + @spec send_state_event( + client_api :: Polyjuice.Client.API.t(), + room :: String.t(), + event_type :: String.t(), + state_key :: String.t(), + event :: map + ) :: String.t() + def send_state_event(client_api, room, event_type, state_key \\ "", event) + when is_binary(event_type) and is_binary(state_key) and is_map(event) and is_binary(room) do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PutRoomsState{ + room: room, + event_type: event_type, + state_key: state_key, + content: event + } + ) + end + + @doc """ Update the client's read receipt (of the given type) to the given message in the given room. """ |