1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.AnnouncementReadRelationship do
10 alias FlakeId.Ecto.CompatType
11 alias Pleroma.Announcement
15 @type t :: %__MODULE__{}
17 schema "announcement_read_relationships" do
18 belongs_to(:user, User, type: CompatType)
19 belongs_to(:announcement, Announcement, type: CompatType)
21 timestamps(updated_at: false)
24 def mark_read(user, announcement) do
26 |> cast(%{user_id: user.id, announcement_id: announcement.id}, [:user_id, :announcement_id])
27 |> validate_required([:user_id, :announcement_id])
28 |> foreign_key_constraint(:user_id)
29 |> foreign_key_constraint(:announcement_id)
30 |> unique_constraint([:user_id, :announcement_id])
34 def mark_unread(user, announcement) do
35 with relationship <- get(user, announcement),
36 {:exists, true} <- {:exists, not is_nil(relationship)},
37 {:ok, _} <- Repo.delete(relationship) do
48 def get(user, announcement) do
49 Repo.get_by(__MODULE__, user_id: user.id, announcement_id: announcement.id)
52 def exists?(user, announcement) do
53 not is_nil(get(user, announcement))