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

Commit 107aaa1

Browse files
committed
refactor(seeds): add tags for default community
1 parent 76c283b commit 107aaa1

File tree

5 files changed

+182
-13
lines changed

5 files changed

+182
-13
lines changed

lib/mastani_server/cms/delegates/seeds.ex

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
1717
alias MastaniServer.{Accounts, CMS}
1818
alias MastaniServer.CMS.{Community, Thread, Category}
1919

20-
@default_threads ["post", "user", "job", "video", "wiki", "cheatsheet", "repo"]
21-
@home_threads ["post", "user", "news", "city", "share", "job"]
20+
alias CMS.Delegate.SeedsConfig
2221

23-
# those thread has tag list
24-
@general_threads ["post", "job", "repo", "video"]
25-
26-
@pl_communities ["javascript", "scala", "haskell", "swift", "typescript", "lua", "racket"]
27-
@default_categories ["pl", "front-end", "back-end", "ai", "design", "mobile", "others"]
22+
@default_threads SeedsConfig.threads(:default)
23+
@home_threads SeedsConfig.threads(:home)
24+
@pl_communities SeedsConfig.communities(:pl)
25+
@default_categories SeedsConfig.categories(:default)
2826

2927
def seed_threads(:default) do
3028
with true <- is_empty_db?(CMS.Thread) do
@@ -81,7 +79,7 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
8179
{:ok, categories} <- seed_categories(bot, :default),
8280
{:ok, communities} <- seed_for_communities(bot, :pl) do
8381
threadify_communities(communities.entries, threads)
84-
# tagfy_threads(communities.entries, threads)
82+
tagfy_threads(communities.entries, threads, bot)
8583

8684
# TODO: set tags for post, video, job, repo thread
8785
categorify_communities(communities.entries, categories)
@@ -152,17 +150,22 @@ defmodule MastaniServer.CMS.Delegate.Seeds do
152150
end
153151

154152
# tagfy only post job repo and video
155-
defp tagfy_threads(communities, threads) when is_list(communities) do
153+
defp tagfy_threads(communities, threads, bot) when is_list(communities) do
156154
Enum.each(communities, fn community ->
157155
Enum.each(threads, fn thread ->
158-
case thread.raw in @general_threads do
159-
true -> IO.inspect(thread.raw, label: "set this thread")
160-
false -> IO.inspect(thread.raw, label: "not target")
161-
end
156+
set_tags(community, thread, bot)
162157
end)
163158
end)
164159
end
165160

161+
defp set_tags(%Community{} = community, %Thread{raw: raw}, bot) do
162+
thread = raw |> String.to_atom()
163+
164+
Enum.each(SeedsConfig.tags(thread), fn attr ->
165+
CMS.create_tag(community, thread, attr, %Accounts.User{id: bot.id})
166+
end)
167+
end
168+
166169
# set categories to given communities
167170
defp categorify_communities(communities, categories) do
168171
Enum.each(communities, fn community ->
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
defmodule MastaniServer.CMS.Delegate.SeedsConfig do
2+
@moduledoc """
3+
init config for seeds
4+
"""
5+
6+
@doc """
7+
default seeds for pragraming lang's communities
8+
"""
9+
def communities(:pl) do
10+
["javascript", "scala", "haskell", "swift", "typescript", "lua", "racket"]
11+
end
12+
13+
@doc """
14+
default categories seeds for general community
15+
"""
16+
def categories(:default),
17+
do: ["pl", "front-end", "back-end", "ai", "design", "mobile", "others"]
18+
19+
@doc """
20+
default threads seeds for general communities
21+
"""
22+
def threads(:default), do: ["post", "user", "job", "video", "wiki", "cheatsheet", "repo"]
23+
24+
@doc """
25+
default threads seeds for home
26+
"""
27+
def threads(:home), do: ["post", "user", "news", "city", "share", "job"]
28+
29+
@doc """
30+
default tags for general communities
31+
currently only support post, job, video, repo
32+
"""
33+
def tags(:post) do
34+
[
35+
%{
36+
title: "refined",
37+
color: :red
38+
},
39+
%{
40+
title: "share",
41+
color: :orange
42+
},
43+
%{
44+
title: "ask",
45+
color: :yellow
46+
},
47+
%{
48+
title: "newbie",
49+
color: :green
50+
},
51+
%{
52+
title: "algorithm",
53+
color: :cyan
54+
},
55+
%{
56+
title: "hangout",
57+
color: :blue
58+
},
59+
%{
60+
title: "spread",
61+
color: :purple
62+
}
63+
]
64+
|> Enum.map(fn attr -> Map.merge(%{thread: :post}, attr) end)
65+
end
66+
67+
def tags(:job) do
68+
[
69+
%{
70+
title: "beijing",
71+
color: :red
72+
},
73+
%{
74+
title: "shanghai",
75+
color: :orange
76+
},
77+
%{
78+
title: "shenzhen",
79+
color: :yellow
80+
},
81+
%{
82+
title: "hangzhou",
83+
color: :green
84+
},
85+
%{
86+
title: "chengdu",
87+
color: :cyan
88+
},
89+
%{
90+
title: "wuhan",
91+
color: :blue
92+
},
93+
%{
94+
title: "oversea",
95+
color: :purple
96+
},
97+
%{
98+
title: "other",
99+
color: :grey
100+
}
101+
]
102+
|> Enum.map(fn attr -> Map.merge(%{thread: :job}, attr) end)
103+
end
104+
105+
def tags(:repo) do
106+
[
107+
%{
108+
title: "framework",
109+
color: :red
110+
},
111+
%{
112+
title: "devops",
113+
color: :orange
114+
},
115+
%{
116+
title: "ai",
117+
color: :yellow
118+
},
119+
%{
120+
title: "test",
121+
color: :green
122+
},
123+
%{
124+
title: "product",
125+
color: :cyan
126+
},
127+
%{
128+
title: "other",
129+
color: :grey
130+
}
131+
]
132+
|> Enum.map(fn attr -> Map.merge(%{thread: :repo}, attr) end)
133+
end
134+
135+
def tags(:video) do
136+
[
137+
%{
138+
title: "conf",
139+
color: :red
140+
},
141+
%{
142+
title: "tuts",
143+
color: :orange
144+
},
145+
%{
146+
title: "security",
147+
color: :yellow
148+
},
149+
%{
150+
title: "other",
151+
color: :grey
152+
}
153+
]
154+
|> Enum.map(fn attr -> Map.merge(%{thread: :video}, attr) end)
155+
end
156+
157+
def tags(_), do: []
158+
end

lib/mastani_server/cms/utils/matcher.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ defmodule MastaniServer.CMS.Utils.Matcher do
125125
def match_action(:video, :star),
126126
do: {:ok, %{target: Video, reactor: VideoStar, preload: :user}}
127127

128+
def match_action(:video, :tag), do: {:ok, %{target: Video, reactor: Tag}}
129+
128130
def match_action(:video, :comment),
129131
do: {:ok, %{target: Video, reactor: VideoComment, preload: :author}}
130132

@@ -143,6 +145,8 @@ defmodule MastaniServer.CMS.Utils.Matcher do
143145
def match_action(:repo, :community),
144146
do: {:ok, %{target: Repo, reactor: Community, flag: RepoCommunityFlag}}
145147

148+
def match_action(:repo, :tag), do: {:ok, %{target: Repo, reactor: Tag}}
149+
146150
def match_action(:repo, :favorite),
147151
do: {:ok, %{target: Repo, reactor: RepoFavorite, preload: :user}}
148152

lib/mastani_server_web/schema/cms/cms_misc.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ defmodule MastaniServerWeb.Schema.CMS.Misc do
131131
value(:cyan)
132132
value(:blue)
133133
value(:purple)
134+
value(:grey)
134135
end
135136

136137
@desc "inline members-like filter for dataloader usage"

test/mastani_server/seeds/communities_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ defmodule MastaniServer.Test.Seeds.Communities do
2626

2727
{:ok, found} = ORM.find(CMS.Community, radom_community.id, preload: :categories)
2828
assert length(found.categories) !== 0
29+
30+
# {:ok, tags} = ORM.find_all(CMS.Tag, %{page: 1, size: 20})
31+
# IO.inspect tags, label: "hello tags"
2932
end
3033

3134
@tag :wip

0 commit comments

Comments
 (0)