|
1 | 1 | defmodule GroupherServer.CMS.Delegate.ArticleReaction do |
2 | 2 | @moduledoc """ |
3 | | - reaction[favorite, star, watch ...] on article [post, job...] |
| 3 | + reaction[upvote, collect, watch ...] on article [post, job...] |
4 | 4 | """ |
5 | 5 | import Helper.Utils, only: [done: 1, done: 2] |
6 | 6 |
|
@@ -208,109 +208,4 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do |
208 | 208 | defp reaction_result({:error, _, result, _steps}) do |
209 | 209 | {:error, result} |
210 | 210 | end |
211 | | - |
212 | | - ##### ######################### |
213 | | - ##### ######################### |
214 | | - ##### ######################### |
215 | | - ##### ######################### |
216 | | - ##### ######################### |
217 | | - |
218 | | - @doc """ |
219 | | - favorite / star / watch CMS contents like post / tuts ... |
220 | | - """ |
221 | | - def reaction(thread, react, content_id, %User{id: user_id}) do |
222 | | - with {:ok, action} <- match_action(thread, react), |
223 | | - {:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]), |
224 | | - {:ok, user} <- ORM.find(Accounts.User, user_id) do |
225 | | - Multi.new() |
226 | | - |> Multi.run(:create_reaction_record, fn _, _ -> |
227 | | - create_reaction_record(action, user, thread, content) |
228 | | - end) |
229 | | - |> Multi.run(:add_achievement, fn _, _ -> |
230 | | - achiever_id = content.author.user_id |
231 | | - Accounts.achieve(%User{id: achiever_id}, :inc, react) |
232 | | - end) |
233 | | - |> Repo.transaction() |
234 | | - |> old_reaction_result() |
235 | | - end |
236 | | - end |
237 | | - |
238 | | - defp old_reaction_result({:ok, %{create_reaction_record: result}}), do: result |> done() |
239 | | - |
240 | | - defp old_reaction_result({:error, :create_reaction_record, %Ecto.Changeset{} = result, _steps}) do |
241 | | - {:error, result} |
242 | | - end |
243 | | - |
244 | | - defp old_reaction_result({:error, :create_reaction_record, _result, _steps}) do |
245 | | - {:error, [message: "create reaction fails", code: ecode(:react_fails)]} |
246 | | - end |
247 | | - |
248 | | - defp old_reaction_result({:error, :add_achievement, _result, _steps}), |
249 | | - do: {:error, [message: "achieve fails", code: ecode(:react_fails)]} |
250 | | - |
251 | | - defp create_reaction_record(action, %User{id: user_id}, thread, content) do |
252 | | - attrs = %{} |> Map.put("user_id", user_id) |> Map.put("#{thread}_id", content.id) |
253 | | - |
254 | | - action.reactor |
255 | | - |> ORM.create(attrs) |
256 | | - |> done(with: content) |
257 | | - end |
258 | | - |
259 | | - # ------ |
260 | | - @doc """ |
261 | | - unfavorite / unstar / unwatch CMS contents like post / tuts ... |
262 | | - """ |
263 | | - def undo_reaction(thread, react, content_id, %User{id: user_id}) do |
264 | | - with {:ok, action} <- match_action(thread, react), |
265 | | - {:ok, content} <- ORM.find(action.target, content_id, preload: [author: :user]), |
266 | | - {:ok, user} <- ORM.find(Accounts.User, user_id) do |
267 | | - Multi.new() |
268 | | - |> Multi.run(:delete_reaction_record, fn _, _ -> |
269 | | - delete_reaction_record(action, user, thread, content) |
270 | | - end) |
271 | | - |> Multi.run(:minus_achievement, fn _, _ -> |
272 | | - achiever_id = content.author.user_id |
273 | | - Accounts.achieve(%User{id: achiever_id}, :dec, react) |
274 | | - end) |
275 | | - |> Repo.transaction() |
276 | | - |> undo_reaction_result() |
277 | | - end |
278 | | - end |
279 | | - |
280 | | - defp undo_reaction_result({:ok, %{delete_reaction_record: result}}), do: result |> done() |
281 | | - |
282 | | - defp undo_reaction_result({:error, :delete_reaction_record, _result, _steps}) do |
283 | | - {:error, [message: "delete reaction fails", code: ecode(:react_fails)]} |
284 | | - end |
285 | | - |
286 | | - defp undo_reaction_result({:error, :minus_achievement, _result, _steps}), |
287 | | - do: {:error, [message: "achieve fails", code: ecode(:react_fails)]} |
288 | | - |
289 | | - defp delete_reaction_record(action, %User{id: user_id}, thread, content) do |
290 | | - user_where = dynamic([u], u.user_id == ^user_id) |
291 | | - reaction_where = dynamic_reaction_where(thread, content.id, user_where) |
292 | | - |
293 | | - query = from(f in action.reactor, where: ^reaction_where) |
294 | | - |
295 | | - case Repo.one(query) do |
296 | | - nil -> |
297 | | - {:error, "record not found"} |
298 | | - |
299 | | - record -> |
300 | | - Repo.delete(record) |
301 | | - {:ok, content} |
302 | | - end |
303 | | - end |
304 | | - |
305 | | - defp dynamic_reaction_where(:post, id, user_where) do |
306 | | - dynamic([p], p.post_id == ^id and ^user_where) |
307 | | - end |
308 | | - |
309 | | - defp dynamic_reaction_where(:job, id, user_where) do |
310 | | - dynamic([p], p.job_id == ^id and ^user_where) |
311 | | - end |
312 | | - |
313 | | - defp dynamic_reaction_where(:repo, id, user_where) do |
314 | | - dynamic([p], p.repo_id == ^id and ^user_where) |
315 | | - end |
316 | 211 | end |
0 commit comments