-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhambot.ex
37 lines (29 loc) · 936 Bytes
/
hambot.ex
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
defmodule Hambot do
@moduledoc """
Hambot keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
alias Hambot.Slack.Team
alias Hambot.Archive
def collect_urls(payload), do: Enum.uniq(collect_urls(payload, []))
defp collect_urls(%{"type" => "link", "url" => url}, acc) do
[url | acc]
end
defp collect_urls(s, acc) when not is_map(s) and not is_list(s), do: acc
defp collect_urls(payload = %{}, acc) do
Enum.reduce(payload, acc, fn {_k, v}, acc ->
collect_urls(v, acc)
end)
end
defp collect_urls(payload, acc) when is_list(payload) do
Enum.reduce(payload, acc, fn v, acc ->
collect_urls(v, acc)
end)
end
def archive_urls(team = %Team{}, payload) do
urls = collect_urls(payload)
Archive.archive_urls(team, urls)
end
end