|
| 1 | +defmodule GroupherServer.CMS.Delegate.ArticleCollect do |
| 2 | + @moduledoc """ |
| 3 | + reaction[upvote, collect, watch ...] on article [post, job...] |
| 4 | + """ |
| 5 | + import GroupherServer.CMS.Helper.Matcher2 |
| 6 | + import Ecto.Query, warn: false |
| 7 | + import Helper.Utils, only: [done: 1] |
| 8 | + |
| 9 | + import GroupherServer.CMS.Delegate.Helper, |
| 10 | + only: [ |
| 11 | + load_reaction_users: 4, |
| 12 | + update_article_reactions_count: 4, |
| 13 | + update_article_reaction_user_list: 4 |
| 14 | + ] |
| 15 | + |
| 16 | + # import Helper.ErrorCode |
| 17 | + alias Helper.{ORM} |
| 18 | + alias GroupherServer.{Accounts, CMS, Repo} |
| 19 | + |
| 20 | + alias Accounts.User |
| 21 | + alias CMS.ArticleCollect |
| 22 | + |
| 23 | + alias Ecto.Multi |
| 24 | + |
| 25 | + @doc """ |
| 26 | + get paged collected users |
| 27 | + """ |
| 28 | + def collected_users(thread, article_id, filter) do |
| 29 | + load_reaction_users(ArticleCollect, thread, article_id, filter) |
| 30 | + end |
| 31 | + |
| 32 | + @doc """ |
| 33 | + collect an article |
| 34 | + """ |
| 35 | + def collect_article(thread, article_id, %User{id: user_id}) do |
| 36 | + with {:ok, info} <- match(thread), |
| 37 | + {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do |
| 38 | + Multi.new() |
| 39 | + |> Multi.run(:inc_author_achieve, fn _, _ -> |
| 40 | + Accounts.achieve(article.author.user, :inc, :collect) |
| 41 | + end) |
| 42 | + |> Multi.run(:inc_article_collects_count, fn _, _ -> |
| 43 | + update_article_reactions_count(info, article, :collects_count, :inc) |
| 44 | + end) |
| 45 | + |> Multi.run(:update_article_reaction_user_list, fn _, _ -> |
| 46 | + update_article_reaction_user_list(:collect, article, user_id, :add) |
| 47 | + end) |
| 48 | + |> Multi.run(:create_collect, fn _, _ -> |
| 49 | + thread_upcase = thread |> to_string |> String.upcase() |
| 50 | + args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id) |
| 51 | + |
| 52 | + ORM.create(ArticleCollect, args) |
| 53 | + end) |
| 54 | + |> Repo.transaction() |
| 55 | + |> result() |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # 用于在收藏时,用户添加文章到不同的收藏夹中的情况 |
| 60 | + # 如果是同一篇文章,只创建一次,collect_article 不创建记录,只是后续设置不同的收藏夹即可 |
| 61 | + # 如果是第一次收藏,那么才创建文章收藏记录 |
| 62 | + # 避免因为同一篇文章在不同收藏夹内造成的统计和用户成就系统的混乱 |
| 63 | + def collect_article_ifneed(thread, article_id, %User{id: user_id} = user) do |
| 64 | + with findby_args <- collection_findby_args(thread, article_id, user_id) do |
| 65 | + already_collected = ORM.find_by(ArticleCollect, findby_args) |
| 66 | + |
| 67 | + case already_collected do |
| 68 | + {:ok, article_collect} -> {:ok, article_collect} |
| 69 | + {:error, _} -> collect_article(thread, article_id, user) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def undo_collect_article(thread, article_id, %User{id: user_id}) do |
| 75 | + with {:ok, info} <- match(thread), |
| 76 | + {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do |
| 77 | + Multi.new() |
| 78 | + |> Multi.run(:dec_author_achieve, fn _, _ -> |
| 79 | + Accounts.achieve(article.author.user, :dec, :collect) |
| 80 | + end) |
| 81 | + |> Multi.run(:inc_article_collects_count, fn _, _ -> |
| 82 | + update_article_reactions_count(info, article, :collects_count, :dec) |
| 83 | + end) |
| 84 | + |> Multi.run(:update_article_reaction_user_list, fn _, _ -> |
| 85 | + update_article_reaction_user_list(:collect, article, user_id, :remove) |
| 86 | + end) |
| 87 | + |> Multi.run(:undo_collect, fn _, _ -> |
| 88 | + args = Map.put(%{user_id: user_id}, info.foreign_key, article.id) |
| 89 | + |
| 90 | + ORM.findby_delete(ArticleCollect, args) |
| 91 | + end) |
| 92 | + |> Repo.transaction() |
| 93 | + |> result() |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + def undo_collect_article_ifneed(thread, article_id, %User{id: user_id} = user) do |
| 98 | + with findby_args <- collection_findby_args(thread, article_id, user_id), |
| 99 | + {:ok, article_collect} = ORM.find_by(ArticleCollect, findby_args) do |
| 100 | + case article_collect.collect_folders |> length <= 1 do |
| 101 | + true -> undo_collect_article(thread, article_id, user) |
| 102 | + false -> {:ok, article_collect} |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def set_collect_folder(%ArticleCollect{} = collect, folder) do |
| 108 | + collect_folders = (collect.collect_folders ++ [folder]) |> Enum.uniq() |
| 109 | + |
| 110 | + collect |
| 111 | + |> Ecto.Changeset.change() |
| 112 | + |> Ecto.Changeset.put_embed(:collect_folders, collect_folders) |
| 113 | + |> Repo.update() |
| 114 | + end |
| 115 | + |
| 116 | + def undo_set_collect_folder(%ArticleCollect{} = collect, folder) do |
| 117 | + collect_folders = Enum.reject(collect.collect_folders, &(&1.id == folder.id)) |
| 118 | + |
| 119 | + case collect_folders do |
| 120 | + # means collect already delete |
| 121 | + [] -> |
| 122 | + {:ok, :pass} |
| 123 | + |
| 124 | + _ -> |
| 125 | + collect |
| 126 | + |> Ecto.Changeset.change() |
| 127 | + |> Ecto.Changeset.put_embed(:collect_folders, collect_folders) |
| 128 | + |> Repo.update() |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + defp collection_findby_args(thread, article_id, user_id) do |
| 133 | + with {:ok, info} <- match(thread) do |
| 134 | + thread_upcase = thread |> to_string |> String.upcase() |
| 135 | + %{thread: thread_upcase, user_id: user_id} |> Map.put(info.foreign_key, article_id) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + ############# |
| 140 | + defp result({:ok, %{create_collect: result}}), do: result |> done() |
| 141 | + defp result({:ok, %{undo_collect: result}}), do: result |> done() |
| 142 | + defp result({:error, _, result, _steps}), do: {:error, result} |
| 143 | +end |
0 commit comments