diff --git a/lib/bump/database.ex b/lib/bump/database.ex index 8ee4ea9..fd0d6fb 100644 --- a/lib/bump/database.ex +++ b/lib/bump/database.ex @@ -52,15 +52,31 @@ defmodule Bump.Database do Repo.delete_all(query) end - def push(sender, message) do + + def push(sender, title, data) do time = DateTime.utc_now |> DateTime.truncate(:second) - Repo.insert(%Message{sender: sender, data: message, timestamp: time}) + Repo.insert(%Message{sender: sender, title: title, data: data, timestamp: time}) end def addSender(sender, %{"firebase" => firebase}) do - Repo.insert(%Sender{sender: sender, firebase_token: firebase}) + query = from s in "senders", + where: s.sender == ^sender and + s.firebase_token == ^firebase, + select: %{id: s.id} + + if(Repo.all(query) == []) do + IO.puts("Adding sender: #{sender}") + Repo.insert(%Sender{sender: sender, firebase_token: firebase}) + end + end + + def deleteSender(sender) do + query = from s in "senders", + where: s.sender == ^sender + + Repo.delete_all(query) end def tokens(sender) do diff --git a/lib/bump/firebase.ex b/lib/bump/firebase.ex index f0d86e0..1f97023 100644 --- a/lib/bump/firebase.ex +++ b/lib/bump/firebase.ex @@ -5,11 +5,13 @@ defmodule Bump.Firebase do end - def push(%{firebase_token: token}, message) do - IO.puts "Pushing #{message} to #{token}" - n = Pigeon.FCM.Notification.new({:token, token}, %{"body" => message}) + def push(%{"firebase_token" => token, "title" => title, "data" => data}) do + IO.puts "Pushing #{title}" + n = Pigeon.FCM.Notification.new( + {:token, token}, + %{"body" => title}, + %{"data" => data}) Bump.FCM.push(n) - %{status: 'cheese'} end end diff --git a/lib/bump/messages.ex b/lib/bump/messages.ex index 7aa11e3..db8925a 100644 --- a/lib/bump/messages.ex +++ b/lib/bump/messages.ex @@ -18,12 +18,12 @@ defmodule Bump.Messages do Database.clear(sender) end - def push(sender, message) do - Database.push(sender, message) + def push(sender, title, data) do + Database.push(sender, title, data) tokens = Database.tokens(sender) for token <- tokens do - Firebase.push(token, message) + Firebase.push(%{"firebase_token" => token.firebase_token, "title" => title, "data" => data}) end end diff --git a/lib/bump/messages/message.ex b/lib/bump/messages/message.ex index e3e0412..6047532 100644 --- a/lib/bump/messages/message.ex +++ b/lib/bump/messages/message.ex @@ -4,6 +4,7 @@ defmodule Bump.Messages.Message do schema "messages" do field :sender, :string + field :title, :string, default: "" field :data, :string, default: "" field :timestamp, :utc_datetime timestamps() diff --git a/lib/bump/senders.ex b/lib/bump/senders.ex index f844e82..9a822b0 100644 --- a/lib/bump/senders.ex +++ b/lib/bump/senders.ex @@ -5,6 +5,10 @@ defmodule Bump.Senders do Database.addSender(sender, %{"firebase" => firebase}) end + def delete(sender) do + Database.deleteSender(sender) + end + def peek(sender) do Database.peek(sender) end diff --git a/lib/bump_web/controllers/message_controller.ex b/lib/bump_web/controllers/message_controller.ex index f802496..c8fac02 100644 --- a/lib/bump_web/controllers/message_controller.ex +++ b/lib/bump_web/controllers/message_controller.ex @@ -17,11 +17,11 @@ defmodule BumpWeb.MessageController do def clear(conn, %{"sender" => sender}) do Messages.clear(sender) text conn, Jason.encode!(%{"status" => "OK"}) - end - def push(conn, %{"sender" => sender, "data" => message}) do - Messages.push(sender, message) + + def push(conn, %{"sender" => sender, "title"=> title, "data" => data}) do + Messages.push(sender, title, data) text conn, Jason.encode!(%{"status" => "OK"}) end @@ -36,8 +36,4 @@ defmodule BumpWeb.MessageController do text conn, Jason.encode!(message) end - def firebase(conn, %{"sender" => sender, "token" => token}) do - Senders.push(sender, token) - text conn, Jason.encode!(%{"status" => "OK"}) - end end diff --git a/lib/bump_web/controllers/sender_controller.ex b/lib/bump_web/controllers/sender_controller.ex index 56e4405..baeb46b 100644 --- a/lib/bump_web/controllers/sender_controller.ex +++ b/lib/bump_web/controllers/sender_controller.ex @@ -7,4 +7,10 @@ defmodule BumpWeb.SenderController do Senders.firebase(sender, token) text conn, Jason.encode!(%{"status" => "OK"}) end + + def deleteSender(conn, %{"sender" => sender}) do + Senders.delete(sender) + text conn, Jason.encode!(%{"status" => "OK"}) + + end end diff --git a/lib/bump_web/router.ex b/lib/bump_web/router.ex index ff76710..5b810a5 100644 --- a/lib/bump_web/router.ex +++ b/lib/bump_web/router.ex @@ -13,6 +13,7 @@ defmodule BumpWeb.Router do post "/list", MessageController, :list post "/clear", MessageController, :clear post "/firebase", SenderController, :firebase + post "/delete_sender", SenderController, :deleteSender end diff --git a/priv/repo/migrations/20220103100336_add_message_title.exs b/priv/repo/migrations/20220103100336_add_message_title.exs new file mode 100644 index 0000000..c709d94 --- /dev/null +++ b/priv/repo/migrations/20220103100336_add_message_title.exs @@ -0,0 +1,10 @@ +defmodule Bump.Repo.Migrations.AddMessageTitle do + use Ecto.Migration + + def change do + alter table :messages do + add :title, :string, size: 1024 + end + + end +end