diff options
author | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-05-09 16:44:13 +0200 |
---|---|---|
committer | Pierre de Lacroix <pierre@pdelacroix.com> | 2020-05-25 23:29:17 +0200 |
commit | 98e40cb578ecec91a1c0842a4e2376cd9953dcfb (patch) | |
tree | 7d63815f6046ec46045e81c05619934b14374312 /lib | |
parent | add some more filtering methods and reduce duplicate code (diff) |
add endpoint for getting room state
Diffstat (limited to 'lib')
-rw-r--r-- | lib/polyjuice/client/endpoint/get_rooms_state.ex | 72 | ||||
-rw-r--r-- | lib/polyjuice/client/room.ex | 25 |
2 files changed, 97 insertions, 0 deletions
diff --git a/lib/polyjuice/client/endpoint/get_rooms_state.ex b/lib/polyjuice/client/endpoint/get_rooms_state.ex new file mode 100644 index 0000000..4d05dd5 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_rooms_state.ex @@ -0,0 +1,72 @@ +# 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.GetRoomsState do + @moduledoc """ + Get a room state. + + https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey + https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-rooms-roomid-state + + """ + + @type t :: %__MODULE__{ + room: String.t(), + event_type: String.t(), + state_key: String.t() + } + + @enforce_keys [:room, :event_type, :state_key] + defstruct [ + :room, + :event_type, + :state_key + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec( + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: room, + event_type: event_type, + state_key: state_key + }, + base_url + ) do + e = &URI.encode_www_form/1 + + path = + if event_type == nil do + "#{Polyjuice.Client.prefix_r0()}/rooms/#{e.(room)}/state" + else + "#{Polyjuice.Client.prefix_r0()}/rooms/#{e.(room)}/state/#{e.(event_type)}/#{ + e.(state_key) + }" + end + + url = URI.merge(base_url, path) + + %Polyjuice.Client.Endpoint.HttpSpec{ + method: :get, + headers: [ + {"Accept", "application/json"} + ], + url: to_string(url) + } + end + + def transform_http_result(req, status_code, resp_headers, body) do + Polyjuice.Client.Endpoint.parse_response(req, status_code, resp_headers, body) + end + end +end diff --git a/lib/polyjuice/client/room.ex b/lib/polyjuice/client/room.ex index f640861..886a38e 100644 --- a/lib/polyjuice/client/room.ex +++ b/lib/polyjuice/client/room.ex @@ -265,4 +265,29 @@ defmodule Polyjuice.Client.Room do fn _ -> nil end ) end + + @doc """ + Get a room state. + If `event_type` is not provided, returns a list of events. + If `event_type` and `state_key` are provided, returns an event content. + `state_key` can be omitted but this will return events that have a blank state key, not events that have "any state key". + """ + @spec get_state( + client_api :: Polyjuice.Client.API.t(), + room :: String.t(), + event_type :: String.t(), + state_key :: String.t() + ) :: {:ok, String.t()} | Any + def get_state(client_api, room, event_type \\ nil, state_key \\ "") + when (is_nil(event_type) or is_binary(event_type)) and is_binary(state_key) and + is_binary(room) do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetRoomsState{ + room: room, + event_type: event_type, + state_key: state_key + } + ) + end end |