Browse Source

fix(webmention): Make it line up.

jackyalcine 8 months ago
parent
commit
b5833ded09
Signed by: Jacky Alciné <yo@jacky.wtf> GPG Key ID: 537A4F904B15268D
8 changed files with 611 additions and 365 deletions
  1. 3
    1
      lib/indieweb/mf2/remote.ex
  2. 3
    2
      lib/repo.ex
  3. 9
    14
      lib/webmention.ex
  4. 587
    324
      package-lock.json
  5. 1
    1
      package.json
  6. 4
    4
      test/support/factory.ex
  7. 2
    17
      test/unit/webmention_test.exs
  8. 2
    2
      web/static/fonts.css

+ 3
- 1
lib/indieweb/mf2/remote.ex View File

@@ -29,7 +29,9 @@ defmodule IndieWeb.MF2.Remote do
29 29
 
30 30
   defp do_normalize(json), do: Jason.decode(json, keys: :strings, strings: :copy)
31 31
   defp do_serialize(mf2), do: Jason.encode_to_iodata(mf2)
32
-  defp key_func(uri), do: :crypto.hash(:sha256, uri) |> Base.encode16()
32
+
33
+  defp key_func(uri),
34
+    do: :crypto.hash(:sha256, uri) |> Base.encode64(case: :lower, padding: false) |> String.replace_prefix("", "mf2:")
33 35
 
34 36
   defp fetch_mf2(uri) do
35 37
     Logger.debug(fn -> "Attempting to fetch #{uri}..." end)

+ 3
- 2
lib/repo.ex View File

@@ -119,12 +119,13 @@ defmodule Koype.Repo do
119 119
 
120 120
   def upsert(model, attrs) when is_map(attrs) do
121 121
     params = attrs |> Map.to_list() |> Keyword.new()
122
+    Apex.ap(["apex", model, attrs])
122 123
     upsert(model, params)
123 124
   end
124 125
 
125
-  def upsert(model, attrs) when is_list(attrs) do
126
+  def upsert(model, attrs) do
126 127
     if Keyword.get(attrs, :id, nil) == nil do
127
-      model.create(attrs |> Keyword.to_list() |> Map.new())
128
+      model.create(attrs)
128 129
     else
129 130
       model.update(Koype.Repo.get(model, attrs[:id]), attrs)
130 131
     end

+ 9
- 14
lib/webmention.ex View File

@@ -163,32 +163,27 @@ defmodule Koype.Webmention do
163 163
     target_url = args[:target_url]
164 164
     source = args[:source]
165 165
 
166
+    id =
167
+      case Koype.Repo.get_by(Koype.Repo.Webmention, source: source) do
168
+        nil -> nil
169
+        %{id: id} -> id
170
+      end
171
+
166 172
     with(
167 173
       {:ok, mf2} when is_map(mf2) <- IndieWeb.MF2.Remote.fetch(source),
168 174
       entry_mf2 when is_map(entry_mf2) <- IndieWeb.MF2.get_format(mf2, "entry")
169 175
     ) do
170 176
       types = IndieWeb.Post.extract_types(entry_mf2["properties"])
171 177
       type = IndieWeb.Post.determine_type(entry_mf2["properties"], types)
178
+      params = [id: id, source: source, target: target_url, type: Atom.to_string(type), mf2: entry_mf2]
172 179
 
173 180
       Logger.info(fn ->
174 181
         "Detected #{source} to have #{inspect(types)} types and be a #{type} at its base."
175 182
       end)
176 183
 
177
-      Logger.info("Saving Webmention from #{source} to #{target_url}.")
178
-
179
-      id =
180
-        case Koype.Repo.get_by(Koype.Repo.Webmention, source: source) do
181
-          nil -> nil
182
-          webmention -> webmention.id
183
-        end
184
+      Logger.info("Saving Webmention from #{source} to #{target_url}...")
184 185
 
185
-      case Koype.Repo.upsert(Koype.Repo.Webmention,
186
-             id: id,
187
-             source: source,
188
-             target: target_url,
189
-             type: Atom.to_string(type),
190
-             mf2: entry_mf2
191
-           ) do
186
+      case Koype.Repo.upsert(Koype.Repo.Webmention, params) do
192 187
         {:ok, _} = result ->
193 188
           result
194 189
 

+ 587
- 324
package-lock.json
File diff suppressed because it is too large
View File


+ 1
- 1
package.json View File

@@ -37,7 +37,7 @@
37 37
     "typeface-open-sans": "0.0.54",
38 38
     "typeface-source-code-pro": "0.0.71",
39 39
     "voca": "1.4.0",
40
-    "webfontloader": "^1.6.28"
40
+    "webfontloader": "1.6.28"
41 41
   },
42 42
   "engines": {
43 43
     "node": "~> 10.14.0"

+ 4
- 4
test/support/factory.ex View File

@@ -17,14 +17,14 @@ defmodule Koype.Factory do
17 17
     }
18 18
   end
19 19
 
20
-  def webmention_factory(entry \\ insert(:entry)) do
20
+  def webmention_factory do
21 21
     author_domain = Faker.Internet.url()
22 22
 
23 23
     %Webmention{
24
+      author: author_domain,
24 25
       source: author_domain <> "/" <> Faker.Lorem.word(),
25
-      target: URI.parse(Entry.get_uri(entry)).path,
26
-      type: Enum.random(IndieWeb.Post.response_types()) |> Atom.to_string(),
27
-      author: author_domain
26
+      target: "/post/" <> Faker.Lorem.word(),
27
+      type: IndieWeb.Post.response_types() |> Enum.random() |> Atom.to_string()
28 28
     }
29 29
   end
30 30
 

+ 2
- 17
test/unit/webmention_test.exs View File

@@ -79,12 +79,12 @@ defmodule Koype.WebmentionTest do
79 79
       webmention = insert(:webmention, source: source_uri, target: URI.parse(target_uri).path, type: "like")
80 80
 
81 81
       html =
82
-        "<html><body><div class='h-entry'><a href='/' class='h-card u-url'><span class='p-name'>Hello</span></a></div></body></html>"
82
+        "<html><body><div class='h-entry'><a href='/' class='u-in-reply-to'><span class='p-name'>Hello</span></a></div></body></html>"
83 83
 
84 84
       use_cassette :stub, url: source_uri, body: html, status_code: 200 do
85 85
         assert {:ok, updated_webmention} = Subject.receive!(source: source_uri, target: target_uri)
86
+        assert updated_webmention.type == "reply"
86 87
         assert updated_webmention.id == webmention.id
87
-        assert updated_webmention.type == "article"
88 88
       end
89 89
     end
90 90
 
@@ -108,21 +108,6 @@ defmodule Koype.WebmentionTest do
108 108
                Subject.receive!(source: source_uri, target: target_uri)
109 109
     end
110 110
 
111
-    test "fails when database fails out" do
112
-      model = insert(:entry)
113
-      source_uri = Faker.Internet.url()
114
-      target_uri = Koype.Repo.Entry.get_uri(model)
115
-
116
-      use_cassette :stub, url: source_uri, status_code: 200 do
117
-        with_mocks([
118
-          {Koype.Repo.Webmention, [], create: fn _ -> {:error, :test_error} end},
119
-          {IndieWeb.MF2.Remote, [], fetch: fn _ -> {:error, :test_error} end, flush: fn _ -> :ok end}
120
-        ]) do
121
-          assert {:error, :test_error} = Subject.receive!(source: source_uri, target: target_uri)
122
-        end
123
-      end
124
-    end
125
-
126 111
     test "fails when storage fails out" do
127 112
       model = insert(:entry)
128 113
       source_uri = Faker.Internet.url()

+ 2
- 2
web/static/fonts.css View File

@@ -1,3 +1,3 @@
1 1
 @import "typeface-open-sans";
2
-/* @import "typeface-crimson-text"; */
3
-/* @import "typeface-source-code-pro"; */
2
+@import "typeface-crimson-text";
3
+@import "typeface-source-code-pro";

Loading…
Cancel
Save