Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit ac43691

Browse files
committed
refactor(mention): rename type -> thread
1 parent 69f37b6 commit ac43691

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

lib/groupher_server/cms/delegates/comment_action.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
141141
end)
142142
|> Multi.run(:after_hooks, fn _, %{create_reply_comment: replyed_comment} ->
143143
Later.run({Hooks.Notify, :handle, [:reply, replyed_comment, user]})
144+
Later.run({Hooks.Mention, :handle, [replyed_comment]})
144145
end)
145146
|> Repo.transaction()
146147
|> result()

lib/groupher_server/cms/delegates/comment_curd.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
117117
|> Multi.run(:after_hooks, fn _, %{create_comment: comment} ->
118118
Later.run({Hooks.Cite, :handle, [comment]})
119119
Later.run({Hooks.Notify, :handle, [:comment, comment, user]})
120+
Later.run({Hooks.Mention, :handle, [comment]})
120121
end)
121122
|> Repo.transaction()
122123
|> result()

lib/groupher_server/cms/delegates/hooks/mention.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
55
parse and fmt(see shape function) mentions to Delivery module
66
"""
77
import Ecto.Query, warn: false
8-
import Helper.Utils, only: [get_config: 2, thread_of_article: 2]
8+
import Helper.Utils, only: [get_config: 2, thread_of_article: 1]
99

1010
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1]
1111
import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2]
@@ -71,7 +71,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
7171
parent_article = comment |> Map.get(article_thread)
7272

7373
%{
74-
type: "COMMENT",
74+
thread: article_thread,
7575
title: parent_article.title,
7676
article_id: parent_article.id,
7777
comment_id: comment.id,
@@ -85,10 +85,10 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
8585
end
8686

8787
defp shape(article, to_user_id, block_id) do
88-
{:ok, thread} = thread_of_article(article, :upcase)
88+
{:ok, thread} = thread_of_article(article)
8989

9090
%{
91-
type: thread,
91+
thread: thread,
9292
title: article.title,
9393
article_id: article.id,
9494
block_linker: [block_id],

lib/groupher_server/delivery/delegates/mention.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
33
The Delivery context.
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, thread_of_article: 1]
6+
import Helper.Utils, only: [done: 1, thread_of_article: 2, atom_values_to_upcase: 1]
77
import ShortMaps
88

99
alias GroupherServer.{Accounts, CMS, Delivery, Repo}
@@ -30,7 +30,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
3030
batch_delete_mentions(comment, from_user)
3131
end)
3232
|> Multi.run(:batch_insert_mentions, fn _, _ ->
33-
case {0, nil} !== Repo.insert_all(Mention, mentions) do
33+
case {0, nil} !== Repo.insert_all(Mention, atom_values_to_upcase(mentions)) do
3434
true -> {:ok, :pass}
3535
false -> {:error, "insert mentions error"}
3636
end
@@ -45,7 +45,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
4545
batch_delete_mentions(article, from_user)
4646
end)
4747
|> Multi.run(:batch_insert_mentions, fn _, _ ->
48-
case {0, nil} !== Repo.insert_all(Mention, mentions) do
48+
case {0, nil} !== Repo.insert_all(Mention, atom_values_to_upcase(mentions)) do
4949
true -> {:ok, :pass}
5050
false -> {:error, "insert mentions error"}
5151
end
@@ -74,12 +74,10 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
7474
end
7575

7676
defp batch_delete_mentions(article, %User{} = from_user) do
77-
with {:ok, thread} <- thread_of_article(article) do
78-
thread = thread |> to_string |> String.upcase()
79-
77+
with {:ok, thread} <- thread_of_article(article, :upcase) do
8078
from(m in Mention,
8179
where: m.article_id == ^article.id,
82-
where: m.type == ^thread,
80+
where: m.thread == ^thread,
8381
where: m.from_user_id == ^from_user.id
8482
)
8583
|> ORM.delete_all(:if_exist)
@@ -98,7 +96,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
9896

9997
mention
10098
|> Map.take([
101-
:type,
99+
:thread,
102100
:article_id,
103101
:comment_id,
104102
:title,

lib/groupher_server/delivery/models/mention.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ defmodule GroupherServer.Delivery.Model.Mention do
77

88
alias GroupherServer.Accounts.Model.User
99

10-
@required_fields ~w(from_user_id to_user_id title article_id type)a
10+
@required_fields ~w(from_user_id to_user_id title article_id thread)a
1111
@optional_fields ~w(comment_id read)a
1212

1313
@type t :: %Mention{}
1414
schema "mentions" do
15-
field(:type, :string)
15+
field(:thread, :string)
1616
field(:article_id, :id)
1717
field(:title, :string)
1818
field(:comment_id, :id)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule GroupherServer.Repo.Migrations.RenameMentionsTypeToThread do
2+
use Ecto.Migration
3+
4+
def change do
5+
rename(table(:mentions), :type, to: :thread)
6+
end
7+
end

0 commit comments

Comments
 (0)