2021-12-05 00:36:13 +01:00
|
|
|
defmodule Bump.Messages do
|
|
|
|
alias Bump.Repo
|
2021-12-10 12:46:34 +01:00
|
|
|
alias Bump.Messages.Message
|
2021-12-05 00:36:13 +01:00
|
|
|
import Ecto.Query
|
|
|
|
|
2021-12-10 12:46:34 +01:00
|
|
|
def get_newest_message(sender) do
|
|
|
|
query = from m in "messages",
|
|
|
|
where: m.sender == ^sender,
|
|
|
|
order_by: [desc: m.timestamp],
|
|
|
|
limit: 1,
|
|
|
|
select: m.data
|
|
|
|
Repo.one(query)
|
2021-12-05 00:36:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_all_messages_since(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: m.data
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
2021-12-10 12:46:34 +01:00
|
|
|
def remove_sender(sender) do
|
|
|
|
query = from m in "messages",
|
|
|
|
where: m.sender == ^sender,
|
|
|
|
select: m.id
|
|
|
|
Repo.delete_all(query)
|
2021-12-05 00:36:13 +01:00
|
|
|
end
|
|
|
|
|
2021-12-10 12:46:34 +01:00
|
|
|
def add_message(sender, message) do
|
|
|
|
time =
|
|
|
|
DateTime.utc_now
|
|
|
|
|> DateTime.truncate(:second)
|
|
|
|
|
|
|
|
Repo.insert(%Message{sender: sender, data: message, timestamp: time})
|
|
|
|
|
2021-12-05 00:36:13 +01:00
|
|
|
end
|
|
|
|
|
2021-12-10 12:46:34 +01:00
|
|
|
|
2021-12-05 00:36:13 +01:00
|
|
|
end
|