Skip to content

Commit f1b8154

Browse files
committed
Add delimeter + bug fixes
1 parent af395df commit f1b8154

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

lib/cadet/assessments/assessments.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,6 @@ end
31353135
question = Map.put(ans.question, :question, empty_contest_leaderboard)
31363136
Map.put(ans, :question, question)
31373137
else
3138-
31393138
ans
31403139
end
31413140
end)

lib/cadet/courses/course.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Cadet.Courses.Course do
55
use Cadet, :model
66

77
alias Cadet.Courses.AssessmentConfig
8-
alias Cadet.AICommentsHelpers
8+
alias CadetWeb.AICommentsHelpers
99

1010
@type t :: %__MODULE__{
1111
course_name: String.t(),

lib/cadet_web/admin_views/admin_grading_view.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ defmodule CadetWeb.AdminGradingView do
152152
def render("grading_info.json", %{answer: answer}) do
153153
transform_map_for_view(answer, %{
154154
id: &(&1.id),
155-
ai_comments: &extract_ai_comments_per_answer(&1.question_id, &1.ai_comments),
155+
ai_comments: &extract_ai_comments_per_answer(&1.id, &1.ai_comments),
156156
student: &extract_student_data(&1.submission.student),
157157
team: &extract_team_data(&1.submission.team),
158158
question: &build_grading_question/1,
@@ -165,11 +165,11 @@ defmodule CadetWeb.AdminGradingView do
165165
%{cols: cols, rows: summary}
166166
end
167167

168-
defp extract_ai_comments_per_answer(question_id, ai_comments) do
168+
defp extract_ai_comments_per_answer(id, ai_comments) do
169169
matching_comment =
170170
ai_comments
171171
# Equivalent to fn comment -> comment.question_id == question_id end
172-
|> Enum.find(&(&1.question_id == question_id))
172+
|> Enum.find(&(&1.answer_id == id))
173173

174174
case matching_comment do
175175
nil -> nil

lib/cadet_web/helpers/ai_comments_helpers.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ defmodule CadetWeb.AICommentsHelpers do
1212

1313
case Base.decode64(encrypted_key) do
1414
{:ok, decoded} ->
15-
iv = binary_part(decoded, 0, 16)
16-
tag = binary_part(decoded, 16, 16)
17-
ciphertext = binary_part(decoded, 32, byte_size(decoded) - 32)
1815

19-
case :crypto.crypto_one_time_aead(:aes_gcm, key, iv, ciphertext, "", tag, false) do
20-
plain_text when is_binary(plain_text) -> {:ok, plain_text}
21-
_ -> {:decrypt_error, :decryption_failed}
16+
with [iv, tag, ciphertext] <- :binary.split(decoded, <<"|">>, [:global]) do
17+
case :crypto.crypto_one_time_aead(:aes_gcm, key, iv, ciphertext, "", tag, false) do
18+
plain_text when is_binary(plain_text) -> {:ok, plain_text}
19+
_ -> {:decrypt_error, :decryption_failed}
20+
end
21+
else
22+
_ -> {:error, :invalid_format}
2223
end
2324

25+
26+
2427
_ ->
2528
Logger.error(
2629
"Failed to decode encrypted key, is it a valid AES-256 key of 16, 24 or 32 bytes?"
@@ -55,7 +58,7 @@ defmodule CadetWeb.AICommentsHelpers do
5558
)
5659

5760
# Store both the IV, ciphertext and tag
58-
encrypted = Base.encode64(iv <> tag <> ciphertext)
61+
encrypted = Base.encode64(iv <> "|" <> tag <> "|" <> ciphertext)
5962
else
6063
{:error, :invalid_encryption_key}
6164
end

0 commit comments

Comments
 (0)