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

Commit 5aab9ba

Browse files
committed
refactor(article-tags): remove old tags system
1 parent 00299b3 commit 5aab9ba

File tree

32 files changed

+181
-503
lines changed

32 files changed

+181
-503
lines changed

cover/excoveralls.json

+1-1
Large diffs are not rendered by default.

docs/testing/unit-testing.zh-CN.md

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
### 单元测试
32

43
单元测试部分全部位于 `test/groupher_server/` 目录下, 按照接口的 Context 分为
@@ -7,13 +6,13 @@
76
accounts billing cms delivery logs seeds statistics
87
```
98

10-
> Phoenix 使用 ExUnit(link) 作为测试模块,测试文件必须以 '_test.exs' 作为结尾,否则会被忽略。
9+
> Phoenix 使用 ExUnit(link) 作为测试模块,测试文件必须以 '\_test.exs' 作为结尾,否则会被忽略。
1110
1211
#### 运行测试
1312

1413
在项目根目录执行 `make test` 即可运行所有测试, 你也可以使用 `make test.watch`
1514
`make test.watch.wip` 以 watch mode 运行全部或其中一部分测试。 更多命令可以使用
16-
`make test.help` 查看:
15+
`make test.help` 查看:
1716

1817
```text
1918
@@ -38,7 +37,7 @@ accounts billing cms delivery logs seeds statistics
3837

3938
#### Helper 函数
4039

41-
以一个实际例子作为说明:
40+
以一个实际例子作为说明:
4241

4342
```elixir
4443
defmodule GroupherServer.Test.CMS do
@@ -60,9 +59,9 @@ defmodule GroupherServer.Test.CMS do
6059

6160
describe "[cms tag]" do
6261
test "create tag with valid data", ~m(community user)a do
63-
valid_attrs = mock_attrs(:tag)
62+
valid_attrs = mock_attrs(:article_tag)
6463

65-
{:ok, tag} = CMS.create_tag(community, :post, valid_attrs, %User{id: user.id})
64+
{:ok, tag} = CMS.create_article_tag(community, :post, valid_attrs, %User{id: user.id})
6665
assert tag.title == valid_attrs.title
6766
end
6867
end
@@ -81,8 +80,3 @@ use GroupherServer.TestTools
8180
3. 这里测试的都是 `lib/groupher_server` 下的模块,不涉及 Graphql
8281

8382
4. 更多的技巧你可以参照文档或现有的测试用例,通常它们都浅显易懂。
84-
85-
86-
87-
88-

lib/groupher_server/cms/delegates/article_community.ex

+1-42
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
1313
alias Helper.Types, as: T
1414
alias Helper.ORM
1515

16-
alias GroupherServer.CMS.{Embeds, Community, Tag, PinnedArticle}
16+
alias GroupherServer.CMS.{Embeds, Community, PinnedArticle}
1717
alias GroupherServer.Repo
1818

1919
alias Ecto.Multi
@@ -128,47 +128,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
128128
defp result({:ok, %{mirror_target_community: result}}), do: result |> done()
129129
defp result({:error, _, result, _steps}), do: {:error, result}
130130

131-
@doc """
132-
set general tag for post / tuts ...
133-
"""
134-
# check community first
135-
def set_tag(thread, %Tag{id: tag_id}, content_id) do
136-
with {:ok, action} <- match_action(thread, :tag),
137-
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
138-
{:ok, tag} <- ORM.find(action.reactor, tag_id) do
139-
update_content_tag(content, tag)
140-
141-
# NOTE: this should be control by Middleware
142-
# case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do
143-
# true ->
144-
# content
145-
# |> Ecto.Changeset.change()
146-
# |> Ecto.Changeset.put_assoc(:tags, content.tags ++ [tag])
147-
# |> Repo.update()
148-
149-
# _ ->
150-
# {:error, message: "Tag,Community,Thread not match", code: ecode(:custom)}
151-
# end
152-
end
153-
end
154-
155-
def unset_tag(thread, %Tag{id: tag_id}, content_id) do
156-
with {:ok, action} <- match_action(thread, :tag),
157-
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
158-
{:ok, tag} <- ORM.find(action.reactor, tag_id) do
159-
update_content_tag(content, tag, :drop)
160-
end
161-
end
162-
163-
defp update_content_tag(content, %Tag{} = tag, opt \\ :add) do
164-
new_tags = if opt == :add, do: content.tags ++ [tag], else: content.tags -- [tag]
165-
166-
content
167-
|> Ecto.Changeset.change()
168-
|> Ecto.Changeset.put_assoc(:tags, new_tags)
169-
|> Repo.update()
170-
end
171-
172131
@doc "update isEdited meta label if needed"
173132
# TODO: diff history
174133
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content) do

lib/groupher_server/cms/delegates/community_curd.ex

+4-35
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
1010

1111
alias Helper.ORM
1212
alias Helper.QueryBuilder
13-
alias GroupherServer.{Accounts, Repo}
13+
alias GroupherServer.{Accounts, CMS}
1414

15-
alias GroupherServer.CMS.{
16-
Category,
17-
Community,
18-
CommunityEditor,
19-
CommunitySubscriber,
20-
Tag,
21-
Thread
22-
}
15+
alias CMS.{ArticleTag, Category, Community, CommunityEditor, CommunitySubscriber, Thread}
2316

2417
@doc """
2518
return paged community subscribers
@@ -55,30 +48,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5548
end
5649
end
5750

58-
@doc """
59-
create a Tag base on type: post / tuts ...
60-
"""
61-
def create_tag(%Community{id: community_id}, thread, attrs, %Accounts.User{id: user_id}) do
62-
with {:ok, action} <- match_action(thread, :tag),
63-
{:ok, author} <- ensure_author_exists(%Accounts.User{id: user_id}),
64-
{:ok, _community} <- ORM.find(Community, community_id) do
65-
attrs =
66-
attrs
67-
|> Map.merge(%{author_id: author.id, community_id: community_id})
68-
|> map_atom_value(:string)
69-
|> Map.merge(%{thread: thread |> to_string |> String.downcase()})
70-
71-
action.reactor |> ORM.create(attrs)
72-
end
73-
end
74-
75-
def update_tag(%{id: _id} = attrs) do
76-
~m(id title color)a = attrs |> map_atom_value(:string)
77-
78-
Tag
79-
|> ORM.find_update(~m(id title color)a)
80-
end
81-
8251
def create_category(attrs, %Accounts.User{id: user_id}) do
8352
with {:ok, author} <- ensure_author_exists(%Accounts.User{id: user_id}) do
8453
attrs = attrs |> Map.merge(%{author_id: author.id})
@@ -128,10 +97,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
12897
end
12998

13099
@doc "count the total tags in community"
131-
def count(%Community{id: id}, :tags) do
100+
def count(%Community{id: id}, :article_tags) do
132101
with {:ok, community} <- ORM.find(Community, id) do
133102
result =
134-
Tag
103+
ArticleTag
135104
|> where([t], t.community_id == ^community.id)
136105
|> ORM.paginater(page: 1, size: 1)
137106

lib/groupher_server/cms/helper/matcher.ex

-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ defmodule GroupherServer.CMS.Helper.Matcher do
1717
# commtnes reaction
1818
PostCommentLike,
1919
#
20-
Tag,
2120
Community
2221
}
2322

@@ -27,13 +26,6 @@ defmodule GroupherServer.CMS.Helper.Matcher do
2726
def match_action(:post, :self),
2827
do: {:ok, %{target: Post, reactor: Post, preload: :author}}
2928

30-
def match_action(:post, :tag), do: {:ok, %{target: Post, reactor: Tag}}
31-
# NOTE: the tech, radar, share, city thread also use common tag
32-
def match_action(:radar, :tag), do: {:ok, %{target: Post, reactor: Tag}}
33-
def match_action(:share, :tag), do: {:ok, %{target: Post, reactor: Tag}}
34-
def match_action(:city, :tag), do: {:ok, %{target: Post, reactor: Tag}}
35-
def match_action(:tech, :tag), do: {:ok, %{target: Post, reactor: Tag}}
36-
3729
def match_action(:post, :community),
3830
do: {:ok, %{target: Post, reactor: Community}}
3931

@@ -52,8 +44,6 @@ defmodule GroupherServer.CMS.Helper.Matcher do
5244
def match_action(:job, :community),
5345
do: {:ok, %{target: Job, reactor: Community}}
5446

55-
def match_action(:job, :tag), do: {:ok, %{target: Job, reactor: Tag}}
56-
5747
#########################################
5848
## repos ...
5949
#########################################
@@ -63,8 +53,6 @@ defmodule GroupherServer.CMS.Helper.Matcher do
6353
def match_action(:repo, :community),
6454
do: {:ok, %{target: Repo, reactor: Community}}
6555

66-
def match_action(:repo, :tag), do: {:ok, %{target: Repo, reactor: Tag}}
67-
6856
# dynamic where query match
6957
def dynamic_where(thread, id) do
7058
case thread do

lib/groupher_server/cms/job.ex

+1-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Job do
99
import GroupherServer.CMS.Helper.Macros
1010

1111
alias GroupherServer.CMS
12-
alias CMS.{Embeds, Tag}
12+
alias CMS.Embeds
1313
alias Helper.HTML
1414

1515
@timestamps_opts [type: :utc_datetime_usec]
@@ -40,16 +40,6 @@ defmodule GroupherServer.CMS.Job do
4040
field(:digest, :string)
4141
field(:length, :integer)
4242

43-
many_to_many(
44-
:tags,
45-
Tag,
46-
join_through: "jobs_tags",
47-
join_keys: [job_id: :id, tag_id: :id],
48-
# :delete_all will only remove data from the join source
49-
on_delete: :delete_all,
50-
on_replace: :delete
51-
)
52-
5343
article_tags_field(:job)
5444
article_community_field(:job)
5545
general_article_fields()

lib/groupher_server/cms/post.ex

+1-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Post do
99
import GroupherServer.CMS.Helper.Macros
1010

1111
alias GroupherServer.CMS
12-
alias CMS.{Embeds, PostComment, Tag}
12+
alias CMS.{Embeds, PostComment}
1313

1414
alias Helper.HTML
1515

@@ -32,18 +32,6 @@ defmodule GroupherServer.CMS.Post do
3232
# TODO: remove after legacy data migrated
3333
has_many(:comments, {"posts_comments", PostComment})
3434

35-
# The keys are inflected from the schema names!
36-
# see https://hexdocs.pm/ecto/Ecto.Schema.html
37-
many_to_many(
38-
:tags,
39-
Tag,
40-
join_through: "posts_tags",
41-
join_keys: [post_id: :id, tag_id: :id],
42-
# :delete_all will only remove data from the join source
43-
on_delete: :delete_all,
44-
on_replace: :delete
45-
)
46-
4735
article_tags_field(:post)
4836
article_community_field(:post)
4937
general_article_fields()

lib/groupher_server/cms/repo.ex

+1-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Repo do
99
import GroupherServer.CMS.Helper.Macros
1010

1111
alias GroupherServer.CMS
12-
alias CMS.{Embeds, RepoContributor, RepoLang, Tag}
12+
alias CMS.{Embeds, RepoContributor, RepoLang}
1313

1414
alias Helper.HTML
1515

@@ -41,15 +41,6 @@ defmodule GroupherServer.CMS.Repo do
4141
embeds_many(:contributors, RepoContributor, on_replace: :delete)
4242
field(:last_sync, :utc_datetime)
4343

44-
many_to_many(
45-
:tags,
46-
Tag,
47-
join_through: "repos_tags",
48-
join_keys: [repo_id: :id, tag_id: :id],
49-
on_delete: :delete_all,
50-
on_replace: :delete
51-
)
52-
5344
article_tags_field(:repo)
5445
article_community_field(:repo)
5546
general_article_fields()

lib/groupher_server/cms/tag.ex

-52
This file was deleted.

lib/groupher_server/statistics/delegates/status.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ defmodule GroupherServer.Statistics.Delegate.Status do
1818
{:ok, %{total_count: repos_count}} = find_total_count(CMS.Repo)
1919

2020
{:ok, %{total_count: threads_count}} = find_total_count(CMS.Thread)
21-
{:ok, %{total_count: tags_count}} = find_total_count(CMS.Tag)
21+
{:ok, %{total_count: article_tags_count}} = find_total_count(CMS.ArticleTag)
2222
{:ok, %{total_count: categories_count}} = find_total_count(CMS.Category)
2323

2424
{:ok,
25-
~m(communities_count posts_count jobs_count repos_count threads_count tags_count categories_count)a}
25+
~m(communities_count posts_count jobs_count repos_count threads_count article_tags_count categories_count)a}
2626
end
2727

2828
defp find_total_count(queryable), do: ORM.find_all(queryable, @count_filter)

lib/groupher_server_web/resolvers/cms_resolver.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
388388
CMS.count(%Community{id: root.id}, :threads)
389389
end
390390

391-
def tags_count(root, _, _) do
392-
CMS.count(%Community{id: root.id}, :tags)
391+
def article_tags_count(root, _, _) do
392+
CMS.count(%Community{id: root.id}, :article_tags)
393393
end
394394
end

lib/groupher_server_web/schema/Helper/fields.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
3939
quote do
4040
field(:when, :when_enum)
4141
field(:length, :length_enum)
42-
field(:tag, :string, default_value: :all)
42+
field(:article_tag, :string)
4343
field(:community, :string)
4444
end
4545
end

lib/groupher_server_web/schema/account/account_misc.ex

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule GroupherServerWeb.Schema.Account.Misc do
99
pagination_args()
1010
# field(:when, :when_enum)
1111
# field(:sort, :sort_enum)
12-
# field(:tag, :string, default_value: :all)
1312
# field(:community, :string)
1413
end
1514

lib/groupher_server_web/schema/cms/cms_misc.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
174174
field(:first, :integer)
175175

176176
@desc "Matching a tag"
177-
field(:tag, :string, default_value: :all)
177+
field(:article_tag, :string)
178178
# field(:sort, :sort_input)
179179
field(:when, :when_enum)
180180
field(:sort, :sort_enum)

0 commit comments

Comments
 (0)