|
| 1 | +defmodule GroupherServer.CMS.Delegate.ArticleEmotion do |
| 2 | + @moduledoc """ |
| 3 | + CURD and operations for article comments |
| 4 | + """ |
| 5 | + import Ecto.Query, warn: false |
| 6 | + import GroupherServer.CMS.Helper.Matcher2 |
| 7 | + |
| 8 | + alias Helper.ORM |
| 9 | + alias GroupherServer.{Accounts, CMS, Repo} |
| 10 | + |
| 11 | + alias Accounts.User |
| 12 | + alias CMS.{ArticleUserEmotion} |
| 13 | + |
| 14 | + alias Ecto.Multi |
| 15 | + |
| 16 | + @type t_user_list :: [%{login: String.t()}] |
| 17 | + @type t_mention_status :: %{user_list: t_user_list, user_count: Integer.t()} |
| 18 | + |
| 19 | + # ArticleComment.max_latest_emotion_users_count() |
| 20 | + @max_latest_emotion_users_count 4 |
| 21 | + |
| 22 | + @doc "make emotion to a comment" |
| 23 | + def emotion_to_article(thread, article_id, emotion, %User{} = user) do |
| 24 | + with {:ok, info} <- match(thread), |
| 25 | + {:ok, article} <- ORM.find(info.model, article_id, preload: :author) do |
| 26 | + Multi.new() |
| 27 | + |> Multi.run(:create_user_emotion, fn _, _ -> |
| 28 | + target = |
| 29 | + %{recived_user_id: article.author.user_id, user_id: user.id} |
| 30 | + |> Map.put(info.foreign_key, article_id) |
| 31 | + |
| 32 | + args = Map.put(target, :"#{emotion}", true) |
| 33 | + |
| 34 | + case ORM.find_by(ArticleUserEmotion, target) do |
| 35 | + {:ok, article_user_emotion} -> article_user_emotion |> ORM.update(args) |
| 36 | + {:error, _} -> ArticleUserEmotion |> ORM.create(args) |
| 37 | + end |
| 38 | + end) |
| 39 | + |> Multi.run(:query_emotion_status, fn _, _ -> |
| 40 | + query_emotion_status(thread, article.id, emotion) |
| 41 | + end) |
| 42 | + |> Multi.run(:update_emotion, fn _, %{query_emotion_status: status} -> |
| 43 | + update_emotion(article, emotion, status, user) |
| 44 | + end) |
| 45 | + |> Repo.transaction() |
| 46 | + |> update_emotion_result |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def undo_emotion_to_article(thread, article_id, emotion, %User{} = user) do |
| 51 | + with {:ok, info} <- match(thread), |
| 52 | + {:ok, article} <- ORM.find(info.model, article_id, preload: :author) do |
| 53 | + Multi.new() |
| 54 | + |> Multi.run(:update_user_emotion, fn _, _ -> |
| 55 | + target = |
| 56 | + %{recived_user_id: article.author.id, user_id: user.id} |
| 57 | + |> Map.put(info.foreign_key, article_id) |
| 58 | + |
| 59 | + {:ok, article_user_emotion} = ORM.find_by(ArticleUserEmotion, target) |
| 60 | + args = Map.put(target, :"#{emotion}", false) |
| 61 | + article_user_emotion |> ORM.update(args) |
| 62 | + end) |
| 63 | + |> Multi.run(:query_emotion_status, fn _, _ -> |
| 64 | + query_emotion_status(thread, article.id, emotion) |
| 65 | + end) |
| 66 | + |> Multi.run(:update_emotion, fn _, %{query_emotion_status: status} -> |
| 67 | + update_emotion(article, emotion, status, user) |
| 68 | + end) |
| 69 | + |> Repo.transaction() |
| 70 | + |> update_emotion_result |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + # @spec query_emotion_status(ArticleComment.t(), Atom.t()) :: {:ok, t_mention_status} |
| 75 | + defp query_emotion_status(thread, article_id, emotion) do |
| 76 | + with {:ok, info} <- match(thread) do |
| 77 | + # 每次被 emotion 动作触发后重新查询,主要原因 |
| 78 | + # 1.并发下保证数据准确,类似 views 阅读数的统计 |
| 79 | + # 2. 前端使用 nickname 而非 login 展示,如果用户改了 nickname, 可以"自动纠正" |
| 80 | + query = |
| 81 | + from(a in ArticleUserEmotion, |
| 82 | + join: user in User, |
| 83 | + on: a.user_id == user.id, |
| 84 | + where: field(a, ^info.foreign_key) == ^article_id, |
| 85 | + where: field(a, ^emotion) == true, |
| 86 | + select: %{login: user.login, nickname: user.nickname} |
| 87 | + ) |
| 88 | + |
| 89 | + emotioned_user_info_list = Repo.all(query) |> Enum.uniq() |
| 90 | + emotioned_user_count = length(emotioned_user_info_list) |
| 91 | + |
| 92 | + {:ok, %{user_list: emotioned_user_info_list, user_count: emotioned_user_count}} |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + @spec update_emotion(ArticleComment.t(), Atom.t(), t_mention_status, User.t()) :: |
| 97 | + {:ok, ArticleComment.t()} | {:error, any} |
| 98 | + defp update_emotion(comment, emotion, status, user) do |
| 99 | + %{user_count: user_count, user_list: user_list} = status |
| 100 | + |
| 101 | + emotions = |
| 102 | + %{} |
| 103 | + |> Map.put(:"#{emotion}_count", user_count) |
| 104 | + |> Map.put(:"#{emotion}_user_logins", user_list |> Enum.map(& &1.login)) |
| 105 | + |> Map.put( |
| 106 | + :"latest_#{emotion}_users", |
| 107 | + Enum.slice(user_list, 0, @max_latest_emotion_users_count) |
| 108 | + ) |
| 109 | + |
| 110 | + viewer_has_emotioned = user.login in Map.get(emotions, :"#{emotion}_user_logins") |
| 111 | + emotions = emotions |> Map.put(:"viewer_has_#{emotion}ed", viewer_has_emotioned) |
| 112 | + |
| 113 | + comment |
| 114 | + |> Ecto.Changeset.change() |
| 115 | + |> Ecto.Changeset.put_embed(:emotions, emotions) |
| 116 | + |> Repo.update() |
| 117 | + # virtual field can not be updated |
| 118 | + |> add_viewer_emotioned_ifneed(emotions) |
| 119 | + end |
| 120 | + |
| 121 | + defp add_viewer_emotioned_ifneed({:error, error}, _), do: {:error, error} |
| 122 | + |
| 123 | + defp add_viewer_emotioned_ifneed({:ok, comment}, emotions) do |
| 124 | + # Map.merge(comment, %{emotion: emotions}) |
| 125 | + {:ok, Map.merge(comment, %{emotion: emotions})} |
| 126 | + end |
| 127 | + |
| 128 | + defp update_emotion_result({:ok, %{update_emotion: result}}), do: {:ok, result} |
| 129 | + |
| 130 | + defp update_emotion_result({:error, _, result, _steps}) do |
| 131 | + {:error, result} |
| 132 | + end |
| 133 | +end |
0 commit comments