save threads in redis

master
cynic 3 years ago
parent d352666d96
commit cac850d2e0
  1. 2
      lib/tir_inna_noc/imageboard/post.ex
  2. 18
      lib/tir_inna_noc/imageboard/thread.ex
  3. 2
      lib/tir_inna_noc/meldh.ex
  4. 29
      lib/tir_inna_noc/perenelle.ex
  5. 2
      readme.txt

@ -1,3 +1,3 @@
defmodule TirInnaNoc.Imageboard.Post do defmodule TirInnaNoc.Imageboard.Post do
defstruct postJson: "", sage: "u" defstruct post: %{}, sage: "u"
end end

@ -1,3 +1,19 @@
defmodule TirInnaNoc.Imageboard.Thread do defmodule TirInnaNoc.Imageboard.Thread do
defstruct board: "", no: 0, last_update: 0, on_page: 0, reply_number: 0, posts: "" defstruct board: "", no: 0, last_update: 0, on_page: 0, reply_number: 0, posts: []
def save(t) do
GenServer.call(
TirInnaNoc.Db,
{
:cmd,
[
"HSET",
"thread/"<>t.board<>"/"<>to_string(t.no),
"last_update", t.last_update,
"on_page", t.on_page,
"reply_number", t.reply_number,
"posts", t.posts|>Enum.map(fn p -> to_string(p.post["no"]) end)|>Enum.join(",")
]
}
)
end
end end

@ -27,7 +27,7 @@ defmodule TirInnaNoc.Meldh do
no: thread["no"], no: thread["no"],
last_update: thread["last_modified"], last_update: thread["last_modified"],
reply_number: thread["replies"], reply_number: thread["replies"],
on_page: page on_page: page["page"]
} }
} }
) )

@ -9,7 +9,34 @@ 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()})
TirInnaNoc.Imageboard.thread(state.board, state.no) |> inspect |> IO.puts send(self(), {:update, state.on_page, state.reply_number})
{:ok, state} {:ok, state}
end 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}
{:noreply, state}
end
@impl true
def handle_info(:save, state) do
TirInnaNoc.Imageboard.Thread.save(state)
{:noreply, state}
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)
end
state = %TirInnaNoc.Imageboard.Thread{state | on_page: new_page, reply_number: new_replynum}
send(self(), :save)
{:noreply, state}
end
end end

@ -1,6 +1,6 @@
hash post/<board>/<id> -> hash post/<board>/<id> ->
string postjson string postjson
string sage //y/n/m/u string sage //y/n
hash thread/<board>/<id> -> hash thread/<board>/<id> ->
string last_update string last_update

Loading…
Cancel
Save