From 980717fdceb39235a3f1e7f6e76d1828630f1989 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Mar 2017 21:30:18 +0100 Subject: [PATCH] Add Twitter API user representer. --- .../representers/base_representer.ex | 28 +++++++++++++ .../representers/user_representer.ex | 26 ++++++++++++ .../representers/user_representer_test.exs | 41 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 lib/pleroma/web/twitter_api/representers/base_representer.ex create mode 100644 lib/pleroma/web/twitter_api/representers/user_representer.ex create mode 100644 test/web/twitter_api/representers/user_representer_test.exs 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