Skip to content

Commit

Permalink
feat: create linkedin post when TIL is created
Browse files Browse the repository at this point in the history
avogel3 committed Feb 10, 2023
1 parent c835e42 commit fcd1ad3
Showing 7 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -50,6 +50,9 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Use Finch and Tesla for HTTP client
config :tesla, adapter: Tilex.Finch

# Provide reasonable default for configuration options
config :tilex, :page_size, 5
config :tilex, :auth_controller, AuthController
@@ -67,6 +70,7 @@ config :tilex, :slack_endpoint, "https://hooks.slack.com#{System.get_env("slack_
config :tilex, :banner_image_source, System.get_env("BANNER_IMAGE_SOURCE")
config :tilex, :banner_image_link, System.get_env("BANNER_IMAGE_LINK")
config :tilex, :banner_image_alt, System.get_env("BANNER_IMAGE_ALT")
config :tilex, :linkedin_organization_id, System.get_env("LINKEDIN_ORGANIZATION_ID")

config :tilex,
:rate_limiter_requests_per_time_period,
15 changes: 15 additions & 0 deletions lib/test/notifications/notifiers/linkedin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Test.Notifications.Notifiers.Linkedin do
use Tilex.Notifications.Notifier

def handle_post_created(_post, _developer, _channel, _url) do
:ok
end

def handle_post_liked(_post, _developer, _url) do
:ok
end

def handle_page_views_report(_report) do
:ok
end
end
3 changes: 2 additions & 1 deletion lib/tilex/application.ex
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@ defmodule Tilex.Application do
{Cachex, name: :tilex_cache},
Tilex.Notifications,
Tilex.RateLimiter,
Tilex.Notifications.NotifiersSupervisor
Tilex.Notifications.NotifiersSupervisor,
{Finch, name: Tilex.Finch}
]

:telemetry.attach(
35 changes: 35 additions & 0 deletions lib/tilex/linkedin_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Tilex.LinkedinApi do
use Tesla, only: [:post]

plug Tesla.Middleware.BaseUrl, "https://api.linkedin.com/v2"
plug Tesla.Middleware.Headers, [{"Content-Type", "application/json"}]
plug Tesla.Middleware.BearerAuth, token: Application.get_env(:tilex, :linkedin_access_token)
plug Tesla.Middleware.JSON

def create_post(post_body) do
post("/posts", post_body)
end

def create_post_body(commentary, title, url) do
%{
author: "urn:li:organization:" <> org_id(),
commentary: commentary,
visibility: "PUBLIC",
distribution: %{
feedDistribution: "MAIN_FEED",
targetEntities: [],
thirdPartyDistributionChannels: []
},
content: %{
article: %{
source: url,
title: title
}
},
lifecycleState: "PUBLISHED",
isReshareDisabledByAuthor: false
}
end

defp org_id, do: Application.get_env(:tilex, :linkedin_organization_id)
end
24 changes: 24 additions & 0 deletions lib/tilex/notifications/notifiers/linkedin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Tilex.Notifications.Notifiers.Linkedin do
alias Tilex.Blog.Developer

import Tilex.LinkedinApi

use Tilex.Notifications.Notifier

@impl true
def handle_post_created(post, developer, channel, url) do
"#{post.title} #{url} via @#{Developer.twitter_handle(developer)} #til ##{channel.twitter_hashtag}"
|> create_post_body(post.title, url)
|> create_post()
end

@impl true
def handle_post_liked(_post, _dev, _url) do
:ok
end

@impl true
def handle_page_views_report(_report) do
:ok
end
end
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ defmodule Tilex.Mixfile do
{:ecto_sql, "~> 3.6"},
{:esbuild, "~> 0.4", runtime: Mix.env() == :dev},
{:extwitter, "~> 0.13"},
{:finch, "~> 0.14"},
{:floki, "~>0.33.1"},
{:gettext, "~> 0.18"},
{:guardian, "~> 2.0"},
@@ -61,6 +62,7 @@ defmodule Tilex.Mixfile do
{:swoosh, "~> 1.3"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:tesla, "~> 1.4"},
{:timex, "~> 3.1"},
{:tzdata, "~> 1.1.0"},
{:ueberauth_google, "~> 0.5"},
5 changes: 5 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -24,10 +24,12 @@
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
"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"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"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"},
"floki": {:hex, :floki, "0.33.1", "f20f1eb471e726342b45ccb68edb9486729e7df94da403936ea94a794f072781", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "461035fd125f13fdf30f243c85a0b1e50afbec876cbf1ceefe6fddd2e6d712c6"},
"gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"},
"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"},
"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"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.2", "c479398b6de798c03eb5d04a0a9a9159d73508f83f6590a00b8eacba3619cf4c", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "aef6c28585d06a9109ad591507e508854c5559561f950bbaea773900dd369b0e"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
@@ -38,7 +40,10 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"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"},
"mochiweb": {:hex, :mochiweb, "2.22.0", "f104d6747c01a330c38613561977e565b788b9170055c5241ac9dd6e4617cba5", [:rebar3], [], "hexpm", "cbbd1fd315d283c576d1c8a13e0738f6dafb63dc840611249608697502a07655"},
"nimble_options": {:hex, :nimble_options, "0.5.2", "42703307b924880f8c08d97719da7472673391905f528259915782bb346e0a1b", [:mix], [], "hexpm", "4da7f904b915fd71db549bcdc25f8d56f378ef7ae07dc1d372cbe72ba950dce0"},
"nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"},
"oauth2": {:hex, :oauth2, "2.0.1", "70729503e05378697b958919bb2d65b002ba6b28c8112328063648a9348aaa3f", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "c64e20d4d105bcdbcbe03170fb530d0eddc3a3e6b135a87528a22c8aecf74c52"},
"oauther": {:git, "https://github.com/tobstarr/oauther.git", "e81fc6588e52eeaf41cdf51ed9d968da46b0b67d", [branch: "master"]},
"optimus": {:hex, :optimus, "0.3.0", "72754d1a06bab7b4b7f59b05622e442e5ab7909b9db5d8b01dc3d2792559fa9e", [:mix], [], "hexpm", "d0d026fdf068e461e1173c463ea2da15e109942135aa4e2490dd2dc9ef05946c"},

0 comments on commit fcd1ad3

Please sign in to comment.