raphael
60888fd63c
- senders, which now have a firebase token are added as their own table in the database - firebase token api call added, which allows a user to connect a sender id to a firebase token - firebase library not yet connected to the saved firebase tokens
67 lines
1.5 KiB
Elixir
67 lines
1.5 KiB
Elixir
defmodule Bump.Database do
|
|
alias Bump.Repo
|
|
alias Bump.Messages.Message
|
|
alias Bump.Senders.Sender
|
|
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
|
|
|
|
def addSender(sender, %{"firebase" => firebase}) do
|
|
Repo.insert(%Sender{sender: sender, firebase_token: firebase})
|
|
end
|
|
|
|
end
|