From 526e0499ec0f498136b0f25fa394dbd4862e16fd Mon Sep 17 00:00:00 2001 From: cynic Date: Thu, 25 Aug 2022 23:17:10 -0400 Subject: [PATCH] save posts in redis --- lib/tir_inna_noc/imageboard/post.ex | 14 ++++++++++++++ lib/tir_inna_noc/meldh.ex | 10 ++++++++-- lib/tir_inna_noc/perenelle.ex | 21 ++++++++++++--------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/tir_inna_noc/imageboard/post.ex b/lib/tir_inna_noc/imageboard/post.ex index 6ddeaa2..0b381a7 100644 --- a/lib/tir_inna_noc/imageboard/post.ex +++ b/lib/tir_inna_noc/imageboard/post.ex @@ -1,3 +1,17 @@ defmodule TirInnaNoc.Imageboard.Post do defstruct post: %{}, sage: "u" + def save(t, board) do + GenServer.call( + TirInnaNoc.Db, + { + :cmd, + [ + "HSET", + "post/"<>board<>"/"<>to_string(t.post["no"]), + "postjson", Jason.encode!(t.post), + "sage", t.sage + ] + } + ) + end end diff --git a/lib/tir_inna_noc/meldh.ex b/lib/tir_inna_noc/meldh.ex index cebfae1..c6d7edd 100644 --- a/lib/tir_inna_noc/meldh.ex +++ b/lib/tir_inna_noc/meldh.ex @@ -12,8 +12,7 @@ defmodule TirInnaNoc.Meldh do end @impl true - def init(state) do - IO.puts("started meldh to archive board "<>state.board) + def handle_info(:update, state) do {:ok, res} = TirInnaNoc.Imageboard.threads(state.board) res.body |> Enum.each(fn page -> @@ -33,6 +32,13 @@ defmodule TirInnaNoc.Meldh do ) end) end) + {:noreply, state} + end + + @impl true + def init(state) do + IO.puts("started meldh to archive board "<>state.board) + send(self(), :update) {:ok, state} end end diff --git a/lib/tir_inna_noc/perenelle.ex b/lib/tir_inna_noc/perenelle.ex index bc1da83..7816096 100644 --- a/lib/tir_inna_noc/perenelle.ex +++ b/lib/tir_inna_noc/perenelle.ex @@ -9,13 +9,14 @@ defmodule TirInnaNoc.Perenelle do def init(state) do IO.puts("watching thread "<>inspect(state)) send(String.to_atom(state.board<>"Meldh"), {:checkin, state.no, self()}) - send(self(), {:update, state.on_page, state.reply_number}) + send(self(), {:update, state.on_page, state.reply_number, true}) {:ok, state} end @impl true def handle_info({:addpost, post, sage_status}, state) do state = %TirInnaNoc.Imageboard.Thread{state | posts: [%TirInnaNoc.Imageboard.Post{post: post, sage: sage_status}]++state.posts} + TirInnaNoc.Imageboard.Post.save(%TirInnaNoc.Imageboard.Post{post: post, sage: sage_status}, state.board) {:noreply, state} end @@ -26,14 +27,16 @@ defmodule TirInnaNoc.Perenelle do end @impl true - def handle_info({:update, new_page, new_replynum}, state) do - {:ok, res} = TirInnaNoc.Imageboard.thread(state.board, state.no) - if res.status == 200 do - res.body["posts"] - |> Enum.each(fn post -> - sage_status = if (new_page>state.on_page and new_replynum == state.reply_number+1), do: "y", else: "n" - send(self(), {:addpost, post, sage_status}) - end) + def handle_info({:update, new_page, new_replynum, first_run}, state) do + if (new_replynum > state.reply_number) or first_run do + {:ok, res} = TirInnaNoc.Imageboard.thread(state.board, state.no) + if res.status == 200 do + res.body["posts"] + |> Enum.each(fn post -> + sage_status = if (new_page>state.on_page and new_replynum == state.reply_number+1), do: "y", else: "n" + send(self(), {:addpost, post, sage_status}) + end) + end end state = %TirInnaNoc.Imageboard.Thread{state | on_page: new_page, reply_number: new_replynum} send(self(), :save)