save posts in redis

master
cynic 3 years ago
parent cac850d2e0
commit 526e0499ec
  1. 14
      lib/tir_inna_noc/imageboard/post.ex
  2. 10
      lib/tir_inna_noc/meldh.ex
  3. 21
      lib/tir_inna_noc/perenelle.ex

@ -1,3 +1,17 @@
defmodule TirInnaNoc.Imageboard.Post do defmodule TirInnaNoc.Imageboard.Post do
defstruct post: %{}, sage: "u" 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 end

@ -12,8 +12,7 @@ defmodule TirInnaNoc.Meldh do
end end
@impl true @impl true
def init(state) do def handle_info(:update, state) do
IO.puts("started meldh to archive board "<>state.board)
{:ok, res} = TirInnaNoc.Imageboard.threads(state.board) {:ok, res} = TirInnaNoc.Imageboard.threads(state.board)
res.body res.body
|> Enum.each(fn page -> |> Enum.each(fn page ->
@ -33,6 +32,13 @@ defmodule TirInnaNoc.Meldh do
) )
end) end)
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} {:ok, state}
end end
end end

@ -9,13 +9,14 @@ defmodule TirInnaNoc.Perenelle do
def init(state) do def init(state) do
IO.puts("watching thread "<>inspect(state)) IO.puts("watching thread "<>inspect(state))
send(String.to_atom(state.board<>"Meldh"), {:checkin, state.no, self()}) 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} {:ok, state}
end end
@impl true @impl true
def handle_info({:addpost, post, sage_status}, state) do 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} 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} {:noreply, state}
end end
@ -26,14 +27,16 @@ defmodule TirInnaNoc.Perenelle do
end end
@impl true @impl true
def handle_info({:update, new_page, new_replynum}, state) do def handle_info({:update, new_page, new_replynum, first_run}, state) do
{:ok, res} = TirInnaNoc.Imageboard.thread(state.board, state.no) if (new_replynum > state.reply_number) or first_run do
if res.status == 200 do {:ok, res} = TirInnaNoc.Imageboard.thread(state.board, state.no)
res.body["posts"] if res.status == 200 do
|> Enum.each(fn post -> res.body["posts"]
sage_status = if (new_page>state.on_page and new_replynum == state.reply_number+1), do: "y", else: "n" |> Enum.each(fn post ->
send(self(), {:addpost, post, sage_status}) sage_status = if (new_page>state.on_page and new_replynum == state.reply_number+1), do: "y", else: "n"
end) send(self(), {:addpost, post, sage_status})
end)
end
end end
state = %TirInnaNoc.Imageboard.Thread{state | on_page: new_page, reply_number: new_replynum} state = %TirInnaNoc.Imageboard.Thread{state | on_page: new_page, reply_number: new_replynum}
send(self(), :save) send(self(), :save)

Loading…
Cancel
Save