end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
- l = status_text |> String.trim |> String.length
- if l > 0 && l < 5000 do
- media_ids = extract_media_ids(status_data)
- {:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
+ with media_ids <- extract_media_ids(status_data),
+ {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
conn
- |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
+ |> json(ActivityRepresenter.to_map(activity, %{user: user}))
else
- empty_status_reply(conn)
+ _ -> empty_status_reply(conn)
end
end
def user_timeline(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.get_user(user, params) do
{:ok, target_user} ->
- params = Map.merge(params, %{"actor_id" => target_user.ap_id})
+ params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true})
statuses = TwitterAPI.fetch_user_statuses(user, params)
conn
|> json_reply(200, statuses |> Poison.encode!)
end
end
+ def block(%{assigns: %{user: user}} = conn, params) do
+ case TwitterAPI.block(user, params) do
+ {:ok, user, blocked} ->
+ render conn, UserView, "show.json", %{user: blocked, for: user}
+ {:error, msg} -> forbidden_json_reply(conn, msg)
+ end
+ end
+
+ def unblock(%{assigns: %{user: user}} = conn, params) do
+ case TwitterAPI.unblock(user, params) do
+ {:ok, user, blocked} ->
+ render conn, UserView, "show.json", %{user: blocked, for: user}
+ {:error, msg} -> forbidden_json_reply(conn, msg)
+ end
+ end
+
def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, delete} <- CommonAPI.delete(id, user) do
json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
response <- Poison.encode!(user_map) do
conn
|> json_reply(200, response)
+ else
+ _e ->
+ conn
+ |> put_status(404)
+ |> json(%{error: "Can't find user"})
end
end
end
end
+ def friends_ids(%{assigns: %{user: user}} = conn, _params) do
+ with {:ok, friends} <- User.get_friends(user) do
+ ids = friends
+ |> Enum.map(fn x -> x.id end)
+ |> Poison.encode!
+
+ json(conn, ids)
+ else
+ _e -> bad_request_reply(conn, "Can't get friends")
+ end
+ end
+
+ def empty_array(conn, _params) do
+ json(conn, Poison.encode!([]))
+ end
+
def update_profile(%{assigns: %{user: user}} = conn, params) do
params = if bio = params["description"] do
Map.put(params, "bio", bio)
end
end
+ def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
+ conn
+ |> json(TwitterAPI.search(user, params))
+ end
+
defp bad_request_reply(conn, error_message) do
json = error_json(conn, error_message)
json_reply(conn, 400, json)