parent
095c3123ab
commit
af557ffc6e
@ -0,0 +1,15 @@
|
||||
-- +migrate up
|
||||
CREATE TABLE notifications (
|
||||
id SERIAL PRIMARY KEY,
|
||||
|
||||
account_id INTEGER NOT NULL references accounts(id),
|
||||
type TEXT NOT NULL,
|
||||
target INTEGER, -- represents whatever is affected; for SQL querying assistance. INTEGER because it's always an ID, might be tweaked in the future
|
||||
details TEXT NOT NULL, -- a JSON of various things relevant to displaying the notification
|
||||
|
||||
created_at TEXT NOT NULL DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'now')),
|
||||
read_at TEXT
|
||||
);
|
||||
|
||||
-- +migrate down
|
||||
DROP TABLE notifications;
|
@ -0,0 +1,17 @@
|
||||
require "json"
|
||||
|
||||
include CrystalGauntlet
|
||||
|
||||
module CrystalGauntlet::Notifications
|
||||
extend self
|
||||
|
||||
alias NotificationValue = Hash(String, String | Int64 | Bool | Float64 | Nil)
|
||||
|
||||
def clear_previous_notifications(account_id : Int32, type : String, target : Int32)
|
||||
DATABASE.exec("delete from notifications where account_id = ? and type = ? and target = ?", account_id, type, target)
|
||||
end
|
||||
|
||||
def send_notification(account_id : Int32, type : String, target : Int32?, details : NotificationValue? = nil)
|
||||
DATABASE.exec("insert into notifications (id, account_id, type, target, details) values (?, ?, ?, ?, ?)", IDs.get_next_id("notifications"), account_id, type, target, details.try &.to_json || "{}")
|
||||
end
|
||||
end
|
Loading…
Reference in new issue