Skip to content

Commit 99aba97

Browse files
committed
Minor fixes
1 parent dd7c7ce commit 99aba97

File tree

2 files changed

+80
-83
lines changed

2 files changed

+80
-83
lines changed

apps/codebattle/lib/codebattle_web/channels/game_channel.ex

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -282,86 +282,6 @@ defmodule CodebattleWeb.GameChannel do
282282
{:reply, {:ok, %{head_to_head: head_to_head}}, socket}
283283
end
284284

285-
defp assign_editor_summary_rate_limit(socket) do
286-
now_ms = System.monotonic_time(:millisecond)
287-
288-
assign(socket, :editor_summary_rate_limit, %{
289-
window_started_at_ms: now_ms,
290-
summary_count: 0,
291-
event_count: 0
292-
})
293-
end
294-
295-
defp allow_editor_summary?(socket, summary_event_count) do
296-
now_ms = System.monotonic_time(:millisecond)
297-
state = socket.assigns[:editor_summary_rate_limit] || %{}
298-
window_started_at_ms = Map.get(state, :window_started_at_ms, now_ms)
299-
300-
state =
301-
if now_ms - window_started_at_ms >= @telemetry_limit_window_ms do
302-
%{
303-
window_started_at_ms: now_ms,
304-
summary_count: 0,
305-
event_count: 0
306-
}
307-
else
308-
state
309-
end
310-
311-
next_summary_count = Map.get(state, :summary_count, 0) + 1
312-
next_event_count = Map.get(state, :event_count, 0) + summary_event_count
313-
314-
if next_summary_count > @max_editor_summaries_per_window ||
315-
next_event_count > @max_editor_summary_events_per_window do
316-
{:error, :editor_summary_rate_limited}
317-
else
318-
{:ok,
319-
assign(socket, :editor_summary_rate_limit, %{
320-
state
321-
| summary_count: next_summary_count,
322-
event_count: next_event_count
323-
})}
324-
end
325-
end
326-
327-
defp parse_editor_summary_payload(payload) do
328-
summary = payload["summary"] || %{}
329-
330-
%{
331-
summary: summary,
332-
lang_slug: payload["lang_slug"] || payload["langSlug"],
333-
summary_event_count: normalize_non_negative_integer(summary["event_count"] || summary["eventCount"])
334-
}
335-
end
336-
337-
defp store_editor_summary(socket, summary, lang_slug) do
338-
game_id = socket.assigns.game_id
339-
user = socket.assigns.current_user
340-
341-
case Context.store_editor_summary(game_id, user, summary, lang_slug) do
342-
{:ok, _result} ->
343-
{:noreply, socket}
344-
345-
{:error, %Ecto.Changeset{} = changeset} ->
346-
Logger.warning("Failed to persist editor summary: #{inspect(changeset.errors)}")
347-
{:reply, {:error, %{reason: :invalid_editor_summary}}, socket}
348-
349-
{:error, reason} ->
350-
{:reply, {:error, %{reason: reason}}, socket}
351-
end
352-
end
353-
354-
defp normalize_non_negative_integer(value) when is_integer(value) and value >= 0, do: value
355-
356-
defp normalize_non_negative_integer(value) when is_binary(value) do
357-
case Integer.parse(value) do
358-
{integer, ""} when integer >= 0 -> integer
359-
_ -> 0
360-
end
361-
end
362-
363-
defp normalize_non_negative_integer(_value), do: 0
364-
365285
def handle_in("rematch:send_offer", _, socket) do
366286
game_id = socket.assigns.game_id
367287
user = socket.assigns.current_user
@@ -471,6 +391,86 @@ defmodule CodebattleWeb.GameChannel do
471391
{:noreply, socket}
472392
end
473393

394+
defp assign_editor_summary_rate_limit(socket) do
395+
now_ms = System.monotonic_time(:millisecond)
396+
397+
assign(socket, :editor_summary_rate_limit, %{
398+
window_started_at_ms: now_ms,
399+
summary_count: 0,
400+
event_count: 0
401+
})
402+
end
403+
404+
defp allow_editor_summary?(socket, summary_event_count) do
405+
now_ms = System.monotonic_time(:millisecond)
406+
state = socket.assigns[:editor_summary_rate_limit] || %{}
407+
window_started_at_ms = Map.get(state, :window_started_at_ms, now_ms)
408+
409+
state =
410+
if now_ms - window_started_at_ms >= @telemetry_limit_window_ms do
411+
%{
412+
window_started_at_ms: now_ms,
413+
summary_count: 0,
414+
event_count: 0
415+
}
416+
else
417+
state
418+
end
419+
420+
next_summary_count = Map.get(state, :summary_count, 0) + 1
421+
next_event_count = Map.get(state, :event_count, 0) + summary_event_count
422+
423+
if next_summary_count > @max_editor_summaries_per_window ||
424+
next_event_count > @max_editor_summary_events_per_window do
425+
{:error, :editor_summary_rate_limited}
426+
else
427+
{:ok,
428+
assign(socket, :editor_summary_rate_limit, %{
429+
state
430+
| summary_count: next_summary_count,
431+
event_count: next_event_count
432+
})}
433+
end
434+
end
435+
436+
defp parse_editor_summary_payload(payload) do
437+
summary = payload["summary"] || %{}
438+
439+
%{
440+
summary: summary,
441+
lang_slug: payload["lang_slug"] || payload["langSlug"],
442+
summary_event_count: normalize_non_negative_integer(summary["event_count"] || summary["eventCount"])
443+
}
444+
end
445+
446+
defp store_editor_summary(socket, summary, lang_slug) do
447+
game_id = socket.assigns.game_id
448+
user = socket.assigns.current_user
449+
450+
case Context.store_editor_summary(game_id, user, summary, lang_slug) do
451+
{:ok, _result} ->
452+
{:noreply, socket}
453+
454+
{:error, %Ecto.Changeset{} = changeset} ->
455+
Logger.warning("Failed to persist editor summary: #{inspect(changeset.errors)}")
456+
{:reply, {:error, %{reason: :invalid_editor_summary}}, socket}
457+
458+
{:error, reason} ->
459+
{:reply, {:error, %{reason: reason}}, socket}
460+
end
461+
end
462+
463+
defp normalize_non_negative_integer(value) when is_integer(value) and value >= 0, do: value
464+
465+
defp normalize_non_negative_integer(value) when is_binary(value) do
466+
case Integer.parse(value) do
467+
{integer, ""} when integer >= 0 -> integer
468+
_ -> 0
469+
end
470+
end
471+
472+
defp normalize_non_negative_integer(_value), do: 0
473+
474474
def handle_info(%{event: "user:banned", payload: %{player: player}}, socket) do
475475
user_id = socket.assigns.current_user.id
476476

apps/codebattle/priv/gettext/ru/LC_MESSAGES/default.po

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,6 @@ msgstr "Тесты"
322322
msgid "Send"
323323
msgstr "Отправить"
324324

325-
msgid "Notification"
326-
msgstr "Нотификация"
327-
328325
msgid "Run"
329326
msgstr "Запустить"
330327

0 commit comments

Comments
 (0)