2022-03-07 22:06:07 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2022-03-07 22:06:07 -07:00
|
|
|
# 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 16:21:20 -07:00
|
|
|
import Ecto.Query
|
2022-03-07 22:06:07 -07:00
|
|
|
|
2022-03-08 10:07:21 -07:00
|
|
|
alias Pleroma.AnnouncementReadRelationship
|
2022-03-07 22:06:07 -07:00
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
@primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
|
|
|
|
|
|
|
|
schema "announcements" do
|
|
|
|
field(:data, :map)
|
2022-03-18 07:36:13 -06:00
|
|
|
field(:starts_at, :utc_datetime)
|
|
|
|
field(:ends_at, :utc_datetime)
|
2022-03-08 21:00:51 -07:00
|
|
|
field(:rendered, :map)
|
2022-03-07 22:06:07 -07:00
|
|
|
|
2022-03-18 07:36:13 -06:00
|
|
|
timestamps(type: :utc_datetime)
|
2022-03-07 22:06:07 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def change(struct, params \\ %{}) do
|
|
|
|
struct
|
2022-03-08 21:00:51 -07:00
|
|
|
|> cast(validate_params(struct, params), [:data, :starts_at, :ends_at, :rendered])
|
2022-03-07 22:06:07 -07:00
|
|
|
|> validate_required([:data])
|
|
|
|
end
|
|
|
|
|
2022-03-08 18:55:41 -07:00
|
|
|
defp validate_params(struct, params) do
|
|
|
|
base_data =
|
|
|
|
%{
|
|
|
|
"content" => "",
|
|
|
|
"all_day" => false
|
|
|
|
}
|
|
|
|
|> Map.merge((struct && struct.data) || %{})
|
2022-03-08 16:21:20 -07:00
|
|
|
|
|
|
|
merged_data =
|
2022-03-08 18:55:41 -07:00
|
|
|
Map.merge(base_data, params.data)
|
2022-03-08 16:21:20 -07:00
|
|
|
|> Map.take(["content", "all_day"])
|
|
|
|
|
2022-03-08 17:12:01 -07:00
|
|
|
params
|
|
|
|
|> Map.merge(%{data: merged_data})
|
2022-03-08 21:00:51 -07:00
|
|
|
|> add_rendered_properties()
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_rendered_properties(params) do
|
|
|
|
{content_html, _, _} =
|
|
|
|
Pleroma.Web.CommonAPI.Utils.format_input(params.data["content"], "text/plain",
|
|
|
|
mentions_format: :full
|
|
|
|
)
|
|
|
|
|
|
|
|
rendered = %{
|
|
|
|
"content" => content_html
|
|
|
|
}
|
|
|
|
|
|
|
|
params
|
|
|
|
|> Map.put(:rendered, rendered)
|
2022-03-08 16:21:20 -07:00
|
|
|
end
|
|
|
|
|
2022-03-07 22:06:07 -07:00
|
|
|
def add(params) do
|
|
|
|
changeset = change(%__MODULE__{}, params)
|
|
|
|
|
|
|
|
Repo.insert(changeset)
|
|
|
|
end
|
|
|
|
|
2022-03-08 18:55:41 -07:00
|
|
|
def update(announcement, params) do
|
|
|
|
changeset = change(announcement, params)
|
|
|
|
|
|
|
|
Repo.update(changeset)
|
|
|
|
end
|
|
|
|
|
2022-03-07 22:06:07 -07:00
|
|
|
def list_all do
|
|
|
|
__MODULE__
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2022-03-08 19:26:05 -07:00
|
|
|
def list_paginated(%{limit: limited_number, offset: offset_number}) do
|
|
|
|
__MODULE__
|
|
|
|
|> limit(^limited_number)
|
|
|
|
|> offset(^offset_number)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2022-03-07 22:06:07 -07:00
|
|
|
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 07:35:35 -07:00
|
|
|
|
2022-03-08 10:07:21 -07: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 07:35:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_json(announcement, opts \\ []) do
|
|
|
|
extra_params =
|
|
|
|
case Keyword.fetch(opts, :for) do
|
2022-03-08 10:07:21 -07:00
|
|
|
{:ok, user} when not is_nil(user) ->
|
2022-03-08 07:35:35 -07:00
|
|
|
%{read: read_by?(announcement, user)}
|
2022-03-08 10:07:21 -07:00
|
|
|
|
2022-03-08 07:35:35 -07:00
|
|
|
_ ->
|
|
|
|
%{}
|
|
|
|
end
|
|
|
|
|
2022-03-08 21:00:51 -07:00
|
|
|
admin_extra_params =
|
|
|
|
case Keyword.fetch(opts, :admin) do
|
|
|
|
{:ok, true} ->
|
|
|
|
%{pleroma: %{raw_content: announcement.data["content"]}}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
%{}
|
|
|
|
end
|
|
|
|
|
2022-03-08 07:35:35 -07:00
|
|
|
base = %{
|
|
|
|
id: announcement.id,
|
2022-03-08 21:00:51 -07:00
|
|
|
content: announcement.rendered["content"],
|
2022-03-08 17:12:01 -07:00
|
|
|
starts_at: announcement.starts_at,
|
|
|
|
ends_at: announcement.ends_at,
|
|
|
|
all_day: announcement.data["all_day"],
|
2022-03-08 07:35:35 -07:00
|
|
|
published_at: announcement.inserted_at,
|
|
|
|
updated_at: announcement.updated_at,
|
|
|
|
mentions: [],
|
|
|
|
statuses: [],
|
|
|
|
tags: [],
|
|
|
|
emojis: [],
|
|
|
|
reactions: []
|
|
|
|
}
|
|
|
|
|
|
|
|
base
|
|
|
|
|> Map.merge(extra_params)
|
2022-03-08 21:00:51 -07:00
|
|
|
|> Map.merge(admin_extra_params)
|
2022-03-08 07:35:35 -07:00
|
|
|
end
|
2022-03-08 16:21:20 -07:00
|
|
|
|
|
|
|
# "visible" means:
|
|
|
|
# starts_at < time < ends_at
|
|
|
|
def list_all_visible_when(time) do
|
|
|
|
__MODULE__
|
|
|
|
|> where([a], is_nil(a.starts_at) or a.starts_at < ^time)
|
|
|
|
|> where([a], is_nil(a.ends_at) or a.ends_at > ^time)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_all_visible do
|
2022-03-18 07:36:13 -06:00
|
|
|
list_all_visible_when(DateTime.now("Etc/UTC") |> elem(1))
|
2022-03-08 16:21:20 -07:00
|
|
|
end
|
2022-03-07 22:06:07 -07:00
|
|
|
end
|