-
Notifications
You must be signed in to change notification settings - Fork 1
/
live_handler.ex
157 lines (133 loc) · 4.39 KB
/
live_handler.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
defmodule Bonfire.Search.LiveHandler do
use Bonfire.UI.Common.Web, :live_handler
# TODO: put in config
@default_limit 20
def handle_event("go_search", %{"s" => s} = params, socket) do
# TODO: show results in a modal rather than a seperate page
{:noreply, socket |> redirect_to("/search/?s=" <> s)}
end
def handle_event(
"search",
params,
%{assigns: %{search_limit: search_limit}} = socket
) do
live_search(params["s"], search_limit || @default_limit, params["facet"], socket)
end
def handle_event(
"search",
params,
%{assigns: %{__context__: %{search_limit: search_limit}}} = socket
) do
# debug(socket)
live_search(params["s"], search_limit || @default_limit, params["facet"], socket)
end
def handle_event("search", params, %{assigns: _assigns} = socket) do
live_search(
params["s"],
params["search_limit"] || @default_limit,
params["facet"],
socket
)
end
def live_search(
q,
search_limit \\ @default_limit,
facet_filters \\ nil,
socket
)
def live_search(q, search_limit, facet_filters, socket)
when is_binary(search_limit) and search_limit != "" do
search_limit = String.to_integer(search_limit) || @default_limit
live_search(q, search_limit, facet_filters, socket)
end
def live_search(q, search_limit, facet_filters, socket)
when search_limit == "" or is_nil(search_limit) do
live_search(q, @default_limit, facet_filters, socket)
end
def live_search(q, search_limit, facet_filters, socket)
when is_binary(q) and q != "" and is_integer(search_limit) do
debug(q, "SEARCHING")
debug(facet_filters, "FACET")
q = String.trim(q)
opts = %{limit: search_limit, current_user: current_user(socket)}
# TODO: make this a non-blocking operation? (ie. show the other results first and then inject the result of this lookup when ready)
# FIXME: use maybe_apply
# TODO fetch async and use send_update to send results to ResultsLive?
by_link_or_username =
with {:ok, federated_object_or_character} <-
Bonfire.Federate.ActivityPub.AdapterUtils.get_by_url_ap_id_or_username(
q
# fetch_collection: :async
)
|> debug("got_by_url_ap_id_or_username") do
[federated_object_or_character]
# {:noreply, socket |> redirect_to(path(federated_object_or_character))}
else
_ ->
[]
end
# tagged =
# with hashtags when is_list(hashtags) <-
# Bonfire.Tag.Tags.search_hashtag(
# q
# # fetch_collection: :async
# )
# |> debug("got_hashtags") do
# hashtags
# # {:noreply, socket |> redirect_to(path(federated_object_or_character))}
# else
# _ ->
# []
# end
{num_hits, hits, facets} =
do_search(q, facet_filters, opts)
|> debug("did_search1")
# + length(tagged)
num_hits = (num_hits || 0) + length(by_link_or_username)
# ++ tagged
hits =
(by_link_or_username ++ hits)
|> Enum.uniq_by(&Enums.id/1)
|> debug("search2 merged")
{:noreply,
assign_global(socket,
selected_facets: facet_filters,
hits: hits,
facets: facets || e(socket.assigns, :facets, nil),
num_hits: num_hits,
search: q
# current_user: current_user(socket.assigns)
)}
end
def live_search(q, _search_limit, _facet_filters, socket) do
debug(q, "invalid search")
{:noreply, socket}
end
defp do_search(q, facet_filters, opts) do
facet_filters = facet_filters || %{}
search =
Bonfire.Search.search(q, opts, Map.keys(facet_filters), Map.values(facet_filters))
|> debug("did_search1")
hits =
if(
is_map(search) and Map.has_key?(search, "hits") and
length(search["hits"])
) do
search["hits"]
# return object-like results
|> Enum.map(
&(&1
|> input_to_atoms()
|> maybe_to_structs())
)
else
if is_list(search), do: search
end
# note we only get proper facets when not already faceting
facets =
if !facet_filters and e(search, "facetDistribution", nil) do
e(search, "facetDistribution", nil)
end
{e(search, "nbHits", 0) || length(hits), hits || [], facets}
end
end