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

Commit c3e422b

Browse files
committed
refactor(cited-article): doc & naming adjust
1 parent 018d6a4 commit c3e422b

File tree

6 files changed

+53
-60
lines changed

6 files changed

+53
-60
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
1818

1919
alias Accounts.Model.User
2020
alias CMS.Model.{Author, Community, PinnedArticle, Embeds}
21-
alias CMS.Delegate.{ArticleCommunity, ArticleComment, ArticleTag, CommunityCURD}
21+
alias CMS.Delegate.{ArticleCommunity, ArticleComment, ArticleTag, CommunityCURD, CiteTasks}
2222

2323
alias Ecto.Multi
2424

@@ -149,12 +149,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
149149
|> Multi.run(:create_article, fn _, _ ->
150150
do_create_article(info.model, attrs, author, community)
151151
end)
152-
|> Multi.run(:block_tasks, fn _, %{create_article: article} ->
153-
# ArticleCommunity.mirror_article(thread, article.id, community.id)
154-
# Later
155-
# BlockTasks.handle(article)
156-
{:ok, :pass}
157-
end)
158152
|> Multi.run(:mirror_article, fn _, %{create_article: article} ->
159153
ArticleCommunity.mirror_article(thread, article.id, community.id)
160154
end)
@@ -170,6 +164,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
170164
|> Multi.run(:update_user_published_meta, fn _, _ ->
171165
Accounts.update_published_states(uid, thread)
172166
end)
167+
|> Multi.run(:block_tasks, fn _, %{create_article: article} ->
168+
Later.run({CiteTasks, :handle, [article]})
169+
end)
173170
# TODO: run mini tasks
174171
|> Multi.run(:mention_users, fn _, %{create_article: article} ->
175172
# article.body |> Jason.decode!() |> 各种小 task
@@ -452,7 +449,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
452449

453450
# create done
454451
defp result({:ok, %{set_active_at_timestamp: result}}) do
455-
Later.exec({__MODULE__, :notify_admin_new_article, [result]})
452+
Later.run({__MODULE__, :notify_admin_new_article, [result]})
456453
{:ok, result}
457454
end
458455

lib/groupher_server/cms/delegates/block_tasks.ex renamed to lib/groupher_server/cms/delegates/cite_tasks.ex

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
defmodule GroupherServer.CMS.Delegate.BlockTasks do
1+
defmodule GroupherServer.CMS.Delegate.CiteTasks do
22
@moduledoc """
33
run tasks in every article blocks if need
4+
5+
current task: "cite link" and "mention"
6+
7+
## cite link
8+
9+
我被站内哪些文章或评论引用了,是值得关注的事
10+
我引用了谁不重要,帖子里链接已经表明了, 这和 github issue 的双向链接不一样,因为一般不需要关注这个
11+
帖子是否解决,是否被 merge 等状态。
12+
13+
基本结构:
14+
15+
cited_thread, cited_article_id, [xxx_article]_id, [block_id, block2_id],
16+
17+
POST post_333 -> cited_article_333, [block_id, block2_id]]
18+
19+
cited_type, cited_content_id, [contents]_id, [block_id, cited_block_id],
20+
21+
cited_type: thread or comment
22+
content: article or comment
23+
# cited_article_comment_id, [xxx_article]_id, [block_id, block2_id, ...],
424
"""
25+
526
import Ecto.Query, warn: false
627
import Helper.Utils, only: [get_config: 2, thread_of_article: 1, done: 1]
728
import GroupherServer.CMS.Helper.Matcher
@@ -17,47 +38,24 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
1738
@article_threads get_config(:article, :threads)
1839
@valid_article_prefix Enum.map(@article_threads, &"#{@site_host}/#{&1}/")
1940

20-
"""
21-
我被谁引用了 是最重要的信息
22-
我引用了谁不重要,自己扫帖子就行了
23-
cited_thread, cited_article_id, [xxx_article]_id, [block_id, cited_block_id],
24-
25-
POST post_333 -> cited_article_333, [[block_3, cited_block_23]]
26-
27-
cited_type, cited_content_id, [contents]_id, [block_id, cited_block_id],
28-
29-
cited_type: thread or comment
30-
content: article or comment
31-
# cited_article_comment_id, [xxx_article]_id, [block_id, cited_block_id],
32-
"""
33-
34-
# reference
35-
# mention
36-
3741
def handle(%{body: body} = article) do
38-
with {:ok, body_map} <- Jason.decode(body) do
39-
run_tasks(:cite, article, body_map["blocks"])
40-
# run_tasks(:mention, article, body_map["blocks"])
42+
with {:ok, %{"blocks" => blocks}} <- Jason.decode(body),
43+
article <- Repo.preload(article, author: :user) do
44+
Multi.new()
45+
|> Multi.run(:delete_all_cited_contents, fn _, _ ->
46+
delete_all_cited_contents(article)
47+
end)
48+
|> Multi.run(:update_cited_info, fn _, _ ->
49+
blocks
50+
|> Enum.reduce([], &(&2 ++ parse_cited_info_per_block(article, &1)))
51+
|> merge_same_cited_article_block
52+
|> update_cited_info
53+
end)
54+
|> Repo.transaction()
55+
|> result()
4156
end
4257
end
4358

44-
defp run_tasks(:cite, article, blocks) do
45-
article = article |> Repo.preload(author: :user)
46-
47-
Multi.new()
48-
|> Multi.run(:delete_all_cited_contents, fn _, _ ->
49-
delete_all_cited_contents(article)
50-
end)
51-
|> Multi.run(:update_cited_info, fn _, _ ->
52-
blocks
53-
|> Enum.reduce([], &(&2 ++ parse_cited_info_per_block(article, &1)))
54-
|> merge_same_cited_article_block
55-
|> update_cited_info
56-
end)
57-
|> Repo.transaction()
58-
|> result()
59-
end
60-
6159
# delete all records before insert_all, this will dynamiclly update
6260
# those cited info when update article
6361
# 插入引用记录之前先全部清除,这样可以在更新文章的时候自动计算引用信息
@@ -150,11 +148,7 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
150148
...
151149
]
152150
"""
153-
defp parse_cited_info_per_block(article, %{
154-
"id" => block_id,
155-
"data" => %{"text" => text}
156-
}) do
157-
#
151+
defp parse_cited_info_per_block(article, %{"id" => block_id, "data" => %{"text" => text}}) do
158152
links_in_block = Floki.find(text, "a[href]")
159153

160154
Enum.reduce(links_in_block, [], fn link, acc ->

lib/groupher_server/statistics/delegates/contribute.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
134134
end
135135

136136
defp cache_contribute_later(%Community{id: id}) do
137-
Later.exec({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
137+
Later.run({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
138138
end
139139

140140
defp do_get_contributes(%Community{id: id}) do

lib/helper/later.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ defmodule Helper.Later do
55

66
@doc """
77
## Example
8-
iex> Later.exec({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
8+
iex> Later.run({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
99
{:ok, _}
1010
"""
11-
def exec({mod, func, args}) do
11+
def run({mod, func, args}) do
1212
Rihanna.enqueue({mod, func, args})
13+
# weather enqueue success or not, just return {:ok, :pass}, or Multi.Job will be rollback
14+
{:ok, :pass}
1315
end
1416
end

test/groupher_server/cms/cite_content_test.exs renamed to test/groupher_server/cms/cite_contents/cite_post_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule GroupherServer.Test.CMS.CiteContent do
88

99
alias CMS.Model.Post
1010

11-
alias CMS.Delegate.BlockTasks
11+
alias CMS.Delegate.CiteTasks
1212

1313
@site_host get_config(:general, :site_host)
1414

@@ -29,7 +29,7 @@ defmodule GroupherServer.Test.CMS.CiteContent do
2929
end
3030

3131
describe "[cite basic]" do
32-
# @tag :wip
32+
@tag :wip
3333
test "cited multi post should work", ~m(user community post2 post3 post4 post5 post_attrs)a do
3434
body =
3535
mock_rich_text(
@@ -49,8 +49,8 @@ defmodule GroupherServer.Test.CMS.CiteContent do
4949
post_attrs = post_attrs |> Map.merge(%{body: body})
5050
{:ok, post_n} = CMS.create_article(community, :post, post_attrs, user)
5151

52-
BlockTasks.handle(post)
53-
BlockTasks.handle(post_n)
52+
CiteTasks.handle(post)
53+
CiteTasks.handle(post_n)
5454

5555
{:ok, post2} = ORM.find(Post, post2.id)
5656
{:ok, post3} = ORM.find(Post, post3.id)
@@ -70,7 +70,7 @@ defmodule GroupherServer.Test.CMS.CiteContent do
7070
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post.id} />))
7171
{:ok, post} = CMS.update_article(post, %{body: body})
7272

73-
BlockTasks.handle(post)
73+
CiteTasks.handle(post)
7474

7575
{:ok, post} = ORM.find(Post, post.id)
7676
assert post.meta.citing_count == 0

test/groupher_server/statistics/statistics_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ defmodule GroupherServer.Test.Statistics do
222222

223223
Statistics.make_contribute(%Community{id: community.id})
224224

225-
# res = Later.exec({IO, :puts, ["Work, work, work, work, work."]})
225+
# res = Later.run({IO, :puts, ["Work, work, work, work, work."]})
226226
# Process.sleep(1000)
227227
# IO.inspect(res, label: "res")
228228

0 commit comments

Comments
 (0)