@@ -151,17 +151,21 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
151151 end
152152
153153 @ doc """
154- set tag for post / tuts / videos ...
154+ set general tag for post / tuts / videos ...
155+ refined tag can't set by this func, use set_refined_tag instead
155156 """
156157 # check community first
157158 def set_tag ( % Community { id: _communitId } , thread , % Tag { id: tag_id } , content_id ) do
158159 with { :ok , action } <- match_action ( thread , :tag ) ,
159160 { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
160161 { :ok , tag } <- ORM . find ( action . reactor , tag_id ) do
161- content
162- |> Ecto.Changeset . change ( )
163- |> Ecto.Changeset . put_assoc ( :tags , content . tags ++ [ tag ] )
164- |> Repo . update ( )
162+ case tag . title != "refined" do
163+ true ->
164+ update_content_tag ( content , tag )
165+
166+ false ->
167+ { :error , "use set_refined_tag instead" }
168+ end
165169
166170 # NOTE: this should be control by Middleware
167171 # case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do
@@ -181,13 +185,71 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
181185 with { :ok , action } <- match_action ( thread , :tag ) ,
182186 { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
183187 { :ok , tag } <- ORM . find ( action . reactor , tag_id ) do
184- content
185- |> Ecto.Changeset . change ( )
186- |> Ecto.Changeset . put_assoc ( :tags , content . tags -- [ tag ] )
187- |> Repo . update ( )
188+ update_content_tag ( content , tag , :drop )
189+ end
190+ end
191+
192+ @ doc """
193+ set refined_tag to common content
194+ """
195+ def set_refined_tag ( % Community { id: community_id } , thread , topic_raw , content_id ) do
196+ with { :ok , action } <- match_action ( thread , :tag ) ,
197+ { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
198+ { :ok , topic } <- ORM . find_by ( Topic , % { raw: topic_raw } ) ,
199+ { :ok , tag } <-
200+ ORM . find_by ( action . reactor , % {
201+ title: "refined" ,
202+ community_id: community_id ,
203+ topic_id: topic . id
204+ } ) do
205+ update_content_tag ( content , tag )
206+ end
207+ end
208+
209+ def set_refined_tag ( % Community { id: community_id } , thread , content_id ) do
210+ with { :ok , action } <- match_action ( thread , :tag ) ,
211+ { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
212+ { :ok , tag } <-
213+ ORM . find_by ( action . reactor , % { title: "refined" , community_id: community_id } ) do
214+ update_content_tag ( content , tag )
215+ end
216+ end
217+
218+ @ doc """
219+ unset refined_tag to common content
220+ """
221+ def unset_refined_tag ( % Community { id: community_id } , thread , topic_raw , content_id ) do
222+ with { :ok , action } <- match_action ( thread , :tag ) ,
223+ { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
224+ { :ok , topic } <- ORM . find_by ( Topic , % { raw: topic_raw } ) ,
225+ { :ok , tag } <-
226+ ORM . find_by ( action . reactor , % {
227+ title: "refined" ,
228+ community_id: community_id ,
229+ topic_id: topic . id
230+ } ) do
231+ update_content_tag ( content , tag , :drop )
188232 end
189233 end
190234
235+ def unset_refined_tag ( % Community { id: community_id } , thread , content_id ) do
236+ with { :ok , action } <- match_action ( thread , :tag ) ,
237+ { :ok , content } <- ORM . find ( action . target , content_id , preload: :tags ) ,
238+ { :ok , tag } <-
239+ ORM . find_by ( action . reactor , % { title: "refined" , community_id: community_id } ) do
240+ update_content_tag ( content , tag , :drop )
241+ end
242+ end
243+
244+ defp update_content_tag ( content , % Tag { } = tag , opt \\ :add ) do
245+ new_tags = if opt == :add , do: content . tags ++ [ tag ] , else: content . tags -- [ tag ]
246+
247+ content
248+ |> Ecto.Changeset . change ( )
249+ |> Ecto.Changeset . put_assoc ( :tags , new_tags )
250+ |> Repo . update ( )
251+ end
252+
191253 @ doc """
192254 set topic only for post
193255 """
0 commit comments