Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit85bc9a4

Browse files
committed
chore: re-org codebase
1 parent5c5e1d2 commit85bc9a4

File tree

7 files changed

+309
-171
lines changed

7 files changed

+309
-171
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
defmoduleGroupherServer.CMS.Delegate.Seeds.Domaindo
2+
@moduledoc"""
3+
seeds data for init, should be called ONLY in new database, like migration
4+
"""
5+
6+
importEcto.Query,warn:false
7+
8+
importGroupherServer.CMS.Delegate.Seeds.Helper,
9+
only:[
10+
threadify_communities:2,
11+
tagfy_threads:4,
12+
categorify_communities:3,
13+
seed_bot:0,
14+
seed_threads:1,
15+
seed_categories_ifneed:1
16+
]
17+
18+
aliasHelper.ORM
19+
aliasGroupherServer.CMS
20+
21+
aliasCMS.Model.Community
22+
23+
@oss_endpoint"https://cps-oss.oss-cn-shanghai.aliyuncs.com"
24+
25+
# seed community
26+
@doc"""
27+
seed community for home
28+
"""
29+
defseed_community(:home)do
30+
with{:error,_}<-ORM.find_by(Community,%{raw:"home"}),
31+
{:ok,bot}<-seed_bot(),
32+
{:ok,threads}<-seed_threads(:home),
33+
{:ok,categories}<-seed_categories_ifneed(bot)do
34+
args=%{
35+
title:"coderplanets",
36+
desc:"the most sexy community for developers, ever.",
37+
logo:"#{@oss_endpoint}/icons/cmd/keyboard_logo.png",
38+
raw:"home",
39+
user_id:bot.id
40+
}
41+
42+
{:ok,community}=Community|>ORM.create(args)
43+
threadify_communities([community],threads.entries)
44+
tagfy_threads([community],threads.entries,bot,:home)
45+
46+
{:ok,community}
47+
# home 不设置分类,比较特殊
48+
end
49+
end
50+
51+
@doc"""
52+
seed community for blackhole
53+
"""
54+
defseed_community(:blackhole)do
55+
with{:error,_}<-ORM.find_by(Community,%{raw:"blackhole"}),
56+
{:ok,bot}<-seed_bot(),
57+
{:ok,threads}<-seed_threads(:blackhole),
58+
{:ok,categories}<-seed_categories_ifneed(bot)do
59+
args=%{
60+
title:"黑洞",
61+
desc:"这里收录不适合出现在本站的内容。",
62+
logo:"#{@oss_endpoint}/icons/cmd/keyboard_logo.png",
63+
raw:"blackhole",
64+
user_id:bot.id
65+
}
66+
67+
{:ok,community}=Community|>ORM.create(args)
68+
threadify_communities([community],threads.entries)
69+
tagfy_threads([community],threads.entries,bot,:blackhole)
70+
categorify_communities([community],categories,:feedback)
71+
72+
{:ok,community}
73+
end
74+
end
75+
76+
@doc"""
77+
seed community for feedback
78+
"""
79+
defseed_community(:feedback)do
80+
with{:error,_}<-ORM.find_by(Community,%{raw:"feedback"}),
81+
{:ok,bot}<-seed_bot(),
82+
{:ok,threads}<-seed_threads(:feedback),
83+
{:ok,categories}<-seed_categories_ifneed(bot)do
84+
args=%{
85+
title:"反馈与建议",
86+
desc:"关于本站的建议和反馈请发布在这里,谢谢。",
87+
logo:"#{@oss_endpoint}/icons/cmd/keyboard_logo.png",
88+
raw:"feedback",
89+
user_id:bot.id
90+
}
91+
92+
{:ok,community}=Community|>ORM.create(args)
93+
threadify_communities([community],threads.entries)
94+
tagfy_threads([community],threads.entries,bot,:feedback)
95+
categorify_communities([community],categories,:feedback)
96+
97+
{:ok,community}
98+
# home 不设置分类,比较特殊
99+
end
100+
end
101+
end

‎lib/groupher_server/cms/delegates/Seeds/helper.ex‎

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,109 @@
11
defmoduleGroupherServer.CMS.Delegate.Seeds.Helperdo
22
@moduledocfalse
33

4-
aliasGroupherServer.CMS
5-
aliasCMS.Delegate.SeedsConfig
6-
aliasCMS.Model.Community
4+
importEcto.Query,warn:false
5+
importHelper.Utils,only:[done:1]
6+
importShortMaps
77

8+
aliasGroupherServer.{Accounts,CMS}
9+
aliasCMS.Delegate.{Seeds,SeedsConfig}
10+
aliasCMS.Model.{Community,Thread,Category}
11+
12+
aliasAccounts.Model.User
813
aliasHelper.ORM
914

15+
@categoriesSeeds.Categories.get()
16+
17+
# set threads to given communities
18+
defthreadify_communities(communities,threads)whenis_list(communities)do
19+
Enum.each(communities,fncommunity->
20+
Enum.each(threads,fnthread->
21+
{:ok,_}=CMS.set_thread(%Community{id:community.id},%Thread{id:thread.id})
22+
end)
23+
end)
24+
end
25+
26+
# create tags
27+
28+
deftagfy_threads(communities,threads,bot,type)whenis_list(communities)do
29+
Enum.each(communities,fncommunity->
30+
Enum.each(threads,fnthread->
31+
create_tags(community,thread,bot,type)
32+
end)
33+
end)
34+
end
35+
36+
defcreate_tags(%Community{}=community,%Thread{raw:raw},bot,type)do
37+
thread=raw|>String.to_atom()
38+
39+
Enum.each(
40+
Seeds.Tags.get(community,thread,type),
41+
&CMS.create_article_tag(community,thread,&1,bot)
42+
)
43+
end
44+
45+
# create tags end
46+
47+
defcategorify_communities(communities,categories,:editor)do
48+
categorify_communities(communities,categories,:tools)
49+
end
50+
51+
# set categories to given communities
52+
defcategorify_communities(communities,categories,part)
53+
whenis_list(communities)andis_atom(part)do
54+
the_category=categories.entries|>Enum.find(fncat->cat.raw==Atom.to_string(part)end)
55+
56+
Enum.each(communities,fncommunity->
57+
{:ok,_}=CMS.set_category(%Community{id:community.id},%Category{id:the_category.id})
58+
end)
59+
end
60+
61+
# seed community end
62+
63+
# seed thread
64+
defseed_threads(type)do
65+
threads=Seeds.Threads.get(type)
66+
threads_list=threads|>Enum.map(&&1.raw)
67+
68+
with{:error,_}<-ORM.find_by(Thread,%{raw:"post"})do
69+
threads|>Enum.each(&CMS.create_thread(&1))
70+
end
71+
72+
Thread
73+
|>where([t],t.rawin^threads_list)
74+
|>ORM.paginator(page:1,size:10)
75+
|>done()
76+
end
77+
78+
# seed thread end
79+
80+
defseed_categories_ifneed(bot)do
81+
withtrue<-is_empty_in_db?(Category)do
82+
Enum.each(@categories,&CMS.create_category(&1,bot))
83+
end
84+
85+
ORM.find_all(Category,%{page:1,size:20})
86+
end
87+
88+
defseed_bot()do
89+
caseORM.find(User,1)do
90+
{:ok,user}->
91+
{:ok,user}
92+
93+
{:error,_}->
94+
nickname="cps_bot_2398614_2018"
95+
avatar="https://avatars1.githubusercontent.com/u/6184465?s=460&v=4"
96+
97+
User|>ORM.findby_or_insert(~m(nickname avatar)a,~m(nickname avatar)a)
98+
end
99+
end
100+
101+
# check is the seeds alreay runed
102+
defis_empty_in_db?(queryable)do
103+
{:ok,results}=ORM.find_all(queryable,%{page:1,size:20})
104+
results.total_count==0
105+
end
106+
10107
definsert_community(bot,raw,type)do
11108
type=Atom.to_string(type)
12109
ext=ifEnum.member?(SeedsConfig.svg_icons(),raw),do:"svg",else:"png"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp