Group: List members, add them, remove them.

This commit is contained in:
Lain Soykaf 2021-01-22 19:45:52 +01:00
parent 821b36f06c
commit 633d0286b3
4 changed files with 48 additions and 3 deletions

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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