2021-12-30 13:06:26 +01:00
|
|
|
defmodule Bump.Database do
|
|
|
|
alias Bump.Repo
|
|
|
|
alias Bump.Messages.Message
|
2021-12-30 16:11:53 +01:00
|
|
|
alias Bump.Senders.Sender
|
2021-12-30 13:06:26 +01:00
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def pop(sender) do
|
|
|
|
query = from m in "messages",
|
|
|
|
where: m.sender == ^sender,
|
|
|
|
order_by: [desc: m.timestamp],
|
|
|
|
limit: 1,
|
2022-01-17 15:33:59 +01:00
|
|
|
select: %{id: m.id, title: m.title, data: m.data, timestamp: m.timestamp}
|
2021-12-30 13:06:26 +01:00
|
|
|
|
|
|
|
res = Repo.one(query)
|
|
|
|
|
|
|
|
if not is_nil(res) do
|
|
|
|
Repo.delete_all(from m in "messages", where: m.id == ^res.id)
|
2022-01-17 15:33:59 +01:00
|
|
|
%{title: res.title, data: res.data, timestamp: res.timestamp}
|
2021-12-30 13:06:26 +01:00
|
|
|
else
|
|
|
|
%{}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def peek(sender) do
|
|
|
|
query = from m in "messages",
|
|
|
|
where: m.sender == ^sender,
|
|
|
|
order_by: [desc: m.timestamp],
|
|
|
|
limit: 1,
|
2022-01-17 15:33:59 +01:00
|
|
|
select: %{title: m.title, data: m.data, timestamp: m.timestamp}
|
2021-12-30 13:06:26 +01:00
|
|
|
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,
|
2022-01-17 15:33:59 +01:00
|
|
|
select: %{title: m.title, data: m.data, timestamp: m.timestamp}
|
2021-12-30 13:06:26 +01:00
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear(sender) do
|
|
|
|
query = from m in "messages",
|
|
|
|
where: m.sender == ^sender
|
|
|
|
|
|
|
|
Repo.delete_all(query)
|
|
|
|
end
|
|
|
|
|
2022-01-03 13:07:33 +01:00
|
|
|
|
|
|
|
def push(sender, title, data) do
|
2021-12-30 13:06:26 +01:00
|
|
|
time = DateTime.utc_now |> DateTime.truncate(:second)
|
|
|
|
|
2022-01-03 13:07:33 +01:00
|
|
|
Repo.insert(%Message{sender: sender, title: title, data: data, timestamp: time})
|
2021-12-30 13:06:26 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-12-30 16:11:53 +01:00
|
|
|
def addSender(sender, %{"firebase" => firebase}) do
|
2022-01-03 13:07:33 +01:00
|
|
|
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)
|
2021-12-30 16:11:53 +01:00
|
|
|
end
|
2021-12-30 13:06:26 +01:00
|
|
|
|
2021-12-30 16:23:54 +01:00
|
|
|
def tokens(sender) do
|
|
|
|
query = from s in "senders",
|
|
|
|
where: s.sender == ^sender,
|
|
|
|
select: %{firebase_token: s.firebase_token}
|
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
2021-12-30 13:06:26 +01:00
|
|
|
end
|