Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix twitter (x) posts for v2 api #827

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
elixir: [1.14.1]
otp: [25.1.2]
elixir: [1.17.2]
otp: [27.0.1]
steps:
- uses: actions/checkout@v3
- name: Set up Elixir
Expand Down Expand Up @@ -61,8 +61,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
elixir: [1.14.1]
otp: [25.1.2]
elixir: [1.17.2]
otp: [27.0.1]
services:
postgres:
image: postgres:14
Expand Down Expand Up @@ -110,8 +110,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
elixir: [1.14.1]
otp: [25.1.2]
elixir: [1.17.2]
otp: [27.0.1]
steps:
- uses: actions/checkout@v3
- name: Set up Elixir
Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
elixir 1.14.1-otp-25
erlang 25.1.2
elixir 1.17.2-otp-27
erlang 27.0.1
nodejs 19.0.0
6 changes: 3 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ config :tilex, Tilex.Auth.Guardian,
"kty" => "oct"
}

config :extwitter, :oauth,
config :tilex, Tilex.Notifications.Notifiers.Twitter,
consumer_key: System.get_env("twitter_consumer_key"),
consumer_secret: System.get_env("twitter_consumer_secret"),
access_token: System.get_env("twitter_access_token"),
access_token_secret: System.get_env("twitter_access_token_secret")
token: System.get_env("twitter_access_token"),
token_secret: System.get_env("twitter_access_token_secret")

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
2 changes: 1 addition & 1 deletion lib/tilex/notifications/notifiers/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Tilex.Notifications.Notifiers.Slack do
defp send_slack_message(message, http) do
message = String.replace(message, "\"", "'")
endpoint = slack_endpoint() |> String.to_charlist()
request = {endpoint, [], 'application/json', "{\"text\": \"#{message}\"}"}
request = {endpoint, [], ~c"application/json", "{\"text\": \"#{message}\"}"}
http.request(:post, request, [], [])
end

Expand Down
28 changes: 26 additions & 2 deletions lib/tilex/notifications/notifiers/twitter.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
defmodule Tilex.Notifications.Notifiers.Twitter do
alias OAuther
alias Tilex.Blog.Developer

use Tilex.Notifications.Notifier

@tweets_url "https://api.x.com/2/tweets"

def handle_post_created(post, developer, channel, url) do
"#{post.title} #{url} via @#{Developer.twitter_handle(developer)} #til ##{channel.twitter_hashtag}"
|> send_tweet
Expand All @@ -16,7 +19,28 @@ defmodule Tilex.Notifications.Notifiers.Twitter do
:ok
end

def send_tweet(text) do
ExTwitter.update(text)
def send_tweet(message) do
params = %{
"text" => message
}

headers =
oauth_headers("post", @tweets_url)

Req.post!(@tweets_url, headers: headers, json: params)
end

defp oauth_headers(method, url) do
{auth_header, _params} =
OAuther.sign(method, url, [], oauth_creds())
|> OAuther.header()

[auth_header]
end

def oauth_creds do
credentials = Application.get_env(:tilex, __MODULE__)

OAuther.credentials(credentials ++ [method: :hmac_sha1])
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ defmodule Tilex.Mixfile do
{:earmark, "~> 1.4.4"},
{:ecto_sql, "~> 3.6"},
{:esbuild, "~> 0.4", runtime: Mix.env() == :dev},
{:extwitter, "~> 0.13"},
{:floki, "~>0.34"},
{:gettext, "~> 0.18"},
{:guardian, "~> 2.0"},
Expand All @@ -59,6 +58,7 @@ defmodule Tilex.Mixfile do
{:plug_cowboy, "~> 2.5"},
{:postgrex, ">= 0.0.0"},
{:swoosh, "~> 1.3"},
{:req, "~> 0.5.6"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:timex, "~> 3.1"},
Expand Down
123 changes: 63 additions & 60 deletions mix.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/controllers/stats_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Tilex.StatsControllerTest do
use TilexWeb.ConnCase, async: true

test 'developer/2 redirects to stats index when unauthenticated', %{conn: conn} do
test "developer/2 redirects to stats index when unauthenticated", %{conn: conn} do
response = get(conn, Routes.stats_path(conn, :developer))
assert html_response(response, 302)
end
Expand Down
2 changes: 1 addition & 1 deletion test/features/developer_signs_out_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Features.DeveloperSignsOutTest do
use Tilex.IntegrationCase, async: true

test 'signs out and sees a flash message', %{:session => session} do
test "signs out and sees a flash message", %{:session => session} do
developer = Factory.insert!(:developer)

session
Expand Down
8 changes: 4 additions & 4 deletions test/lib/tilex/notifications/notifications_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ defmodule Tilex.NotificationsTest do
%{
time_zone: "America/Chicago",
input: ~N[2021-09-20 08:59:59.999],
expected: ~N[2021-09-20 09:00:00.000]
expected: ~N[2021-09-20 09:00:00.000000]
},
%{
time_zone: "America/Chicago",
input: ~N[2021-09-20 09:00:00.000],
expected: ~N[2021-09-27 09:00:00.000]
expected: ~N[2021-09-27 09:00:00.000000]
},
%{
time_zone: "Europe/Paris",
input: ~N[2021-09-20 08:59:59.999],
expected: ~N[2021-09-20 09:00:00.000]
expected: ~N[2021-09-20 09:00:00.000000]
},
%{
time_zone: "Europe/Paris",
input: ~N[2021-09-20 09:00:00.000],
expected: ~N[2021-09-27 09:00:00.000]
expected: ~N[2021-09-27 09:00:00.000000]
}
]

Expand Down
2 changes: 1 addition & 1 deletion test/support/pages/create_post_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Tilex.Integration.Pages.CreatePostPage do

def click_cancel(session) do
session
|> click(Query.link('cancel'))
|> click(Query.link("cancel"))
end

def expect_form_has_error(session, error_text) do
Expand Down
2 changes: 1 addition & 1 deletion test/tilex/notifications/notifiers/slack_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Tilex.Notifications.Notifiers.SlackTest do
defmodule HTTPMock do
def request(
:post,
{'https://slack.test.com/abc/123', [], 'application/json', payload},
{~c"https://slack.test.com/abc/123", [], ~c"application/json", payload},
[],
[]
) do
Expand Down
Loading