From: Roger Braun Date: Mon, 20 Mar 2017 20:30:18 +0000 (+0100) Subject: Add Twitter API user representer. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=980717fdceb39235a3f1e7f6e76d1828630f1989;p=akkoma Add Twitter API user representer. --- diff --git a/lib/pleroma/web/twitter_api/representers/base_representer.ex b/lib/pleroma/web/twitter_api/representers/base_representer.ex new file mode 100644 index 000000000..a4ef245fc --- /dev/null +++ b/lib/pleroma/web/twitter_api/representers/base_representer.ex @@ -0,0 +1,28 @@ +defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do + defmacro __using__(_opts) do + quote do + def to_json(object) do to_json(object, %{}) end + def to_json(object, options) do + object + |> to_map(options) + |> Poison.encode! + end + + def enum_to_list(enum, options) do + mapping = fn (el) -> to_map(el, options) end + Enum.map(enum, mapping) + end + + def to_map(object) do + to_map(object, %{}) + end + + def enum_to_json(enum) do enum_to_json(enum, %{}) end + def enum_to_json(enum, options) do + enum + |> enum_to_list(options) + |> Poison.encode! + end + end + end +end diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex new file mode 100644 index 000000000..079e25021 --- /dev/null +++ b/lib/pleroma/web/twitter_api/representers/user_representer.ex @@ -0,0 +1,26 @@ +defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do + use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter + + def to_map(user, options) do + image = "https://placehold.it/48x48" + map = %{ + "id" => user.id, + "name" => user.name, + "screen_name" => user.nickname, + "description" => user.bio, + "following" => false, + # Fake fields + "favourites_count" => 0, + "statuses_count" => 0, + "friends_count" => 0, + "followers_count" => 0, + "profile_image_url" => image, + "profile_image_url_https" => image, + "profile_image_url_profile_size" => image, + "profile_image_url_original" => image, + "rights" => %{} + } + + map + end +end diff --git a/test/web/twitter_api/representers/user_representer_test.exs b/test/web/twitter_api/representers/user_representer_test.exs new file mode 100644 index 000000000..f08e392f9 --- /dev/null +++ b/test/web/twitter_api/representers/user_representer_test.exs @@ -0,0 +1,41 @@ +defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do + use Pleroma.DataCase + + alias Pleroma.User + alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter + + setup do + user = %User{ + email: "test@example.org", + name: "Test Name", + nickname: "testname", + password_hash: Comeonin.Pbkdf2.hashpwsalt("test"), + bio: "A tester." + } + user = Repo.insert!(user) + [user: user] + end + + test "A user", %{user: user} do + image = "https://placehold.it/48x48" + represented = %{ + "id" => user.id, + "name" => user.name, + "screen_name" => user.nickname, + "description" => user.bio, + # Fake fields + "favourites_count" => 0, + "statuses_count" => 0, + "friends_count" => 0, + "followers_count" => 0, + "profile_image_url" => image, + "profile_image_url_https" => image, + "profile_image_url_profile_size" => image, + "profile_image_url_original" => image, + "following" => false, + "rights" => %{} + } + + assert represented == UserRepresenter.to_map(user) + end +end