Add Twitter API user representer.
This commit is contained in:
parent
142e8f8f3e
commit
980717fdce
28
lib/pleroma/web/twitter_api/representers/base_representer.ex
Normal file
28
lib/pleroma/web/twitter_api/representers/base_representer.ex
Normal file
|
@ -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
|
26
lib/pleroma/web/twitter_api/representers/user_representer.ex
Normal file
26
lib/pleroma/web/twitter_api/representers/user_representer.ex
Normal file
|
@ -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
|
41
test/web/twitter_api/representers/user_representer_test.exs
Normal file
41
test/web/twitter_api/representers/user_representer_test.exs
Normal file
|
@ -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
|
Loading…
Reference in a new issue