Browse Source

Move to new JSON layout.

jackyalcine 6 months ago
parent
commit
1ac35f6160
Signed by: Jacky Alciné <yo@jacky.wtf> GPG Key ID: 537A4F904B15268D

+ 3
- 3
.lvimrc View File

@@ -25,7 +25,7 @@ endfunction
25 25
 let g:test#custom_transformations = {'docker-compose': function('DockerComposeTransform')}
26 26
 let g:test#transformation = 'docker-compose'
27 27
 let g:test#elixir#exunit#options = {
28
-      \ 'file': '--trace --seed=0 --max-cases=3 --slowest=5 --include acceptance:true',
29
-      \ 'suite': '--trace --max-cases=10 --slowest=10 --include acceptance:true',
30
-      \ 'nearest': '--trace --include skip:true --include acceptance:true'
28
+      \ 'file': '--seed=0 --max-cases=3 --slowest=5 --include acceptance:true',
29
+      \ 'suite': '--slowest=10 --include acceptance:true',
30
+      \ 'nearest': '--include skip:true --include acceptance:true'
31 31
       \ }

+ 1
- 1
config/test.exs View File

@@ -30,4 +30,4 @@ config :hound,
30 30
   host: "webdriver",
31 31
   port: 4444
32 32
 
33
-config :logger, level: :error
33
+config :logger, level: :warn

+ 20
- 0
elixir_buildpack.config View File

@@ -0,0 +1,20 @@
1
+# Erlang version
2
+erlang_version=20.1.0
3
+
4
+# Elixir version
5
+elixir_version=1.7.4
6
+
7
+# Always rebuild from scratch on every deploy?
8
+always_rebuild=false
9
+
10
+# A command to run right before fetching dependencies
11
+hook_pre_fetch_dependencies="npm install"
12
+
13
+# A command to run right before compiling the app (after elixir, .etc)
14
+hook_pre_compile="npm run build"
15
+
16
+# A command to run right after compiling the app
17
+hook_post_compile="docker/scripts/pre-deploy.sh"
18
+
19
+# Set the path the app is run from
20
+runtime_path=/app

+ 2
- 2
lib/feed/atom.ex View File

@@ -55,7 +55,7 @@ defmodule Koype.Feed.Atom do
55 55
   end
56 56
 
57 57
   defp do_wrap_item_into_atom_entry(%Koype.Repo.Entry{} = item) do
58
-    with({:ok, item_json} <- Koype.Repo.Entry.Json.find(item)) do
58
+    with({:ok, item_json} <- Koype.Repo.Entry.json_find(item)) do
59 59
       Entry.new(Koype.Repo.Entry.get_uri(item), item.published_at, item.name)
60 60
       |> Entry.content(item_json["content"]["html"], type: "html")
61 61
       |> Entry.content(item_json["content"]["plain"], type: "text")
@@ -78,7 +78,7 @@ defmodule Koype.Feed.Atom do
78 78
   defp do_wrap_item_into_atom_entry(%Koype.Repo.Webmention{} = item) do
79 79
     if item.author != Koype.host() do
80 80
       with(
81
-        {:ok, webmention_json} <- Koype.Repo.Webmention.Json.find(item),
81
+        {:ok, webmention_json} <- Koype.Repo.Webmention.json_find(item),
82 82
         {:ok, entry} <- IndieWeb.Webmention.resolve_target_from_url(item.target)
83 83
       ) do
84 84
         content =

+ 2
- 2
lib/feed/json_feed.ex View File

@@ -111,7 +111,7 @@ defmodule Koype.Feed.Json do
111 111
   # TODO: Support attachment data if it's video and/or audio.
112 112
   # TODO: Support Webmention as an item.
113 113
   def do_wrap_item_into_json(%Koype.Repo.Entry{} = item) do
114
-    with({:ok, item_json} <- Koype.Repo.Entry.Json.find(item)) do
114
+    with({:ok, item_json} <- Koype.Repo.Entry.json_find(item)) do
115 115
       %{
116 116
         "id" => Koype.Repo.Entry.get_uri(item),
117 117
         "url" => Koype.Repo.Entry.get_uri(item),
@@ -146,7 +146,7 @@ defmodule Koype.Feed.Json do
146 146
   def do_wrap_item_into_json(%Koype.Repo.Webmention{} = item) do
147 147
     if item.author != Koype.host() do
148 148
       with(
149
-        {:ok, webmention_json} <- Koype.Repo.Webmention.Json.find(item),
149
+        {:ok, webmention_json} <- Koype.Repo.Webmention.json_find(item),
150 150
         {:ok, entry} <- IndieWeb.Webmention.resolve_target_from_url(item.target)
151 151
       ) do
152 152
         content =

+ 3
- 3
lib/indieweb/micropub/entry.ex View File

@@ -30,7 +30,7 @@ defmodule IndieWeb.Micropub.Entry do
30 30
   require Logger
31 31
 
32 32
   def send_webmentions(%Model{} = model) do
33
-    case Model.Json.find(model) do
33
+    case Model.json_find(model) do
34 34
       {:ok, json} ->
35 35
         entry_uri = Koype.Repo.Entry.get_uri(model)
36 36
         links = Koype.Post.extract_uris(model.type, json) |> List.flatten()
@@ -366,7 +366,7 @@ defmodule IndieWeb.Micropub.Entry do
366 366
         with(
367 367
           {:ok, refreshed_model} <-
368 368
             do_enact_update_action("delete", new_model, new_properties, %{key => current_value}),
369
-          {:ok, refreshed_properties} <- Model.Json.find(refreshed_model)
369
+          {:ok, refreshed_properties} <- Model.json_find(refreshed_model)
370 370
         ) do
371 371
           {:cont, {:ok, refreshed_model, refreshed_properties}}
372 372
         else
@@ -391,7 +391,7 @@ defmodule IndieWeb.Micropub.Entry do
391 391
 
392 392
   # TODO: Handle rollback cleanly if tx fails
393 393
   defp do_update_entry(model, scope: scope, params: params) do
394
-    case Model.Json.find(model) do
394
+    case Model.json_find(model) do
395 395
       {:ok, properties} ->
396 396
         actionable_data = Micropub.actionable(params)
397 397
 

+ 1
- 1
lib/indieweb/micropub/query.ex View File

@@ -97,7 +97,7 @@ defmodule IndieWeb.Micropub.Query do
97 97
   end
98 98
 
99 99
   defp do_fetch_source_of_entry(%Koype.Repo.Entry{} = model, queried_properties) do
100
-    case Model.Json.find(model) do
100
+    case Model.json_find(model) do
101 101
       {:ok, json} when is_map(json) ->
102 102
         properties =
103 103
           json

+ 13
- 3
lib/repo/entry.ex View File

@@ -24,7 +24,17 @@ defmodule Koype.Repo.Entry do
24 24
     route_token: "post",
25 25
     lookup_columns: ~w(id slug)a
26 26
 
27
-  alias Koype.Storage.Json
27
+  use Koype.Repo.Json,
28
+    version: "2019.04.21",
29
+  type: "entry",
30
+  transform_fn: fn args ->
31
+    record = Map.get(args, :record)
32
+    case Koype.Storage.upload_properties_inline(Map.delete(args, :record), record) do
33
+      {:ok, props} -> props
34
+      _ = error -> error
35
+    end
36
+  end
37
+
28 38
   alias Koype.Repo.Category
29 39
 
30 40
   require Logger
@@ -81,7 +91,7 @@ defmodule Koype.Repo.Entry do
81 91
       {:ok, record} <- do_update_field(:name, record, data),
82 92
       {:ok, record} <- do_update_field(:slug, record, data),
83 93
       {:ok, record} <- do_update_field(:published_at, record, data),
84
-      {:ok, _} <- Json.persist(record, data)
94
+      {:ok, _} <- json_persist(record, data)
85 95
     ) do
86 96
       {:ok, record}
87 97
     else
@@ -97,7 +107,7 @@ defmodule Koype.Repo.Entry do
97 107
       {:ok, record} <- Koype.Repo.update(cs),
98 108
       {:ok, record} <- do_update_field(:name, record, properties),
99 109
       {:ok, record} <- do_update_field(:slug, record, properties),
100
-      {:ok, _path} <- Koype.Storage.Json.persist(record, properties)
110
+      {:ok, _path} <- json_update(record, properties)
101 111
     ) do
102 112
       {:ok, record}
103 113
     else

+ 0
- 38
lib/repo/entry/json.ex View File

@@ -1,38 +0,0 @@
1
-defmodule Koype.Repo.Entry.Json do
2
-  @moduledoc "Represents structured data for an entry."
3
-  @enforce_keys [:data]
4
-  @type t :: %__MODULE__{version: Version.t(), properties: map(), publisher: map()}
5
-
6
-  require Logger
7
-
8
-  defstruct ~w(version properties publisher)a
9
-
10
-  @doc "Obtains the JSON data associated for this entry."
11
-  @spec find(model :: %Koype.Repo.Entry{}) :: {:ok, map()} | {:error, any()}
12
-  def find(model), do: Koype.Storage.Json.find(model)
13
-
14
-  @doc """
15
-  Stores the structured information of the ref'd Entry to object storage.
16
-
17
-  FIXME: Use expanded storage format below.
18
-  {
19
-    "version": "2018.12.21",
20
-    "properties": {},
21
-    "publisher": { // Proxy to IndieWeb.App
22
-      "client": "client_app_uri",
23
-      "occurred_at": "2018-12-21 14:15:37 PST"
24
-    }
25
-  }
26
-  """
27
-  @spec persist(record :: %Koype.Repo.Entry{}, data :: map()) :: {:ok, binary()} | {:error, any()}
28
-  def persist(record, data) do
29
-    case Koype.Storage.upload_properties_inline(data, record) do
30
-      {:error, _} = error ->
31
-        Logger.warn("Failed to update JSON storage for #{record.id}: #{inspect(error)}.")
32
-        error
33
-
34
-      {:ok, properties} ->
35
-        Koype.Storage.Json.persist(record, properties)
36
-    end
37
-  end
38
-end

+ 49
- 0
lib/repo/json.ex View File

@@ -0,0 +1,49 @@
1
+defmodule Koype.Repo.Json do
2
+  @moduledoc "Represents structured information for models."
3
+  @enforce_keys ~w(type version data)a
4
+  @type t :: %__MODULE__{version: Version.t(), data: map(), type: map()}
5
+  defstruct ~w(type version data)a
6
+
7
+  require Logger
8
+
9
+  defmacro __using__(options \\ []) do
10
+    quote do
11
+      import Koype.Repo.Json
12
+
13
+      @json_version Keyword.get(unquote(options), :version, Koype.version())
14
+      @json_type Keyword.get(unquote(options), :type)
15
+
16
+      def json_find(record), do: Koype.Repo.Json.find(record)
17
+      def json_persist(record, data) do
18
+        transform_fn = Keyword.get(unquote(options), :transform_fn, fn data -> data end)
19
+        case transform_fn.(Map.merge(data, %{ record: record })) do
20
+          {:error, _} = error -> error
21
+          result ->
22
+            Koype.Repo.Json.persist(record, result, [type: @json_type, version: @json_version])
23
+        end
24
+      end
25
+      def json_update(record, properties), do: Koype.Repo.Json.update(record, properties, [type: @json_type, version: @json_version])
26
+
27
+    end
28
+  end
29
+
30
+  def find(model) do
31
+    case Koype.Storage.Json.find(model) do
32
+      {:ok, json} -> {:ok, Map.get(json,"data", %{})}
33
+      _ = error -> error
34
+    end
35
+  end
36
+
37
+  def persist(record, data, args) do
38
+    Koype.Storage.Json.persist(record, %{"type" => args[:type], "version" => args[:version], "data" => data})
39
+  end
40
+
41
+  def update(record, properties, args) do
42
+    case find(record) do
43
+      {:ok, record_json} ->
44
+        new_json = Map.merge(record_json || %{}, properties)
45
+        persist(record, new_json, args)
46
+      {:error, _} = error -> error
47
+    end
48
+  end
49
+end

+ 33
- 61
lib/repo/webmention.ex View File

@@ -28,53 +28,47 @@ defmodule Koype.Repo.Webmention do
28 28
     route_token: "~/webmention",
29 29
     lookup_columns: ~w(id)a
30 30
 
31
-  schema "webmentions" do
32
-    field(:mf2, Koype.Storage.Json.Type)
33
-    field(:source, :string, null: false)
34
-    field(:target, :string, null: false)
35
-    field(:author, :string, null: false)
36
-    field(:type, :string, null: false)
37
-    field(:deleted_at, :utc_datetime, default: nil, null: true)
38
-    field(:moderated_at, :utc_datetime, default: nil, null: true)
39
-    field(:moderation_status, :string, default: "pending", null: false)
40
-
41
-    timestamps()
42
-  end
43
-
44
-  defmodule Json do
45
-    @moduledoc "Represents structured data for an entry."
31
+  use Koype.Repo.Json,
32
+    version: "2019-04-21",
33
+    type: "webmentions",
34
+    transform_fn: fn args ->
35
+      record = args[:record]
36
+      mf2 = Map.get(args, :mf2, %{})
37
+      author = case Map.get(args, :author, record.author) do
38
+        nil-> if {:ok, hcard} = IndieWeb.HCard.resolve(record.source), do: hcard, else: {:error, :no_author}
39
+        useful_author -> useful_author
40
+      end
46 41
 
47
-    @doc "Obtains the JSON data associated for this entry."
48
-    @spec find(model :: Entry) :: {:ok, map()} | {:error, any()}
49
-    def find(model) do
50
-      Koype.Storage.Json.find(model)
51
-    end
52
-
53
-    @doc "Stores the structured information of the ref'd Entry to object storage."
54
-    @spec persist(record :: Entry, args :: keyword()) :: {:ok, binary()} | {:error, any()}
55
-    def persist(record, author: author, mf2: mf2) do
56 42
       accessed_dt =
57 43
         Calendar.DateTime.Format.rfc3339(Enum.max([record.updated_at, record.inserted_at]), 3)
58 44
 
59
-      data = %{
45
+      source_mf2 = %{
46
+        "published" => mf2 |> IndieWeb.MF2.get_value!("published", [nil]) |> List.first(),
47
+        "updated" => mf2 |> IndieWeb.MF2.get_value!("updated", [nil]) |> List.first(),
48
+        "title" => mf2 |> IndieWeb.MF2.get_value!("name", [nil]) |> List.first(),
49
+        "content" => mf2 |> IndieWeb.MF2.get_value!("content", [nil]) |> Enum.filter(&is_map/1) |> List.first
50
+      }
51
+
52
+       %{
60 53
         "accessed" => accessed_dt,
61 54
         "author" => author,
62
-        "source" => do_extract_source_mf2(mf2),
55
+        "source" => source_mf2,
63 56
         "mf2" => mf2,
64 57
         "url" => mf2["url"] || record.source
65 58
       }
66
-
67
-      Koype.Storage.Json.persist(record, data)
68 59
     end
69 60
 
70
-    defp do_extract_source_mf2(mf2) do
71
-      %{
72
-        "published" => mf2 |> IndieWeb.MF2.get_value!("published", [nil]) |> List.first(),
73
-        "updated" => mf2 |> IndieWeb.MF2.get_value!("updated", [nil]) |> List.first(),
74
-        "title" => mf2 |> IndieWeb.MF2.get_value!("name", [nil]) |> List.first(),
75
-        "content" => mf2 |> IndieWeb.MF2.get_value!("content", [nil]) |> Enum.filter(&is_map/1) |> List.first
76
-      }
77
-    end
61
+  schema "webmentions" do
62
+    field(:mf2, Koype.Storage.Json.Type)
63
+    field(:source, :string, null: false)
64
+    field(:target, :string, null: false)
65
+    field(:author, :string, null: false)
66
+    field(:type, :string, null: false)
67
+    field(:deleted_at, :utc_datetime, default: nil, null: true)
68
+    field(:moderated_at, :utc_datetime, default: nil, null: true)
69
+    field(:moderation_status, :string, default: "pending", null: false)
70
+
71
+    timestamps()
78 72
   end
79 73
 
80 74
   @doc false
@@ -118,7 +112,7 @@ defmodule Koype.Repo.Webmention do
118 112
     ) do
119 113
       types = IndieWeb.Post.extract_types(entry_mf2["properties"])
120 114
       type = IndieWeb.Post.determine_type(entry_mf2["properties"], types) |> Atom.to_string()
121
-      update(webmention, type: type) |> elem(1)
115
+      update(webmention, type: type, mf2: entry_mf2) |> elem(1)
122 116
     else
123 117
       {:error, _} ->
124 118
         webmention
@@ -135,7 +129,7 @@ defmodule Koype.Repo.Webmention do
135 129
         }),
136 130
       {:ok, record} <- Koype.Repo.insert(cs),
137 131
       {:ok, updated_record} <- update_author(record, args[:author]),
138
-      :ok <- do_update_json(updated_record, args[:mf2])
132
+      {:ok, _} <- json_persist(updated_record, %{mf2: args[:mf2], author: args[:author]})
139 133
     ) do
140 134
       Logger.info("Saved a new #{args[:type]} Webmention from #{args[:source]} to #{args[:target]}")
141 135
 
@@ -149,7 +143,7 @@ defmodule Koype.Repo.Webmention do
149 143
     with(
150 144
       cs <- model |> changeset(%{}) |> change(args |> Keyword.drop(~w(mf2)a)),
151 145
       {:ok, record} <- Koype.Repo.update(cs),
152
-      :ok <- do_update_json(model, args[:mf2])
146
+      {:ok, _} <- json_update(record, %{mf2: args[:mf2], author: args[:author]})
153 147
     ) do
154 148
       Logger.info(
155 149
         "Updated Webmention ##{record.id} from #{record.author} via #{args[:source]} to #{
@@ -163,28 +157,6 @@ defmodule Koype.Repo.Webmention do
163 157
     end
164 158
   end
165 159
 
166
-  def do_update_json(model, args)
167
-
168
-  def do_update_json(model, mf2) when is_map(mf2) do
169
-    with(
170
-      {:ok, author} <- IndieWeb.HCard.resolve(model.source),
171
-      {:ok, _} <- Koype.Repo.Webmention.Json.persist(model, author: author, mf2: mf2)
172
-    ) do
173
-      Logger.info("Updated MF2 of Webmention ##{model.id}.",
174
-        mf2: inspect(mf2),
175
-        author: inspect(author)
176
-      )
177
-
178
-      :ok
179
-    else
180
-      error ->
181
-        Logger.info("Failed to update MF2 of Webmention ##{model.id}.", error: inspect(error))
182
-        error
183
-    end
184
-  end
185
-
186
-  def do_update_json(_, nil), do: :ok
187
-
188 160
   @doc "Looks up Webmentions for a particular target and optionally of provided type."
189 161
   @spec query(path :: URI.t() | binary(), type :: atom()) :: Ecto.Query.t() | nil
190 162
   def query(path, type \\ :all)

+ 1
- 0
lib/storage.ex View File

@@ -154,6 +154,7 @@ defmodule Koype.Storage do
154 154
       end
155 155
     end)
156 156
   end
157
+  def upload_property_for_record(kv, _), do: kv
157 158
 
158 159
   @spec destroy_property_for_record(keypair :: tuple(), record :: any()) :: :ok | {:error, any()}
159 160
   def destroy_property_for_record(keypair, record)

+ 11
- 3
lib/storage/base.ex View File

@@ -22,13 +22,14 @@ defmodule Koype.Storage.Base do
22 22
       use Arc.Definition
23 23
       use Arc.Ecto.Definition
24 24
       require Koype.Storage.Base
25
+      require Logger
25 26
       import Koype.Storage.Base
26 27
 
27 28
       @versions ~w(original square rect_wide rect_tall portrait landscape)a
28 29
       @acl :private
29 30
       @directory unquote(opts)[:directory]
30 31
       @storage_unit unquote(opts)[:storage_unit]
31
-      @extension_whitelist unquote(opts)[:extension_whitelist]
32
+      @extension_whitelist Keyword.get(unquote(opts),:extension_whitelist, [])
32 33
 
33 34
       def storage_dir(version, args)
34 35
 
@@ -53,12 +54,19 @@ defmodule Koype.Storage.Base do
53 54
 
54 55
       def validate({%{filename: filename} = _file, _}) do
55 56
         file_extension = filename |> Path.extname() |> String.downcase()
56
-        Enum.member?(@extension_whitelist, file_extension)
57
+
58
+        if @extension_whitelist != [] do
59
+          Enum.member?(@extension_whitelist, file_extension)
60
+        end
57 61
       end
58 62
 
59 63
       def validate({%{file_name: filename} = _file, _}) do
60 64
         file_extension = filename |> Path.extname() |> String.downcase()
61
-        Enum.member?(@extension_whitelist, file_extension)
65
+
66
+        if @extension_whitelist != [] do
67
+          Enum.member?(@extension_whitelist, file_extension)
68
+        end
69
+        true
62 70
       end
63 71
 
64 72
       def path(value, version \\ :original)

+ 1
- 2
lib/storage/image.ex View File

@@ -19,6 +19,5 @@
19 19
 defmodule Koype.Storage.Image do
20 20
   use Koype.Storage.Base,
21 21
     directory: "images",
22
-    storage_unit: :image,
23
-    extension_whitelist: ~w(.jpg .jpeg .gif .png .webp .bmp .apng)
22
+    storage_unit: :image
24 23
 end

+ 2
- 2
lib/template/format.ex View File

@@ -64,7 +64,7 @@ defimpl Koype.Template.Formatter, for: Koype.Repo.Entry do
64 64
 
65 65
   def format(%Koype.Repo.Entry{} = entry) do
66 66
     with(
67
-      {:ok, json} <- Koype.Repo.Entry.Json.find(entry),
67
+      {:ok, json} <- Koype.Repo.Entry.json_find(entry),
68 68
       props when is_map(props) <- IndieWeb.Micropub.Content.expand_properties(json, entry)
69 69
     ) do
70 70
       uri = entry |> Koype.Repo.Entry.get_uri()
@@ -102,7 +102,7 @@ defimpl Koype.Template.Formatter, for: Koype.Repo.Webmention do
102 102
   require Logger
103 103
 
104 104
   def format(webmention) do
105
-    with({:ok, json} <- Koype.Repo.Webmention.Json.find(webmention)) do
105
+    with({:ok, json} <- Koype.Repo.Webmention.json_find(webmention)) do
106 106
       json |> Map.put("id", webmention.id)
107 107
     else
108 108
       {:error, _} = error ->

+ 1
- 1
lib/webmention.ex View File

@@ -271,7 +271,7 @@ defmodule Koype.Webmention do
271 271
     path
272 272
     |> Koype.Repo.Webmention.all(type)
273 273
     |> Enum.map(fn model ->
274
-      with({:ok, json} <- Koype.Repo.Webmention.Json.find(model)) do
274
+      with({:ok, json} <- Koype.Repo.Webmention.json_find(model)) do
275 275
         json
276 276
         |> Map.put("id", model.id)
277 277
         |> Map.put("url", model.source)

+ 45
- 0
priv/repo/migrations/20190421110555_migrate_json_to_new_layout.exs View File

@@ -0,0 +1,45 @@
1
+defmodule Koype.Repo.Migrations.MigrateJsonToNewLayout do
2
+  use Ecto.Migration
3
+  require Ecto.Query
4
+
5
+  defp do_setup_app do
6
+    Enum.each(~w(cachex ex_aws ex_aws_s3 arc)a, fn app ->
7
+      {:ok, _} = Application.ensure_all_started(app)
8
+    end)
9
+
10
+    Cachex.start(:koype)
11
+    Koype.Application.load_runtime_config()
12
+  end
13
+
14
+
15
+  def up do
16
+    do_setup_app()
17
+
18
+    Koype.Repo.Entry
19
+    |> Koype.Repo.all
20
+    |> Enum.each(fn model -> 
21
+      case Koype.Storage.Json.find(model) do
22
+        {:ok, json} ->
23
+          unless Map.has_key?(json, "version") do
24
+            Koype.Repo.Entry.json_persist(model, json)
25
+          end
26
+        _ -> 
27
+          :ok
28
+      end
29
+    end)
30
+
31
+    Koype.Repo.Webmention
32
+    |> Koype.Repo.all
33
+    |> Enum.each(fn model -> 
34
+      case Koype.Storage.Json.find(model) do
35
+        {:ok, json} ->
36
+          unless Map.has_key?(json, "version") do
37
+            Koype.Repo.Webmention.json_persist(model, %{mf2: json[:mf2], record: model})
38
+          end
39
+        _ -> 
40
+          :ok
41
+      end
42
+    end)
43
+
44
+  end
45
+end

+ 1
- 1
priv/repo/seeds/entry.exs View File

@@ -39,7 +39,7 @@ seed Koype.Repo.Entry, fn entry ->
39 39
 
40 40
   content = Faker.Lorem.sentence()
41 41
 
42
-  Koype.Repo.Entry.Json.persist(model, %{
42
+  Koype.Repo.Entry.json_persist(model, %{
43 43
     "repost-of" => [Faker.Internet.url],
44 44
     "content" => %{
45 45
       "plain" => content,

+ 2
- 2
test/integration/controllers/entry_controller_test.exs View File

@@ -8,7 +8,7 @@ defmodule Koype.Web.EntryControllerTest do
8 8
     test "410 when post is deleted" do
9 9
       entry = insert(:entry, params_for(:entry) |> as_deleted_entry)
10 10
       entry_json = build(:entry_json)
11
-      Model.Json.persist(entry, entry_json)
11
+      Model.json_persist(entry, entry_json)
12 12
       path = URI.parse(Model.get_uri(entry)).path
13 13
       conn = build_conn() |> get(path)
14 14
 
@@ -29,7 +29,7 @@ defmodule Koype.Web.EntryControllerTest do
29 29
     test "200 when renders entry to page" do
30 30
       entry = insert(:entry)
31 31
       entry_json = build(:entry_json)
32
-      Model.Json.persist(entry, entry_json)
32
+      Model.json_persist(entry, entry_json)
33 33
 
34 34
       conn = build_conn() |> get(URI.parse(Model.get_uri(entry)).path)
35 35
       assert html_response(conn, 200)

+ 1
- 1
test/integration/controllers/feed_controller_test.exs View File

@@ -13,7 +13,7 @@ defmodule Koype.Web.FeedControllerTest do
13 13
       post_status: "published"
14 14
     )
15 15
     |> Enum.each(fn entry ->
16
-      Koype.Repo.Entry.Json.persist(entry, build(:entry_json) |> with_html_content)
16
+      Koype.Repo.Entry.json_persist(entry, build(:entry_json) |> with_html_content)
17 17
     end)
18 18
 
19 19
     :ok

+ 53
- 80
test/integration/controllers/indie/micropub_controller_test.exs View File

@@ -78,7 +78,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
78 78
       {:ok, entry} = Koype.Repo.Entry.resolve_from_uri(location)
79 79
 
80 80
       refute [] ==
81
-               entry |> Koype.Repo.preload(:syndication_results) |> Map.get(:syndication_results)
81
+        entry |> Koype.Repo.preload(:syndication_results) |> Map.get(:syndication_results)
82 82
     end
83 83
 
84 84
     test "201 creates a new entry json" do
@@ -141,7 +141,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
141 141
             "content" => Faker.Lorem.paragraph(),
142 142
             "category" => Faker.Lorem.words(),
143 143
             "photo" => [
144
-              Faker.Avatar.image_url()
144
+              "https://placekitten.com/g/200/300"
145 145
             ]
146 146
           }
147 147
         })
@@ -351,73 +351,60 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
351 351
 
352 352
     test "204 deletes individual value from property from an existing entry" do
353 353
       entry = insert(:entry)
354
+      entry_url = Koype.Repo.Entry.get_uri(entry)
354 355
       categories = Faker.Lorem.words() ++ ["test"]
355 356
       entry_json = build(:entry_json) |> with_categories(categories)
357
+      Koype.Repo.Entry.json_persist(entry, entry_json)
356 358
 
357
-      with_mocks([
358
-        {
359
-          IndieWeb.Micropub.Content,
360
-          [],
361
-          handle: fn "update", _ -> {:ok, state: :updated, model: entry, uri: @entry_uri} end
362
-        },
363
-        {
364
-          Koype.Repo.Entry.Json,
365
-          [:passthrough],
366
-          find: fn ^entry -> {:ok, entry_json} end, persist: fn ^entry, data -> {:ok, data} end
367
-        }
368
-      ]) do
369
-        conn =
370
-          build_conn()
371
-          |> indie_sign_in_conn(@client_id, ~w(create update))
372
-          |> put_req_header("content-type", "application/json")
373
-          |> post(@route, %{
374
-            "action" => "update",
375
-            "url" => @entry_uri,
376
-            "delete" => %{
377
-              "category" => ["test"]
378
-            }
379
-          })
359
+      conn =
360
+        build_conn()
361
+        |> indie_sign_in_conn(@client_id, ~w(create update))
362
+        |> put_req_header("content-type", "application/json")
363
+        |> post(@route, %{
364
+          "action" => "update",
365
+          "url" => entry_url,
366
+          "delete" => %{
367
+            "category" => ["test"]
368
+          }
369
+        })
380 370
 
381
-        assert text_response(conn, :no_content) == ""
382
-      end
371
+      assert text_response(conn, :no_content) == ""
383 372
     end
384 373
 
385 374
     test "204 deletes an entire existing entry" do
386 375
       entry = insert(:entry)
376
+      entry_json = build(:entry_json)
377
+      entry_url = Koype.Repo.Entry.get_uri(entry)
378
+      Koype.Repo.Entry.json_persist(entry, entry_json)
387 379
 
388
-      with_mock(IndieWeb.Micropub.Content,
389
-        handle: fn "delete", _ -> {:ok, state: :deleted, model: entry, uri: @entry_uri} end
390
-      ) do
391
-        conn =
392
-          build_conn()
393
-          |> indie_sign_in_conn(@client_id, ~w(delete))
394
-          |> put_req_header("content-type", "application/json")
395
-          |> post(@route, %{
396
-            "action" => "delete",
397
-            "url" => @entry_uri
398
-          })
380
+      conn =
381
+        build_conn()
382
+        |> indie_sign_in_conn(@client_id, ~w(delete))
383
+        |> put_req_header("content-type", "application/json")
384
+        |> post(@route, %{
385
+          "action" => "delete",
386
+          "url" => entry_url
387
+        })
399 388
 
400
-        assert text_response(conn, :no_content)
401
-      end
389
+      assert text_response(conn, :no_content)
402 390
     end
403 391
 
404 392
     test "201 un-deletes an entire existing entry" do
405
-      entry = insert(:entry)
393
+      entry = insert(:entry, deleted_at: Faker.DateTime.forward(1))
394
+      entry_json = build(:entry_json)
395
+      entry_url = Koype.Repo.Entry.get_uri(entry)
396
+      Koype.Repo.Entry.json_persist(entry, entry_json)
406 397
 
407
-      with_mock(IndieWeb.Micropub.Content,
408
-        handle: fn "undelete", _ -> {:ok, state: :undeleted, model: entry, uri: @entry_uri} end
409
-      ) do
410
-        conn =
411
-          build_conn()
412
-          |> indie_sign_in_conn(@client_id, ~w(undelete))
413
-          |> put_req_header("content-type", "application/json")
414
-          |> post(@route, %{
415
-            "action" => "undelete",
416
-            "url" => @entry_uri
417
-          })
398
+      conn =
399
+        build_conn()
400
+        |> indie_sign_in_conn(@client_id, ~w(undelete))
401
+        |> put_req_header("content-type", "application/json")
402
+        |> post(@route, %{
403
+          "action" => "undelete",
404
+          "url" => entry_url
405
+        })
418 406
 
419
-        assert text_response(conn, :created)
420
-      end
407
+      assert text_response(conn, :created)
421 408
     end
422 409
   end
423 410
 
@@ -435,29 +422,15 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
435 422
     test "201 uploads media" do
436 423
       uri = Faker.Internet.url()
437 424
 
438
-      mock_module =
439
-        Koype.Storage.Image
440
-        |> double
441
-        |> allow(:store, fn _ -> {:ok, Faker.Lorem.word()} end)
442
-        |> allow(:path, fn _ -> uri end)
443
-
444
-      with_mocks([
445
-        {
446
-          Koype.Storage,
447
-          [],
448
-          module_for_type: fn _ -> mock_module end
449
-        }
450
-      ]) do
451
-        conn =
452
-          build_conn()
453
-          |> indie_sign_in_conn(@client_id, ~w(create media))
454
-          |> post(@route, %{
455
-            file: make_mock_upload()
456
-          })
425
+      conn =
426
+        build_conn()
427
+        |> indie_sign_in_conn(@client_id, ~w(create media))
428
+        |> post(@route, %{
429
+          file: make_mock_upload()
430
+        })
457 431
 
458
-        assert text_response(conn, :created) == ""
459
-        assert List.first(get_resp_header(conn, "location")) == uri
460
-      end
432
+      assert text_response(conn, :created) == ""
433
+      assert List.first(get_resp_header(conn, "location")) == uri
461 434
     end
462 435
 
463 436
     test "400 fails when no media provided" do
@@ -558,7 +531,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
558 531
     test "200 lists specific properties of a provided entry" do
559 532
       entry = insert(:entry)
560 533
       entry_json = build(:entry_json)
561
-      Koype.Repo.Entry.Json.persist(entry, entry_json)
534
+      Koype.Repo.Entry.json_persist(entry, entry_json)
562 535
       content = Enum.join(entry_json["content"]["value"], "\n")
563 536
       entry_uri = Koype.Repo.Entry.get_uri(entry)
564 537
 
@@ -573,7 +546,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
573 546
 
574 547
     test "200 lists all properties of a provided entry" do
575 548
       entry = insert(:entry)
576
-      Koype.Repo.Entry.Json.persist(entry, build(:entry_json))
549
+      Koype.Repo.Entry.json_persist(entry, build(:entry_json))
577 550
       entry_uri = Koype.Repo.Entry.get_uri(entry)
578 551
 
579 552
       conn =
@@ -588,7 +561,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
588 561
 
589 562
     test "200 returns a list of posts" do
590 563
       entries = insert_list(3, :entry)
591
-      Enum.each(entries, &Koype.Repo.Entry.Json.persist(&1, build(:entry_json)))
564
+      Enum.each(entries, &Koype.Repo.Entry.json_persist(&1, build(:entry_json)))
592 565
 
593 566
       conn =
594 567
         build_conn()
@@ -602,7 +575,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
602 575
     test "200 returns a list of a particular type of posts" do
603 576
       others = insert_list(3, :entry, type: "foo")
604 577
       notes = insert_list(2, :entry, type: "note")
605
-      Enum.each(notes ++ others, &Koype.Repo.Entry.Json.persist(&1, build(:entry_json)))
578
+      Enum.each(notes ++ others, &Koype.Repo.Entry.json_persist(&1, build(:entry_json)))
606 579
 
607 580
       conn =
608 581
         build_conn()

+ 1
- 1
test/support/steps/data.ex View File

@@ -16,7 +16,7 @@ defmodule Koype.Feature.Steps.Shared.Data do
16 16
           case record do
17 17
             %Koype.Repo.Entry{} ->
18 18
               entry_json = build(:entry_json, %{"name" => Faker.Lorem.sentence()})
19
-              Koype.Repo.Entry.Json.persist(record, entry_json)
19
+              Koype.Repo.Entry.json_persist(record, entry_json)
20 20
           end
21 21
 
22 22
         assert {:ok, _} = result

+ 1
- 1
test/unit/feed/atom_test.exs View File

@@ -11,7 +11,7 @@ defmodule Koype.Feed.AtomTest do
11 11
 
12 12
     test "renders a stream for ATOM Feed with items" do
13 13
       entries = insert_list(3, :entry)
14
-      Enum.each(entries, &Koype.Repo.Entry.Json.persist(&1, build(:entry_json)))
14
+      Enum.each(entries, &Koype.Repo.Entry.json_persist(&1, build(:entry_json)))
15 15
       atom_xml = Subject.generate(%{entries: entries}, [])
16 16
       entry_elements = atom_xml |> Floki.parse() |> Floki.find("entry")
17 17
       assert [] != entry_elements

+ 1
- 1
test/unit/feed/json_feed_test.exs View File

@@ -11,7 +11,7 @@ defmodule Koype.Feed.JsonTest do
11 11
 
12 12
     test "renders a stream for JSON Feed with items" do
13 13
       items = insert_list(10, :entry)
14
-      Enum.each(items, fn entry -> Koype.Repo.Entry.Json.persist(entry, build(:entry_json)) end)
14
+      Enum.each(items, fn entry -> Koype.Repo.Entry.json_persist(entry, build(:entry_json)) end)
15 15
 
16 16
       json = Subject.generate(%{entries: items}, []) |> Jason.decode!()
17 17
       refute Enum.empty?(json["items"])

+ 19
- 19
test/unit/indieweb/micropub/entry_test.exs View File

@@ -30,7 +30,7 @@ defmodule IndieWeb.Micropub.EntryTest do
30 30
       entry = insert(:entry)
31 31
       uri = Koype.Repo.Entry.get_uri(entry)
32 32
       entry_json = build(:entry_json)
33
-      Model.Json.persist(entry, entry_json)
33
+      Model.json_persist(entry, entry_json)
34 34
 
35 35
       assert {:error, [message: :scope_missing, scope: :upload, action: "add"]} =
36 36
                Subject.invoke(
@@ -54,7 +54,7 @@ defmodule IndieWeb.Micropub.EntryTest do
54 54
       entry = insert(:entry)
55 55
       uri = Koype.Repo.Entry.get_uri(entry)
56 56
       entry_json = build(:entry_json)
57
-      Model.Json.persist(entry, entry_json)
57
+      Model.json_persist(entry, entry_json)
58 58
 
59 59
       assert {:error, [message: :scope_missing, scope: :upload, action: "replace"]} =
60 60
                Subject.invoke(
@@ -78,7 +78,7 @@ defmodule IndieWeb.Micropub.EntryTest do
78 78
       entry = insert(:entry)
79 79
       uri = Koype.Repo.Entry.get_uri(entry)
80 80
       entry_json = build(:entry_json)
81
-      Model.Json.persist(entry, entry_json)
81
+      Model.json_persist(entry, entry_json)
82 82
 
83 83
       with_mock(Koype.Storage.Image, delete: fn _ -> {:error, :test_error} end) do
84 84
         assert {:error, key: "photo", error: :test_error} =
@@ -103,7 +103,7 @@ defmodule IndieWeb.Micropub.EntryTest do
103 103
       entry = insert(:entry)
104 104
       entry_json = params_for(:entry_json) |> Map.put("photo", [%{"value" => photo_uri}])
105 105
       url = Model.get_uri(entry)
106
-      Model.Json.persist(entry, entry_json)
106
+      Model.json_persist(entry, entry_json)
107 107
 
108 108
       with_mock(Koype.Storage.Image, delete: fn _ -> {:error, :test_error} end) do
109 109
         assert {:error, key: "photo", error: :test_error} =
@@ -127,7 +127,7 @@ defmodule IndieWeb.Micropub.EntryTest do
127 127
       entry = insert(:entry)
128 128
       uri = Koype.Repo.Entry.get_uri(entry)
129 129
       entry_json = build(:entry_json)
130
-      Model.Json.persist(entry, entry_json)
130
+      Model.json_persist(entry, entry_json)
131 131
 
132 132
       with_mock(Koype.Storage.Image, store: fn _ -> {:error, :test_value} end) do
133 133
         assert {:error, key: "photo", error: :test_value} =
@@ -153,7 +153,7 @@ defmodule IndieWeb.Micropub.EntryTest do
153 153
       entry = insert(:entry)
154 154
       uri = Koype.Repo.Entry.get_uri(entry)
155 155
       entry_json = build(:entry_json)
156
-      Model.Json.persist(entry, entry_json)
156
+      Model.json_persist(entry, entry_json)
157 157
 
158 158
       with_mock(Koype.Storage.Image, store: fn _ -> {:error, :test_value} end) do
159 159
         assert {:error, key: "photo", error: :test_value} =
@@ -325,7 +325,7 @@ defmodule IndieWeb.Micropub.EntryTest do
325 325
 
326 326
     test "successfully handles the act of deleting an entry" do
327 327
       entry = insert(:entry)
328
-      Model.Json.persist(entry, %{})
328
+      Model.json_persist(entry, %{})
329 329
       url = Model.get_uri(entry)
330 330
 
331 331
       assert {:ok, state: :deleted, model: deleted_model, uri: _} =
@@ -343,7 +343,7 @@ defmodule IndieWeb.Micropub.EntryTest do
343 343
 
344 344
     test "successfully handles the act of un-deleting an entry" do
345 345
       entry = insert(:entry) |> as_deleted_entry
346
-      Model.Json.persist(entry, %{})
346
+      Model.json_persist(entry, %{})
347 347
       url = Model.get_uri(entry)
348 348
 
349 349
       assert {:ok, state: :undeleted, model: deleted_model, uri: _} =
@@ -362,7 +362,7 @@ defmodule IndieWeb.Micropub.EntryTest do
362 362
     test "successfully handles the act of updating an entry - add" do
363 363
       entry = insert(:entry, %{name: nil}) |> Koype.Repo.preload([:categories])
364 364
       entry_json = params_for(:entry_json, %{"koype" => ["software"]})
365
-      Model.Json.persist(entry, entry_json)
365
+      Model.json_persist(entry, entry_json)
366 366
       url = Model.get_uri(entry)
367 367
 
368 368
       assert {:ok, state: :updated, model: model, uri: _} =
@@ -390,7 +390,7 @@ defmodule IndieWeb.Micropub.EntryTest do
390 390
     test "successfully handles the act of updating an HTML entry - add" do
391 391
       entry = insert(:entry)
392 392
       entry_json = params_for(:entry_json, %{"koype" => ["software"]}) |> with_html_content
393
-      Model.Json.persist(entry, entry_json)
393
+      Model.json_persist(entry, entry_json)
394 394
       url = Model.get_uri(entry)
395 395
 
396 396
       assert {:ok, state: :updated, model: model, uri: _} =
@@ -415,7 +415,7 @@ defmodule IndieWeb.Micropub.EntryTest do
415 415
     test "successfully handles the act of updating an entry - delete [whole property]" do
416 416
       entry = insert(:entry)
417 417
       entry_json = params_for(:entry_json)
418
-      Model.Json.persist(entry, entry_json)
418
+      Model.json_persist(entry, entry_json)
419 419
       url = Model.get_uri(entry)
420 420
 
421 421
       assert {:ok, state: :updated, model: model, uri: _} =
@@ -439,7 +439,7 @@ defmodule IndieWeb.Micropub.EntryTest do
439 439
       entry = insert(:entry)
440 440
       entry_json = params_for(:entry_json) |> Map.put("photo", [%{"value" => photo_uri}])
441 441
       url = Model.get_uri(entry)
442
-      Model.Json.persist(entry, entry_json)
442
+      Model.json_persist(entry, entry_json)
443 443
 
444 444
       assert {:ok, state: :updated, model: model, uri: _} =
445 445
                Subject.invoke(
@@ -467,7 +467,7 @@ defmodule IndieWeb.Micropub.EntryTest do
467 467
         })
468 468
 
469 469
       url = Model.get_uri(entry)
470
-      Model.Json.persist(entry, entry_json)
470
+      Model.json_persist(entry, entry_json)
471 471
 
472 472
       assert {:ok, state: :updated, model: model, uri: _} =
473 473
                Subject.invoke(
@@ -493,7 +493,7 @@ defmodule IndieWeb.Micropub.EntryTest do
493 493
       photo_name = photo_uri |> Path.split() |> List.last()
494 494
       entry_json = params_for(:entry_json) |> Map.put("photo", [%{"value" => photo_uri}])
495 495
       entry = insert(:entry)
496
-      Model.Json.persist(entry, entry_json)
496
+      Model.json_persist(entry, entry_json)
497 497
       url = Model.get_uri(entry)
498 498
 
499 499
       assert {:ok, state: :updated, model: model, uri: _} =
@@ -518,7 +518,7 @@ defmodule IndieWeb.Micropub.EntryTest do
518 518
       entry = insert(:entry)
519 519
       entry_json = params_for(:entry_json, %{"content" => %{"value" => ["foo", "bar", "baz"]}})
520 520
       url = Model.get_uri(entry)
521
-      Model.Json.persist(entry, entry_json)
521
+      Model.json_persist(entry, entry_json)
522 522
 
523 523
       assert {:ok, state: :updated, model: model, uri: _} =
524 524
                Subject.invoke(
@@ -542,7 +542,7 @@ defmodule IndieWeb.Micropub.EntryTest do
542 542
       entry = insert(:entry)
543 543
       entry_json = params_for(:entry_json)
544 544
       url = Model.get_uri(entry)
545
-      Model.Json.persist(entry, entry_json)
545
+      Model.json_persist(entry, entry_json)
546 546
 
547 547
       assert {:ok, state: :updated, model: model, uri: _} =
548 548
                Subject.invoke(
@@ -566,7 +566,7 @@ defmodule IndieWeb.Micropub.EntryTest do
566 566
       entry = insert(:entry)
567 567
       entry_json = params_for(:entry_json)
568 568
       url = Model.get_uri(entry)
569
-      Model.Json.persist(entry, entry_json)
569
+      Model.json_persist(entry, entry_json)
570 570
 
571 571
       assert {:ok, state: :updated, model: model, uri: _} =
572 572
                Subject.invoke(
@@ -605,7 +605,7 @@ defmodule IndieWeb.Micropub.EntryTest do
605 605
     test "passes with no links" do
606 606
       model = insert(:entry)
607 607
       entry_json = params_for(:entry_json)
608
-      Model.Json.persist(model, entry_json)
608
+      Model.json_persist(model, entry_json)
609 609
 
610 610
       assert :ok = Subject.send_webmentions(model)
611 611
     end
@@ -620,7 +620,7 @@ defmodule IndieWeb.Micropub.EntryTest do
620 620
         })
621 621
         |> with_html_content
622 622
 
623
-      Model.Json.persist(model, entry_json)
623
+      Model.json_persist(model, entry_json)
624 624
 
625 625
       use_cassette "micropub_entry_send_webmention_passes" do
626 626
         assert :ok = Subject.send_webmentions(model)

+ 4
- 4
test/unit/indieweb/micropub/query_test.exs View File

@@ -27,7 +27,7 @@ defmodule IndieWeb.Micropub.QueryTest do
27 27
 
28 28
     test "exposes the source MF2 of a particular post" do
29 29
       entry = insert(:entry)
30
-      Koype.Repo.Entry.Json.persist(entry, build(:entry_json))
30
+      Koype.Repo.Entry.json_persist(entry, build(:entry_json))
31 31
       entry_url = Koype.Repo.Entry.get_uri(entry)
32 32
 
33 33
       assert {:ok, %{"properties" => properties, "type" => type}} =
@@ -37,7 +37,7 @@ defmodule IndieWeb.Micropub.QueryTest do
37 37
     test "exposes a set of properties of the source MF2 of a particular post" do
38 38
       name = Faker.Lorem.sentence()
39 39
       entry = insert(:entry, name: name)
40
-      Koype.Repo.Entry.Json.persist(entry, build(:entry_json))
40
+      Koype.Repo.Entry.json_persist(entry, build(:entry_json))
41 41
       entry_url = Koype.Repo.Entry.get_uri(entry)
42 42
 
43 43
       assert {:ok, %{"properties" => properties, "type" => type}} =
@@ -50,7 +50,7 @@ defmodule IndieWeb.Micropub.QueryTest do
50 50
 
51 51
     test "exposes a range of posts and all of their properties" do
52 52
       entries = insert_list(3, :entry)
53
-      Enum.each(entries, &Koype.Repo.Entry.Json.persist(&1, build(:entry_json)))
53
+      Enum.each(entries, &Koype.Repo.Entry.json_persist(&1, build(:entry_json)))
54 54
 
55 55
       assert {:ok, %{"items" => items}} = Subject.invoke("source", %{})
56 56
       assert Enum.all?(items, &is_map/1)
@@ -59,7 +59,7 @@ defmodule IndieWeb.Micropub.QueryTest do
59 59
     test "exposes a set of properties from a range of posts" do
60 60
       entries = insert_list(3, :entry)
61 61
       names = entries |> Enum.map(& &1.name)
62
-      Enum.each(entries, &Koype.Repo.Entry.Json.persist(&1, build(:entry_json)))
62
+      Enum.each(entries, &Koype.Repo.Entry.json_persist(&1, build(:entry_json)))
63 63
 
64 64
       assert {:ok, %{"items" => items}} = Subject.invoke("source", %{"properties" => ~w(name)})
65 65
 

+ 0
- 74
test/unit/repo/entry/json_test.exs View File

@@ -1,74 +0,0 @@
1
-defmodule Koype.Repo.Entry.JsonTest do
2
-  use Koype.DataCase
3
-  import Mock
4
-  import Koype.Factory
5
-  alias Koype.Repo.Entry, as: Model
6
-
7
-  @filename Faker.File.file_name(:text)
8
-
9
-  describe ".persist/2" do
10
-    test "saves json for an entry" do
11
-      record = insert(:entry)
12
-      json = build(:entry_json)
13
-
14
-      with_mocks([
15
-        {Koype.Storage.Json, [], persist: fn ^record, ^json -> {:ok, @filename} end},
16
-        {Koype.Storage, [], upload_properties_inline: fn ^json, ^record -> {:ok, json} end}
17
-      ]) do
18
-        assert {:ok, @filename} = Model.Json.persist(record, json)
19
-        assert_called(Koype.Storage.Json.persist(record, json))
20
-        assert_called(Koype.Storage.upload_properties_inline(json, record))
21
-      end
22
-    end
23
-
24
-    test "fails to save json for an entry" do
25
-      record = insert(:entry)
26
-      json = build(:entry_json)
27
-
28
-      with_mocks([
29
-        {Koype.Storage.Json, [], persist: fn ^record, ^json -> {:error, :test_error} end},
30
-        {Koype.Storage, [], upload_properties_inline: fn ^json, ^record -> {:ok, json} end}
31
-      ]) do
32
-        assert {:error, :test_error} = Model.Json.persist(record, json)
33
-        assert_called(Koype.Storage.Json.persist(record, json))
34
-        assert_called(Koype.Storage.upload_properties_inline(json, record))
35
-      end
36
-    end
37
-
38
-    test "fails to upload properties in json for an entry" do
39
-      record = insert(:entry)
40
-      json = build(:entry_json)
41
-
42
-      with_mocks([
43
-        {Koype.Storage.Json, [], persist: fn ^record, ^json -> {:ok, @filename} end},
44
-        {Koype.Storage, [],
45
-         upload_properties_inline: fn ^json, ^record -> {:error, :test_error} end}
46
-      ]) do
47
-        assert {:error, :test_error} = Model.Json.persist(record, json)
48
-        refute called(Koype.Storage.Json.persist(record, :_))
49
-        assert_called(Koype.Storage.upload_properties_inline(json, record))
50
-      end
51
-    end
52
-  end
53
-
54
-  describe ".find/2" do
55
-    test "loads json for an entry" do
56
-      record = insert(:entry)
57
-      json = build(:entry_json)
58
-
59
-      with_mock(Koype.Storage.Json, find: fn ^record -> {:ok, json} end) do
60
-        assert {:ok, ^json} = Model.Json.find(record)
61
-        assert_called(Koype.Storage.Json.find(record))
62
-      end
63
-    end
64
-
65
-    test "fails to find json for an entry" do
66
-      record = insert(:entry)
67
-
68
-      with_mock(Koype.Storage.Json, find: fn ^record -> {:error, :test_error} end) do
69
-        assert {:error, _} = Model.Json.find(record)
70
-        assert_called(Koype.Storage.Json.find(record))
71
-      end
72
-    end
73
-  end
74
-end

+ 1
- 1
test/unit/repo/webmention_test.exs View File

@@ -47,7 +47,7 @@ defmodule Koype.Repo.WebmentionTest do
47 47
 
48 48
   describe ".create/1" do
49 49
     test "creates new record" do
50
-      params = params_for(:webmention, author: "https://jacky.wtf")
50
+      params = params_for(:webmention, author: "https://jacky.wtf") |> Map.put(:mf2, build(:entry_json))
51 51
       assert {:ok, _} = Subject.create(params)
52 52
     end
53 53
   end

+ 1
- 1
test/unit/template/format_test.exs View File

@@ -67,7 +67,7 @@ defmodule Koype.Template.FormatterTest do
67 67
       categories = insert_list(3, :category)
68 68
       :ok = Enum.each(categories, &Koype.Repo.Entry.Category.bind(entry, &1))
69 69
       syndication_targets = insert_list(4, :syndication_target)
70
-      Koype.Repo.Entry.Json.persist(entry, entry_core_json)
70
+      Koype.Repo.Entry.json_persist(entry, entry_core_json)
71 71
 
72 72
       :ok =
73 73
         [syndication_targets, ~w(failed processed sent completed)]

+ 3
- 3
web/controllers/entry_controller.ex View File

@@ -26,7 +26,7 @@ defmodule Koype.Web.EntryController do
26 26
 
27 27
     with(
28 28
       {:ok, entry} <- Entry.fetch(id),
29
-      {:ok, json} <- Entry.Json.find(entry),
29
+      {:ok, json} <- Entry.json_find(entry),
30 30
       {:ok, updated_model} <- Koype.Repo.Entry.update(entry, Map.drop(json, ~w(name)))
31 31
     ) do
32 32
       conn
@@ -68,7 +68,7 @@ defmodule Koype.Web.EntryController do
68 68
 
69 69
     with(
70 70
       {:ok, entry} <- Entry.fetch(id),
71
-      {:ok, json} <- Entry.Json.find(entry),
71
+      {:ok, json} <- Entry.json_find(entry),
72 72
       {:ok, updated_model} <- IndieWeb.Micropub.Content.parse_extensions(entry, json)
73 73
     ) do
74 74
       conn
@@ -89,7 +89,7 @@ defmodule Koype.Web.EntryController do
89 89
 
90 90
     with(
91 91
       {:ok, entry} <- Entry.fetch(id),
92
-      {:ok, json} <- Entry.Json.find(entry),
92
+      {:ok, json} <- Entry.json_find(entry),
93 93
       {:ok, updated_properties} <- IndieWeb.Micropub.Entry.process_properties(json),
94 94
       {:ok, updated_model} <- Entry.update(entry, updated_properties)
95 95
     ) do

+ 1
- 1
web/views/webmention_view.ex View File

@@ -55,7 +55,7 @@ defmodule Koype.Web.WebmentionView do
55 55
   end
56 56
 
57 57
   def get_content_for_webmention(mention) do
58
-    case Koype.Repo.Webmention.Json.find(mention) do
58
+    case Koype.Repo.Webmention.json_find(mention) do
59 59
       {:ok, json} ->
60 60
         {:safe, json["source"]["content"]["html"]}
61 61
 

Loading…
Cancel
Save