Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Embed #61 #72

Merged
merged 28 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
673d410
Add image os for docker image
alxlion Dec 17, 2022
3c167ba
Add iamge os for doc job
alxlion Dec 17, 2022
423ff06
Merge branch 'dev'
alxlion Jan 30, 2023
161ca09
Merge branch 'dev'
alxlion Feb 27, 2023
fd93e2b
Fix missing volume in docker compose file (#27)
aweher Mar 3, 2023
64cd399
Add funding
alxlion Mar 31, 2023
c1939c5
Merge branch 'dev'
alxlion Apr 21, 2023
bd17ffc
Add stale plugin
alxlion Apr 21, 2023
652a599
Add roadmap label
alxlion Apr 21, 2023
88919be
Merge branch 'dev'
alxlion Jul 2, 2023
897575e
Fix typo
alxlion Jul 2, 2023
e05defb
Merge branch 'dev'
alxlion Aug 11, 2023
3d93a3b
Fix docs typo
alxlion Aug 11, 2023
3d847e7
Fix dockerfile
alxlion Aug 11, 2023
d170cb8
Merge branch 'dev'
alxlion Oct 7, 2023
bdd5bd3
feat: Add Embed
Dhanus3133 Oct 31, 2023
265fab4
feat: Add Embed tests
Dhanus3133 Oct 31, 2023
8018e49
feat: Add Embed i18n translations
Dhanus3133 Oct 31, 2023
6f2c2fa
Merge branch 'dev' into feat/add_embed
Dhanus3133 Oct 31, 2023
6561972
fix: Embed i18n translations and tests
Dhanus3133 Oct 31, 2023
1e15c2e
fix: embed tests formating
Dhanus3133 Oct 31, 2023
c11d294
feat: toggle active and inactive
Dhanus3133 Nov 2, 2023
4ca179d
fix: check enabled status for update event in poll, form, embed
Dhanus3133 Nov 6, 2023
38da3ca
feat: embed display only for attendees
Dhanus3133 Nov 6, 2023
4082d6a
fix: shortcut toggle disabled in input fields
Dhanus3133 Nov 10, 2023
ed2a5c0
fix: shortcut fix updated
Dhanus3133 Nov 10, 2023
7f22f07
feat: display presenter's embed in fullscreen
Dhanus3133 Nov 12, 2023
97eb4fe
feat: validate embed format
Dhanus3133 Nov 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions lib/claper/embeds.ex
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
38 changes: 38 additions & 0 deletions lib/claper/embeds/embed.ex
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
21 changes: 19 additions & 2 deletions lib/claper/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,12 @@ defmodule Claper.Events do
|> Ecto.Multi.run(:from_event, fn _repo, _changes ->
{:ok,
get_user_event!(user_id, from_event_uuid,
presentation_file: [polls: [:poll_opts], forms: []]
presentation_file: [polls: [:poll_opts], forms: [], embeds: []]
)}
end)
|> Ecto.Multi.run(:to_event, fn _repo, _changes ->
{:ok, get_user_event!(user_id, to_event_uuid, presentation_file: [:polls, :forms])}
{:ok,
get_user_event!(user_id, to_event_uuid, presentation_file: [:polls, :forms, :embeds])}
end)
|> Ecto.Multi.run(:polls, fn _repo, %{from_event: from_event, to_event: to_event} ->
{:ok,
Expand Down Expand Up @@ -321,6 +322,22 @@ defmodule Claper.Events do
end
end)}
end)
|> Ecto.Multi.run(:embeds, fn _repo, %{from_event: from_event, to_event: to_event} ->
{:ok,
from_event.presentation_file.embeds
|> Enum.each(fn embed ->
if embed.position < to_event.presentation_file.length do
Claper.Embeds.create_embed(%{
title: embed.title,
content: embed.content,
position: embed.position,
enabled: embed.enabled,
attendee_visibility: embed.attendee_visibility,
presentation_file_id: to_event.presentation_file.id
})
end
end)}
end)
|> Repo.transaction() do
{:ok, %{to_event: to_event}} -> {:ok, to_event}
end
Expand Down
18 changes: 10 additions & 8 deletions lib/claper/forms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,22 @@ defmodule Claper.Forms do
|> Repo.update_all(set: [enabled: false])
end

def set_default(id, presentation_file_id, position) do
from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id != ^id
)
|> Repo.update_all(set: [enabled: false])
def set_status(id, presentation_file_id, position, status) do
if status do
from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id != ^id
)
|> Repo.update_all(set: [enabled: false])
end

from(f in Form,
where:
f.presentation_file_id == ^presentation_file_id and f.position == ^position and
f.id == ^id
)
|> Repo.update_all(set: [enabled: true])
|> Repo.update_all(set: [enabled: status])
end

defp broadcast({:error, _reason} = error, _form), do: error
Expand Down
18 changes: 10 additions & 8 deletions lib/claper/polls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,22 @@ defmodule Claper.Polls do
|> Repo.update_all(set: [enabled: false])
end

def set_default(id, presentation_file_id, position) do
from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id != ^id
)
|> Repo.update_all(set: [enabled: false])
def set_status(id, presentation_file_id, position, status) do
if status do
from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id != ^id
)
|> Repo.update_all(set: [enabled: false])
end

from(p in Poll,
where:
p.presentation_file_id == ^presentation_file_id and p.position == ^position and
p.id == ^id
)
|> Repo.update_all(set: [enabled: true])
|> Repo.update_all(set: [enabled: status])
end

defp broadcast({:error, _reason} = error, _poll), do: error
Expand Down
1 change: 1 addition & 0 deletions lib/claper/presentations/presentation_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Claper.Presentations.PresentationFile do
belongs_to :event, Claper.Events.Event
has_many :polls, Claper.Polls.Poll
has_many :forms, Claper.Forms.Form
has_many :embeds, Claper.Embeds.Embed
has_one :presentation_state, Claper.Presentations.PresentationState, on_replace: :delete

timestamps()
Expand Down
Loading
Loading