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: some more tests and light refactors #9

Merged
merged 1 commit into from
Mar 15, 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
97 changes: 0 additions & 97 deletions lib/tr/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,108 +18,11 @@ defmodule Tr.Release do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end

def start_tracker() do
start_app()
track_posts()

if length(Tr.Tracker.get_unannounced_posts()) > 0 do
announce_posts()
end
end

defp repos do
Application.fetch_env!(@app, :ecto_repos)
end

defp load_app do
Application.load(@app)
end

defp start_app do
load_app()
Application.ensure_all_started(@app)
end

defp track_posts do
posts = Tr.Blog.all_posts()

posts
|> Enum.each(fn row -> track_post(row) end)
end

defp track_post(row) do
post = Tr.Tracker.get_post_by_slug(row.id)

unless post do
Tr.Tracker.track_post(%{slug: row.id, announced: false})
end
end

defp announce_posts do
unanounced_posts = Tr.Tracker.get_unannounced_posts()
notifiable_users = Tr.Accounts.get_all_notifiable_users()

if length(unanounced_posts) > 1 do
url = TrWeb.Endpoint.url() <> "/blog/"
subject = "This is what you have missed so far..."
titles = Enum.map(unanounced_posts, fn p -> p.slug end)

# Fetch all posts
posts =
Enum.map(unanounced_posts, fn p -> Tr.Blog.get_post_by_slug(p.slug) end)

descriptions =
Enum.map(posts, fn p ->
Map.get(p, :description)
end)

# Create the body based in the title and descriptions
body =
Enum.join(
Enum.map(List.zip([titles, descriptions]), fn p ->
elem(p, 0) <> "\n" <> elem(p, 1) <> "\n" <> url <> elem(p, 0) <> "\n\n"
end)
)

Enum.each(unanounced_posts, fn post ->
# Mark posts as already announced
Tr.Tracker.update_post_status(post, %{announced: true})
end)

notify_users(notifiable_users, subject, body, url)
else
unannounced_post = hd(unanounced_posts)
post = Tr.Blog.get_post_by_slug(unannounced_post.slug)
url = TrWeb.Endpoint.url() <> "/blog/" <> post.id
subject = post.title
body = post.title <> "\n" <> post.description <> "\n" <> url

# Mark post as already announced
Tr.Tracker.update_post_status(unannounced_post, %{announced: true})

# Fire in the hole
notify_users(notifiable_users, subject, body, url)
end
end

defp notify_users(users, subject, body, url) do
tasks =
Enum.map(users, fn user ->
# Fire in the hole
Task.Supervisor.async_nolink(
Tr.TaskSupervisor,
fn ->
Tr.PostTracker.Notifier.deliver_new_post_notification(
user,
subject,
body,
url
)
end
)
end)

tasks
|> Enum.map(&Task.await/1)
end
end
105 changes: 104 additions & 1 deletion lib/tr/tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Tr.Tracker do
The Tracker context.
"""

@app :tr

import Ecto.Query, warn: false
alias Tr.Repo

Expand All @@ -20,7 +22,7 @@ defmodule Tr.Tracker do
end

## Database setters
def track_post(attrs) do
def insert_post(attrs) do
%PostTracker{}
|> PostTracker.changeset(attrs)
|> Repo.insert()
Expand All @@ -39,4 +41,105 @@ defmodule Tr.Tracker do
{:error, :post_tracker, changeset, _} -> {:error, changeset}
end
end

defp load_app do
Application.load(@app)
end

defp start_app do
load_app()
Application.ensure_all_started(@app)
end

def start() do
start_app()
track_posts()

if length(get_unannounced_posts()) > 0 do
announce_posts()
end
end

defp track_posts do
posts = Tr.Blog.all_posts()

posts
|> Enum.each(fn row -> track_post(row) end)
end

defp track_post(row) do
post = get_post_by_slug(row.id)

unless post do
insert_post(%{slug: row.id, announced: false})
end
end

defp announce_posts do
unanounced_posts = get_unannounced_posts()
notifiable_users = Tr.Accounts.get_all_notifiable_users()

if length(unanounced_posts) > 1 do
url = TrWeb.Endpoint.url() <> "/blog/"
subject = "This is what you have missed so far..."
titles = Enum.map(unanounced_posts, fn p -> p.slug end)

# Fetch all posts
posts =
Enum.map(unanounced_posts, fn p -> Tr.Blog.get_post_by_slug(p.slug) end)

descriptions =
Enum.map(posts, fn p ->
Map.get(p, :description)
end)

# Create the body based in the title and descriptions
body =
Enum.join(
Enum.map(List.zip([titles, descriptions]), fn p ->
elem(p, 0) <> "\n" <> elem(p, 1) <> "\n" <> url <> elem(p, 0) <> "\n\n"
end)
)

Enum.each(unanounced_posts, fn post ->
# Mark posts as already announced
update_post_status(post, %{announced: true})
end)

notify_users(notifiable_users, subject, body, url)
else
unannounced_post = hd(unanounced_posts)
post = Tr.Blog.get_post_by_slug(unannounced_post.slug)
url = TrWeb.Endpoint.url() <> "/blog/" <> post.id
subject = post.title
body = post.title <> "\n" <> post.description <> "\n" <> url

# Mark post as already announced
update_post_status(unannounced_post, %{announced: true})

# Fire in the hole
notify_users(notifiable_users, subject, body, url)
end
end

defp notify_users(users, subject, body, url) do
tasks =
Enum.map(users, fn user ->
# Fire in the hole
Task.Supervisor.async_nolink(
Tr.TaskSupervisor,
fn ->
Tr.PostTracker.Notifier.deliver_new_post_notification(
user,
subject,
body,
url
)
end
)
end)

tasks
|> Enum.map(&Task.await/1)
end
end
2 changes: 0 additions & 2 deletions lib/tr_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ defmodule TrWeb.Endpoint do

# Remote ip parser
plug RemoteIp
plug PlugForwardedPeer
plug TrWeb.LoggerPlug

# Serve at "/" the static files from "priv/static" directory.
#
Expand Down
45 changes: 0 additions & 45 deletions lib/tr_web/logger_plug.ex

This file was deleted.

52 changes: 0 additions & 52 deletions lib/tr_web/plug_forwarded_peer.ex

This file was deleted.

2 changes: 1 addition & 1 deletion manifests/app/06-cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
command:
- /app/bin/tr
- eval
- Tr.Release.start_tracker
- Tr.Tracker.start_tracker
envFrom:
- secretRef:
name: tr-postgres-config
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ defmodule Tr.MixProject do
{:faker, "~> 0.18"},
{:excoveralls, "~> 0.18", only: :test},
{:git_hooks, "~> 0.7.0", only: [:dev], runtime: false},
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false}
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mint": {:hex, :mint, "1.5.2", "4805e059f96028948870d23d7783613b7e6b0e2fb4e98d720383852a760067fd", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "d77d9e9ce4eb35941907f1d3df38d8f750c357865353e21d335bdcdf6d892a02"},
"mix_test_watch": {:hex, :mix_test_watch, "1.2.0", "1f9acd9e1104f62f280e30fc2243ae5e6d8ddc2f7f4dc9bceb454b9a41c82b42", [:mix], [{:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "278dc955c20b3fb9a3168b5c2493c2e5cffad133548d307e0a50c7f2cfbf34f6"},
"mochiweb": {:hex, :mochiweb, "3.2.1", "ff287e1ec653a0828f226cd5a009d52be74537dc3fc274b765525a77ce01f8ec", [:rebar3], [], "hexpm", "975466d335403a78cd58186636b8e960e3c84c4d9c1a85eb7fe53b6a5dd54de7"},
"nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
Expand Down
2 changes: 1 addition & 1 deletion test/support/fixtures/post_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Tr.PostFixtures do
import Tr.AccountsFixtures

@doc """
Generate a comment.
Generate a post.
"""
def post_fixture(attrs \\ %{}) do
post =
Expand Down
20 changes: 20 additions & 0 deletions test/support/fixtures/post_tracker_fixtures.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Tr.PostTrackerFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Tr.Post` context.
"""

@doc """
Generates a PostTracker entry
"""
def post_tracker_fixture(attrs \\ %{}) do
post =
attrs
|> Enum.into(%{
slug: "some-body",
announced: true
})

post
end
end
Loading
Loading