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

Commit abeb9ff

Browse files
committed
feat(mailer): notify admin user for new content
1 parent 49d3dd2 commit abeb9ff

File tree

4 files changed

+534
-17
lines changed

4 files changed

+534
-17
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
88
import Helper.ErrorCode
99
import ShortMaps
1010

11-
alias GroupherServer.Repo
11+
alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}
1212

13-
alias GroupherServer.Accounts.User
14-
alias GroupherServer.{CMS, Delivery, Statistics}
13+
alias Accounts.User
14+
alias CMS.{Author, Community, Delegate, Tag, Topic}
1515

16-
alias CMS.Delegate.ArticleOperation
17-
alias Helper.{ORM, QueryBuilder}
16+
alias Delegate.ArticleOperation
17+
alias Helper.{Later, ORM, QueryBuilder}
1818

19-
alias CMS.{Author, Community, Tag, Topic}
2019
alias Ecto.Multi
2120

2221
@doc """
@@ -398,7 +397,30 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
398397
end
399398
end
400399

401-
defp create_content_result({:ok, %{create_content: result}}), do: {:ok, result}
400+
defp create_content_result({:ok, %{create_content: result}}) do
401+
# Later.exec({__MODULE__, :nofify_admin_new_content, [result]})
402+
nofify_admin_new_content(result)
403+
{:ok, result}
404+
end
405+
406+
def nofify_admin_new_content(%{id: id} = result) do
407+
target = result.__struct__
408+
preload = [:origial_community, author: :user]
409+
410+
with {:ok, content} <- ORM.find(target, id, preload: preload) do
411+
info = %{
412+
id: content.id,
413+
title: content.title,
414+
digest: content.digest,
415+
author_name: content.author.user.nickname,
416+
community_raw: content.origial_community.raw,
417+
type:
418+
result.__struct__ |> to_string |> String.split(".") |> List.last() |> String.downcase()
419+
}
420+
421+
Email.notify_admin(info, :new_content)
422+
end
423+
end
402424

403425
defp create_content_result({:error, :create_content, %Ecto.Changeset{} = result, _steps}) do
404426
{:error, result}

lib/groupher_server/mailer/email.ex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ defmodule GroupherServer.Email do
77
import Bamboo.Email
88
import Helper.Utils, only: [get_config: 2]
99

10-
alias GroupherServer.{Accounts, Billing, Email, Mailer}
10+
alias GroupherServer.{Accounts, Billing, CMS, Email, Mailer}
1111

1212
alias Accounts.User
1313
alias Billing.BillRecord
14+
alias CMS.{Post, Job, Repo, Video}
1415
alias Email.Templates
1516
alias Mailer
1617

@@ -86,15 +87,16 @@ defmodule GroupherServer.Email do
8687
end
8788

8889
# notify admin when new post has created
89-
def notify_admin(:create, _post) do
90+
def notify_admin(%{type: type, title: title} = info, :new_content) do
9091
case @conf_notify_admin_on_content_created do
9192
true ->
92-
base_mail()
93-
|> to(@admin_email)
94-
|> subject("新帖子: ... ")
95-
# |> html_body(Templates.NotifyAdminRegister.html(user))
96-
# |> text_body(Templates.NotifyAdminRegister.text())
97-
|> Mailer.deliver_later()
93+
hello =
94+
base_mail()
95+
|> to(@admin_email)
96+
|> subject("new #{type}: #{title}")
97+
|> html_body(Templates.NotifyAdminOnContentCreated.html(info))
98+
|> text_body(Templates.NotifyAdminRegister.text())
99+
|> Mailer.deliver_later()
98100

99101
false ->
100102
{:ok, :pass}

0 commit comments

Comments
 (0)