mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-17 10:39:17 +00:00
Add conversation fetching to TwAPI controller.
This commit is contained in:
parent
0dd3373304
commit
d2cdcc097b
|
@ -24,6 +24,7 @@ def user_fetcher(username) do
|
|||
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
|
||||
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
|
||||
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
|
||||
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
|
||||
end
|
||||
|
||||
scope "/api", Pleroma.Web do
|
||||
|
|
|
@ -3,6 +3,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
def create_status(user = %User{}, data = %{}) do
|
||||
date = DateTime.utc_now() |> DateTime.to_iso8601
|
||||
|
||||
|
@ -55,6 +57,29 @@ def fetch_public_statuses(user, opts \\ %{}) do
|
|||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_conversation(user, id) do
|
||||
query = from activity in Activity,
|
||||
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
|
||||
limit: 1
|
||||
|
||||
with %Activity{} = activity <- Repo.one(query),
|
||||
context <- activity.data["context"],
|
||||
activities <- ActivityPub.fetch_activities_for_context(context),
|
||||
statuses <- activities |> activities_to_statuses(%{for: user})
|
||||
do
|
||||
statuses
|
||||
else e ->
|
||||
IO.inspect(e)
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_status(user, id) do
|
||||
with %Activity{} = activity <- Repo.get(Activity, id) do
|
||||
activity_to_status(activity, %{for: user})
|
||||
end
|
||||
end
|
||||
|
||||
def follow(%User{} = follower, followed_id) do
|
||||
with %User{} = followed <- Repo.get(User, followed_id),
|
||||
{ :ok, follower } <- User.follow(follower, followed)
|
||||
|
@ -95,10 +120,4 @@ defp activity_to_status(activity, opts) do
|
|||
user = Repo.get_by!(User, ap_id: actor)
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
|
||||
end
|
||||
|
||||
def fetch_status(user, id) do
|
||||
with %Activity{} = activity <- Repo.get(Activity, id) do
|
||||
activity_to_status(activity, %{for: user})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -57,6 +57,15 @@ def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
|
|||
|> json_reply(200, response)
|
||||
end
|
||||
|
||||
def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
|
||||
id = String.to_integer(id)
|
||||
response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
end
|
||||
|
||||
|
||||
defp json_reply(conn, status, json) do
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|
|
|
@ -67,6 +67,22 @@ test "returns one status", %{conn: conn} do
|
|||
end
|
||||
end
|
||||
|
||||
describe "GET /statusnet/conversation/:id.json" do
|
||||
test "returns the statuses in the conversation", %{conn: conn} do
|
||||
{:ok, _user} = UserBuilder.insert
|
||||
{:ok, _activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
|
||||
{:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
|
||||
{:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
|
||||
|
||||
conn = conn
|
||||
|> get("/api/statusnet/conversation/1.json")
|
||||
|
||||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /statuses/friends_timeline.json" do
|
||||
setup [:valid_user]
|
||||
test "without valid credentials", %{conn: conn} do
|
||||
|
|
|
@ -103,4 +103,17 @@ test "Unfollow another user" do
|
|||
|
||||
assert user.following == []
|
||||
end
|
||||
|
||||
test "fetch statuses in a context using the conversation id" do
|
||||
{:ok, user} = UserBuilder.insert()
|
||||
{:ok, activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
|
||||
{:ok, activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
|
||||
{:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
|
||||
|
||||
statuses = TwitterAPI.fetch_conversation(user, 1)
|
||||
|
||||
assert length(statuses) == 2
|
||||
assert Enum.at(statuses, 0)["id"] == activity.id
|
||||
assert Enum.at(statuses, 1)["id"] == activity_two.id
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue