From 64c1c3a4071f3f99a59f38e2dcde499bda3969cf Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 10 Apr 2019 15:12:01 +0200 Subject: [PATCH] Participations: Add marking as read and unread. --- lib/conversation/participation.ex | 18 ++++++++++++++++++ test/conversation/participation_test.exs | 14 ++++++++++++++ test/support/factory.ex | 11 +++++++++++ 3 files changed, 43 insertions(+) diff --git a/lib/conversation/participation.ex b/lib/conversation/participation.ex index 244d37c46..ab59a529e 100644 --- a/lib/conversation/participation.ex +++ b/lib/conversation/participation.ex @@ -28,4 +28,22 @@ defmodule Pleroma.Conversation.Participation do |> creation_cng(%{user_id: user.id, conversation_id: conversation.id}) |> Repo.insert() end + + def read_cng(struct, params) do + struct + |> cast(params, [:read]) + |> validate_required([:read]) + end + + def mark_as_read(participation) do + participation + |> read_cng(%{read: true}) + |> Repo.update() + end + + def mark_as_unread(participation) do + participation + |> read_cng(%{read: false}) + |> Repo.update() + end end diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index 8dc15a802..eae1873ca 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -19,4 +19,18 @@ defmodule Pleroma.Conversation.ParticipationTest do assert participation.user_id == user.id assert participation.conversation_id == conversation.id end + + test "it marks a participation as read" do + participation = insert(:participation, %{read: false}) + {:ok, participation} = Participation.mark_as_read(participation) + + assert participation.read + end + + test "it marks a participation as unread" do + participation = insert(:participation, %{read: true}) + {:ok, participation} = Participation.mark_as_unread(participation) + + refute participation.read + end end diff --git a/test/support/factory.ex b/test/support/factory.ex index af38be46c..2a2954ad6 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -5,6 +5,17 @@ defmodule Pleroma.Factory do use ExMachina.Ecto, repo: Pleroma.Repo + def participation_factory do + conversation = insert(:conversation) + user = insert(:user) + + %Pleroma.Conversation.Participation{ + conversation: conversation, + user: user, + read: false + } + end + def conversation_factory do %Pleroma.Conversation{ ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")