Participations: Add marking as read and unread.
authorlain <lain@soykaf.club>
Wed, 10 Apr 2019 13:12:01 +0000 (15:12 +0200)
committerlain <lain@soykaf.club>
Wed, 10 Apr 2019 13:12:01 +0000 (15:12 +0200)
lib/conversation/participation.ex
test/conversation/participation_test.exs
test/support/factory.ex

index 244d37c467dd6fac5517782a6678477929a9e5b4..ab59a529e61f2b6824247553acbd21d790fb8dbc 100644 (file)
@@ -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
index 8dc15a8029b6ce3ce717d8f0f487a74637d84afc..eae1873cacc2bc457b65aa3e257ec77ffb385ad0 100644 (file)
@@ -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
index af38be46c68bedb31ac98f1ef7a03aec14e4f134..2a2954ad615ea0993b1479f3f81b6c697bfdc267 100644 (file)
@@ -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}")