aboutsummaryrefslogtreecommitdiff
path: root/shardweb/lib/views/layout_view.ex
blob: 40863a17fe0925f69231c45a7ea3d2600ea53047 (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
49
50
51
defmodule ShardWeb.LayoutView do
  use ShardWeb, :view

  def shard_list do
    Shard.Manager.list_shards
    |> Enum.map(fn {id, manifest, _} -> {id, manifest, Shard.Manager.find_or_start manifest} end)
  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}] = SApp.Chat.read_history(pid, 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}, _} <- shard_list(),
      pid = Shard.Manager.find_or_start(%SApp.Chat.PrivChat.Manifest{pk_list: pk_list}),
      conn.assigns.pk in pk_list,
      unread_time = SApp.Chat.has_unread?(pid),
      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}, _} <- shard_list(),
      pid = Shard.Manager.find_or_start(%SApp.Chat.Manifest{channel: c}),
      unread_time = SApp.Chat.has_unread?(pid),
      unread_time != nil,
    do: {id, %SApp.Chat.Manifest{channel: c}, pid}
  end
end