diff --git a/lib/tir_inna_noc/imageboard/post.ex b/lib/tir_inna_noc/imageboard/post.ex index 0068822..6ddeaa2 100644 --- a/lib/tir_inna_noc/imageboard/post.ex +++ b/lib/tir_inna_noc/imageboard/post.ex @@ -1,3 +1,3 @@ defmodule TirInnaNoc.Imageboard.Post do - defstruct postJson: "", sage: "u" + defstruct post: %{}, sage: "u" end diff --git a/lib/tir_inna_noc/imageboard/thread.ex b/lib/tir_inna_noc/imageboard/thread.ex index eab996a..38b4be1 100644 --- a/lib/tir_inna_noc/imageboard/thread.ex +++ b/lib/tir_inna_noc/imageboard/thread.ex @@ -1,3 +1,19 @@ 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 diff --git a/lib/tir_inna_noc/meldh.ex b/lib/tir_inna_noc/meldh.ex index 52925a4..cebfae1 100644 --- a/lib/tir_inna_noc/meldh.ex +++ b/lib/tir_inna_noc/meldh.ex @@ -27,7 +27,7 @@ defmodule TirInnaNoc.Meldh do no: thread["no"], last_update: thread["last_modified"], reply_number: thread["replies"], - on_page: page + on_page: page["page"] } } ) diff --git a/lib/tir_inna_noc/perenelle.ex b/lib/tir_inna_noc/perenelle.ex index 638fe9e..bc1da83 100644 --- a/lib/tir_inna_noc/perenelle.ex +++ b/lib/tir_inna_noc/perenelle.ex @@ -9,7 +9,34 @@ 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()}) - TirInnaNoc.Imageboard.thread(state.board, state.no) |> inspect |> IO.puts + send(self(), {:update, state.on_page, state.reply_number}) {: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} + {: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 diff --git a/readme.txt b/readme.txt index 3e01377..a9918cb 100644 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,6 @@ hash post// -> string postjson - string sage //y/n/m/u + string sage //y/n hash thread// -> string last_update