adds message title, adds sender delete

- message now contains a second string
  which is meant for unencrypted title
  information, which can be displayed
  without e2e encryption.
- senders can now be deleted
- the same sender / token connection is
  no longer added to the database
This commit is contained in:
Raphael Maenle 2022-01-03 13:07:33 +01:00
parent a1cccbf270
commit d2fe15d3ae
9 changed files with 53 additions and 17 deletions

View File

@ -52,16 +52,32 @@ defmodule Bump.Database do
Repo.delete_all(query) Repo.delete_all(query)
end end
def push(sender, message) do
def push(sender, title, data) do
time = DateTime.utc_now |> DateTime.truncate(:second) 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 end
def addSender(sender, %{"firebase" => firebase}) do def addSender(sender, %{"firebase" => firebase}) do
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}) Repo.insert(%Sender{sender: sender, firebase_token: firebase})
end end
end
def deleteSender(sender) do
query = from s in "senders",
where: s.sender == ^sender
Repo.delete_all(query)
end
def tokens(sender) do def tokens(sender) do
query = from s in "senders", query = from s in "senders",

View File

@ -5,11 +5,13 @@ defmodule Bump.Firebase do
end end
def push(%{firebase_token: token}, message) do def push(%{"firebase_token" => token, "title" => title, "data" => data}) do
IO.puts "Pushing #{message} to #{token}" IO.puts "Pushing #{title}"
n = Pigeon.FCM.Notification.new({:token, token}, %{"body" => message}) n = Pigeon.FCM.Notification.new(
{:token, token},
%{"body" => title},
%{"data" => data})
Bump.FCM.push(n) Bump.FCM.push(n)
%{status: 'cheese'}
end end
end end

View File

@ -18,12 +18,12 @@ defmodule Bump.Messages do
Database.clear(sender) Database.clear(sender)
end end
def push(sender, message) do def push(sender, title, data) do
Database.push(sender, message) Database.push(sender, title, data)
tokens = Database.tokens(sender) tokens = Database.tokens(sender)
for token <- tokens do for token <- tokens do
Firebase.push(token, message) Firebase.push(%{"firebase_token" => token.firebase_token, "title" => title, "data" => data})
end end
end end

View File

@ -4,6 +4,7 @@ defmodule Bump.Messages.Message do
schema "messages" do schema "messages" do
field :sender, :string field :sender, :string
field :title, :string, default: ""
field :data, :string, default: "" field :data, :string, default: ""
field :timestamp, :utc_datetime field :timestamp, :utc_datetime
timestamps() timestamps()

View File

@ -5,6 +5,10 @@ defmodule Bump.Senders do
Database.addSender(sender, %{"firebase" => firebase}) Database.addSender(sender, %{"firebase" => firebase})
end end
def delete(sender) do
Database.deleteSender(sender)
end
def peek(sender) do def peek(sender) do
Database.peek(sender) Database.peek(sender)
end end

View File

@ -17,11 +17,11 @@ defmodule BumpWeb.MessageController do
def clear(conn, %{"sender" => sender}) do def clear(conn, %{"sender" => sender}) do
Messages.clear(sender) Messages.clear(sender)
text conn, Jason.encode!(%{"status" => "OK"}) text conn, Jason.encode!(%{"status" => "OK"})
end 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"}) text conn, Jason.encode!(%{"status" => "OK"})
end end
@ -36,8 +36,4 @@ defmodule BumpWeb.MessageController do
text conn, Jason.encode!(message) text conn, Jason.encode!(message)
end end
def firebase(conn, %{"sender" => sender, "token" => token}) do
Senders.push(sender, token)
text conn, Jason.encode!(%{"status" => "OK"})
end
end end

View File

@ -7,4 +7,10 @@ defmodule BumpWeb.SenderController do
Senders.firebase(sender, token) Senders.firebase(sender, token)
text conn, Jason.encode!(%{"status" => "OK"}) text conn, Jason.encode!(%{"status" => "OK"})
end end
def deleteSender(conn, %{"sender" => sender}) do
Senders.delete(sender)
text conn, Jason.encode!(%{"status" => "OK"})
end
end end

View File

@ -13,6 +13,7 @@ defmodule BumpWeb.Router do
post "/list", MessageController, :list post "/list", MessageController, :list
post "/clear", MessageController, :clear post "/clear", MessageController, :clear
post "/firebase", SenderController, :firebase post "/firebase", SenderController, :firebase
post "/delete_sender", SenderController, :deleteSender
end end

View File

@ -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