add user import
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Mon, 13 Dec 2021 20:27:35 +0000 (20:27 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Mon, 13 Dec 2021 20:27:35 +0000 (20:27 +0000)
lib/mix/tasks/pleroma/search.ex
lib/pleroma/elasticsearch/document_mappings/activity.ex [moved from lib/pleroma/elasticsearch/document_mappings/note.ex with 100% similarity]
lib/pleroma/elasticsearch/document_mappings/user.ex [new file with mode: 0644]
lib/pleroma/elasticsearch/store.ex
priv/es-mappings/user.json

index 4e8e480d4cfb8bf22765bfdc37c80f58d60bc3e8..64396b92fe792f51e38b1e3494755520e97994da 100644 (file)
@@ -8,21 +8,28 @@ defmodule Mix.Tasks.Pleroma.Search do
   import Ecto.Query
   alias Pleroma.Activity
   alias Pleroma.Pagination
+  alias Pleroma.User
 
   @shortdoc "Manages elasticsearch"
 
-  def run(["import" | _rest]) do
+  def run(["import", "activities" | _rest]) do
     start_pleroma()
 
     from(a in Activity, where: not ilike(a.actor, "%/relay"))
     |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
     |> Activity.with_preloaded_object()
     |> Activity.with_preloaded_user_actor()
-    |> get_all
+    |> get_all(:activities)
   end
 
-  defp get_all(query, max_id \\ nil) do
-    IO.puts(max_id)
+  def run(["import", "users" | _rest]) do
+    start_pleroma()  
+                     
+    from(u in User, where: not ilike(u.ap_id, "%/relay"))
+    |> get_all(:users)
+  end
+
+  defp get_all(query, index, max_id \\ nil) do
     params = %{limit: 2000}
 
     params =
@@ -40,15 +47,9 @@ defmodule Mix.Tasks.Pleroma.Search do
       :ok
     else
       res
-      |> Enum.filter(fn x -> 
-        t = x.object
-       |> Map.get(:data, %{})
-       |> Map.get("type", "")
-       t == "Note"
-      end)
-      |> Pleroma.Elasticsearch.bulk_post(:activities)
-
-      get_all(query, List.last(res).id)
+      |> Pleroma.Elasticsearch.bulk_post(index)
+
+      get_all(query, index, List.last(res).id)
     end
   end
 end
diff --git a/lib/pleroma/elasticsearch/document_mappings/user.ex b/lib/pleroma/elasticsearch/document_mappings/user.ex
new file mode 100644 (file)
index 0000000..5d9945c
--- /dev/null
@@ -0,0 +1,12 @@
+defmodule Pleroma.Elasticsearch.DocumentMappings.User do
+  def id(obj), do: obj.id
+
+  def encode(%{actor_type: "Person"} = user) do
+    %{
+      timestamp: user.inserted_at,
+      instance: URI.parse(user.ap_id).host,
+      nickname: user.nickname,
+      bio: user.bio
+    }
+  end
+end
index f309bf7a5e484ca54652f0d30c8c159123383af9..776bad92133a610675a40b43080562f9d39ecad2 100644 (file)
@@ -44,6 +44,12 @@ defmodule Pleroma.Elasticsearch do
   def bulk_post(data, :activities) do
     d =
       data
+      |> Enum.filter(fn x ->
+        t = x.object
+        |> Map.get(:data, %{})
+        |> Map.get("type", "")
+        t == "Note"
+      end)
       |> Enum.map(fn d ->
         [
           %{index: %{_id: DocumentMappings.Activity.id(d)}},
@@ -60,6 +66,25 @@ defmodule Pleroma.Elasticsearch do
     )
   end
 
+  def bulk_post(data, :users) do
+    d =
+      data
+      |> Enum.map(fn d ->
+        [
+          %{index: %{_id: DocumentMappings.User.id(d)}},
+          DocumentMappings.User.encode(d)
+        ]
+      end)
+      |> List.flatten()
+
+    Elastix.Bulk.post(
+      url(),
+      d,
+      index: "users",
+      type: "user"
+    )
+  end
+
   def search_activities(q) do
     Elastix.Search.search(
       url(),
index 20beb75c2d3a13151d2adc54603a962a771bd38c..77cc66a4b29e50723924a6f57bb63403380967ed 100644 (file)
@@ -1,5 +1,9 @@
 {
   "properties": {
+    "timestamp": {
+      "type": "date",
+      "index": true
+    },
     "instance": {
       "type": "keyword"
     },