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:
parent
a1cccbf270
commit
d2fe15d3ae
@ -52,16 +52,32 @@ 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
|
||||
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
|
||||
query = from s in "senders",
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
10
priv/repo/migrations/20220103100336_add_message_title.exs
Normal file
10
priv/repo/migrations/20220103100336_add_message_title.exs
Normal 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
|
Loading…
Reference in New Issue
Block a user