mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 09:32:51 +00:00
server - update view count on threads
This commit is contained in:
@@ -44,7 +44,7 @@ defmodule MyApp.Data.Reply do
|
|||||||
thread_id = Map.get(reply, :thread_id)
|
thread_id = Map.get(reply, :thread_id)
|
||||||
user_id = Map.get(reply, :user_id)
|
user_id = Map.get(reply, :user_id)
|
||||||
query = from t in Data.Thread, where: t.id == ^thread_id,
|
query = from t in Data.Thread, where: t.id == ^thread_id,
|
||||||
update: [set: [last_reply_id: ^user_id], inc: [reply_count: 1]]
|
update: [set: [last_reply_id: ^user_id, updated_at: ^DateTime.utc_now], inc: [reply_count: 1]]
|
||||||
|
|
||||||
case Repo.update_all(query, []) do
|
case Repo.update_all(query, []) do
|
||||||
nil -> {:error, "update thread error"}
|
nil -> {:error, "update thread error"}
|
||||||
|
|||||||
@@ -110,6 +110,14 @@ defmodule MyApp.Data.Thread do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# this doesn't update the 'updated_at' field which is what we want
|
||||||
|
def update_view_count(thread_id) do
|
||||||
|
query = from t in Data.Thread,
|
||||||
|
update: [inc: [view_count: 1]],
|
||||||
|
where: t.id == ^thread_id
|
||||||
|
Repo.update_all(query, [])
|
||||||
|
end
|
||||||
|
|
||||||
# TODO: delete thread
|
# TODO: delete thread
|
||||||
|
|
||||||
defp process_user_update(thread, _params) when is_nil(thread), do: {:error, "Invalid thread"}
|
defp process_user_update(thread, _params) when is_nil(thread), do: {:error, "Invalid thread"}
|
||||||
|
|||||||
20
lib/myapp/view_counter.ex
Normal file
20
lib/myapp/view_counter.ex
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
defmodule MyApp.ViewCounter do
|
||||||
|
alias MyApp.Data
|
||||||
|
|
||||||
|
# cache end points for 5 minutes based on the browser fingerprint
|
||||||
|
|
||||||
|
@spec thread_view_count(String.t, integer) :: {:error, String.t} | {any, boolean}
|
||||||
|
def thread_view_count(fingerprint, thread_id) do
|
||||||
|
|
||||||
|
key = "#{fingerprint}:#{thread_id}"
|
||||||
|
|
||||||
|
case Cachex.get(:myapp, key) do
|
||||||
|
{:ok, _} -> {:error, "Already viewed."}
|
||||||
|
{:missing, _} ->
|
||||||
|
{1, nil} = Data.Thread.update_view_count(thread_id)
|
||||||
|
Cachex.set(:myapp, key, true, ttl: :timer.minutes(5))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -50,7 +50,14 @@ defmodule MyAppWeb.ThreadController do
|
|||||||
@spec get(map, map) :: any
|
@spec get(map, map) :: any
|
||||||
def get(conn, params) do
|
def get(conn, params) do
|
||||||
|
|
||||||
{output, status} = params["id"]
|
fingerprint = get_req_header(conn, "fp")
|
||||||
|
thread_id = params["id"]
|
||||||
|
|
||||||
|
spawn fn ->
|
||||||
|
MyApp.ViewCounter.thread_view_count(fingerprint, thread_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
{output, status} = thread_id
|
||||||
|> Data.Thread.get
|
|> Data.Thread.get
|
||||||
|> Response.put_resp
|
|> Response.put_resp
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user