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.

Commitd27a043

Browse files
authored
refactor(article-comment): forbid comment when article lock (#321)
* refactor(article-comments): lock article comment logic* refactor(article): fix general meta
1 parent271d095 commitd27a043

File tree

14 files changed

+84
-43
lines changed

14 files changed

+84
-43
lines changed

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ defmodule GroupherServer.CMS do
9292
defdelegatepin_content(queryable,community_id),to:ArticleOperation
9393
defdelegateundo_pin_content(queryable,community_id),to:ArticleOperation
9494

95+
defdelegatelock_article_comment(content),to:ArticleOperation
9596
# defdelegate pin_content(queryable, community_id, thread), to: ArticleOperation
9697
# defdelegate undo_pin_content(queryable, community_id, thread, topic), to: ArticleOperation
9798
# defdelegate undo_pin_content(queryable, community_id, thread), to: ArticleOperation

‎lib/groupher_server/cms/delegates/article_curd.ex‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
1111
aliasGroupherServer.{Accounts,CMS,Delivery,Email,Repo,Statistics}
1212

1313
aliasAccounts.User
14-
aliasCMS.{Author,Community,Delegate,Tag,Topic}
14+
aliasCMS.{Author,Community,Embeds,Delegate,Tag,Topic}
1515

1616
aliasDelegate.ArticleOperation
1717
aliasHelper.{Later,ORM,QueryBuilder}
1818

1919
aliasEcto.Multi
2020

21+
@default_article_metaEmbeds.ArticleMeta.default_meta()
22+
2123
@doc"""
2224
login user read cms content by add views count and viewer record
2325
"""
@@ -69,14 +71,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6971
{:ok,community}<-ORM.find(Community,cid)do
7072
Multi.new()
7173
|>Multi.run(:create_content,fn_,_->
72-
exec_create_content(action.target,attrs,author,community)
74+
do_create_content(action.target,attrs,author,community)
7375
end)
7476
|>Multi.run(:set_community,fn_,%{create_content:content}->
7577
ArticleOperation.set_community(community,thread,content.id)
7678
end)
77-
|>Multi.run(:set_meta,fn_,%{create_content:content}->
78-
ArticleOperation.set_meta(thread,content.id)
79-
end)
8079
|>Multi.run(:set_topic,fn_,%{create_content:content}->
8180
exec_set_topic(thread,content.id,attrs)
8281
end)
@@ -130,8 +129,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
130129
|>Multi.run(:update_content,fn_,_->
131130
ORM.update(content,args)
132131
end)
133-
|>Multi.run(:update_meta,fn_,%{update_content:update_content}->
134-
ArticleOperation.update_meta(update_content,:is_edited)
132+
|>Multi.run(:update_edit_status,fn_,%{update_content:update_content}->
133+
ArticleOperation.update_edit_status(update_content)
135134
end)
136135
|>Multi.run(:update_tag,fn_,_->
137136
# TODO: move it to ArticleOperation moudel
@@ -400,10 +399,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
400399
{:error,[message:"set community",code:ecode(:create_fails)]}
401400
end
402401

403-
defpcreate_content_result({:error,:set_meta,_result,_steps})do
404-
{:error,[message:"set meta info",code:ecode(:create_fails)]}
405-
end
406-
407402
defpcreate_content_result({:error,:set_community_flag,_result,_steps})do
408403
{:error,[message:"set community flag",code:ecode(:create_fails)]}
409404
end
@@ -420,7 +415,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
420415
{:error,[message:"log action",code:ecode(:create_fails)]}
421416
end
422417

423-
defpupdate_content_result({:ok,%{update_meta:result}}),do:{:ok,result}
418+
defpupdate_content_result({:ok,%{update_edit_status:result}}),do:{:ok,result}
424419
defpupdate_content_result({:error,:update_content,result,_steps}),do:{:error,result}
425420
defpupdate_content_result({:error,:update_tag,result,_steps}),do:{:error,result}
426421

@@ -429,12 +424,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
429424
defpcontent_id(:repo,id),do:%{repo_id:id}
430425

431426
# for create content step in Multi.new
432-
defpexec_create_content(target,attrs,%Author{id:aid},%Community{id:cid})do
427+
defpdo_create_content(target,attrs,%Author{id:aid},%Community{id:cid})do
433428
target
434429
|>struct()
435430
|>target.changeset(attrs)
436431
|>Ecto.Changeset.put_change(:author_id,aid)
437432
|>Ecto.Changeset.put_change(:origial_community_id,integerfy(cid))
433+
|>Ecto.Changeset.put_embed(:meta,@default_article_meta)
438434
|>Repo.insert()
439435
end
440436

‎lib/groupher_server/cms/delegates/article_operation.ex‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
255255

256256
defset_topic(_topic,_thread,_content_id),do:{:ok,:pass}
257257

258-
@doc"set meta info"
259-
# embeds_one do not have default option, so we init it with empty map mannully
260-
# see: https://github.com/elixir-ecto/ecto/issues/2634
261-
defset_meta(:post,content_id)do
262-
ORM.update_by(Post,[id:content_id],%{meta:%{}})
263-
end
264-
265-
defset_meta(_,_),do:{:ok,:pass}
266-
267258
@doc"update isEdited meta label if needed"
268-
defupdate_meta(%Post{meta:%Embeds.ArticleMeta{is_edited:false}=meta}=content,:is_edited)do
259+
# TODO: diff history
260+
defupdate_edit_status(%{meta:%Embeds.ArticleMeta{is_edited:false}=meta}=content)do
269261
new_meta=
270262
meta
271263
|>Map.from_struct()
@@ -276,13 +268,29 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
276268
end
277269

278270
# for test or exsiting articles
279-
defupdate_meta(%Post{meta:nil}=content,:is_edited)do
271+
defupdate_edit_status(%{meta:nil}=content)do
280272
new_meta=Embeds.ArticleMeta.default_meta()|>Map.merge(%{is_edited:true})
281273

282274
do_update_meta(content,new_meta)
283275
end
284276

285-
defupdate_meta(content,_),do:{:ok,content}
277+
defupdate_edit_status(content,_),do:{:ok,content}
278+
279+
@doc"lock comment of a article"
280+
# TODO: record it to ArticleLog
281+
deflock_article_comment(
282+
%{meta:%Embeds.ArticleMeta{is_comment_locked:false}=meta}=content
283+
)do
284+
new_meta=
285+
meta
286+
|>Map.from_struct()
287+
|>Map.delete(:id)
288+
|>Map.merge(%{is_comment_locked:true})
289+
290+
do_update_meta(content,new_meta)
291+
end
292+
293+
deflock_article_comment(content),do:{:ok,content}
286294

287295
# TODO: put it into ORM helper
288296
defpdo_update_meta(%{meta:_}=content,meta_params)do

‎lib/groupher_server/cms/embeds/article_meta.ex‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
33
general article meta info for article-like content, like post, job, works ...
44
"""
55
useEcto.Schema
6+
importEcto.Changeset
7+
8+
@optional_fields~w(is_edited is_comment_locked is_reported)a
69

710
@default_meta%{
811
is_edited:false,
9-
forbid_comment:false,
12+
is_comment_locked:false,
1013
is_reported:false
11-
# linkedPostsCount: 0,
12-
# linkedJobsCount: 0,
13-
# linkedWorksCount: 0,
14-
# reaction: %{
15-
# rocketCount: 0,
16-
# heartCount: 0,
17-
# }
1814
}
1915

2016
@doc"for test usage"
2117
defdefault_meta(),do:@default_meta
2218

2319
embedded_schemado
2420
field(:is_edited,:boolean,default:false)
25-
field(:forbid_comment,:boolean,default:false)
21+
field(:is_comment_locked,:boolean,default:false)
2622
field(:is_reported,:boolean,default:false)
2723
end
24+
25+
defchangeset(struct,params)do
26+
struct
27+
|>cast(params,@optional_fields)
28+
end
2829
end

‎lib/groupher_server/cms/job.ex‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule GroupherServer.CMS.Job do
1111

1212
aliasCMS.{
1313
Author,
14+
Embeds,
1415
ArticleComment,
1516
Community,
1617
JobFavorite,
@@ -36,6 +37,9 @@ defmodule GroupherServer.CMS.Job do
3637
field(:body,:string)
3738
belongs_to(:author,Author)
3839
field(:views,:integer,default:0)
40+
41+
embeds_one(:meta,Embeds.ArticleMeta,on_replace::update)
42+
3943
field(:link_addr,:string)
4044
field(:copy_right,:string)
4145

@@ -89,13 +93,15 @@ defmodule GroupherServer.CMS.Job do
8993
job
9094
|>cast(attrs,@optional_fields++@required_fields)
9195
|>validate_required(@required_fields)
96+
|>cast_embed(:meta,required:false,with:&Embeds.ArticleMeta.changeset/2)
9297
|>generl_changeset
9398
end
9499

95100
@docfalse
96101
defupdate_changeset(%Job{}=job,attrs)do
97102
job
98103
|>cast(attrs,@optional_fields++@required_fields)
104+
# |> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
99105
|>generl_changeset
100106
end
101107

‎lib/groupher_server/cms/post.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule GroupherServer.CMS.Post do
1313
Embeds,
1414
Author,
1515
ArticleComment,
16-
ArticleCommentParticipator,
1716
Community,
1817
PostComment,
1918
PostCommunityFlag,
@@ -105,6 +104,7 @@ defmodule GroupherServer.CMS.Post do
105104
post
106105
|>cast(attrs,@optional_fields++@required_fields)
107106
|>validate_required(@required_fields)
107+
|>cast_embed(:meta,required:false,with:&Embeds.ArticleMeta.changeset/2)
108108
|>generl_changeset
109109
end
110110

‎lib/groupher_server/cms/repo.ex‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule GroupherServer.CMS.Repo do
99

1010
aliasCMS.{
1111
Author,
12+
Embeds,
1213
Community,
1314
RepoContributor,
1415
RepoFavorite,
@@ -48,6 +49,9 @@ defmodule GroupherServer.CMS.Repo do
4849
embeds_many(:contributors,RepoContributor,on_replace::delete)
4950

5051
field(:views,:integer,default:0)
52+
53+
embeds_one(:meta,Embeds.ArticleMeta,on_replace::update)
54+
5155
belongs_to(:author,Author)
5256
has_many(:community_flags,{"repos_communities_flags",RepoCommunityFlag})
5357

@@ -87,13 +91,15 @@ defmodule GroupherServer.CMS.Repo do
8791
repo
8892
|>cast(attrs,@optional_fields++@required_fields)
8993
|>validate_required(@required_fields)
94+
|>cast_embed(:meta,required:false,with:&Embeds.ArticleMeta.changeset/2)
9095
|>generl_changeset
9196
end
9297

9398
@docfalse
9499
defupdate_changeset(%Repo{}=repo,attrs)do
95100
repo
96101
|>cast(attrs,@optional_fields++@required_fields)
102+
# |> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
97103
|>generl_changeset
98104
end
99105

‎lib/groupher_server_web/schema/cms/cms_types.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
428428
@desc"article meta info"
429429
object:article_metado
430430
field(:is_edited,:boolean)
431-
field(:forbid_comment,:boolean)
431+
field(:is_comment_locked,:boolean)
432432
# field(:isReported, :boolean)
433433
# field(:linked_posts_count, :integer)
434434
# field(:linked_jobs_count, :integer)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmoduleGroupherServer.Repo.Migrations.AddMetaToArticleContentdo
2+
useEcto.Migration
3+
4+
defchangedo
5+
altertable(:cms_jobs)do
6+
add(:meta,:map)
7+
end
8+
9+
altertable(:cms_repos)do
10+
add(:meta,:map)
11+
end
12+
end
13+
end

‎test/groupher_server/accounts/published_contents_test.exs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ defmodule GroupherServer.Test.Accounts.PublishedContents do
6767
assertresults.total_count==0
6868
end
6969

70+
@tag:wip
7071
test"user can get paged published jobs",~m(user user2 community community2)ado
7172
pub_jobs=
7273
Enum.reduce(1..@publish_count,[],fn_,acc->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp