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

Followed field

parent 0bc43aa6
......@@ -41,5 +41,8 @@ defmodule ActivityPub.ObjectAspect do
# adding because it is easier
assoc(:likers)
field(:likers_count, :integer, autogenerated: true)
field(:followed, :boolean, virtual: true)
field(:liked, :boolean, virtual: true)
end
end
......@@ -5,7 +5,8 @@ defmodule ActivityPub.Field do
functional: true,
type: nil,
default: nil,
autogenerated: false
autogenerated: false,
virtual: false
def build(opts) do
opts = add_default_value(opts)
......
......@@ -52,7 +52,9 @@ defmodule ActivityPub.SQL.Query do
def get_by_id(id, opts \\ []) when is_binary(id) do
case UrlBuilder.get_local_id(id) do
{:ok, local_id} -> get_by_local_id(local_id, opts)
{:ok, local_id} ->
get_by_local_id(local_id, opts)
:error ->
new()
|> where(id: id)
......
defmodule ActivityPub.SQLAspect do
alias ActivityPub.{SQLObjectAspect, SQLActorAspect, SQLActivityAspect, SQLCollectionAspect, SQLResourceAspect}
alias ActivityPub.{
SQLObjectAspect,
SQLActorAspect,
SQLActivityAspect,
SQLCollectionAspect,
SQLResourceAspect
}
alias ActivityPub.SQL.Associations.{ManyToMany, BelongsTo, Collection}
def all(), do: [SQLObjectAspect, SQLActorAspect, SQLActivityAspect, SQLCollectionAspect, SQLResourceAspect]
def all(),
do: [
SQLObjectAspect,
SQLActorAspect,
SQLActivityAspect,
SQLCollectionAspect,
SQLResourceAspect
]
# FIXME make this similar to aspect where the user can redifine
# assocs and fields to be persisted in another way than the default!
......@@ -106,7 +119,12 @@ defmodule ActivityPub.SQLAspect do
type = if field_def.functional, do: field_def.type, else: {:array, field_def.type}
field(name, type)
opts =
field_def
|> Map.take([:virtual, :default])
|> Keyword.new()
field(name, type, opts)
end
end
end
......
......@@ -108,6 +108,8 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
field(:published, :string)
field(:updated, :string)
field(:followed, non_null(:boolean), do: resolve(with_bool_join(:follow)))
end
input_object :community_input do
......@@ -158,6 +160,8 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
field(:published, :string)
field(:updated, :string)
field(:followed, non_null(:boolean), do: resolve(with_bool_join(:follow)))
end
input_object :collection_input do
......@@ -843,6 +847,22 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
end
end
defp with_bool_join(:follow) do
fn parent, _, info ->
{:ok, current_actor} = current_actor(info)
collection_id = ActivityPub.SQL.Common.local_id(current_actor.following)
args = {__MODULE__, :preload_bool_join, {:follow, collection_id}}
batch(
args,
parent,
fn children_map ->
Map.fetch(children_map, Entity.local_id(parent))
end
)
end
end
defp ensure_single(children, false), do: children
defp ensure_single(children, true) do
......@@ -904,6 +924,24 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
end)
end
def preload_bool_join({:follow, collection_id}, parent_list) do
import Ecto.Query, only: [from: 2]
parent_ids = Enum.map(parent_list, &Entity.local_id/1)
ret =
from(f in "activity_pub_collection_items",
where: f.subject_id == ^collection_id,
where: f.target_id in ^parent_ids,
select: {f.target_id, true}
)
|> MoodleNet.Repo.all()
|> Map.new()
Enum.reduce(parent_ids, ret, fn id, ret ->
Map.put_new(ret, id, false)
end)
end
defp load_actor(user) do
Query.new()
|> Query.preload_aspect(:actor)
......
......@@ -26,8 +26,6 @@ defmodule MoodleNet.AccountsTest do
end
test "works with moodle.com emails" do
icon_attrs = Factory.attributes(:image)
attrs = Factory.attributes(:user, email: "any_email_or_whatever@moodle.com")
assert {:ok, _} = Accounts.register_user(attrs)
......
......@@ -13,6 +13,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
communities {
id
localId
followed
followersCount
followers {
id
......@@ -38,6 +39,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert community_map["id"] == community.id
assert community_map["localId"] == local_id(community)
assert community_map["followed"] == true
assert community_map["followersCount"] == 1
assert [user_map] = community_map["followers"]
......@@ -81,6 +83,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
communities {
id
localId
followed
followersCount
followers {
id
......@@ -98,6 +101,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert community_map["id"] == community.id
assert community_map["localId"] == local_id(community)
assert community_map["followed"] == false
assert community_map["followersCount"] == 0
assert [] = community_map["followers"]
......@@ -121,6 +125,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
communities {
id
localId
followed
followersCount
followers {
id
......@@ -138,6 +143,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert community_map["id"] == community.id
assert community_map["localId"] == local_id(community)
assert community_map["followed"] == true
assert community_map["followersCount"] == 1
actor_id = actor.id
......@@ -155,6 +161,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
collection(localId: #{collection_id}) {
id
localId
followed
followersCount
followers {
id
......@@ -180,6 +187,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert collection_map["id"] == collection.id
assert collection_map["localId"] == local_id(collection)
assert collection_map["followed"] == true
assert collection_map["followersCount"] == 1
assert [user_map] = collection_map["followers"]
......@@ -223,6 +231,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
collection(localId: #{collection_id}) {
id
localId
followed
followersCount
followers {
id
......@@ -240,6 +249,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert collection_map["id"] == collection.id
assert collection_map["localId"] == local_id(collection)
assert collection_map["followed"] == false
assert collection_map["followersCount"] == 0
assert [] = collection_map["followers"]
......@@ -263,6 +273,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
collection(localId: #{collection_id}) {
id
localId
followed
followersCount
followers {
id
......@@ -280,6 +291,7 @@ defmodule MoodleNetWeb.GraphQL.FollowTest do
assert collection_map["id"] == collection.id
assert collection_map["localId"] == local_id(collection)
assert collection_map["followed"] == true
assert collection_map["followersCount"] == 1
actor_id = actor.id
......
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