Skip to content

Commit fcd1ad3

Browse files
committed
feat: create linkedin post when TIL is created
1 parent c835e42 commit fcd1ad3

File tree

7 files changed

+87
-1
lines changed

7 files changed

+87
-1
lines changed

config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ config :logger, :console,
5050
# Use Jason for JSON parsing in Phoenix
5151
config :phoenix, :json_library, Jason
5252

53+
# Use Finch and Tesla for HTTP client
54+
config :tesla, adapter: Tilex.Finch
55+
5356
# Provide reasonable default for configuration options
5457
config :tilex, :page_size, 5
5558
config :tilex, :auth_controller, AuthController
@@ -67,6 +70,7 @@ config :tilex, :slack_endpoint, "https://hooks.slack.com#{System.get_env("slack_
6770
config :tilex, :banner_image_source, System.get_env("BANNER_IMAGE_SOURCE")
6871
config :tilex, :banner_image_link, System.get_env("BANNER_IMAGE_LINK")
6972
config :tilex, :banner_image_alt, System.get_env("BANNER_IMAGE_ALT")
73+
config :tilex, :linkedin_organization_id, System.get_env("LINKEDIN_ORGANIZATION_ID")
7074

7175
config :tilex,
7276
:rate_limiter_requests_per_time_period,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Test.Notifications.Notifiers.Linkedin do
2+
use Tilex.Notifications.Notifier
3+
4+
def handle_post_created(_post, _developer, _channel, _url) do
5+
:ok
6+
end
7+
8+
def handle_post_liked(_post, _developer, _url) do
9+
:ok
10+
end
11+
12+
def handle_page_views_report(_report) do
13+
:ok
14+
end
15+
end

lib/tilex/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ defmodule Tilex.Application do
1515
{Cachex, name: :tilex_cache},
1616
Tilex.Notifications,
1717
Tilex.RateLimiter,
18-
Tilex.Notifications.NotifiersSupervisor
18+
Tilex.Notifications.NotifiersSupervisor,
19+
{Finch, name: Tilex.Finch}
1920
]
2021

2122
:telemetry.attach(

lib/tilex/linkedin_api.ex

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule Tilex.LinkedinApi do
2+
use Tesla, only: [:post]
3+
4+
plug Tesla.Middleware.BaseUrl, "https://api.linkedin.com/v2"
5+
plug Tesla.Middleware.Headers, [{"Content-Type", "application/json"}]
6+
plug Tesla.Middleware.BearerAuth, token: Application.get_env(:tilex, :linkedin_access_token)
7+
plug Tesla.Middleware.JSON
8+
9+
def create_post(post_body) do
10+
post("/posts", post_body)
11+
end
12+
13+
def create_post_body(commentary, title, url) do
14+
%{
15+
author: "urn:li:organization:" <> org_id(),
16+
commentary: commentary,
17+
visibility: "PUBLIC",
18+
distribution: %{
19+
feedDistribution: "MAIN_FEED",
20+
targetEntities: [],
21+
thirdPartyDistributionChannels: []
22+
},
23+
content: %{
24+
article: %{
25+
source: url,
26+
title: title
27+
}
28+
},
29+
lifecycleState: "PUBLISHED",
30+
isReshareDisabledByAuthor: false
31+
}
32+
end
33+
34+
defp org_id, do: Application.get_env(:tilex, :linkedin_organization_id)
35+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule Tilex.Notifications.Notifiers.Linkedin do
2+
alias Tilex.Blog.Developer
3+
4+
import Tilex.LinkedinApi
5+
6+
use Tilex.Notifications.Notifier
7+
8+
@impl true
9+
def handle_post_created(post, developer, channel, url) do
10+
"#{post.title} #{url} via @#{Developer.twitter_handle(developer)} #til ##{channel.twitter_hashtag}"
11+
|> create_post_body(post.title, url)
12+
|> create_post()
13+
end
14+
15+
@impl true
16+
def handle_post_liked(_post, _dev, _url) do
17+
:ok
18+
end
19+
20+
@impl true
21+
def handle_page_views_report(_report) do
22+
:ok
23+
end
24+
end

mix.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ defmodule Tilex.Mixfile do
4141
{:ecto_sql, "~> 3.6"},
4242
{:esbuild, "~> 0.4", runtime: Mix.env() == :dev},
4343
{:extwitter, "~> 0.13"},
44+
{:finch, "~> 0.14"},
4445
{:floki, "~>0.33.1"},
4546
{:gettext, "~> 0.18"},
4647
{:guardian, "~> 2.0"},
@@ -61,6 +62,7 @@ defmodule Tilex.Mixfile do
6162
{:swoosh, "~> 1.3"},
6263
{:telemetry_metrics, "~> 0.6"},
6364
{:telemetry_poller, "~> 1.0"},
65+
{:tesla, "~> 1.4"},
6466
{:timex, "~> 3.1"},
6567
{:tzdata, "~> 1.1.0"},
6668
{:ueberauth_google, "~> 0.5"},

mix.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
2525
"extwitter": {:hex, :extwitter, "0.13.1", "b984a5d38be2088adb434d0ead3554fff3a301b83b54ba415d608cd55eb4296c", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:oauther, "~> 1.3", [hex: :oauther, repo: "hexpm", optional: false]}], "hexpm", "7806c14cc6bb32dc6ba98d6a2e516ca882fe061a0d7ec64f3f8a33148ad07dba"},
2626
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
27+
"finch": {:hex, :finch, "0.14.0", "619bfdee18fc135190bf590356c4bf5d5f71f916adb12aec94caa3fa9267a4bc", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5459acaf18c4fdb47a8c22fb3baff5d8173106217c8e56c5ba0b93e66501a8dd"},
2728
"floki": {:hex, :floki, "0.33.1", "f20f1eb471e726342b45ccb68edb9486729e7df94da403936ea94a794f072781", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "461035fd125f13fdf30f243c85a0b1e50afbec876cbf1ceefe6fddd2e6d712c6"},
2829
"gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"},
2930
"guardian": {:hex, :guardian, "2.3.0", "1e2a90e809fbd99439f5279db03fb30b7b2b2fc0d3870a0d76a84b099f1a2892", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "ced3ace74fc2b22b2b25dbe99e6d205443dd0d57d1cc25155a223b5e9656205e"},
3031
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
32+
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
3133
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
3234
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.2", "c479398b6de798c03eb5d04a0a9a9159d73508f83f6590a00b8eacba3619cf4c", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "aef6c28585d06a9109ad591507e508854c5559561f950bbaea773900dd369b0e"},
3335
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
@@ -38,7 +40,10 @@
3840
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
3941
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
4042
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
43+
"mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"},
4144
"mochiweb": {:hex, :mochiweb, "2.22.0", "f104d6747c01a330c38613561977e565b788b9170055c5241ac9dd6e4617cba5", [:rebar3], [], "hexpm", "cbbd1fd315d283c576d1c8a13e0738f6dafb63dc840611249608697502a07655"},
45+
"nimble_options": {:hex, :nimble_options, "0.5.2", "42703307b924880f8c08d97719da7472673391905f528259915782bb346e0a1b", [:mix], [], "hexpm", "4da7f904b915fd71db549bcdc25f8d56f378ef7ae07dc1d372cbe72ba950dce0"},
46+
"nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"},
4247
"oauth2": {:hex, :oauth2, "2.0.1", "70729503e05378697b958919bb2d65b002ba6b28c8112328063648a9348aaa3f", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "c64e20d4d105bcdbcbe03170fb530d0eddc3a3e6b135a87528a22c8aecf74c52"},
4348
"oauther": {:git, "https://github.com/tobstarr/oauther.git", "e81fc6588e52eeaf41cdf51ed9d968da46b0b67d", [branch: "master"]},
4449
"optimus": {:hex, :optimus, "0.3.0", "72754d1a06bab7b4b7f59b05622e442e5ab7909b9db5d8b01dc3d2792559fa9e", [:mix], [], "hexpm", "d0d026fdf068e461e1173c463ea2da15e109942135aa4e2490dd2dc9ef05946c"},

0 commit comments

Comments
 (0)