adds Firebase communication stump
- Firebase connection via Pigeon library - sends bump to a single token at the moment - encrypted test is sent as notification message
This commit is contained in:
@ -15,15 +15,17 @@ defmodule Bump.Application do
|
||||
# Start the PubSub system
|
||||
{Phoenix.PubSub, name: Bump.PubSub},
|
||||
# Start the Endpoint (http/https)
|
||||
BumpWeb.Endpoint
|
||||
BumpWeb.Endpoint,
|
||||
# Start a worker by calling: Bump.Worker.start_link(arg)
|
||||
# {Bump.Worker, arg}
|
||||
Bump.FCM
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Bump.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
|
||||
end
|
||||
|
||||
# Tell Phoenix to update the endpoint configuration
|
||||
|
62
lib/bump/database.ex
Normal file
62
lib/bump/database.ex
Normal file
@ -0,0 +1,62 @@
|
||||
defmodule Bump.Database do
|
||||
alias Bump.Repo
|
||||
alias Bump.Messages.Message
|
||||
import Ecto.Query
|
||||
|
||||
def pop(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender,
|
||||
order_by: [desc: m.timestamp],
|
||||
limit: 1,
|
||||
select: %{id: m.id, data: m.data, timestamp: m.timestamp}
|
||||
|
||||
res = Repo.one(query)
|
||||
|
||||
if not is_nil(res) do
|
||||
Repo.delete_all(from m in "messages", where: m.id == ^res.id)
|
||||
%{data: res.data, timestamp: res.timestamp}
|
||||
else
|
||||
%{}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def peek(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender,
|
||||
order_by: [desc: m.timestamp],
|
||||
limit: 1,
|
||||
select: %{data: m.data, timestamp: m.timestamp}
|
||||
Repo.one(query)
|
||||
end
|
||||
|
||||
def list(sender, minutes) do
|
||||
|
||||
ago = DateTime.utc_now
|
||||
|> Timex.shift(minutes: -minutes)
|
||||
|> DateTime.truncate(:second)
|
||||
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender and
|
||||
m.timestamp >= ^ago,
|
||||
select: %{data: m.data, timestamp: m.timestamp}
|
||||
|
||||
Repo.all(query)
|
||||
end
|
||||
|
||||
def clear(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender
|
||||
|
||||
Repo.delete_all(query)
|
||||
end
|
||||
|
||||
def push(sender, message) do
|
||||
time = DateTime.utc_now |> DateTime.truncate(:second)
|
||||
|
||||
Repo.insert(%Message{sender: sender, data: message, timestamp: time})
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
14
lib/bump/firebase.ex
Normal file
14
lib/bump/firebase.ex
Normal file
@ -0,0 +1,14 @@
|
||||
defmodule Bump.Firebase do
|
||||
|
||||
def test() do
|
||||
IO.puts "Hello, world!"
|
||||
end
|
||||
|
||||
def push(sender, message) do
|
||||
IO.puts "Pushing #{message} to #{sender}"
|
||||
n = Pigeon.FCM.Notification.new({:token, "dTm8S2bfTdKYQTjrxnwFFg:APA91bHGgM7IdRS5uxD0ljmwmP6cAec2icX0VBs69iRB2ApsohyOWzTzontO7cBkjNYbWV87zvxrXIs5jHkJ-8mSWa_-RiU2Y8-XEy3g-Fep3z6dhDeM3KazP58jDRgbdB5cVpDcIEWL"}, %{"body" => message})
|
||||
Bump.FCM.push(n)
|
||||
%{status: 'cheese'}
|
||||
end
|
||||
|
||||
end
|
@ -1,61 +1,26 @@
|
||||
defmodule Bump.Messages do
|
||||
alias Bump.Repo
|
||||
alias Bump.Messages.Message
|
||||
import Ecto.Query
|
||||
alias Bump.Database
|
||||
alias Bump.Firebase
|
||||
|
||||
def pop(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender,
|
||||
order_by: [desc: m.timestamp],
|
||||
limit: 1,
|
||||
select: %{id: m.id, data: m.data, timestamp: m.timestamp}
|
||||
|
||||
res = Repo.one(query)
|
||||
|
||||
if not is_nil(res) do
|
||||
Repo.delete_all(from m in "messages", where: m.id == ^res.id)
|
||||
%{data: res.data, timestamp: res.timestamp}
|
||||
else
|
||||
%{}
|
||||
end
|
||||
|
||||
Database.pop(sender)
|
||||
end
|
||||
|
||||
def peek(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender,
|
||||
order_by: [desc: m.timestamp],
|
||||
limit: 1,
|
||||
select: %{data: m.data, timestamp: m.timestamp}
|
||||
Repo.one(query)
|
||||
Database.peek(sender)
|
||||
end
|
||||
|
||||
def list(sender, minutes) do
|
||||
|
||||
ago = DateTime.utc_now
|
||||
|> Timex.shift(minutes: -minutes)
|
||||
|> DateTime.truncate(:second)
|
||||
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender and
|
||||
m.timestamp >= ^ago,
|
||||
select: %{data: m.data, timestamp: m.timestamp}
|
||||
|
||||
Repo.all(query)
|
||||
Database.list(sender, minutes)
|
||||
end
|
||||
|
||||
def clear(sender) do
|
||||
query = from m in "messages",
|
||||
where: m.sender == ^sender
|
||||
|
||||
Repo.delete_all(query)
|
||||
Database.clear(sender)
|
||||
end
|
||||
|
||||
def push(sender, message) do
|
||||
time = DateTime.utc_now |> DateTime.truncate(:second)
|
||||
|
||||
Repo.insert(%Message{sender: sender, data: message, timestamp: time})
|
||||
|
||||
Database.push(sender, message)
|
||||
Firebase.push(sender, message)
|
||||
end
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user