2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.Posts do
|
|
|
|
@moduledoc """
|
|
|
|
The Posts context.
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.Tag
|
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
require Logger
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
@post_preloads [:author, :attributed_to, :picture, :media, :tags]
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
import EctoEnum
|
|
|
|
|
|
|
|
defenum(PostVisibility, :post_visibility, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
|
|
|
:restricted,
|
|
|
|
:private
|
|
|
|
])
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec list_public_local_posts(integer | nil, integer | nil) :: Page.t(Post.t())
|
2021-03-26 15:40:10 +01:00
|
|
|
def list_public_local_posts(page \\ nil, limit \\ nil) do
|
|
|
|
Post
|
|
|
|
|> filter_public()
|
|
|
|
|> filter_local()
|
|
|
|
|> preload_post_associations()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2020-08-12 16:05:34 +02:00
|
|
|
@spec list_posts_for_stream :: Enum.t()
|
|
|
|
def list_posts_for_stream do
|
|
|
|
Post
|
|
|
|
|> filter_public()
|
|
|
|
|> Repo.stream()
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of recent posts for a group
|
|
|
|
"""
|
|
|
|
@spec get_posts_for_group(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def get_posts_for_group(%Actor{id: group_id}, page \\ nil, limit \\ nil) do
|
|
|
|
group_id
|
|
|
|
|> do_get_posts_for_group()
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_public_posts_for_group(Actor.t(), integer | nil, integer | nil) :: Page.t()
|
|
|
|
def get_public_posts_for_group(%Actor{id: group_id}, page \\ nil, limit \\ nil) do
|
|
|
|
group_id
|
|
|
|
|> do_get_posts_for_group()
|
2020-08-12 16:05:34 +02:00
|
|
|
|> filter_public()
|
2020-07-09 17:24:28 +02:00
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get a post by it's ID
|
|
|
|
"""
|
|
|
|
@spec get_post(integer | String.t()) :: Post.t() | nil
|
|
|
|
def get_post(nil), do: nil
|
|
|
|
def get_post(id), do: Repo.get(Post, id)
|
|
|
|
|
|
|
|
@spec get_post_with_preloads(integer | String.t()) :: Post.t() | nil
|
|
|
|
def get_post_with_preloads(id) do
|
|
|
|
Post
|
|
|
|
|> Repo.get(id)
|
|
|
|
|> Repo.preload(@post_preloads)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_post_by_slug_with_preloads(String.t()) :: Post.t() | nil
|
|
|
|
def get_post_by_slug_with_preloads(slug) do
|
|
|
|
Post
|
|
|
|
|> Repo.get_by(slug: slug)
|
|
|
|
|> Repo.preload(@post_preloads)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get a post by it's URL
|
|
|
|
"""
|
|
|
|
@spec get_post_by_url(String.t()) :: Post.t() | nil
|
|
|
|
def get_post_by_url(url), do: Repo.get_by(Post, url: url)
|
|
|
|
|
|
|
|
@spec get_post_by_url_with_preloads(String.t()) :: Post.t() | nil
|
|
|
|
def get_post_by_url_with_preloads(url) do
|
|
|
|
Post
|
|
|
|
|> Repo.get_by(url: url)
|
|
|
|
|> Repo.preload(@post_preloads)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a post.
|
|
|
|
"""
|
|
|
|
@spec create_post(map) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def create_post(attrs \\ %{}) do
|
2020-10-01 15:07:15 +02:00
|
|
|
with {:ok, %Post{} = post} <-
|
|
|
|
%Post{}
|
|
|
|
|> Post.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, Repo.preload(post, @post_preloads)}
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a post.
|
|
|
|
"""
|
|
|
|
@spec update_post(Post.t(), map) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def update_post(%Post{} = post, attrs) do
|
|
|
|
post
|
2020-11-26 11:41:13 +01:00
|
|
|
|> Repo.preload([:tags, :media])
|
2020-07-09 17:24:28 +02:00
|
|
|
|> Post.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a post
|
|
|
|
"""
|
|
|
|
@spec delete_post(Post.t()) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_post(%Post{} = post), do: Repo.delete(post)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of tags for the post.
|
|
|
|
"""
|
|
|
|
@spec list_tags_for_post(integer | String.t()) :: [Tag.t()]
|
|
|
|
def list_tags_for_post(post_id) do
|
|
|
|
{:ok, uuid} = Ecto.UUID.dump(post_id)
|
|
|
|
|
|
|
|
uuid
|
|
|
|
|> tags_for_post_query()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec tags_for_post_query(String.t()) :: Ecto.Query.t()
|
2020-07-09 17:24:28 +02:00
|
|
|
defp tags_for_post_query(post_id) do
|
|
|
|
from(
|
|
|
|
t in Tag,
|
|
|
|
join: p in "posts_tags",
|
|
|
|
on: t.id == p.tag_id,
|
|
|
|
where: p.post_id == ^post_id
|
|
|
|
)
|
|
|
|
end
|
2020-08-12 16:05:34 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_public(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2020-08-12 16:05:34 +02:00
|
|
|
defp filter_public(query) do
|
|
|
|
where(query, [p], p.visibility == ^:public and not p.draft)
|
|
|
|
end
|
2020-08-27 11:53:24 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec filter_local(Ecto.Queryable.t()) :: Ecto.Query.t()
|
2021-03-26 15:40:10 +01:00
|
|
|
defp filter_local(query) do
|
|
|
|
where(query, [q], q.local == true)
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
defp do_get_posts_for_group(group_id) do
|
|
|
|
Post
|
|
|
|
|> where(attributed_to_id: ^group_id)
|
|
|
|
|> order_by(desc: :inserted_at)
|
2021-03-26 15:40:10 +01:00
|
|
|
|> preload_post_associations()
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec preload_post_associations(Ecto.Queryable.t(), list()) :: Ecto.Query.t()
|
2021-03-26 15:40:10 +01:00
|
|
|
defp preload_post_associations(query, associations \\ @post_preloads) do
|
|
|
|
preload(query, ^associations)
|
2020-08-27 11:53:24 +02:00
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|