summaryrefslogtreecommitdiff
path: root/lib/nola/membership.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nola/membership.ex')
-rw-r--r--lib/nola/membership.ex43
1 files changed, 26 insertions, 17 deletions
diff --git a/lib/nola/membership.ex b/lib/nola/membership.ex
index 1c7303b..182f44d 100644
--- a/lib/nola/membership.ex
+++ b/lib/nola/membership.ex
@@ -7,11 +7,11 @@ defmodule Nola.Membership do
# Format: {key, last_seen}
defp dets() do
- to_charlist(Nola.data_path <> "/memberships.dets")
+ to_charlist(Nola.data_path() <> "/memberships.dets")
end
def start_link() do
- GenServer.start_link(__MODULE__, [], [name: __MODULE__])
+ GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
@@ -25,9 +25,10 @@ defmodule Nola.Membership do
end
def merge_account(old_id, new_id) do
- #iex(37)> :ets.fun2ms(fn({{old_id, _, _}, _}=obj) when old_id == "42" -> obj end)
+ # iex(37)> :ets.fun2ms(fn({{old_id, _, _}, _}=obj) when old_id == "42" -> obj end)
spec = [{{{:"$1", :_, :_}, :_}, [{:==, :"$1", {:const, old_id}}], [:"$_"]}]
- Util.ets_mutate_select_each(:dets, dets(), spec, fn(table, obj = {{_old, net, chan}, ts}) ->
+
+ Util.ets_mutate_select_each(:dets, dets(), spec, fn table, obj = {{_old, net, chan}, ts} ->
:dets.delete_object(table, obj)
:dets.insert(table, {{new_id, net, chan}, ts})
end)
@@ -36,6 +37,7 @@ defmodule Nola.Membership do
def touch(%Nola.Account{id: id}, network, channel) do
:dets.insert(dets(), {{id, network, channel}, NaiveDateTime.utc_now()})
end
+
def touch(account_id, network, channel) do
if account = Nola.Account.get(account_id) do
touch(account, network, channel)
@@ -43,21 +45,28 @@ defmodule Nola.Membership do
end
def notify_channels(account, minutes \\ 30, last_active \\ true) do
- not_before = NaiveDateTime.add(NaiveDateTime.utc_now(), (minutes*-60), :second)
+ not_before = NaiveDateTime.add(NaiveDateTime.utc_now(), minutes * -60, :second)
spec = [{{{:"$1", :_, :_}, :_}, [{:==, :"$1", {:const, account.id}}], [:"$_"]}]
- memberships = :dets.select(dets(), spec)
- |> Enum.sort_by(fn({_, ts}) -> ts end, {:desc, NaiveDateTime})
- active_memberships = Enum.filter(memberships, fn({_, ts}) -> NaiveDateTime.compare(ts, not_before) == :gt end)
+
+ memberships =
+ :dets.select(dets(), spec)
+ |> Enum.sort_by(fn {_, ts} -> ts end, {:desc, NaiveDateTime})
+
+ active_memberships =
+ Enum.filter(memberships, fn {_, ts} -> NaiveDateTime.compare(ts, not_before) == :gt end)
+
cond do
active_memberships == [] && last_active ->
case memberships do
- [{{_, net, chan}, _}|_] -> [{net, chan}]
+ [{{_, net, chan}, _} | _] -> [{net, chan}]
_ -> []
end
+
active_memberships == [] ->
[]
+
true ->
- Enum.map(active_memberships, fn({{_, net, chan}, _}) -> {net,chan} end)
+ Enum.map(active_memberships, fn {{_, net, chan}, _} -> {net, chan} end)
end
end
@@ -78,16 +87,18 @@ defmodule Nola.Membership do
end
def members(network, channel) do
- #iex(19)> :ets.fun2ms(fn({{id, net, chan}, ts}) when net == network and chan == channel and ts > min_seen -> id end)
- limit = 0 # NaiveDateTime.add(NaiveDateTime.utc_now, 30*((24*-60)*60), :second)
+ # iex(19)> :ets.fun2ms(fn({{id, net, chan}, ts}) when net == network and chan == channel and ts > min_seen -> id end)
+ # NaiveDateTime.add(NaiveDateTime.utc_now, 30*((24*-60)*60), :second)
+ limit = 0
+
spec = [
{{{:"$1", :"$2", :"$3"}, :"$4"},
[
- {:andalso,
- {:andalso, {:==, :"$2", {:const, network}}, {:==, :"$3", {:const, channel}}},
+ {:andalso, {:andalso, {:==, :"$2", {:const, network}}, {:==, :"$3", {:const, channel}}},
{:>, :"$4", {:const, limit}}}
], [:"$1"]}
]
+
:dets.select(dets(), spec)
end
@@ -122,8 +133,6 @@ defmodule Nola.Membership do
{account, user, nick}
end
end
- |> Enum.filter(fn(x) -> x end)
+ |> Enum.filter(fn x -> x end)
end
-
-
end