basic notifications impl for back-end

This commit is contained in:
Jill 2023-01-16 20:26:52 +03:00
parent 095c3123ab
commit af557ffc6e
4 changed files with 46 additions and 1 deletions

View File

@ -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;

View File

@ -26,6 +26,7 @@ require "./lib/creator_points"
require "./lib/versions"
require "./lib/ips"
require "./lib/ranks"
require "./lib/notifications"
require "./patch-exe.cr"

View File

@ -105,7 +105,19 @@ CrystalGauntlet.endpoints["/suggestGJStars20.php"] = ->(context : HTTP::Server::
return "-2"
end
DATABASE.exec "update levels set stars = ?, featured = ?, difficulty = ? where id = ?", params["stars"].to_i, params["feature"].to_i, (stars_to_difficulty(params["stars"].to_i) || LevelDifficulty::Easy).to_i, params["levelID"].to_i
level_id = params["levelID"].to_i
stars = params["stars"].to_i
difficulty = stars_to_difficulty(params["stars"].to_i) || LevelDifficulty::Easy
author_account_id = DATABASE.query_one("select users.account_id from levels left join users on users.id = levels.user_id where levels.id = ?", level_id, as: {Int32?})
if author_account_id
notif_type = params["feature"] == "1" ? "authored_level_featured" : "authored_level_rated"
Notifications.clear_previous_notifications(author_account_id, notif_type, level_id)
Notifications.send_notification(author_account_id, notif_type, level_id, {"stars" => stars.to_i64, "difficulty" => difficulty.to_i64})
end
DATABASE.exec "update levels set stars = ?, featured = ?, difficulty = ? where id = ?", stars, params["feature"].to_i, difficulty.to_i, level_id
"1"
}

17
src/lib/notifications.cr Normal file
View File

@ -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