mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 04:27:59 +01:00
4dcb2958d3
commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
250 lines
5.5 KiB
Elixir
250 lines
5.5 KiB
Elixir
defmodule Philomena.Conversations do
|
|
@moduledoc """
|
|
The Conversations context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Ecto.Multi
|
|
alias Philomena.Repo
|
|
|
|
alias Philomena.Conversations.Conversation
|
|
|
|
@doc """
|
|
Gets a single conversation.
|
|
|
|
Raises `Ecto.NoResultsError` if the Conversation does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_conversation!(123)
|
|
%Conversation{}
|
|
|
|
iex> get_conversation!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_conversation!(id), do: Repo.get!(Conversation, id)
|
|
|
|
@doc """
|
|
Creates a conversation.
|
|
|
|
## Examples
|
|
|
|
iex> create_conversation(%{field: value})
|
|
{:ok, %Conversation{}}
|
|
|
|
iex> create_conversation(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_conversation(from, attrs \\ %{}) do
|
|
%Conversation{}
|
|
|> Conversation.creation_changeset(from, attrs)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a conversation.
|
|
|
|
## Examples
|
|
|
|
iex> update_conversation(conversation, %{field: new_value})
|
|
{:ok, %Conversation{}}
|
|
|
|
iex> update_conversation(conversation, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_conversation(%Conversation{} = conversation, attrs) do
|
|
conversation
|
|
|> Conversation.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a Conversation.
|
|
|
|
## Examples
|
|
|
|
iex> delete_conversation(conversation)
|
|
{:ok, %Conversation{}}
|
|
|
|
iex> delete_conversation(conversation)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_conversation(%Conversation{} = conversation) do
|
|
Repo.delete(conversation)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking conversation changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_conversation(conversation)
|
|
%Ecto.Changeset{source: %Conversation{}}
|
|
|
|
"""
|
|
def change_conversation(%Conversation{} = conversation) do
|
|
Conversation.changeset(conversation, %{})
|
|
end
|
|
|
|
def count_unread_conversations(user) do
|
|
Conversation
|
|
|> where(
|
|
[c],
|
|
((c.to_id == ^user.id and c.to_read == false) or
|
|
(c.from_id == ^user.id and c.from_read == false)) and
|
|
not ((c.to_id == ^user.id and c.to_hidden == true) or
|
|
(c.from_id == ^user.id and c.from_hidden == true))
|
|
)
|
|
|> Repo.aggregate(:count, :id)
|
|
end
|
|
|
|
def mark_conversation_read(conversation, user, read \\ true)
|
|
|
|
def mark_conversation_read(
|
|
%Conversation{to_id: user_id, from_id: user_id} = conversation,
|
|
%{id: user_id},
|
|
read
|
|
) do
|
|
conversation
|
|
|> Conversation.read_changeset(%{to_read: read, from_read: read})
|
|
|> Repo.update()
|
|
end
|
|
|
|
def mark_conversation_read(%Conversation{to_id: user_id} = conversation, %{id: user_id}, read) do
|
|
conversation
|
|
|> Conversation.read_changeset(%{to_read: read})
|
|
|> Repo.update()
|
|
end
|
|
|
|
def mark_conversation_read(%Conversation{from_id: user_id} = conversation, %{id: user_id}, read) do
|
|
conversation
|
|
|> Conversation.read_changeset(%{from_read: read})
|
|
|> Repo.update()
|
|
end
|
|
|
|
def mark_conversation_read(_conversation, _user, _read), do: {:ok, nil}
|
|
|
|
def mark_conversation_hidden(conversation, user, hidden \\ true)
|
|
|
|
def mark_conversation_hidden(
|
|
%Conversation{to_id: user_id} = conversation,
|
|
%{id: user_id},
|
|
hidden
|
|
) do
|
|
conversation
|
|
|> Conversation.hidden_changeset(%{to_hidden: hidden})
|
|
|> Repo.update()
|
|
end
|
|
|
|
def mark_conversation_hidden(
|
|
%Conversation{from_id: user_id} = conversation,
|
|
%{id: user_id},
|
|
hidden
|
|
) do
|
|
conversation
|
|
|> Conversation.hidden_changeset(%{from_hidden: hidden})
|
|
|> Repo.update()
|
|
end
|
|
|
|
def mark_conversation_hidden(_conversation, _user, _read), do: {:ok, nil}
|
|
|
|
alias Philomena.Conversations.Message
|
|
|
|
@doc """
|
|
Gets a single message.
|
|
|
|
Raises `Ecto.NoResultsError` if the Message does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_message!(123)
|
|
%Message{}
|
|
|
|
iex> get_message!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_message!(id), do: Repo.get!(Message, id)
|
|
|
|
@doc """
|
|
Creates a message.
|
|
|
|
## Examples
|
|
|
|
iex> create_message(%{field: value})
|
|
{:ok, %Message{}}
|
|
|
|
iex> create_message(%{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def create_message(conversation, user, attrs \\ %{}) do
|
|
message =
|
|
Ecto.build_assoc(conversation, :messages)
|
|
|> Message.creation_changeset(attrs, user)
|
|
|
|
conversation_query =
|
|
Conversation
|
|
|> where(id: ^conversation.id)
|
|
|
|
now = DateTime.utc_now()
|
|
|
|
Multi.new()
|
|
|> Multi.insert(:message, message)
|
|
|> Multi.update_all(:conversation, conversation_query,
|
|
set: [from_read: false, to_read: false, last_message_at: now]
|
|
)
|
|
|> Repo.transaction()
|
|
end
|
|
|
|
@doc """
|
|
Updates a message.
|
|
|
|
## Examples
|
|
|
|
iex> update_message(message, %{field: new_value})
|
|
{:ok, %Message{}}
|
|
|
|
iex> update_message(message, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_message(%Message{} = message, attrs) do
|
|
message
|
|
|> Message.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a Message.
|
|
|
|
## Examples
|
|
|
|
iex> delete_message(message)
|
|
{:ok, %Message{}}
|
|
|
|
iex> delete_message(message)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_message(%Message{} = message) do
|
|
Repo.delete(message)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking message changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_message(message)
|
|
%Ecto.Changeset{source: %Message{}}
|
|
|
|
"""
|
|
def change_message(%Message{} = message) do
|
|
Message.changeset(message, %{})
|
|
end
|
|
end
|