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

feat(social): post to linkedin when TIL is created #824

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ 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, {Tesla.Adapter.Finch, name: Tilex.Finch}

# Provide reasonable default for configuration options
config :tilex, :page_size, 5
config :tilex, :auth_controller, AuthController
config :tilex, :slack_notifier, Tilex.Notifications.Notifiers.Slack
config :tilex, :twitter_notifier, Tilex.Notifications.Notifiers.Twitter
config :tilex, :linkedin_notifier, Tilex.Notifications.Notifiers.Linkedin
config :tilex, :organization_name, System.get_env("ORGANIZATION_NAME")
config :tilex, :canonical_domain, System.get_env("CANONICAL_DOMAIN")
config :tilex, :default_twitter_handle, System.get_env("DEFAULT_TWITTER_HANDLE")
Expand Down
4 changes: 4 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ if config_env() == :prod do
# config :swoosh, :api_client, Swoosh.ApiClient.Hackney
#
# See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details.

config :tilex, Tilex.LinkedinApi,
access_token: System.get_env("LINKEDIN_ACCESS_TOKEN"),
organization_id: System.get_env("LINKEDIN_ORGANIZATION_ID")
end
7 changes: 7 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ config :tilex, :hosted_domain, "hashrocket.com"
config :tilex, :auth_controller, Test.AuthController
config :tilex, :slack_notifier, Test.Notifications.Notifiers.Slack
config :tilex, :twitter_notifier, Test.Notifications.Notifiers.Twitter
config :tilex, :linkedin_notifier, Test.Notifications.Notifiers.Linkedin
config :tilex, :date_time_module, Tilex.DateTimeMock
config :tilex, :date_display_tz, "America/Chicago"
config :tilex, :slack_endpoint, "https://slack.test.com/abc/123"
Expand All @@ -54,3 +55,9 @@ config :wallaby,
config :tilex, :request_tracking, true

config :appsignal, :config, active: false

config :tilex, Tilex.LinkedinApi,
access_token: "my-access-token",
organization_id: "my-org-id"

config :tesla, adapter: Tesla.Mock
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
Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion lib/tilex/blog/developer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Tilex.Blog.Developer do
field(:twitter_handle, :string)
field(:admin, :boolean)
field(:editor, :string)
field(:name, :string)

has_many(:posts, Post)

Expand All @@ -21,7 +22,7 @@ defmodule Tilex.Blog.Developer do

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email, :username, :twitter_handle, :editor])
|> cast(params, [:email, :username, :twitter_handle, :editor, :name])
|> validate_required([:email, :username])
|> clean_twitter_handle
end
Expand All @@ -40,6 +41,9 @@ defmodule Tilex.Blog.Developer do
end
end

def name(%Developer{name: name}) when is_binary(name), do: name
def name(developer), do: twitter_handle(developer)

def twitter_handle(%Developer{twitter_handle: twitter_handle}) do
twitter_handle || Application.get_env(:tilex, :default_twitter_handle)
end
Expand Down
49 changes: 49 additions & 0 deletions lib/tilex/linkedin_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Tilex.LinkedinApi do
use Tesla, only: [:post]

@middleware [
{Tesla.Middleware.BaseUrl, "https://api.linkedin.com/v2"},
Tesla.Middleware.JSON
]

def create_post(commentary, title, url) do
body = %{
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
}

client()
|> post("/posts", body)
|> case do
{:ok, %Tesla.Env{status: 200}} -> :ok
env -> {:error, env}
end
end

defp client() do
middleware =
[
{Tesla.Middleware.BearerAuth, token: access_token()}
] ++ @middleware

Tesla.client(middleware)
end

defp config, do: Application.get_env(:tilex, __MODULE__)
defp org_id, do: Keyword.fetch!(config(), :organization_id)
defp access_token, do: Keyword.fetch!(config(), :access_token)
end
23 changes: 23 additions & 0 deletions lib/tilex/notifications/notifiers/linkedin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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} by #{Developer.name(developer)} #til ##{channel.twitter_hashtag}"
|> create_post(post.title, url)
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
4 changes: 3 additions & 1 deletion lib/tilex/notifications/notifiers_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ defmodule Tilex.Notifications.NotifiersSupervisor do
def children do
[
slack_notifier(),
twitter_notifier()
twitter_notifier(),
linkedin_notifier()
]
end

defp slack_notifier, do: Application.get_env(:tilex, :slack_notifier)
defp twitter_notifier, do: Application.get_env(:tilex, :twitter_notifier)
defp linkedin_notifier, do: Application.get_env(:tilex, :linkedin_notifier)
end
3 changes: 2 additions & 1 deletion lib/tilex_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule TilexWeb.AuthController do
{:ok, email} ->
attrs = %{
email: email,
username: Developer.format_username(name)
username: Developer.format_username(name),
name: name
}

Developer.find_or_create(Repo, attrs)
Expand Down
2 changes: 2 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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.34"},
{:gettext, "~> 0.18"},
{:guardian, "~> 2.0"},
Expand All @@ -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"},
Expand Down
5 changes: 5 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"expo": {:hex, :expo, "0.1.0", "d4e932bdad052c374118e312e35280f1919ac13881cb3ac07a209a54d0c81dd8", [:mix], [], "hexpm", "c22c536021c56de058aaeedeabb4744eb5d48137bacf8c29f04d25b6c6bbbf45"},
"extwitter": {:hex, :extwitter, "0.14.0", "5e15c5ea5e6a09baaf03fd5da6de1431eeeca0ef05a9c0f6cc62872e5b8816ed", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:oauther, "~> 1.3", [hex: :oauther, repo: "hexpm", optional: false]}], "hexpm", "5e9bbb491531317062df42ab56ccb8368addb00931b0785c8a5e1d652813b0a8"},
"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.34.0", "002d0cc194b48794d74711731db004fafeb328fe676976f160685262d43706a8", [:mix], [], "hexpm", "9c3a9f43f40dde00332a589bd9d389b90c1f518aef500364d00636acc5ebc99c"},
"gettext": {:hex, :gettext, "0.21.0", "15bbceb20b317b706a8041061a08e858b5a189654128618b53746bf36c84352b", [:mix], [{:expo, "~> 0.1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "04a66db4103b6d1d18f92240bb2c73167b517229316b7bef84e4eebbfb2f14f6"},
"guardian": {:hex, :guardian, "2.3.1", "2b2d78dc399a7df182d739ddc0e566d88723299bfac20be36255e2d052fd215d", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "bbe241f9ca1b09fad916ad42d6049d2600bbc688aba5b3c4a6c82592a54274c3"},
"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_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"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
Expand All @@ -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.1.0", "beb657f393814a3a7a8a15bd5e5776ecae341fd344df425342a3b6f1904c2989", [:mix], [{:tesla, "~> 1.5", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "8ac07f85b3307dd1acfeb0ec852f64161b22f57d0ce0c15e616a1dfc8ebe2b41"},
"oauther": {:git, "https://github.com/tobstarr/oauther.git", "e81fc6588e52eeaf41cdf51ed9d968da46b0b67d", [branch: "master"]},
"optimus": {:hex, :optimus, "0.3.0", "72754d1a06bab7b4b7f59b05622e442e5ab7909b9db5d8b01dc3d2792559fa9e", [:mix], [], "hexpm", "d0d026fdf068e461e1173c463ea2da15e109942135aa4e2490dd2dc9ef05946c"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Tilex.Repo.Migrations.AddNameToDevelopers do
use Ecto.Migration

def change do
alter table(:developers) do
add(:name, :text)
end
end
end
4 changes: 3 additions & 1 deletion priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ CREATE TABLE public.developers (
updated_at timestamp(0) without time zone NOT NULL,
twitter_handle character varying(255),
admin boolean DEFAULT false,
editor character varying(255) DEFAULT 'Text Field'::character varying
editor character varying(255) DEFAULT 'Text Field'::character varying,
name text
);


Expand Down Expand Up @@ -295,3 +296,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20190827182708);
INSERT INTO public."schema_migrations" (version) VALUES (20200518184142);
INSERT INTO public."schema_migrations" (version) VALUES (20220425135720);
INSERT INTO public."schema_migrations" (version) VALUES (20220429184256);
INSERT INTO public."schema_migrations" (version) VALUES (20230127213117);
44 changes: 25 additions & 19 deletions test/lib/tilex/tracking_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,86 +88,92 @@ defmodule Tilex.TrackingTest do
channel = Factory.insert!(:channel, name: "example-channel")

Factory.insert!(:post, title: "The Enderman Returns", slug: "101", channel: channel)
|> create_requests_for_post(view_count: 4)
|> create_requests_for_post(view_count: 10)

Factory.insert!(:post, title: "Subnautica", slug: "102", channel: channel)
|> create_requests_for_post(view_count: 3)
|> create_requests_for_post(view_count: 9)

Factory.insert!(:post, title: "Spiderman", slug: "103", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 8)

Factory.insert!(:post, title: "Witcher 3", slug: "104", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 7)

Factory.insert!(:post, title: "Doom", slug: "105", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 6)

Factory.insert!(:post, title: "Doom 2", slug: "106", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 5)

Factory.insert!(:post, title: "Doom 3", slug: "107", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 4)

Factory.insert!(:post, title: "Castle Wolfenstein", slug: "108", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 3)

Factory.insert!(:post, title: "Minecraft", slug: "109", channel: channel)
|> create_requests_for_post(view_count: 2)

Factory.insert!(:post, title: "Red Dead Redemption", slug: "110", channel: channel)
|> create_requests_for_post(view_count: 2)
|> create_requests_for_post(view_count: 1)

Factory.insert!(:post, title: "Halo", slug: "111")

Factory.insert!(
:request,
page: "/posts/111-halo",
request_time: start_date |> DateTime.truncate(:second)
request_time: start_date |> DateTime.add(-2) |> DateTime.truncate(:second)
)

Factory.insert!(
:request,
page: "/posts/111-halo",
request_time: start_date |> DateTime.add(-1) |> DateTime.truncate(:second)
)

expected_stats = [
%{
channel_name: "example-channel",
title: "The Enderman Returns",
url: "/posts/101-the-enderman-returns",
view_count: 4
view_count: 10
},
%{
channel_name: "example-channel",
title: "Subnautica",
url: "/posts/102-subnautica",
view_count: 3
view_count: 9
},
%{
channel_name: "example-channel",
title: "Spiderman",
url: "/posts/103-spiderman",
view_count: 2
view_count: 8
},
%{
channel_name: "example-channel",
title: "Witcher 3",
url: "/posts/104-witcher-3",
view_count: 2
view_count: 7
},
%{channel_name: "example-channel", title: "Doom", url: "/posts/105-doom", view_count: 2},
%{channel_name: "example-channel", title: "Doom", url: "/posts/105-doom", view_count: 6},
%{
channel_name: "example-channel",
title: "Doom 2",
url: "/posts/106-doom-2",
view_count: 2
view_count: 5
},
%{
channel_name: "example-channel",
title: "Doom 3",
url: "/posts/107-doom-3",
view_count: 2
view_count: 4
},
%{
channel_name: "example-channel",
title: "Castle Wolfenstein",
url: "/posts/108-castle-wolfenstein",
view_count: 2
view_count: 3
},
%{
channel_name: "example-channel",
Expand All @@ -179,7 +185,7 @@ defmodule Tilex.TrackingTest do
channel_name: "example-channel",
title: "Red Dead Redemption",
url: "/posts/110-red-dead-redemption",
view_count: 2
view_count: 1
}
]

Expand Down
Loading