pipe_through :authenticated_api
post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials
+ post "/statuses/update.json", TwitterAPI.Controller, :status_update
end
end
def create_status(user = %User{}, data = %{}) do
activity = %{
- type: "Create",
- to: [
+ "type" => "Create",
+ "to" => [
User.ap_followers(user),
"https://www.w3.org/ns/activitystreams#Public"
],
- actor: User.ap_id(user),
- object: %{
- type: "Note",
- content: data.status
+ "actor" => User.ap_id(user),
+ "object" => %{
+ "type" => "Note",
+ "content" => data["status"]
}
}
defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller
-
- alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+ alias Pleroma.Web.TwitterAPI.TwitterAPI
+ alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
response = user |> UserRepresenter.to_json
|> json_reply(200, response)
end
+ def status_update(%{assigns: %{user: user}} = conn, status_data) do
+ {:ok, activity} = TwitterAPI.create_status(user, status_data)
+ conn
+ |> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
+ end
+
defp json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")
use Pleroma.DataCase
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Activity
- alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
+ alias Pleroma.Builders.ActivityBuilder
describe "insertion" do
test "inserts a given map into the activity database" do
describe "fetch activities" do
test "retrieves all public activities" do
- %{user: user, public: public} = ActivityBuilder.public_and_non_public
+ %{public: public} = ActivityBuilder.public_and_non_public
activities = ActivityPub.fetch_public_activities
assert length(activities) == 1
defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase
- alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+ alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Builders.UserBuilder
+ alias Pleroma.{Repo, Activity}
describe "POST /api/account/verify_credentials" do
setup [:valid_user]
end
end
+ describe "POST /statuses/update.json" do
+ setup [:valid_user]
+ test "without valid credentials", %{conn: conn} do
+ conn = post conn, "/api/statuses/update.json"
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: user} do
+ conn = conn
+ |> with_credentials(user.nickname, "test")
+ |> post("/api/statuses/update.json", %{ status: "Nice meme." })
+
+ assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
+ end
+ end
+
defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert
[user: user]
alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.{Activity, User}
- alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
- alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
test "create a status" do
user = UserBuilder.build
input = %{
- status: "Hello again."
+ "status" => "Hello again."
}
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
- assert get_in(activity.data, [:object, :content]) == "Hello again."
- assert get_in(activity.data, [:object, :type]) == "Note"
- assert get_in(activity.data, [:actor]) == User.ap_id(user)
- assert Enum.member?(get_in(activity.data, [:to]), User.ap_followers(user))
- assert Enum.member?(get_in(activity.data, [:to]), "https://www.w3.org/ns/activitystreams#Public")
+ assert get_in(activity.data, ["object", "content"]) == "Hello again."
+ assert get_in(activity.data, ["object", "type"]) == "Note"
+ assert get_in(activity.data, ["actor"]) == User.ap_id(user)
+ assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
+ assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
end
test "fetch public activities" do