<commit_before>defmodule Mail.Templates do
  require EEx
  EEx.function_from_file :def, :common, "data/mailtemplates/common.eex", [:title, :content]
  EEx.function_from_file :def, :collab_notification_html, "data/mailtemplates/collab_notification.html.eex", [:name, :entered_name, :design_name, :cookie, :basename]
  EEx.function_from_file :def, :collab_notification_text, "data/mailtemplates/collab_notification.txt.eex", [:name, :entered_name, :design_name, :cookie, :basename]
end

defmodule Mail do
  alias Mail.Templates
  require Logger
  import Ecto.Query
  require Ecto.Query
  alias Plug.Conn

  @basename Application.get_env(:mailer, :basename)

  def send_notification(conn, room, entered, design, userids) when is_list(userids) do
    if Application.get_env(:mailer, :disabled) do
      Logger.warn("Emailing disabled")
    else
      Task.Supervisor.start_child(:email_sup, fn ->
        Enum.each(userids, fn [id, nick] -> 
          if !Survey.User.is_unsubscribed?(id, "collab") &&
            Survey.ChatPresence.not_online?(room, id) do
              generate_notification(conn, entered, design, [id, nick])
              |> Survey.Mailer.deliver

              Logger.info("Sent email to #{id}")
          end
        end)
      end)
    end
  end

  def generate_notification(conn, entered, design, [id, nick]) do
    email = Survey.User.get_email(id)

    cookie = conn
    |> Conn.clear_session
    |> Conn.put_session(:repo_userid, id)
    |> Conn.put_session(:email, true)
    |> ParamSession.gen_cookie

    text = Templates.collab_notification_text(nick, entered, design, 
      cookie, @basename)
    html = Templates.collab_notification_html(nick, entered, design, 
      cookie, @basename)

    %Mailman.Email{
      subject: "#{entered} entered the collaborative workbench",
      from: "noreply<EMAIL>",
      to: email,
      text: text,
      html: html }
  end

  def gen_url(conn, id, url) do
    tmp_conn = Plug.Conn.put_session(conn, :edx_userid, id)
    ParamSession.gen_url(tmp_conn, @basename <> url)
  end

  def user_mail(id) do<commit_msg>Adding proper lti authentication to sessions in email notifications<commit_after>defmodule Mail.Templates do
  require EEx
  EEx.function_from_file :def, :common, "data/mailtemplates/common.eex", [:title, :content]
  EEx.function_from_file :def, :collab_notification_html, "data/mailtemplates/collab_notification.html.eex", [:name, :entered_name, :design_name, :cookie, :basename]
  EEx.function_from_file :def, :collab_notification_text, "data/mailtemplates/collab_notification.txt.eex", [:name, :entered_name, :design_name, :cookie, :basename]
end

defmodule Mail do
  alias Mail.Templates
  require Logger
  import Ecto.Query
  require Ecto.Query
  alias Plug.Conn

  @basename Application.get_env(:mailer, :basename)

  def send_notification(conn, room, entered, design, userids) when is_list(userids) do
    if Application.get_env(:mailer, :disabled) do
      Logger.warn("Emailing disabled")
    else
      Task.Supervisor.start_child(:email_sup, fn ->
        Enum.each(userids, fn [id, nick] -> 
          if !Survey.User.is_unsubscribed?(id, "collab") &&
            Survey.ChatPresence.not_online?(room, id) do
              generate_notification(conn, entered, design, [id, nick])
              |> Survey.Mailer.deliver

              Logger.info("Sent email to #{id}")
          end
        end)
      end)
    end
  end

  def generate_notification(conn, entered, design, [id, nick]) do
    [email, hash] = (from f in Survey.User,
    where: f.id == ^id,
    select: [f.edx_email, f.hash]) |> Survey.Repo.one
    
    cookie = conn
    |> Conn.clear_session
    |> Conn.put_session(:repo_userid, id)
    |> Conn.put_session(:lti_userid, hash)
    |> Conn.put_session(:email, true)
    |> ParamSession.gen_cookie

    text = Templates.collab_notification_text(nick, entered, design, 
      cookie, @basename)
    html = Templates.collab_notification_html(nick, entered, design, 
      cookie, @basename)

    %Mailman.Email{
      subject: "#{entered} entered the collaborative workbench",
      from: "<EMAIL>",
      to: [email],
      text: text,
      html: html }
  end

  def gen_url(conn, id, url) do
    tmp_conn = Plug.Conn.put_session(conn, :edx_userid, id)
    ParamSession.gen_url(tmp_conn, @basename <> url)
  end

  def user_mail(id) do
{'fraction_non_alphanumeric': 0.11924493554327809, 'fraction_numerical': 0.0, 'mean_word_length': 3.470164609053498, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697755', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1428, 'n_words': 320}