diff --git a/lib/pleroma/ecto_enums.ex b/lib/pleroma/ecto_enums.ex index f198cccb7..cd8606ae6 100644 --- a/lib/pleroma/ecto_enums.ex +++ b/lib/pleroma/ecto_enums.ex @@ -9,7 +9,8 @@ defenum(Pleroma.UserRelationship.Type, mute: 2, reblog_mute: 3, notification_mute: 4, - inverse_subscription: 5 + inverse_subscription: 5, + membership: 6 ) defenum(Pleroma.FollowingRelationship.State, diff --git a/lib/pleroma/group.ex b/lib/pleroma/group.ex index 6cc208f15..732791ec2 100644 --- a/lib/pleroma/group.ex +++ b/lib/pleroma/group.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Group do alias Pleroma.User alias Pleroma.Repo alias Pleroma.Web - alias Pleroma.EctoType.ActivityPub.ObjectValidators + alias Pleroma.UserRelationship @moduledoc """ Groups contain all the additional information about a group that's not stored @@ -29,7 +29,8 @@ defmodule Pleroma.Group do belongs_to(:user, User, type: FlakeId.Ecto.CompatType) belongs_to(:owner, User, type: FlakeId.Ecto.CompatType, foreign_key: :owner_id) - field(:members, {:array, ObjectValidators.ObjectID}) + has_many(:members, through: [:user, :group_members]) + field(:name, :string) field(:description, :string) field(:members_collection, :string) @@ -71,4 +72,24 @@ defmodule Pleroma.Group do |> cast(params, [:user_id, :owner_id, :name, :description, :members_collection]) |> validate_required([:user_id, :owner_id, :members_collection]) end + + def is_member?(%{user_id: user_id}, member) do + UserRelationship.membership_exists?(%User{id: user_id}, member) + end + + def members(group) do + Repo.preload(group, :members).members + end + + def add_member(%{user_id: user_id} = group, member) do + with {:ok, _relationship} <- UserRelationship.create_membership(%User{id: user_id}, member) do + {:ok, group} + end + end + + def remove_member(%{user_id: user_id} = group, member) do + with {:ok, _relationship} <- UserRelationship.delete_membership(%User{id: user_id}, member) do + {:ok, group} + end + end end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3da658950..1e5c87403 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -58,6 +58,10 @@ defmodule Pleroma.User do # AP ID user relationships (blocks, mutes etc.) # Format: [rel_type: [outgoing_rel: :outgoing_rel_target, incoming_rel: :incoming_rel_source]] @user_relationships_config [ + membership: [ + group_memberships: :group_members, + user_memberships: :joined_groups + ], block: [ blocker_blocks: :blocked_users, blockee_blocks: :blocker_users diff --git a/test/pleroma/group_test.exs b/test/pleroma/group_test.exs index f73abd237..e4c6cfd92 100644 --- a/test/pleroma/group_test.exs +++ b/test/pleroma/group_test.exs @@ -33,4 +33,23 @@ defmodule Pleroma.GroupTest do Repo.delete(group.user) refute Repo.get(Group, group.id) end + + test "group members can be seen and added" do + user = insert(:user) + other_user = insert(:user) + third_user = insert(:user) + + {:ok, group} = Group.create(%{owner_id: user.id, name: "cofe", description: "corndog"}) + + assert [] == Group.members(group) + + {:ok, group} = Group.add_member(group, other_user) + assert [other_user] == Group.members(group) + + assert Group.is_member?(group, other_user) + refute Group.is_member?(group, third_user) + + {:ok, group} = Group.remove_member(group, other_user) + refute Group.is_member?(group, other_user) + end end