|
| 1 | +defmodule GroupherServer.CMS.Delegate.BlockTasks do |
| 2 | + @moduledoc """ |
| 3 | + run tasks in every article blocks if need |
| 4 | + """ |
| 5 | + import Helper.Utils, only: [get_config: 2] |
| 6 | + import GroupherServer.CMS.Helper.Matcher |
| 7 | + |
| 8 | + alias GroupherServer.Repo |
| 9 | + |
| 10 | + alias GroupherServer.CMS.Model.CitedContent |
| 11 | + alias Helper.ORM |
| 12 | + |
| 13 | + @site_host get_config(:general, :site_host) |
| 14 | + @article_threads get_config(:article, :threads) |
| 15 | + @valid_article_prefix Enum.map(@article_threads, &"#{@site_host}/#{&1}/") |
| 16 | + |
| 17 | + """ |
| 18 | + 我被谁引用了 是最重要的信息 |
| 19 | + 我引用了谁不重要,自己扫帖子就行了 |
| 20 | + cited_thread, cited_article_id, [xxx_article]_id, [block_id, cited_block_id], |
| 21 | +
|
| 22 | + POST post_333 -> cited_article_333, [[block_3, cited_block_23]] |
| 23 | +
|
| 24 | + cited_type, cited_content_id, [contents]_id, [block_id, cited_block_id], |
| 25 | +
|
| 26 | + cited_type: thread or comment |
| 27 | + content: article or comment |
| 28 | + # cited_article_comment_id, [xxx_article]_id, [block_id, cited_block_id], |
| 29 | + """ |
| 30 | + |
| 31 | + # reference |
| 32 | + # mention |
| 33 | + |
| 34 | + def handle(%{body: body} = article) do |
| 35 | + with {:ok, body_map} <- Jason.decode(body) do |
| 36 | + run_tasks(:cite, article, body_map["blocks"]) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + defp run_tasks(:cite, article, blocks) do |
| 41 | + article = article |> Repo.preload(author: :user) |
| 42 | + |
| 43 | + # NOTE: |
| 44 | + cited_contents = |
| 45 | + blocks |
| 46 | + |> Enum.reduce([], &(&2 ++ get_cited_contents_per_block(article, &1))) |
| 47 | + |> merge_same_cited_article_block |
| 48 | + |
| 49 | + IO.inspect(cited_contents, label: "fuck all") |
| 50 | + |
| 51 | + # CitedContent |> ORM.create |
| 52 | + end |
| 53 | + |
| 54 | + @doc """ |
| 55 | + e.g: |
| 56 | + [ |
| 57 | + %{ |
| 58 | + block_linker: ["block-zByQI"], |
| 59 | + cited_by_id: 190058, |
| 60 | + cited_by_type: "POST", |
| 61 | + post_id: 190059, |
| 62 | + user_id: 1413053 |
| 63 | + }, |
| 64 | + %{ |
| 65 | + block_linker: ["block-zByQI"], |
| 66 | + cited_by_id: 190057, |
| 67 | + cited_by_type: "POST", |
| 68 | + post_id: 190059, |
| 69 | + user_id: 1413053 |
| 70 | + }, |
| 71 | + %{ |
| 72 | + block_linker: ["block-ZgKJs"], |
| 73 | + cited_by_id: 190057, |
| 74 | + cited_by_type: "POST", |
| 75 | + post_id: 190059, |
| 76 | + user_id: 1413053 |
| 77 | + } |
| 78 | + ] |
| 79 | + into: |
| 80 | + [ |
| 81 | + %{ |
| 82 | + block_linker: ["block-zByQI"], |
| 83 | + cited_by_id: 190058, |
| 84 | + cited_by_type: "POST", |
| 85 | + post_id: 190059, |
| 86 | + user_id: 1413053 |
| 87 | + }, |
| 88 | + %{ |
| 89 | + block_linker: ["block-zByQI", "block-ZgKJs"], |
| 90 | + cited_by_id: 190057, |
| 91 | + cited_by_type: "POST", |
| 92 | + post_id: 190059, |
| 93 | + user_id: 1413053 |
| 94 | + }, |
| 95 | + ] |
| 96 | + """ |
| 97 | + defp merge_same_cited_article_block(cited_contents) do |
| 98 | + cited_contents |
| 99 | + |> Enum.chunk_by(& &1.cited_by_id) |
| 100 | + |> Enum.map(fn content_by_group -> |
| 101 | + Enum.reduce(content_by_group, fn content, acc -> |
| 102 | + Map.merge(content, %{block_linker: content.block_linker ++ acc.block_linker}) |
| 103 | + end) |
| 104 | + end) |
| 105 | + end |
| 106 | + |
| 107 | + defp get_cited_contents_per_block(article, %{ |
| 108 | + "id" => block_id, |
| 109 | + "data" => %{"text" => text} |
| 110 | + }) do |
| 111 | + # |
| 112 | + links_in_block = Floki.find(text, "a[href]") |
| 113 | + |
| 114 | + Enum.reduce(links_in_block, [], fn link, acc -> |
| 115 | + with {:ok, cited_article} <- parse_cited_article(link) do |
| 116 | + List.insert_at(acc, 0, shape_cited_content(article, cited_article, block_id)) |
| 117 | + else |
| 118 | + {:error, _} -> acc |
| 119 | + end |
| 120 | + end) |
| 121 | + |> Enum.uniq() |
| 122 | + end |
| 123 | + |
| 124 | + defp shape_cited_content(article, cited_article, block_id) do |
| 125 | + thread = article.meta.thread |> String.downcase() |> String.to_atom() |
| 126 | + {:ok, info} = match(thread) |
| 127 | + |
| 128 | + %{ |
| 129 | + cited_by_id: cited_article.id, |
| 130 | + cited_by_type: cited_article.meta.thread, |
| 131 | + block_linker: [block_id], |
| 132 | + user_id: article.author.user.id |
| 133 | + } |
| 134 | + |> Map.put(info.foreign_key, article.id) |
| 135 | + end |
| 136 | + |
| 137 | + defp parse_cited_article({"a", attrs, _}) do |
| 138 | + with {:ok, link} <- get_link(Enum.find(attrs, fn {a, v} -> a == "href" end)), |
| 139 | + true <- is_valid_article_link?(link) do |
| 140 | + get_cited_article(link) |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + defp get_link({"href", link}), do: {:ok, link} |
| 145 | + defp get_link(_), do: {:error, "invalid fmt"} |
| 146 | + |
| 147 | + defp is_valid_article_link?(url) do |
| 148 | + Enum.any?(@valid_article_prefix, &String.starts_with?(url, &1)) |
| 149 | + end |
| 150 | + |
| 151 | + # get cited article from url |
| 152 | + # e.g: https://coderplanets.com/post/189993 -> ORM.find(Post, 189993) |
| 153 | + defp get_cited_article(url) do |
| 154 | + %{path: path} = URI.parse(url) |
| 155 | + path_list = path |> String.split("/") |
| 156 | + thread = path_list |> Enum.at(1) |> String.downcase() |> String.to_atom() |
| 157 | + article_id = path_list |> Enum.at(2) |
| 158 | + |
| 159 | + with {:ok, info} <- match(thread) do |
| 160 | + ORM.find(info.model, article_id) |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + defp run_tasks(:mention) do |
| 165 | + # |
| 166 | + end |
| 167 | + |
| 168 | + # 还是单抽一个模块吧 |
| 169 | + defp run_tasks(:danger_words) do |
| 170 | + # title & body |
| 171 | + end |
| 172 | + |
| 173 | + defp parse_blocks(%{body: body} = article) do |
| 174 | + with {:ok, body_map} <- Jason.decode(body) do |
| 175 | + # |
| 176 | + end |
| 177 | + end |
| 178 | +end |
0 commit comments