2023-01-16 18:26:52 +01:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
include CrystalGauntlet
|
|
|
|
|
|
|
|
module CrystalGauntlet::Notifications
|
|
|
|
extend self
|
|
|
|
|
2023-01-16 19:43:54 +01:00
|
|
|
alias NotificationDetails = Hash(String, String | Int64 | Bool | Float64 | Nil)
|
2023-01-16 18:26:52 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-01-16 19:43:54 +01:00
|
|
|
def send_notification(account_id : Int32, type : String, target : Int32?, details : NotificationDetails? = nil)
|
2023-01-16 18:26:52 +01:00
|
|
|
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
|
2023-01-16 19:43:54 +01:00
|
|
|
|
|
|
|
NOTIFICATION_STRINGS = {
|
|
|
|
"authored_level_featured" => %(Your level <b>%{level_name}</b> has been featured!),
|
|
|
|
"authored_level_rated" => %(Your level <b>%{level_name}</b> has been rated!)
|
|
|
|
}
|
|
|
|
|
2023-01-18 19:03:03 +01:00
|
|
|
def format_notification(type : String, target : Int32?, details : NotificationDetails? = nil, html_safe : Bool = false)
|
2023-01-16 19:43:54 +01:00
|
|
|
details = details || {} of String => String | Int64 | Bool | Float64 | Nil
|
|
|
|
string = NOTIFICATION_STRINGS[type]
|
|
|
|
|
|
|
|
#case type
|
|
|
|
#when "authored_level_featured", "authored_level_rated"
|
|
|
|
# details["action"] = (type == "authored_level_featured") ? "featured" : "rated"
|
|
|
|
#end
|
|
|
|
|
|
|
|
if html_safe
|
2023-01-18 19:03:03 +01:00
|
|
|
string % details.transform_values { |v| HTML.escape v.to_s }
|
2023-01-16 19:43:54 +01:00
|
|
|
else
|
|
|
|
string % details
|
|
|
|
end
|
|
|
|
end
|
2023-01-16 18:26:52 +01:00
|
|
|
end
|