diff options
author | multi prise <korokoko.toi@gmail.com> | 2021-02-06 01:03:34 +0000 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2021-02-06 01:03:34 +0000 |
commit | 5e5da3051a3fc932759425c39774faee2329ae6a (patch) | |
tree | 38d89654b5b66c70c7e0aab5adcdf4b9508e284b /lib | |
parent | Merge branch 'user_impersonation' into 'master' (diff) |
rename display_name_test and remove get_acount_test
corrected displayname
corrected format
Diffstat (limited to 'lib')
-rw-r--r-- | lib/polyjuice/client/endpoint/get_profile.ex | 48 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/get_profile_avatar_url.ex | 58 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/get_profile_displayname.ex | 58 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/put_profile_avatar_url.ex | 67 | ||||
-rw-r--r-- | lib/polyjuice/client/endpoint/put_profile_displayname.ex | 67 | ||||
-rw-r--r-- | lib/polyjuice/client/profile.ex | 126 |
6 files changed, 424 insertions, 0 deletions
diff --git a/lib/polyjuice/client/endpoint/get_profile.ex b/lib/polyjuice/client/endpoint/get_profile.ex new file mode 100644 index 0000000..9684c85 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile.ex @@ -0,0 +1,48 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.GetProfile do + @moduledoc """ + Get the combined profile information for this user + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(req) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(req.user_id)}", + headers: [ + {"Accept", "application/json"} + ] + ) + 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/endpoint/get_profile_avatar_url.ex b/lib/polyjuice/client/endpoint/get_profile_avatar_url.ex new file mode 100644 index 0000000..3242407 --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile_avatar_url.ex @@ -0,0 +1,58 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.GetProfileAvatarUrl do + @moduledoc """ + Get avatar URL for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid-avatar-url + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: user_id + }) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(user_id)}/avatar_url", + headers: [ + {"Accept", "application/json"} + ], + auth_required: true + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "avatar_url")} + end + end +end diff --git a/lib/polyjuice/client/endpoint/get_profile_displayname.ex b/lib/polyjuice/client/endpoint/get_profile_displayname.ex new file mode 100644 index 0000000..305f9fb --- /dev/null +++ b/lib/polyjuice/client/endpoint/get_profile_displayname.ex @@ -0,0 +1,58 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.GetProfileDisplayname do + @moduledoc """ + Get display name for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-r0-profile-userid-displayname + """ + + @type t :: %__MODULE__{ + user_id: String.t() + } + + @enforce_keys [:user_id] + + defstruct [ + :user_id + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: user_id + }) do + e = &URI.encode_www_form/1 + + Polyjuice.Client.Endpoint.HttpSpec.get( + :r0, + "profile/#{e.(user_id)}/displayname", + headers: [ + {"Accept", "application/json"} + ], + auth_required: true + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, parsed) do + {:ok, Map.get(parsed, "displayname")} + end + end +end diff --git a/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex b/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex new file mode 100644 index 0000000..2649b1e --- /dev/null +++ b/lib/polyjuice/client/endpoint/put_profile_avatar_url.ex @@ -0,0 +1,67 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.PutProfileAvatarUrl do + @moduledoc """ + Change avatar URL for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#put-matrix-client-r0-profile-userid-avatar-url + """ + + @type t :: %__MODULE__{ + user_id: String.t(), + avatar_url: String.t() + } + + @enforce_keys [:user_id, :avatar_url] + + defstruct [ + :user_id, + :avatar_url + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: user_id, + avatar_url: avatar_url + }) do + e = &URI.encode_www_form/1 + + body = + %{avatar_url: avatar_url} + |> Jason.encode!() + + Polyjuice.Client.Endpoint.HttpSpec.put( + :r0, + "profile/#{e.(user_id)}/avatar_url", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + body: body, + auth_required: true + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + :ok + end + end +end diff --git a/lib/polyjuice/client/endpoint/put_profile_displayname.ex b/lib/polyjuice/client/endpoint/put_profile_displayname.ex new file mode 100644 index 0000000..a08ada3 --- /dev/null +++ b/lib/polyjuice/client/endpoint/put_profile_displayname.ex @@ -0,0 +1,67 @@ +# Copyright 2020 Multi Prise <multiestunhappydev@gmail.com> +# +# 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.PutProfileDisplayname do + @moduledoc """ + Change display name for the user. + + https://matrix.org/docs/spec/client_server/r0.5.0#put-matrix-client-r0-profile-userid-displayname + """ + + @type t :: %__MODULE__{ + user_id: String.t(), + displayname: String.t() + } + + @enforce_keys [:user_id, :displayname] + + defstruct [ + :user_id, + :displayname + ] + + defimpl Polyjuice.Client.Endpoint.Proto do + def http_spec(%Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: user_id, + displayname: displayname + }) do + e = &URI.encode_www_form/1 + + body = + %{displayname: displayname} + |> Jason.encode!() + + Polyjuice.Client.Endpoint.HttpSpec.put( + :r0, + "profile/#{e.(user_id)}/displayname", + headers: [ + {"Accept", "application/json"}, + {"Content-Type", "application/json"} + ], + body: body, + auth_required: true + ) + 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 + + defimpl Polyjuice.Client.Endpoint.BodyParser do + def parse(_req, _body) do + :ok + end + end +end diff --git a/lib/polyjuice/client/profile.ex b/lib/polyjuice/client/profile.ex new file mode 100644 index 0000000..74612c3 --- /dev/null +++ b/lib/polyjuice/client/profile.ex @@ -0,0 +1,126 @@ +# Copyright 2020 Multi Prise <multiprisestunhappydev@gmail.com> +# +# 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.Profile do + @moduledoc """ + Profile-related functions. + """ + require Logger + + @doc """ + Get user profile. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_profile( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, map()} | any + def get_profile(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfile{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Get avatar url for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_avatar_url( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, String.t()} | any + def get_avatar_url(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfileAvatarUrl{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Get display name for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + """ + @spec get_displayname( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil + ) :: {:ok, String.t()} | any + def get_displayname(client_api, user_id \\ nil) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.GetProfileDisplayname{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0) + } + ) + end + + @doc """ + Modify avatar url for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + `avatar_url` is an url specifying the location of the new profile avatar + """ + @spec put_avatar_url( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil, + avatar_url :: String.t() + ) :: :ok | any + def put_avatar_url(client_api, user_id \\ nil, avatar_url) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PutProfileAvatarUrl{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), + avatar_url: avatar_url + } + ) + end + + @doc """ + Modify display name for the user. + + `user_id` is a string representing the user whose profile should be retrieved + If `user_id` is ommited it defaults to the user_id represented by the client + `displayname` is a string representing the new displayname + """ + @spec put_displayname( + client_api :: Polyjuice.Client.API.t(), + user_id :: String.t() | nil, + displayname :: String.t() + ) :: :ok | any + def put_displayname(client_api, user_id \\ nil, displayname) + when is_binary(user_id) or user_id == nil do + Polyjuice.Client.API.call( + client_api, + %Polyjuice.Client.Endpoint.PutProfileDisplayname{ + user_id: user_id || Polyjuice.Client.API.get_user_and_device(client_api) |> elem(0), + displayname: displayname + } + ) + end +end |