Commit f6d78528 authored by Alex Castaño's avatar Alex Castaño

List of threads for community/collection

parent 2993a973
......@@ -47,6 +47,24 @@ defmodule MoodleNet do
list_resources(ActivityPub.Entity.local_id(entity), opts)
end
def list_threads(context_id, opts \\ %{}) do
Query.new()
|> Query.with_type("Note")
|> Query.has(:context, context_id)
|> has_no_replies()
|> Query.paginate(opts)
|> Query.all()
end
defp has_no_replies(query) do
import Ecto.Query, only: [from: 2]
from([entity: entity] in query,
left_join: rel in fragment("activity_pub_object_in_reply_tos"),
on: entity.local_id == rel.subject_id,
where: is_nil(rel.target_id)
)
end
def list_comments(context_id, opts \\ %{}) do
Query.new()
|> Query.with_type("Note")
......
......@@ -42,6 +42,12 @@ defmodule MoodleNetWeb.GraphQL.Schema do
resolve(MoodleNetSchema.resolve_by_id_and_type("MoodleNet:EducationalResource"))
end
@desc "Get list of threads"
field :threads, non_null(list_of(non_null(:comment))) do
arg(:context_local_id, non_null(:integer))
resolve(&MoodleNetSchema.list_threads/2)
end
@desc "Get list of comments"
field :comments, non_null(list_of(non_null(:comment))) do
arg(:context_local_id, non_null(:integer))
......
......@@ -287,6 +287,16 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
{:ok, resources}
end
def list_threads(%{context_local_id: context_local_id}, info) do
fields = requested_fields(info)
comments =
MoodleNet.list_threads(context_local_id)
|> prepare(fields)
{:ok, comments}
end
def list_comments(%{context_local_id: context_local_id}, info) do
fields = requested_fields(info)
......
defmodule MoodleNetTest do
use MoodleNet.DataCase, async: true
describe "list_threads" do
test "works" do
owner = Factory.actor()
comm = Factory.community(owner)
thread = Factory.comment(owner, comm)
_reply = Factory.reply(owner, thread)
assert threads = MoodleNet.list_threads(comm)
assert length(threads) == 1
assert [ret] = threads
assert thread.id == ret.id
end
end
describe "create_collection" do
test "works" do
owner = Factory.actor()
......
......@@ -7,6 +7,47 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
import ActivityPub.Entity, only: [local_id: 1]
@moduletag format: :json
@tag :user
test "list threads", %{conn: conn, actor: actor} do
community = Factory.community(actor)
thread = Factory.comment(actor, community)
reply = Factory.reply(actor, thread)
Factory.reply(actor, reply)
query = """
{
threads(context_local_id: #{local_id(community)}) {
id
author {
id
}
replies {
id
author {
id
}
}
}
}
"""
assert [thread_map] =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("threads")
assert %{
"id" => thread.id,
"author" => %{"id" => actor.id},
"replies" => [%{
"id" => reply.id,
"author" => %{"id" => actor.id},
}]
} == thread_map
end
@tag :user
test "bug when same comment author is repeated", %{conn: conn, actor: actor} do
community = Factory.community(actor)
......
Markdown is supported
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