Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit fa6efa1

Browse files
committed
refactor(cite-task): fmt naming
1 parent 485335e commit fa6efa1

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

lib/groupher_server/cms/delegates/cite_tasks.ex

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,8 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
170170
# IO.inspect(links, label: "links -> ")
171171

172172
Enum.reduce(links, [], fn link, acc ->
173-
with {:ok, cited_content} <- parse_cited_content(link) do
174-
# IO.inspect(cited_content, label: "before shape cited_content")
175-
List.insert_at(acc, 0, shape_cited_content(comment, cited_content, block_id))
176-
else
173+
case parse_cited(link) do
174+
{:ok, cited} -> List.insert_at(acc, 0, shape_cited(comment, cited, block_id))
177175
_ -> acc
178176
end
179177
end)
@@ -185,11 +183,8 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
185183
# [{"a", [{"href", "https://coderplanets.com/post/195675"}], []},]
186184
defp do_parse_cited_info(article, block_id, links) do
187185
Enum.reduce(links, [], fn link, acc ->
188-
with {:ok, cited_content} <- parse_cited_content(link) do
189-
# do not cite artilce itself
190-
# true <- article.id !== cited_content.id do
191-
List.insert_at(acc, 0, shape_cited_content(article, cited_content, block_id))
192-
else
186+
case parse_cited(link) do
187+
{:ok, cited} -> List.insert_at(acc, 0, shape_cited(article, cited, block_id))
193188
_ -> acc
194189
end
195190
end)
@@ -198,60 +193,52 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
198193

199194
# cite article in comment
200195
# 在评论中引用文章
201-
defp shape_cited_content(
202-
%Comment{} = comment,
203-
%{type: :article, content: cited_article},
204-
block_id
205-
) do
196+
defp shape_cited(%Comment{} = comment, %{type: :article, content: cited}, block_id) do
206197
%{
207-
cited_by_id: cited_article.id,
208-
cited_by_type: cited_article.meta.thread,
198+
cited_by_id: cited.id,
199+
cited_by_type: cited.meta.thread,
209200
comment_id: comment.id,
210201
block_linker: [block_id],
211202
user_id: comment.author_id,
212203
# extra fields for next-step usage
213204
# used for updating citing_count, avoid load again
214-
cited_content: cited_article,
205+
cited_content: cited,
215206
# for later insert all
216207
citing_time: comment.updated_at |> DateTime.truncate(:second)
217208
}
218209
end
219210

220211
# cite comment in comment
221212
# 评论中引用评论
222-
defp shape_cited_content(
223-
%Comment{} = comment,
224-
%{type: :comment, content: cited_comment},
225-
block_id
226-
) do
213+
defp shape_cited(%Comment{} = comment, %{type: :comment, content: cited}, block_id) do
227214
%{
228-
cited_by_id: cited_comment.id,
215+
cited_by_id: cited.id,
229216
cited_by_type: "COMMENT",
230217
comment_id: comment.id,
231218
block_linker: [block_id],
232219
user_id: comment.author_id,
233220
# extra fields for next-step usage
234221
# used for updating citing_count, avoid load again
235-
cited_content: cited_comment,
222+
cited_content: cited,
236223
# for later insert all
237224
citing_time: comment.updated_at |> DateTime.truncate(:second)
238225
}
239226
end
240227

241228
# cite article in article
242229
# 文章之间相互引用
243-
defp shape_cited_content(article, %{type: :article, content: cited_article}, block_id) do
230+
defp shape_cited(article, %{type: :article, content: cited}, block_id) do
244231
{:ok, thread} = thread_of_article(article)
245232
{:ok, info} = match(thread)
246233

247234
%{
248-
cited_by_id: cited_article.id,
249-
cited_by_type: cited_article.meta.thread,
235+
cited_by_id: cited.id,
236+
cited_by_type: cited.meta.thread,
250237
block_linker: [block_id],
251238
user_id: article.author.user.id,
252239
# extra fields for next-step usage
253240
# used for updating citing_count, avoid load again
254-
cited_content: cited_article,
241+
cited_content: cited,
255242
# for later insert all
256243
citing_time: article.updated_at |> DateTime.truncate(:second)
257244
}
@@ -260,26 +247,26 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
260247

261248
# cite comment in article
262249
# 文章中引用评论
263-
defp shape_cited_content(article, %{type: :comment, content: cited_comment}, block_id) do
250+
defp shape_cited(article, %{type: :comment, content: cited}, block_id) do
264251
{:ok, thread} = thread_of_article(article)
265252
{:ok, info} = match(thread)
266253

267254
%{
268-
cited_by_id: cited_comment.id,
255+
cited_by_id: cited.id,
269256
cited_by_type: "COMMENT",
270257
block_linker: [block_id],
271258
user_id: article.author.user.id,
272259
# extra fields for next-step usage
273260
# used for updating citing_count, avoid load again
274-
cited_content: cited_comment,
261+
cited_content: cited,
275262
# for later insert all
276263
citing_time: article.updated_at |> DateTime.truncate(:second)
277264
}
278265
|> Map.put(info.foreign_key, article.id)
279266
end
280267

281268
# 要考虑是否有 comment_id 的情况,如果有,那么 就应该 load comment 而不是 article
282-
defp parse_cited_content({"a", attrs, _}) do
269+
defp parse_cited({"a", attrs, _}) do
283270
with {:ok, link} <- parse_link(attrs),
284271
true <- is_site_article_link?(link) do
285272
# IO.inspect(link, label: "parse link")

0 commit comments

Comments
 (0)