From aade90a1bed7c70e947f0462197b888ec695fbcd Mon Sep 17 00:00:00 2001 From: Jerod Santo Date: Thu, 28 Dec 2023 13:45:28 -0600 Subject: [PATCH] Notify Slack people when new posts are published --- lib/changelog/notifier.ex | 10 ++++++++++ lib/changelog/slack/messages.ex | 8 +++++++- lib/changelog_web/views/post_view.ex | 2 +- test/changelog/notifier_test.exs | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/changelog/notifier.ex b/lib/changelog/notifier.ex index 687eb53c6c..bc2d546c1a 100644 --- a/lib/changelog/notifier.ex +++ b/lib/changelog/notifier.ex @@ -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 @@ -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 diff --git a/lib/changelog/slack/messages.ex b/lib/changelog/slack/messages.ex index a54cd48ada..14f85c7186 100644 --- a/lib/changelog/slack/messages.ex +++ b/lib/changelog/slack/messages.ex @@ -1,5 +1,5 @@ defmodule Changelog.Slack.Messages do - alias ChangelogWeb.{NewsItemView, EpisodeView} + alias ChangelogWeb.{NewsItemView, EpisodeView, PostView} def new_comment(comment) do ~s""" @@ -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: diff --git a/lib/changelog_web/views/post_view.ex b/lib/changelog_web/views/post_view.ex index 84fb05de06..5f5a519cce 100644 --- a/lib/changelog_web/views/post_view.ex +++ b/lib/changelog_web/views/post_view.ex @@ -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 diff --git a/test/changelog/notifier_test.exs b/test/changelog/notifier_test.exs index ba422c8fd6..e2d190c19d 100644 --- a/test/changelog/notifier_test.exs +++ b/test/changelog/notifier_test.exs @@ -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