Compare commits

...

2 Commits

Author SHA1 Message Date
Jill 12ae7504a0 wip notification view 2023-01-16 21:43:54 +03:00
Jill af557ffc6e basic notifications impl for back-end 2023-01-16 20:26:52 +03:00
10 changed files with 213 additions and 18 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

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bell"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path><path d="M13.73 21a2 2 0 0 1-3.46 0"></path></svg>

After

Width:  |  Height:  |  Size: 321 B

View File

@ -14,14 +14,17 @@
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
background-color: #fff;
color: #111;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
--accent-color: rgb(245, 141, 210);
--accent-color-bri: rgb(255, 191, 234);
accent-color: var(--accent-color);
--text-color: #111;
--background-color: #fff;
background-color: var(--background-color);
color: var(--text-color);
}
::selection {
@ -47,8 +50,8 @@ a:hover {
@media (prefers-color-scheme: dark) {
:root {
background-color: #111;
color: #fff;
--text-color: #fff;
--background-color: #111;
}
.dim {
@ -113,3 +116,36 @@ pre {
.error {
color: #f33;
}
.circle-button {
border-radius: 10px;
border: 1px solid var(--accent-color);
color: var(--accent-color);
transition: 0.1s border-color, 0.1s color;
padding: 0.5em;
margin: 0.5em;
height: 48px;
width: 48px;
}
.circle-button:hover {
border-color: var(--accent-color-bri);
color: var(--accent-color-bri);
}
.circle-button > svg, .circle-button > img {
display: block;
width: 100%;
height: 100%;
object-fit: contain;
}
@media (max-width: 650px) {
/* mobile layout gets bigger buttons */
.circle-button {
height: 54px;
width: 54px;
}
}
.circle-button.notifications-unread > svg {
fill: currentColor;
}

View File

@ -21,7 +21,6 @@
color: #000;
border-radius: 1.5em;
padding: 1em;
margin: 1em;
display: flex;
flex-direction: row;
@ -85,11 +84,29 @@
margin: auto;
display: block;
}
.header {
height: 96px;
display: flex;
flex-direction: row;
align-items: center;
gap: 1em;
margin: 1em;
}
.header > .greeting {
flex: 1 1 0px;
min-width: 0;
}
.header > .circle-button {
flex: 0 0 auto;
max-width: 100%;
}
</style>
</head>
<body>
<a href="/"><img src="/favicon.png" width="64" height="auto" class="spinny favicon"></a><br>
<div class="header">
<div class="greeting">
<img src="https://gdicon.oat.zone/icon.png?type=<%=type_str%>&value=<%=icon_value%>&color1=<%=color1%>&color2=<%=color2%><%=glow ? "&glow=1" : ""%>" width="150" height="150" class="greeting-l">
<div class="greeting-r">
@ -106,6 +123,10 @@
</div>
</div>
</div>
<a href="/accounts/notifications/" title="Notifiations" class="circle-button notifications <%= unread_notifications ? "notifications-unread" : "" %>">
<%= File.read("public/assets/icons/bell.svg") %>
</a>
</div>
<br><br>
blablabla lorem ipsum whatever. put stuff here later
</body>

View File

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/style.css" />
<title>Account Management</title>
<style>
body {
max-width: 800px;
margin: auto;
padding: 1em;
}
.header {
height: 96px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.title {
font-size: x-large;
display: flex;
flex-direction: row;
gap: 0.5em;
align-items: center;
}
</style>
</head>
<body>
<div class="header">
<a href="/accounts/" title="<%=username%>" class="circle-button">
<img src="https://gdicon.oat.zone/icon.png?type=<%=type_str%>&value=<%=icon_value%>&color1=<%=color1%>&color2=<%=color2%><%=glow ? "&glow=1" : ""%>" class="icon">
</a>
<span class="title">
<a href="/"><img src="/favicon.png" width="64" height="auto" class="spinny favicon"></a>
Notifications
</span>
<a href="/accounts/notifications/" title="Notifiations" class="circle-button notifications <%= unread_notifications ? "notifications-unread" : "" %>">
<%= # todo: unhardcode public dir location
File.read("public/assets/icons/bell.svg") %>
</a>
</div>
</body>
</html>

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, level_name = DATABASE.query_one("select users.account_id, levels.name from levels left join users on users.id = levels.user_id where levels.id = ?", level_id, as: {Int32?, String})
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, "level_name" => level_name})
end
DATABASE.exec "update levels set stars = ?, featured = ?, difficulty = ? where id = ?", stars, params["feature"].to_i, difficulty.to_i, level_id
"1"
}

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

@ -0,0 +1,38 @@
require "json"
include CrystalGauntlet
module CrystalGauntlet::Notifications
extend self
alias NotificationDetails = 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 : NotificationDetails? = 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
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!)
}
def format_notification(type : String, target : Int32, details : NotificationDetails? = nil, html_safe : Bool = false)
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
string % details.transform_values ->HTML.escape
else
string % details
end
end
end

View File

@ -8,6 +8,7 @@ CrystalGauntlet.template_endpoints["/#{config_get("general.append_path").as(Stri
CrystalGauntlet.template_endpoints["/accounts"] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
account_id = nil
user_id = nil
username = nil
Templates.auth()
@ -17,5 +18,7 @@ CrystalGauntlet.template_endpoints["/accounts"] = ->(context : HTTP::Server::Con
icon_value = [cube, ship, ball, ufo, wave, robot, spider][icon_type]
type_str = ["cube", "ship", "ball", "ufo", "wave", "robot", "spider"][icon_type]
unread_notifications = DATABASE.scalar("select count(*) from notifications where account_id = ? and read_at is null", account_id).as(Int64) > 0
ECR.embed("./public/template/account_management.ecr", context.response)
}

View File

@ -0,0 +1,20 @@
include CrystalGauntlet
CrystalGauntlet.template_endpoints["/accounts/notifications"] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
account_id = nil
user_id = nil
username = nil
Templates.auth()
icon_type, color1, color2, cube, ship, ball, ufo, wave, robot, spider, glow = DATABASE.query_one("select icon_type, color1, color2, cube, ship, ball, ufo, wave, robot, spider, glow from users where id = ?", user_id, as: {Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32})
icon_value = [cube, ship, ball, ufo, wave, robot, spider][icon_type]
type_str = ["cube", "ship", "ball", "ufo", "wave", "robot", "spider"][icon_type]
notification_count = DATABASE.scalar("select count(*) from notifications where account_id = ? and read_at is null", account_id).as(Int64)
unread_notifications = notification_count > 0
ECR.embed("./public/template/notifications.ecr", context.response)
}