Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions lib/claper/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ defmodule Claper.Posts do

with %Claper.Presentations.PresentationState{} <- state,
{:ok, author_attrs} <- post_author_attrs(actor),
:ok <- attendee_chat_allowed(state, author_attrs.name) do
:ok <- attendee_chat_allowed(state, author_attrs) do
Post
|> struct(Map.merge(author_attrs, %{event: event, position: state.position}))
|> Post.attendee_changeset(attrs)
Expand Down Expand Up @@ -422,12 +422,15 @@ defmodule Claper.Posts do

defp post_author_attrs(_actor), do: {:error, :forbidden}

defp attendee_chat_allowed(state, name) do
defp attendee_chat_allowed(state, author_attrs) do
cond do
state.chat_enabled == false ->
{:error, :chat_disabled}

state.anonymous_chat_enabled == false and name in [nil, ""] ->
state.authenticated_chat_only == true and not Map.has_key?(author_attrs, :user_id) ->
{:error, :login_required}

state.anonymous_chat_enabled == false and author_attrs.name in [nil, ""] ->
{:error, :nickname_required}

true ->
Expand Down
66 changes: 66 additions & 0 deletions lib/claper/presentations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,76 @@ defmodule Claper.Presentations do
def update_presentation_state(%PresentationState{} = presentation_state, attrs) do
presentation_state
|> PresentationState.changeset(attrs)
|> validate_chat_settings_while_chat_off(presentation_state)
|> Repo.update()
|> broadcast(:state_updated)
end

# Who may write a message only means something while messages are on at all,
# which is why the moderator's screen renders both toggles disabled then. The
# same rule belongs here, so a push that skips the rendered control is refused
# with an error instead of quietly rewriting the setting.
@chat_audience_settings [:anonymous_chat_enabled, :authenticated_chat_only]

defp validate_chat_settings_while_chat_off(changeset, presentation_state) do
if chat_enabled?(changeset, presentation_state) do
changeset
else
Enum.reduce(@chat_audience_settings, changeset, &refuse_change_while_chat_off/2)
end
end

defp refuse_change_while_chat_off(field, changeset) do
case Ecto.Changeset.fetch_change(changeset, field) do
{:ok, _value} ->
Ecto.Changeset.add_error(
changeset,
field,
"can only be changed while messages are enabled"
)

:error ->
changeset
end
end

# The stored row decides, not the struct the caller happens to hold, unless
# the very same update switches messages on.
defp chat_enabled?(changeset, presentation_state) do
case Ecto.Changeset.fetch_change(changeset, :chat_enabled) do
{:ok, chat_enabled} ->
chat_enabled

:error ->
from(ps in PresentationState,
where: ps.id == ^presentation_state.id and ps.chat_enabled == true
)
|> Repo.exists?()
end
end

@doc """
Returns true when the event only accepts messages from authenticated users.

Always reads the current state from the database, so a caller holding a
presentation state loaded earlier cannot accept a message with a stale value
after the moderator turned the setting on.

## Examples

iex> authenticated_chat_only?(event_id)
false

"""
def authenticated_chat_only?(event_id) do
from(ps in PresentationState,
join: pf in PresentationFile,
on: ps.presentation_file_id == pf.id,
where: pf.event_id == ^event_id and ps.authenticated_chat_only == true
)
|> Repo.exists?()
end

@interaction_schemas [
Claper.Polls.Poll,
Claper.Forms.Form,
Expand Down
3 changes: 3 additions & 0 deletions lib/claper/presentations/presentation_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Claper.Presentations.PresentationState do
join_screen_visible: boolean() | nil,
chat_enabled: boolean() | nil,
anonymous_chat_enabled: boolean() | nil,
authenticated_chat_only: boolean() | nil,
message_reaction_enabled: boolean() | nil,
banned: [String.t()] | nil,
show_only_pinned: boolean() | nil,
Expand All @@ -26,6 +27,7 @@ defmodule Claper.Presentations.PresentationState do
field :join_screen_visible, :boolean
field :chat_enabled, :boolean
field :anonymous_chat_enabled, :boolean
field :authenticated_chat_only, :boolean, default: false
field :message_reaction_enabled, :boolean, default: true
field :banned, {:array, :string}, default: []
field :show_only_pinned, :boolean, default: false
Expand All @@ -48,6 +50,7 @@ defmodule Claper.Presentations.PresentationState do
:presentation_file_id,
:chat_enabled,
:anonymous_chat_enabled,
:authenticated_chat_only,
:show_only_pinned,
:show_attendee_count,
:message_reaction_enabled
Expand Down
27 changes: 26 additions & 1 deletion lib/claper_web/controllers/user_session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@ defmodule ClaperWeb.UserSessionController do
alias Claper.Accounts
alias ClaperWeb.UserAuth

def new(conn, _params) do
def new(conn, params) do
oidc_auto_redirect_login = Application.get_env(:claper, :oidc)[:auto_redirect_login]

conn
|> maybe_store_return_to(params)
|> redirect_to_login(oidc_auto_redirect_login)
end

# A page that sends a visitor here can name where to send them back to, the
# same session key `UserAuth.log_in_user/3` already reads for LTI launches.
defp maybe_store_return_to(conn, %{"return_to" => return_to}) when is_binary(return_to) do
if local_path?(return_to) do
put_session(conn, :user_return_to, return_to)
else
conn
end
end

defp maybe_store_return_to(conn, _params), do: conn

# The characters `Phoenix.Controller.redirect/2` refuses in a local path. A
# path holding one of them reaches `redirect(to: ...)` and raises there, so it
# must not make it into the session in the first place.
@unsafe_local_path_chars ["\\", "/%09", "/\t"]

# Only same origin paths, so a crafted link cannot turn the login form into
# an open redirect or crash the login it is attached to.
defp local_path?(path) do
String.starts_with?(path, "/") and not String.starts_with?(path, "//") and
not String.contains?(path, @unsafe_local_path_chars)
end

defp redirect_to_login(conn, true) do
conn |> redirect(to: "/users/oidc")
end
Expand Down
27 changes: 17 additions & 10 deletions lib/claper_web/live/event_live/manage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -908,18 +908,25 @@ defmodule ClaperWeb.EventLive.Manage do
@impl true
def handle_event(
"checked",
%{"key" => "anonymous_chat_enabled", "value" => value},
%{"key" => key, "value" => value},
%{assigns: %{state: state}} = socket
) do
{:ok, new_state} =
Claper.Presentations.update_presentation_state(
state,
%{
:anonymous_chat_enabled => value
}
)

{:noreply, socket |> assign(:state, new_state)}
when key in ["anonymous_chat_enabled", "authenticated_chat_only"] do
# Both toggles are rendered disabled while messages are off, and the context
# refuses the change as well. A push that arrives anyway is answered rather
# than swallowed.
case Claper.Presentations.update_presentation_state(
state,
%{String.to_existing_atom(key) => value}
) do
{:ok, new_state} ->
{:noreply, socket |> assign(:state, new_state)}

{:error, %Ecto.Changeset{}} ->
{:noreply,
socket
|> put_flash(:error, gettext("Turn messages on before changing who can post"))}
end
end

@impl true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ defmodule ClaperWeb.EventLive.ManageAttendeesOptionsComponent do
</svg>
</:icon>
</.toggle_row>
<.toggle_row
label={
if @state.authenticated_chat_only,
do: gettext("Allow messages without login"),
else: gettext("Require login to post")
}
checked={@state.authenticated_chat_only}
key={:authenticated_chat_only}
shortcut={if @create == nil, do: "F", else: nil}
disabled={!@state.chat_enabled}
show_shortcut={@show_shortcut}
>
<:icon>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5"
>
<path
fill-rule="evenodd"
d="M10 1a4.5 4.5 0 0 0-4.5 4.5V9H5a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-6a2 2 0 0 0-2-2h-.5V5.5A4.5 4.5 0 0 0 10 1Zm3 8V5.5a3 3 0 1 0-6 0V9h6Z"
clip-rule="evenodd"
/>
</svg>
</:icon>
</.toggle_row>
<.toggle_row
label={
if @state.message_reaction_enabled,
Expand Down
14 changes: 14 additions & 0 deletions lib/claper_web/live/event_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,17 @@ defmodule ClaperWeb.EventLive.Show do
{:noreply, socket}
end

@impl true
def handle_event(
"save",
_params,
%{assigns: %{state: %{authenticated_chat_only: true}, current_user: current_user}} =
socket
)
when not is_map(current_user) do
{:noreply, socket |> put_flash(:error, gettext("You must be logged in to post a message"))}
end

@impl true
def handle_event("save", %{"post" => post_params}, socket) do
if is_map(post_params) &&
Expand All @@ -513,6 +524,9 @@ defmodule ClaperWeb.EventLive.Show do
SecurityAudit.observe(socket, "message", nil, "chat_disabled")
{:noreply, socket}

{:error, :login_required} ->
{:noreply, put_flash(socket, :error, gettext("You must be logged in to post a message"))}

{:error, _reason} ->
{:noreply, socket}
end
Expand Down
109 changes: 63 additions & 46 deletions lib/claper_web/live/event_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
match?(%Claper.Quizzes.Quiz{}, @current_interaction) ||
match?(%Claper.WordClouds.WordCloud{}, @current_interaction) ||
match?(%Claper.Embeds.Embed{attendee_visibility: true}, @current_interaction) %>
<% composer_open? =
@state.chat_enabled && !(@state.authenticated_chat_only && is_nil(@current_user)) %>
<% captions_visible? =
@transcription_config && @transcription_config.enabled &&
@transcription_config.visibility in ["both", "attendee"] %>
Expand Down Expand Up @@ -56,6 +58,7 @@
</div>

<div
:if={composer_open?}
id="identity-menu"
phx-click-away={JS.hide(to: "#identity-menu")}
class="absolute bottom-16 left-2 z-40 hidden w-64 rounded-2xl border border-white/10 bg-gray-900 p-2 text-white shadow-2xl"
Expand Down Expand Up @@ -497,55 +500,69 @@
id="room-composer"
class="absolute inset-x-0 bottom-0 z-30 bg-transparent px-3 pb-[max(0.75rem,env(safe-area-inset-bottom))] pt-2"
>
<%= if @state.chat_enabled do %>
<.form
:let={f}
for={@post_changeset}
id="post-form"
class="attendee-composer flex items-center gap-1.5 rounded-2xl border border-white/20 px-1.5 py-1 shadow-2xl"
phx-hook="PostForm"
data-nickname={@nickname}
data-storage-key={"post-draft:#{@event.uuid}"}
phx-submit="save"
>
<button
id="composer-identity-button"
type="button"
aria-label={gettext("Choose identity")}
phx-click={JS.toggle(to: "#identity-menu")}
class="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-black/60 text-xs font-bold text-white"
<%= cond do %>
<% !@state.chat_enabled -> %>
<div class="attendee-composer flex min-h-10 items-center rounded-2xl border border-white/20 px-3 text-sm text-white/60 shadow-2xl">
{gettext("Messages deactivated")}
</div>
<% !composer_open? -> %>
<div
id="login-required-composer"
class="attendee-composer flex min-h-10 items-center justify-between gap-3 rounded-2xl border border-white/20 px-3 py-1.5 text-sm text-white/60 shadow-2xl"
>
{if @nickname in [nil, ""], do: "A", else: String.first(@nickname)}
</button>
<span>{gettext("Log in to post a message")}</span>
<a
href={~p"/users/log_in?#{[return_to: "/e/#{@event.code}"]}"}
class="shrink-0 rounded-full bg-primary-500 px-3 py-1.5 text-xs font-bold text-white"
>
{gettext("Log in")}
</a>
</div>
<% true -> %>
<.form
:let={f}
for={@post_changeset}
id="post-form"
class="attendee-composer flex items-center gap-1.5 rounded-2xl border border-white/20 px-1.5 py-1 shadow-2xl"
phx-hook="PostForm"
data-nickname={@nickname}
data-storage-key={"post-draft:#{@event.uuid}"}
phx-submit="save"
>
<button
id="composer-identity-button"
type="button"
aria-label={gettext("Choose identity")}
phx-click={JS.toggle(to: "#identity-menu")}
class="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-black/60 text-xs font-bold text-white"
>
{if @nickname in [nil, ""], do: "A", else: String.first(@nickname)}
</button>

{textarea(f, :body,
id: "postFormTA",
disabled: !@state.anonymous_chat_enabled && @nickname in [nil, ""],
rows: 1,
maxlength: 255,
class:
"min-h-9 max-h-16 min-w-0 flex-1 resize-none border-0 bg-transparent px-2 py-2 text-base leading-5 text-white outline-hidden placeholder:text-white/70 focus:ring-0 disabled:cursor-not-allowed disabled:opacity-50",
placeholder:
if(!@state.anonymous_chat_enabled && @nickname in [nil, ""],
do: gettext("Set your nickname to participate"),
else: gettext("Ask, comment...")
)
)}
{textarea(f, :body,
id: "postFormTA",
disabled: !@state.anonymous_chat_enabled && @nickname in [nil, ""],
rows: 1,
maxlength: 255,
class:
"min-h-9 max-h-16 min-w-0 flex-1 resize-none border-0 bg-transparent px-2 py-2 text-base leading-5 text-white outline-hidden placeholder:text-white/70 focus:ring-0 disabled:cursor-not-allowed disabled:opacity-50",
placeholder:
if(!@state.anonymous_chat_enabled && @nickname in [nil, ""],
do: gettext("Set your nickname to participate"),
else: gettext("Ask, comment...")
)
)}

<button
type="submit"
id="submitBtn"
disabled
aria-label={gettext("Send message")}
class="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-black/20 text-white opacity-50 transition disabled:cursor-not-allowed"
>
<img src="/images/icons/send.svg" class="h-5 w-5" />
</button>
</.form>
<% else %>
<div class="attendee-composer flex min-h-10 items-center rounded-2xl border border-white/20 px-3 text-sm text-white/60 shadow-2xl">
{gettext("Messages deactivated")}
</div>
<button
type="submit"
id="submitBtn"
disabled
aria-label={gettext("Send message")}
class="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-black/20 text-white opacity-50 transition disabled:cursor-not-allowed"
>
<img src="/images/icons/send.svg" class="h-5 w-5" />
</button>
</.form>
<% end %>
</footer>
</main>
Expand Down
Loading
Loading