-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Post new episode notifications to Zulip
- Loading branch information
1 parent
58ac25b
commit d4388a8
Showing
5 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Changelog.Zulip.Client do | ||
use HTTPoison.Base | ||
|
||
def process_url(url), do: "https://changelog.zulipchat.com/api/v1#{url}" | ||
|
||
def process_response_body(body) do | ||
try do | ||
Jason.decode!(body) | ||
rescue | ||
_ -> body | ||
end | ||
end | ||
|
||
def process_request_headers(headers) do | ||
username = Application.get_env(:changelog, :zulip_user) | ||
password = Application.get_env(:changelog, :zulip_api_key) | ||
auth = Base.encode64("#{username}:#{password}") | ||
[ | ||
{"authorization", "Basic #{auth}"}, | ||
{"content-type", "application/x-www-form-urlencoded"} | headers] | ||
end | ||
|
||
def handle({:ok, %{status_code: 200, body: body}}), do: body | ||
def handle({:ok, %{status_code: 400, body: body}}), do: handle({:error, %{reason: body["msg"]}}) | ||
def handle({:error, %{reason: reason}}), do: %{"ok" => false, "error" => "#{reason}"} | ||
|
||
def get_message(message_id) do | ||
"/messages/#{message_id}" | ||
|> get() | ||
|> handle() | ||
end | ||
|
||
def post_message(channel, topic, content) do | ||
channel = URI.encode_www_form(channel) | ||
topic = URI.encode_www_form(topic) | ||
text = URI.encode_www_form(content) | ||
|
||
"/messages" | ||
|> post(~s(type=stream&to=#{channel}&topic=#{topic}&content=#{text})) | ||
|> handle() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Changelog.Zulip do | ||
|
||
alias Changelog.Zulip.Client | ||
alias Changelog.Episode | ||
alias ChangelogWeb.EpisodeView | ||
|
||
def post(episode = %Episode{}) do | ||
episode = Episode.preload_all(episode) | ||
|
||
channel = episode.podcast.slug | ||
topic = "#{episode.slug}: #{episode.title}" | ||
content = """ | ||
#{episode.summary} | ||
🔗 #{EpisodeView.share_url(episode)} | ||
""" | ||
|
||
case Client.post_message(channel, topic, content) do | ||
%{"result" => "success"} -> cross_post(episode, topic) | ||
_else -> nil | ||
end | ||
end | ||
|
||
defp cross_post(episode, topic) do | ||
channel = "general" | ||
content = "#{EpisodeView.podcast_name_and_number(episode)}! Discuss 👉 #**#{channel}>#{topic}**" | ||
|
||
Client.post_message(channel, "new episodes", content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters