aboutsummaryrefslogtreecommitdiff
path: root/shardweb/lib/views/layout_view.ex
blob: 990df553d18416994f24b0247c5c6f5eabbe91d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule ShardWeb.LayoutView do
  use ShardWeb, :view

  def shard_list do
    Shard.Manager.list_shards
  end

  def str_of_pk_list(conn, pk_list) do
    case pk_list
    |> Enum.filter(&(&1 != conn.assigns.pk))
    |> Enum.map(&Base.encode16/1)
    do
      [] -> conn.assigns.pk |> Base.encode16
      l ->  Enum.join(l, ",")
    end
  end

  def nicks_of_pk_list(conn, pk_list) do
    case pk_list
    |> Enum.filter(&(&1 != conn.assigns.pk))
    |> Enum.map(&SApp.Identity.get_nick/1)
    do
      [] -> "(myself)"
      l -> Enum.join(l, ", ")
    end
  end

  def chat_shard_last_msg(pid) do
    [{{_, msgbin, _}, true}] = GenServer.call(pid, {:read_history, nil, 1})
    {_, msg} = SData.term_unbin msgbin
    msg
  end

  def privchat_with_unread(conn) do
    for {id, %SApp.Chat.PrivChat.Manifest{pk_list: pk_list}, pid} <- shard_list(),
      conn.assigns.pk in pk_list,
      unread_time = GenServer.call(pid, :has_unread),
      unread_time != nil,
    do: {id, %SApp.Chat.PrivChat.Manifest{pk_list: pk_list}, pid}
  end

  def chat_with_unread(conn) do
    for {id, %SApp.Chat.Manifest{channel: c}, pid} <- shard_list(),
      unread_time = GenServer.call(pid, :has_unread),
      unread_time != nil,
    do: {id, %SApp.Chat.Manifest{channel: c}, pid}
  end
end