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

Commit e1bb1b8

Browse files
authored
refactor(cms): remove post topic concept (#326)
* refactor(drop-topic): remove from table * refactor(drop-topic): wip * refactor(drop-topic): wip * chore: wip * chore: wip
1 parent 1de1a89 commit e1bb1b8

35 files changed

+118
-671
lines changed

lib/groupher_server/cms/cms.ex

-8
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

+4-24
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,13 +291,9 @@ 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-
)
303-
|> select([p, c, t, content], content)
295+
|> where([p, c, content], c.raw == ^community)
296+
|> select([p, c, content], content)
304297
# 10 pined contents per community/thread, at most
305298
|> ORM.paginater(%{page: 1, size: 10})
306299
|> done()
@@ -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

+10-55
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

+6-49
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

@@ -59,15 +58,13 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5958
@doc """
6059
create a Tag base on type: post / tuts ...
6160
"""
62-
# TODO: change to create_tag(community, thread, attrs, ....)
6361
def create_tag(%Community{id: community_id}, thread, attrs, %Accounts.User{id: user_id}) do
6462
with {:ok, action} <- match_action(thread, :tag),
6563
{: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
64+
{:ok, _community} <- ORM.find(Community, community_id) do
6865
attrs =
6966
attrs
70-
|> Map.merge(%{author_id: author.id, topic_id: topic.id, community_id: community_id})
67+
|> Map.merge(%{author_id: author.id, community_id: community_id})
7168
|> map_atom_value(:string)
7269
|> Map.merge(%{thread: thread |> to_string |> String.downcase()})
7370

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

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
78+
Tag
79+
|> ORM.find_update(~m(id title color)a)
8580
end
8681

8782
@doc """
@@ -116,31 +111,12 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
116111
@doc """
117112
get tags belongs to a community / thread
118113
"""
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()
114+
def get_tags(%Community{raw: community_raw}, thread) when not is_nil(community_raw) do
122115
thread = thread |> to_string |> String.downcase()
123-
topic = topic |> to_string |> String.downcase()
124116

125117
Tag
126118
|> 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)
119+
|> where([t, c], c.raw == ^community_raw and t.thread == ^thread)
144120
|> distinct([t], t.title)
145121
|> Repo.all()
146122
|> done()
@@ -248,25 +224,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
248224
end
249225
end
250226

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-
270227
defp load_community_members(%Community{id: id}, queryable, %{page: page, size: size} = filters)
271228
when not is_nil(id) do
272229
queryable

0 commit comments

Comments
 (0)