Commit ad303783 authored by lain's avatar lain
Browse files

Add status posting via TwAPI.

parent 9a8850eb
......@@ -21,5 +21,6 @@ def user_fetcher(username) do
pipe_through :authenticated_api
post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials
post "/statuses/update.json", TwitterAPI.Controller, :status_update
end
end
......@@ -6,15 +6,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
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
......@@ -10,6 +10,12 @@ def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|> 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")
......
......@@ -2,7 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
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
......@@ -17,7 +17,7 @@ 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]
......@@ -19,6 +20,22 @@ test "with credentials", %{conn: conn, user: user} do
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]
......
......@@ -3,22 +3,21 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment