This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy patharticle_community.ex
227 lines (193 loc) · 7.78 KB
/
article_community.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
@moduledoc """
set / unset operations for Article-like resource
"""
import GroupherServer.CMS.Helper.Matcher
import Ecto.Query, warn: false
import Helper.ErrorCode
import Helper.Utils, only: [strip_struct: 1, done: 1]
import GroupherServer.CMS.Helper.Matcher
alias Helper.Types, as: T
alias Helper.ORM
alias GroupherServer.{CMS, Repo}
alias CMS.Model.{Embeds, Community, PinnedArticle}
alias CMS.Delegate.{ArticleTag}
alias Ecto.Multi
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
def pin_article(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
args <- pack_pin_args(thread, article_id, community_id),
{:ok, _} <- check_pinned_article_count(args.community_id, thread),
{:ok, _} <- ORM.create(PinnedArticle, args) do
ORM.find(info.model, article_id)
end
end
@spec undo_pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
def undo_pin_article(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
args <- pack_pin_args(thread, article_id, community_id) do
ORM.findby_delete(PinnedArticle, args)
ORM.find(info.model, article_id)
end
end
defp pack_pin_args(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find(Community, community_id) do
thread = thread |> to_string |> String.upcase()
Map.put(
%{community_id: community.id, thread: thread},
info.foreign_key,
article_id
)
end
end
########
########
########
@doc """
mirror article to other community
"""
def mirror_article(thread, article_id, community_id, article_tag_ids \\ []) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id, preload: :communities),
{:ok, community} <- ORM.find(Community, community_id) do
Multi.new()
|> Multi.run(:mirror_target_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{mirror_target_community: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end
@doc """
unmirror article to a community
"""
def unmirror_article(thread, article_id, community_id) do
preload = [:communities, :original_community, :article_tags]
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload),
{:ok, community} <- ORM.find(Community, community_id) do
case article.original_community.id == community.id do
true ->
raise_error(:mirror_article, "can not unmirror original_community")
false ->
article_tags = tags_without_community(article, community)
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
|> Ecto.Changeset.put_assoc(:article_tags, article_tags)
|> Repo.update()
end
end
end
@doc """
move article original community to other community
"""
def move_article(thread, article_id, community_id, article_tag_ids \\ []) do
preload = [:communities, :original_community, :article_tags]
with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find(Community, community_id),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload) do
original_community = article.original_community
Multi.new()
|> Multi.run(:move_article, fn _, _ ->
communities = (article.communities -- [original_community]) ++ [community]
article_tags = tags_without_community(article, original_community)
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:original_community_id, community.id)
|> Ecto.Changeset.put_assoc(:communities, communities)
|> Ecto.Changeset.put_assoc(:article_tags, article_tags)
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{move_article: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end
@doc """
shortcut for mirror article to home page
"""
def mirror_to_home(thread, article_id, article_tag_ids \\ []) do
preload = [:communities, :article_tags]
with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find_by(Community, %{raw: "home"}),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload) do
Multi.new()
|> Multi.run(:set_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{set_community: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end
@doc """
shortcut for move article to blackhole
"""
def move_to_blackhole(thread, article_id, article_tag_ids \\ []) do
preload = [:communities, :original_community, :article_tags]
with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find_by(Community, %{raw: "blackhole"}),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload) do
Multi.new()
|> Multi.run(:set_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:original_community_id, community.id)
|> Ecto.Changeset.put_assoc(:communities, [community])
|> Ecto.Changeset.put_assoc(:article_tags, [])
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{set_community: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end
@doc "update isEdited meta label if needed"
# TODO: diff history
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: _} = meta} = content) do
meta = meta |> Map.merge(%{is_edited: true})
ORM.update_meta(content, meta)
end
# for test or exsiting articles
def update_edit_status(%{meta: nil} = content) do
meta = Embeds.ArticleMeta.default_meta() |> Map.merge(%{is_edited: true})
ORM.update_meta(content, meta)
end
def update_edit_status(content, _), do: {:ok, content}
# check if the thread has aready enough pinned articles
defp check_pinned_article_count(community_id, thread) do
thread = thread |> to_string |> String.upcase()
query =
from(p in PinnedArticle, where: p.community_id == ^community_id and p.thread == ^thread)
pinned_articles = query |> Repo.all()
case length(pinned_articles) >= @max_pinned_article_count_per_thread do
true -> raise_error(:too_much_pinned_article, "too much pinned article")
_ -> {:ok, :pass}
end
end
defp tags_without_community(article, %Community{id: community_id}) do
%{article_tags: article_tags} = article
article_tags -- Enum.filter(article_tags, &(&1.community_id === community_id))
end
defp result({:ok, %{set_target_tags: result}}), do: result |> done()
defp result({:ok, %{mirror_target_community: result}}), do: result |> done()
defp result({:error, _, result, _steps}), do: {:error, result}
end