2022-03-08 05:06:07 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Announcement do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset, only: [cast: 3, validate_required: 2]
|
|
|
|
|
2022-03-08 17:07:21 +00:00
|
|
|
alias Pleroma.AnnouncementReadRelationship
|
2022-03-08 05:06:07 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
@primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
|
|
|
|
|
|
|
|
schema "announcements" do
|
|
|
|
field(:data, :map)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def change(struct, params \\ %{}) do
|
|
|
|
struct
|
|
|
|
|> cast(params, [:data])
|
|
|
|
|> validate_required([:data])
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(params) do
|
|
|
|
changeset = change(%__MODULE__{}, params)
|
|
|
|
|
|
|
|
Repo.insert(changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_all do
|
|
|
|
__MODULE__
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_by_id(id) do
|
|
|
|
Repo.get_by(__MODULE__, id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_by_id(id) do
|
|
|
|
with announcement when not is_nil(announcement) <- get_by_id(id),
|
|
|
|
{:ok, _} <- Repo.delete(announcement) do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
end
|
2022-03-08 14:35:35 +00:00
|
|
|
|
2022-03-08 17:07:21 +00:00
|
|
|
def read_by?(announcement, user) do
|
|
|
|
AnnouncementReadRelationship.exists?(user, announcement)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_read_by(announcement, user) do
|
|
|
|
AnnouncementReadRelationship.mark_read(user, announcement)
|
2022-03-08 14:35:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_json(announcement, opts \\ []) do
|
|
|
|
extra_params =
|
|
|
|
case Keyword.fetch(opts, :for) do
|
2022-03-08 17:07:21 +00:00
|
|
|
{:ok, user} when not is_nil(user) ->
|
2022-03-08 14:35:35 +00:00
|
|
|
%{read: read_by?(announcement, user)}
|
2022-03-08 17:07:21 +00:00
|
|
|
|
2022-03-08 14:35:35 +00:00
|
|
|
_ ->
|
|
|
|
%{}
|
|
|
|
end
|
|
|
|
|
|
|
|
base = %{
|
|
|
|
id: announcement.id,
|
|
|
|
content: announcement.data["content"],
|
|
|
|
starts_at: :null,
|
|
|
|
ends_at: :null,
|
|
|
|
all_day: false,
|
|
|
|
published_at: announcement.inserted_at,
|
|
|
|
updated_at: announcement.updated_at,
|
|
|
|
mentions: [],
|
|
|
|
statuses: [],
|
|
|
|
tags: [],
|
|
|
|
emojis: [],
|
|
|
|
reactions: []
|
|
|
|
}
|
|
|
|
|
|
|
|
base
|
|
|
|
|> Map.merge(extra_params)
|
|
|
|
end
|
2022-03-08 05:06:07 +00:00
|
|
|
end
|