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

Commit a361b9f

Browse files
committed
refactor(drop-topic): wip
1 parent 91ef100 commit a361b9f

File tree

8 files changed

+26
-196
lines changed

8 files changed

+26
-196
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ defmodule GroupherServer.CMS do
4444
defdelegate create_tag(community, thread, attrs, user), to: CommunityCURD
4545
defdelegate update_tag(attrs), to: CommunityCURD
4646
defdelegate get_tags(community, thread), to: CommunityCURD
47-
defdelegate get_tags(community, thread, topic), to: CommunityCURD
4847
defdelegate get_tags(filter), to: CommunityCURD
4948
# >> wiki & cheatsheet (sync with github)
5049
defdelegate get_wiki(community), to: CommunitySync
@@ -87,22 +86,15 @@ defmodule GroupherServer.CMS do
8786
# ArticleOperation
8887
# >> set flag on article, like: pin / unpin article
8988
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
90-
defdelegate pin_content(queryable, community_id, topic), to: ArticleOperation
91-
defdelegate undo_pin_content(queryable, community_id, topic), to: ArticleOperation
9289
defdelegate pin_content(queryable, community_id), to: ArticleOperation
9390
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation
9491

9592
defdelegate lock_article_comment(content), to: ArticleOperation
96-
# defdelegate pin_content(queryable, community_id, thread), to: ArticleOperation
97-
# defdelegate undo_pin_content(queryable, community_id, thread, topic), to: ArticleOperation
98-
# defdelegate undo_pin_content(queryable, community_id, thread), to: ArticleOperation
9993

10094
# >> tag: set / unset
10195
defdelegate set_tag(thread, tag, content_id), to: ArticleOperation
10296
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
103-
defdelegate set_refined_tag(community, thread, topic, content_id), to: ArticleOperation
10497
defdelegate set_refined_tag(community, thread, content_id), to: ArticleOperation
105-
defdelegate unset_refined_tag(community, thread, topic, content_id), to: ArticleOperation
10698
defdelegate unset_refined_tag(community, thread, content_id), to: ArticleOperation
10799
# >> community: set / unset
108100
defdelegate set_community(community, thread, content_id), to: ArticleOperation

lib/groupher_server/cms/delegates/article_curd.ex

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

1313
alias Accounts.User
14-
alias CMS.{Author, Community, Embeds, Delegate, Tag, Topic}
14+
alias CMS.{Author, Community, Embeds, Delegate, Tag}
1515

1616
alias Delegate.ArticleOperation
1717
alias Helper.{Later, ORM, QueryBuilder}
@@ -76,9 +76,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
7676
|> Multi.run(:set_community, fn _, %{create_content: content} ->
7777
ArticleOperation.set_community(community, thread, content.id)
7878
end)
79-
|> Multi.run(:set_topic, fn _, %{create_content: content} ->
80-
exec_set_topic(thread, content.id, attrs)
81-
end)
8279
|> Multi.run(:set_community_flag, fn _, %{create_content: content} ->
8380
exec_set_community_flag(community, content, action)
8481
end)
@@ -294,12 +291,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
294291
{:ok, pined_content} =
295292
CMS.PinedPost
296293
|> join(:inner, [p], c in assoc(p, :community))
297-
|> join(:inner, [p], t in assoc(p, :topic))
298294
|> join(:inner, [p], content in assoc(p, :post))
299-
|> where(
300-
[p, c, t, content],
301-
c.raw == ^community and t.raw == ^Map.get(filter, :topic, "posts")
302-
)
295+
|> where([p, c, t, content], c.raw == ^community)
303296
|> select([p, c, t, content], content)
304297
# 10 pined contents per community/thread, at most
305298
|> ORM.paginater(%{page: 1, size: 10})
@@ -348,7 +341,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
348341
defp should_add_pin?(%{page: 1, tag: :all, sort: :desc_inserted, read: :all} = filter) do
349342
filter
350343
|> Map.keys()
351-
|> Enum.reject(fn x -> x in [:community, :tag, :sort, :read, :topic, :page, :size] end)
344+
|> Enum.reject(fn x -> x in [:community, :tag, :sort, :read, :page, :size] end)
352345
|> case do
353346
[] -> {:ok, :pass}
354347
_ -> {:error, :pass}
@@ -403,10 +396,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
403396
{:error, [message: "set community flag", code: ecode(:create_fails)]}
404397
end
405398

406-
defp create_content_result({:error, :set_topic, _result, _steps}) do
407-
{:error, [message: "set topic", code: ecode(:create_fails)]}
408-
end
409-
410399
defp create_content_result({:error, :set_tag, result, _steps}) do
411400
{:error, result}
412401
end
@@ -434,15 +423,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
434423
|> Repo.insert()
435424
end
436425

437-
defp exec_set_topic(thread, id, %{topic: topic}) do
438-
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
439-
end
440-
441-
# if topic is not provide, use posts as default
442-
defp exec_set_topic(thread, id, _attrs) do
443-
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
444-
end
445-
446426
defp exec_set_tag(thread, id, %{tags: tags}) do
447427
try do
448428
Enum.each(tags, fn tag ->

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
1818
JobCommunityFlag,
1919
RepoCommunityFlag,
2020
Tag,
21-
Topic,
2221
PinedPost,
2322
PinedJob,
2423
PinedRepo
@@ -27,13 +26,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2726
alias GroupherServer.CMS.Repo, as: CMSRepo
2827
alias GroupherServer.Repo
2928

30-
def pin_content(%Post{id: post_id}, %Community{id: community_id}, topic) do
31-
with {:ok, %{id: topic_id}} <- ORM.find_by(Topic, %{raw: topic}),
32-
{:ok, pined} <-
29+
def pin_content(%Post{id: post_id}, %Community{id: community_id}) do
30+
with {:ok, pined} <-
3331
ORM.findby_or_insert(
3432
PinedPost,
35-
~m(post_id community_id topic_id)a,
36-
~m(post_id community_id topic_id)a
33+
~m(post_id community_id)a,
34+
~m(post_id community_id)a
3735
) do
3836
Post |> ORM.find(pined.post_id)
3937
end
@@ -55,9 +53,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
5553
end
5654
end
5755

58-
def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}, topic) do
59-
with {:ok, %{id: topic_id}} <- ORM.find_by(Topic, %{raw: topic}),
60-
{:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id topic_id)a),
56+
def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}) do
57+
with {:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id)a),
6158
{:ok, deleted} <- ORM.delete(pined) do
6259
Post |> ORM.find(deleted.post_id)
6360
end
@@ -177,55 +174,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
177174
@doc """
178175
set refined_tag to common content
179176
"""
180-
def set_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
177+
def set_refined_tag(%Community{id: community_id}, thread, content_id) do
181178
with {:ok, action} <- match_action(thread, :tag),
182179
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
183-
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
184180
{:ok, tag} <-
185181
ORM.find_by(action.reactor, %{
186182
title: "refined",
187-
community_id: community_id,
188-
topic_id: topic.id
183+
community_id: community_id
189184
}) do
190185
update_content_tag(content, tag)
191186
end
192187
end
193188

194-
def set_refined_tag(%Community{id: community_id}, thread, content_id) do
195-
with {:ok, action} <- match_action(thread, :tag),
196-
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
197-
{:ok, tag} <-
198-
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
199-
update_content_tag(content, tag)
200-
end
201-
end
202-
203189
@doc """
204190
unset refined_tag to common content
205191
"""
206-
def unset_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
192+
def unset_refined_tag(%Community{id: community_id}, thread, content_id) do
207193
with {:ok, action} <- match_action(thread, :tag),
208194
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
209-
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
210195
{:ok, tag} <-
211196
ORM.find_by(action.reactor, %{
212197
title: "refined",
213-
community_id: community_id,
214-
topic_id: topic.id
198+
community_id: community_id
215199
}) do
216200
update_content_tag(content, tag, :drop)
217201
end
218202
end
219203

220-
def unset_refined_tag(%Community{id: community_id}, thread, content_id) do
221-
with {:ok, action} <- match_action(thread, :tag),
222-
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
223-
{:ok, tag} <-
224-
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
225-
update_content_tag(content, tag, :drop)
226-
end
227-
end
228-
229204
defp update_content_tag(content, %Tag{} = tag, opt \\ :add) do
230205
new_tags = if opt == :add, do: content.tags ++ [tag], else: content.tags -- [tag]
231206

@@ -235,26 +210,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
235210
|> Repo.update()
236211
end
237212

238-
@doc """
239-
set topic only for post
240-
"""
241-
def set_topic(%Topic{title: title}, :post, content_id) do
242-
with {:ok, content} <- ORM.find(Post, content_id, preload: :topics),
243-
{:ok, topic} <-
244-
ORM.findby_or_insert(Topic, %{title: title}, %{
245-
title: title,
246-
thread: "post",
247-
raw: title
248-
}) do
249-
content
250-
|> Ecto.Changeset.change()
251-
|> Ecto.Changeset.put_assoc(:topics, content.topics ++ [topic])
252-
|> Repo.update()
253-
end
254-
end
255-
256-
def set_topic(_topic, _thread, _content_id), do: {:ok, :pass}
257-
258213
@doc "update isEdited meta label if needed"
259214
# TODO: diff history
260215
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content) do

lib/groupher_server/cms/delegates/community_curd.ex

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
1818
CommunityEditor,
1919
CommunitySubscriber,
2020
Tag,
21-
Topic,
2221
Thread
2322
}
2423

@@ -63,11 +62,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
6362
def create_tag(%Community{id: community_id}, thread, attrs, %Accounts.User{id: user_id}) do
6463
with {:ok, action} <- match_action(thread, :tag),
6564
{:ok, author} <- ensure_author_exists(%Accounts.User{id: user_id}),
66-
{:ok, _community} <- ORM.find(Community, community_id),
67-
{:ok, topic} = find_or_insert_topic(attrs) do
65+
{:ok, _community} <- ORM.find(Community, community_id) do
6866
attrs =
6967
attrs
70-
|> Map.merge(%{author_id: author.id, topic_id: topic.id, community_id: community_id})
68+
|> Map.merge(%{author_id: author.id, community_id: community_id})
7169
|> map_atom_value(:string)
7270
|> Map.merge(%{thread: thread |> to_string |> String.downcase()})
7371

@@ -78,10 +76,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
7876
def update_tag(%{id: _id} = attrs) do
7977
~m(id title color)a = attrs |> map_atom_value(:string)
8078

81-
with {:ok, %{id: topic_id}} = find_or_insert_topic(attrs) do
82-
Tag
83-
|> ORM.find_update(~m(id title color topic_id)a)
84-
end
79+
Tag
80+
|> ORM.find_update(~m(id title color)a)
8581
end
8682

8783
@doc """
@@ -116,31 +112,12 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
116112
@doc """
117113
get tags belongs to a community / thread
118114
"""
119-
def get_tags(%Community{id: community_id}, thread, topic) when not is_nil(community_id) do
120-
# thread = thread |> to_string |> String.upcase()
121-
# topic = topic |> to_string |> String.upcase()
115+
def get_tags(%Community{raw: community_raw}, thread) when not is_nil(community_raw) do
122116
thread = thread |> to_string |> String.downcase()
123-
topic = topic |> to_string |> String.downcase()
124117

125118
Tag
126119
|> join(:inner, [t], c in assoc(t, :community))
127-
|> join(:inner, [t], cp in assoc(t, :topic))
128-
|> where([t, c, cp], c.id == ^community_id and t.thread == ^thread and cp.title == ^topic)
129-
|> distinct([t], t.title)
130-
|> Repo.all()
131-
|> done()
132-
end
133-
134-
def get_tags(%Community{raw: community_raw}, thread, topic) when not is_nil(community_raw) do
135-
# thread = thread |> to_string |> String.upcase()
136-
# topic = topic |> to_string |> String.upcase()
137-
thread = thread |> to_string |> String.downcase()
138-
topic = topic |> to_string |> String.downcase()
139-
140-
Tag
141-
|> join(:inner, [t], c in assoc(t, :community))
142-
|> join(:inner, [t], cp in assoc(t, :topic))
143-
|> where([t, c, cp], c.raw == ^community_raw and t.thread == ^thread and cp.title == ^topic)
120+
|> where([t, c], c.raw == ^community_raw and t.thread == ^thread)
144121
|> distinct([t], t.title)
145122
|> Repo.all()
146123
|> done()
@@ -248,25 +225,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
248225
end
249226
end
250227

251-
defp find_or_insert_topic(%{topic: title} = attrs) when is_binary(title) do
252-
title = title |> to_string() |> String.downcase()
253-
thread = attrs.thread |> to_string() |> String.downcase()
254-
255-
ORM.findby_or_insert(Topic, %{title: title}, %{
256-
title: title,
257-
thread: thread,
258-
raw: title
259-
})
260-
end
261-
262-
defp find_or_insert_topic(%{thread: thread}) do
263-
find_or_insert_topic(%{topic: "posts", thread: thread})
264-
end
265-
266-
defp find_or_insert_topic(_attrs) do
267-
find_or_insert_topic(%{topic: "posts", thread: :post})
268-
end
269-
270228
defp load_community_members(%Community{id: id}, queryable, %{page: page, size: size} = filters)
271229
when not is_nil(id) do
272230
queryable

lib/groupher_server/cms/pined_post.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ defmodule GroupherServer.CMS.PinedPost do
66
import Ecto.Changeset
77

88
alias GroupherServer.CMS
9-
alias CMS.{Community, Post, Topic}
9+
alias CMS.{Community, Post}
1010

1111
@required_fields ~w(post_id community_id)a
12-
@optional_fields ~w(topic_id)a
1312

1413
@type t :: %PinedPost{}
1514
schema "pined_posts" do
1615
belongs_to(:post, Post, foreign_key: :post_id)
1716
belongs_to(:community, Community, foreign_key: :community_id)
18-
belongs_to(:topic, Topic, foreign_key: :topic_id)
1917

2018
timestamps(type: :utc_datetime)
2119
end
2220

2321
@doc false
2422
def changeset(%PinedPost{} = pined_post, attrs) do
2523
pined_post
26-
|> cast(attrs, @optional_fields ++ @required_fields)
24+
|> cast(attrs, @required_fields)
2725
|> validate_required(@required_fields)
2826
|> foreign_key_constraint(:post_id)
2927
|> foreign_key_constraint(:community_id)
30-
|> unique_constraint(:pined_posts, name: :pined_posts_post_id_community_id_topic_id_index)
28+
|> unique_constraint(:pined_posts, name: :pined_posts_post_id_community_id_index)
3129
end
3230
end

lib/groupher_server/cms/post.ex

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ defmodule GroupherServer.CMS.Post do
2020
PostFavorite,
2121
PostStar,
2222
PostViewer,
23-
Tag,
24-
Topic
23+
Tag
2524
}
2625

2726
alias Helper.HTML
@@ -78,16 +77,6 @@ defmodule GroupherServer.CMS.Post do
7877
on_replace: :delete
7978
)
8079

81-
many_to_many(
82-
:topics,
83-
Topic,
84-
join_through: "posts_topics",
85-
join_keys: [post_id: :id, topic_id: :id],
86-
# :delete_all will only remove data from the join source
87-
on_delete: :delete_all,
88-
on_replace: :delete
89-
)
90-
9180
belongs_to(:origial_community, Community)
9281

9382
many_to_many(

0 commit comments

Comments
 (0)