Skip to content

Commit

Permalink
Notify Slack people when new posts are published
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Dec 28, 2023
1 parent a26f830 commit aade90a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/changelog/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ defmodule Changelog.Notifier do
def notify(item = %NewsItem{}) do
item = NewsItem.preload_all(item)

if NewsItem.is_post(item) do
post = item |> NewsItem.load_object() |> Map.get(:object)
deliver_slack_new_post_message(post)
end

if item.submitter == item.author do
deliver_submitter_email(item.submitter, item)
else
Expand Down Expand Up @@ -194,6 +199,11 @@ defmodule Changelog.Notifier do
Slack.Client.message("#main", message)
end

defp deliver_slack_new_post_message(post) do
message = Slack.Messages.new_post(post)
Slack.Client.message("#main", message)
end

defp deliver_submitter_email(nil, _item), do: false

defp deliver_submitter_email(person, item) do
Expand Down
8 changes: 7 additions & 1 deletion lib/changelog/slack/messages.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Changelog.Slack.Messages do
alias ChangelogWeb.{NewsItemView, EpisodeView}
alias ChangelogWeb.{NewsItemView, EpisodeView, PostView}

def new_comment(comment) do
~s"""
Expand All @@ -13,6 +13,12 @@ defmodule Changelog.Slack.Messages do
"""
end

def new_post(post) do
~s"""
#{post.title} #{celebrate_emoji()} #{PostView.url(post)}
"""
end

def welcome do
~s"""
Welcome to Changelog's Community Slack! :clap:
Expand Down
2 changes: 1 addition & 1 deletion lib/changelog_web/views/post_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ defmodule ChangelogWeb.PostView do
|> length()
end

def url(post, action), do: Routes.post_url(Endpoint, action, post.slug)
def url(post, action \\ :show), do: Routes.post_url(Endpoint, action, post.slug)
end
19 changes: 19 additions & 0 deletions test/changelog/notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,23 @@ defmodule Changelog.NotifierTest do
assert_no_emails_delivered()
end
end

describe "notify/1 with a post item" do
setup_with_mocks([
{Slack.Client, [], [message: fn _, _ -> true end]}
]) do
:ok
end

test "when post is published" do
post = insert(:published_post)
item = post |> post_news_item() |> insert()
Notifier.notify(item)

assert %{success: 0, failure: 0} = Oban.drain_queue(queue: :email)

assert_no_emails_delivered()
assert called(Slack.Client.message("#main", :_))
end
end
end

0 comments on commit aade90a

Please sign in to comment.