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

Commit fd939b5

Browse files
committed
feat: add community flags to repos / videos / jobs
1 parent 479a4b2 commit fd939b5

21 files changed

+317
-108
lines changed

lib/mastani_server/cms/delegates/article_curd.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
3030
defp flag_query(queryable, filter, flag \\ %{}) do
3131
flag = %{pin: false, trash: false} |> Map.merge(flag)
3232

33-
# TODO: remove case judge ?
33+
# NOTE: this case judge is used for test case
3434
case filter |> Map.has_key?(:community) do
3535
true ->
3636
queryable
@@ -40,7 +40,6 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
4040
|> where([q, f, c], c.raw == ^filter.community)
4141

4242
false ->
43-
# for runing tests usage, no flag when db_insert
4443
queryable
4544
end
4645
end
@@ -128,7 +127,7 @@ defmodule MastaniServer.CMS.Delegate.ArticleCURD do
128127
{:ok, "pass"}
129128
end
130129

131-
# Repo.insert(%CMS.PostCommunityFlags{post_id: content.id, community_id: community.id, })
130+
# Repo.insert(%CMS.PostCommunityFlag{post_id: content.id, community_id: community.id, })
132131
end)
133132
|> Multi.run(:set_tag, fn %{add_content_author: content} ->
134133
case attrs |> Map.has_key?(:tags) do

lib/mastani_server/cms/delegates/article_operation.ex

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,73 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
55
import MastaniServer.CMS.Utils.Matcher
66
import Ecto.Query, warn: false
77
import Helper.ErrorCode
8-
# import ShortMaps
8+
import ShortMaps
99

1010
alias Helper.ORM
1111
alias MastaniServer.Accounts.User
12-
alias MastaniServer.CMS.{Community, PinState, Post, PostCommunityFlags, Tag}
12+
13+
alias MastaniServer.CMS.{
14+
Community,
15+
PinState,
16+
Post,
17+
PostCommunityFlag,
18+
Job,
19+
JobCommunityFlag,
20+
RepoCommunityFlag,
21+
Video,
22+
VideoCommunityFlag,
23+
Tag
24+
}
25+
26+
alias MastaniServer.CMS.Repo, as: CMSRepo
1327
alias MastaniServer.Repo
1428

1529
@doc """
1630
pin / unpin, trash / untrash articles
1731
"""
18-
def set_community_flags(%Post{id: post_id}, community_id, attrs) do
19-
with {:ok, post} <- ORM.find(Post, post_id),
32+
def set_community_flags(%Post{id: _} = content, community_id, attrs),
33+
do: do_set_flag(content, community_id, attrs)
34+
35+
def set_community_flags(%Job{id: _} = content, community_id, attrs),
36+
do: do_set_flag(content, community_id, attrs)
37+
38+
def set_community_flags(%CMSRepo{id: _} = content, community_id, attrs),
39+
do: do_set_flag(content, community_id, attrs)
40+
41+
def set_community_flags(%Video{id: _} = content, community_id, attrs),
42+
do: do_set_flag(content, community_id, attrs)
43+
44+
defp do_set_flag(content, community_id, attrs) do
45+
with {:ok, content} <- ORM.find(content.__struct__, content.id),
2046
{:ok, community} <- ORM.find(Community, community_id),
21-
{:ok, _} <- insert_flag_record(post, community_id, attrs) do
22-
ORM.find(Post, post.id)
47+
{:ok, record} <- insert_flag_record(content, community_id, attrs) do
48+
{:ok, struct(content, %{pin: record.pin, trash: record.trash})}
2349
end
2450
end
2551

26-
defp insert_flag_record(%Post{id: id}, community_id, attrs) do
27-
clauses = %{
28-
post_id: id,
29-
community_id: community_id
30-
}
52+
defp insert_flag_record(%Post{id: post_id}, community_id, attrs) do
53+
clauses = ~m(post_id community_id)a
54+
PostCommunityFlag |> ORM.upsert_by(clauses, Map.merge(attrs, clauses))
55+
end
56+
57+
defp insert_flag_record(%Job{id: job_id}, community_id, attrs) do
58+
clauses = ~m(job_id community_id)a
59+
JobCommunityFlag |> ORM.upsert_by(clauses, Map.merge(attrs, clauses))
60+
end
3161

32-
attrs = attrs |> Map.merge(clauses)
62+
defp insert_flag_record(%CMSRepo{id: repo_id}, community_id, attrs) do
63+
clauses = ~m(repo_id community_id)a
64+
RepoCommunityFlag |> ORM.upsert_by(clauses, Map.merge(attrs, clauses))
65+
end
3366

34-
PostCommunityFlags |> ORM.upsert_by(clauses, attrs)
67+
defp insert_flag_record(%Video{id: video_id}, community_id, attrs) do
68+
clauses = ~m(video_id community_id)a
69+
VideoCommunityFlag |> ORM.upsert_by(clauses, Map.merge(attrs, clauses))
3570
end
3671

72+
@doc """
73+
set content to diffent community
74+
"""
3775
def set_community(%Community{id: community_id}, thread, content_id) when valid_thread(thread) do
3876
with {:ok, action} <- match_action(thread, :community),
3977
{:ok, content} <- ORM.find(action.target, content_id, preload: :communities),

lib/mastani_server/cms/job.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule MastaniServer.CMS.Job do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7-
alias MastaniServer.CMS.{Author, Community, JobFavorite, Tag}
7+
alias MastaniServer.CMS.{Author, Community, JobFavorite, JobCommunityFlag, Tag}
88

99
@required_fields ~w(title company company_logo location body digest length)a
1010
@optional_fields ~w(link_addr link_source min_education)a
@@ -35,6 +35,12 @@ defmodule MastaniServer.CMS.Job do
3535
field(:digest, :string)
3636
field(:length, :integer)
3737

38+
has_many(:community_flags, {"jobs_communities_flags", JobCommunityFlag})
39+
40+
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
41+
field(:pin, :boolean, default_value: false, virtual: true)
42+
field(:trash, :boolean, default_value: false, virtual: true)
43+
3844
# has_many(:comments, {"jobs_comments", JobComment})
3945
has_many(:favorites, {"jobs_favorites", JobFavorite})
4046
# has_many(:stars, {"posts_stars", PostStar})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule MastaniServer.CMS.JobCommunityFlag do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
alias Helper.Certification
8+
alias MastaniServer.Accounts
9+
alias MastaniServer.CMS.{Community, Job}
10+
11+
@required_fields ~w(job_id community_id)a
12+
@optional_fields ~w(pin trash)a
13+
14+
@type t :: %JobCommunityFlag{}
15+
16+
schema "jobs_communities_flags" do
17+
belongs_to(:job, Job, foreign_key: :job_id)
18+
belongs_to(:community, Community, foreign_key: :community_id)
19+
20+
field(:pin, :boolean)
21+
field(:trash, :boolean)
22+
23+
timestamps(type: :utc_datetime)
24+
end
25+
26+
@doc false
27+
def changeset(%JobCommunityFlag{} = job_community_flag, attrs) do
28+
job_community_flag
29+
|> cast(attrs, @optional_fields ++ @required_fields)
30+
|> validate_required(@required_fields)
31+
|> foreign_key_constraint(:job_id)
32+
|> foreign_key_constraint(:community_id)
33+
|> unique_constraint(:job_id, name: :jobs_communities_flags_job_id_community_id_index)
34+
end
35+
end

lib/mastani_server/cms/post.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule MastaniServer.CMS.Post do
99
Author,
1010
Community,
1111
PostComment,
12-
PostCommunityFlags,
12+
PostCommunityFlag,
1313
PostFavorite,
1414
PostStar,
1515
Tag
@@ -27,8 +27,7 @@ defmodule MastaniServer.CMS.Post do
2727
field(:length, :integer)
2828
field(:views, :integer, default: 0)
2929

30-
# has_many(:pins, {"posts_pins", PostPin})
31-
has_many(:community_flags, {"posts_communities_flags", PostCommunityFlags})
30+
has_many(:community_flags, {"posts_communities_flags", PostCommunityFlag})
3231

3332
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
3433
field(:pin, :boolean, default_value: false, virtual: true)

lib/mastani_server/cms/post_community_flag.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule MastaniServer.CMS.PostCommunityFlags do
1+
defmodule MastaniServer.CMS.PostCommunityFlag do
22
@moduledoc false
33
alias __MODULE__
44

@@ -11,7 +11,7 @@ defmodule MastaniServer.CMS.PostCommunityFlags do
1111
@required_fields ~w(post_id community_id)a
1212
@optional_fields ~w(pin trash refined)a
1313

14-
@type t :: %PostCommunityFlags{}
14+
@type t :: %PostCommunityFlag{}
1515

1616
schema "posts_communities_flags" do
1717
belongs_to(:post, Post, foreign_key: :post_id)
@@ -25,8 +25,8 @@ defmodule MastaniServer.CMS.PostCommunityFlags do
2525
end
2626

2727
@doc false
28-
def changeset(%PostCommunityFlags{} = post_community_flags, attrs) do
29-
post_community_flags
28+
def changeset(%PostCommunityFlag{} = post_community_flag, attrs) do
29+
post_community_flag
3030
|> cast(attrs, @optional_fields ++ @required_fields)
3131
|> validate_required(@required_fields)
3232
|> foreign_key_constraint(:post_id)

lib/mastani_server/cms/repo.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule MastaniServer.CMS.Repo do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7-
alias MastaniServer.CMS.{Author, Community, RepoBuilder, Tag}
7+
alias MastaniServer.CMS.{Author, Community, RepoBuilder, RepoCommunityFlag, Tag}
88

99
@required_fields ~w(repo_name desc readme language producer producer_link repo_link repo_star_count repo_fork_count repo_watch_count)a
1010
@optional_fields ~w(views pin trash last_fetch_time)
@@ -26,6 +26,10 @@ defmodule MastaniServer.CMS.Repo do
2626
field(:repo_watch_count, :integer)
2727

2828
field(:views, :integer, default: 0)
29+
30+
has_many(:community_flags, {"repos_communities_flags", RepoCommunityFlag})
31+
32+
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
2933
field(:pin, :boolean, default_value: false)
3034
field(:trash, :boolean, default_value: false)
3135

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule MastaniServer.CMS.RepoCommunityFlag do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
alias Helper.Certification
8+
alias MastaniServer.Accounts
9+
alias MastaniServer.CMS.{Community, Repo}
10+
11+
@required_fields ~w(repo_id community_id)a
12+
@optional_fields ~w(pin trash)a
13+
14+
@type t :: %RepoCommunityFlag{}
15+
16+
schema "repos_communities_flags" do
17+
belongs_to(:repo, Repo, foreign_key: :repo_id)
18+
belongs_to(:community, Community, foreign_key: :community_id)
19+
20+
field(:pin, :boolean)
21+
field(:trash, :boolean)
22+
23+
timestamps(type: :utc_datetime)
24+
end
25+
26+
@doc false
27+
def changeset(%RepoCommunityFlag{} = repo_community_flag, attrs) do
28+
repo_community_flag
29+
|> cast(attrs, @optional_fields ++ @required_fields)
30+
|> validate_required(@required_fields)
31+
|> foreign_key_constraint(:repo_id)
32+
|> foreign_key_constraint(:community_id)
33+
|> unique_constraint(:repo_id, name: :repos_communities_flags_repo_id_community_id_index)
34+
end
35+
end

lib/mastani_server/cms/utils/matcher.ex

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ defmodule MastaniServer.CMS.Utils.Matcher do
2020
Community,
2121
PostCommentLike,
2222
PostCommentDislike,
23-
PostCommunityFlags
23+
PostCommunityFlag,
24+
JobCommunityFlag,
25+
RepoCommunityFlag,
26+
VideoCommunityFlag
2427
}
2528

2629
@support_thread [:post, :video, :repo, :job]
@@ -47,7 +50,7 @@ defmodule MastaniServer.CMS.Utils.Matcher do
4750
def match_action(:post, :tag), do: {:ok, %{target: Post, reactor: Tag}}
4851

4952
def match_action(:post, :community),
50-
do: {:ok, %{target: Post, reactor: Community, flag: PostCommunityFlags}}
53+
do: {:ok, %{target: Post, reactor: Community, flag: PostCommunityFlag}}
5154

5255
def match_action(:post, :comment),
5356
do: {:ok, %{target: Post, reactor: PostComment, preload: :author}}
@@ -59,14 +62,19 @@ defmodule MastaniServer.CMS.Utils.Matcher do
5962
do: {:ok, %{target: PostComment, reactor: PostCommentDislike}}
6063

6164
# videos ...
62-
def match_action(:video, :community), do: {:ok, %{target: Video, reactor: Community}}
65+
def match_action(:video, :community),
66+
do: {:ok, %{target: Video, reactor: Community, flag: VideoCommunityFlag}}
6367

6468
# repos ...
65-
def match_action(:repo, :community), do: {:ok, %{target: Repo, reactor: Community}}
69+
def match_action(:repo, :community),
70+
do: {:ok, %{target: Repo, reactor: Community, flag: RepoCommunityFlag}}
6671

6772
# jobs ...
6873
def match_action(:job, :self), do: {:ok, %{target: Job, reactor: Job, preload: :author}}
69-
def match_action(:job, :community), do: {:ok, %{target: Job, reactor: Community}}
74+
75+
def match_action(:job, :community),
76+
do: {:ok, %{target: Job, reactor: Community, flag: JobCommunityFlags}}
77+
7078
def match_action(:job, :star), do: {:ok, %{target: Job, reactor: JobStar, preload: :user}}
7179
def match_action(:job, :tag), do: {:ok, %{target: Job, reactor: Tag}}
7280

lib/mastani_server/cms/video.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule MastaniServer.CMS.Video do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7-
alias MastaniServer.CMS.{Author, Community, Tag}
7+
alias MastaniServer.CMS.{Author, Community, VideoCommunityFlag, Tag}
88

99
@required_fields ~w(title poster desc duration duration_sec source)a
1010
@optional_fields ~w(link original_author original_author_link publish_at pin trash)
@@ -24,6 +24,10 @@ defmodule MastaniServer.CMS.Video do
2424
field(:original_author_link, :string)
2525

2626
field(:views, :integer, default: 0)
27+
28+
has_many(:community_flags, {"videos_communities_flags", VideoCommunityFlag})
29+
30+
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
2731
field(:pin, :boolean, default_value: false)
2832
field(:trash, :boolean, default_value: false)
2933

0 commit comments

Comments
 (0)