-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4b91b0
commit d398906
Showing
26 changed files
with
1,627 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
defmodule Claper.Embeds do | ||
@moduledoc """ | ||
The Embeds context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Claper.Repo | ||
|
||
alias Claper.Embeds.Embed | ||
|
||
@doc """ | ||
Returns the list of embeds for a given presentation file. | ||
## Examples | ||
iex> list_embeds(123) | ||
[%Embed{}, ...] | ||
""" | ||
def list_embeds(presentation_file_id) do | ||
from(e in Embed, | ||
where: e.presentation_file_id == ^presentation_file_id, | ||
order_by: [asc: e.id, asc: e.position] | ||
) | ||
|> Repo.all() | ||
end | ||
|
||
@doc """ | ||
Returns the list of embeds for a given presentation file and a given position. | ||
## Examples | ||
iex> list_embeds_at_position(123, 0) | ||
[%Embed{}, ...] | ||
""" | ||
def list_embeds_at_position(presentation_file_id, position) do | ||
from(e in Embed, | ||
where: e.presentation_file_id == ^presentation_file_id and e.position == ^position, | ||
order_by: [asc: e.id] | ||
) | ||
|> Repo.all() | ||
end | ||
|
||
@doc """ | ||
Gets a single embed. | ||
Raises `Ecto.NoResultsError` if the Embed does not exist. | ||
## Examples | ||
iex> get_embed!(123) | ||
%Embed{} | ||
iex> get_embed!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_embed!(id, preload \\ []), | ||
do: Repo.get!(Embed, id) |> Repo.preload(preload) | ||
|
||
@doc """ | ||
Gets a single embed for a given position. | ||
## Examples | ||
iex> get_embed_current_position(123, 0) | ||
%Embed{} | ||
""" | ||
def get_embed_current_position(presentation_file_id, position) do | ||
from(e in Embed, | ||
where: | ||
e.position == ^position and e.presentation_file_id == ^presentation_file_id and | ||
e.enabled == true | ||
) | ||
|> Repo.one() | ||
end | ||
|
||
@doc """ | ||
Creates a embed. | ||
## Examples | ||
iex> create_embed(%{field: value}) | ||
{:ok, %Embed{}} | ||
iex> create_embed(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_embed(attrs \\ %{}) do | ||
%Embed{} | ||
|> Embed.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a embed. | ||
## Examples | ||
iex> update_embed("123e4567-e89b-12d3-a456-426614174000", embed, %{field: new_value}) | ||
{:ok, %Embed{}} | ||
iex> update_embed("123e4567-e89b-12d3-a456-426614174000", embed, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_embed(event_uuid, %Embed{} = embed, attrs) do | ||
embed | ||
|> Embed.changeset(attrs) | ||
|> Repo.update() | ||
|> case do | ||
{:ok, embed} -> | ||
broadcast({:ok, embed, event_uuid}, :embed_updated) | ||
|
||
{:error, changeset} -> | ||
{:error, %{changeset | action: :update}} | ||
end | ||
end | ||
|
||
@doc """ | ||
Deletes a embed. | ||
## Examples | ||
iex> delete_embed("123e4567-e89b-12d3-a456-426614174000", embed) | ||
{:ok, %Embed{}} | ||
iex> delete_embed("123e4567-e89b-12d3-a456-426614174000", embed) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_embed(event_uuid, %Embed{} = embed) do | ||
{:ok, embed} = Repo.delete(embed) | ||
broadcast({:ok, embed, event_uuid}, :embed_deleted) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking embed changes. | ||
## Examples | ||
iex> change_embed(embed) | ||
%Ecto.Changeset{data: %Embed{}} | ||
""" | ||
def change_embed(%Embed{} = embed, attrs \\ %{}) do | ||
Embed.changeset(embed, attrs) | ||
end | ||
|
||
def disable_all(presentation_file_id, position) do | ||
from(e in Embed, | ||
where: e.presentation_file_id == ^presentation_file_id and e.position == ^position | ||
) | ||
|> Repo.update_all(set: [enabled: false]) | ||
end | ||
|
||
def set_status(id, presentation_file_id, position, status) do | ||
if status do | ||
from(e in Embed, | ||
where: | ||
e.presentation_file_id == ^presentation_file_id and e.position == ^position and | ||
e.id != ^id | ||
) | ||
|> Repo.update_all(set: [enabled: false]) | ||
end | ||
|
||
from(e in Embed, | ||
where: | ||
e.presentation_file_id == ^presentation_file_id and e.position == ^position and | ||
e.id == ^id | ||
) | ||
|> Repo.update_all(set: [enabled: status]) | ||
end | ||
|
||
defp broadcast({:error, _reason} = error, _embed), do: error | ||
|
||
defp broadcast({:ok, embed, event_uuid}, event) do | ||
Phoenix.PubSub.broadcast( | ||
Claper.PubSub, | ||
"event:#{event_uuid}", | ||
{event, embed} | ||
) | ||
|
||
{:ok, embed} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Claper.Embeds.Embed do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@derive {Jason.Encoder, only: [:title, :content, :position, :attendee_visibility]} | ||
schema "embeds" do | ||
field :title, :string | ||
field :content, :string | ||
field :enabled, :boolean, default: true | ||
field :position, :integer, default: 0 | ||
field :attendee_visibility, :boolean, default: false | ||
|
||
belongs_to :presentation_file, Claper.Presentations.PresentationFile | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(embed, attrs \\ %{}) do | ||
embed | ||
|> cast(attrs, [ | ||
:enabled, | ||
:title, | ||
:content, | ||
:presentation_file_id, | ||
:position, | ||
:attendee_visibility | ||
]) | ||
|> validate_required([ | ||
:title, | ||
:content, | ||
:presentation_file_id, | ||
:position, | ||
:attendee_visibility | ||
]) | ||
|> validate_format(:content, ~r/<iframe.*<\/iframe>/, message: "Invalid embed format") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.