wip comments (currently non-functional)

This commit is contained in:
Jill 2022-12-31 23:25:06 +03:00
parent c83975ddfa
commit 43455b44d6
3 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,15 @@
-- +migrate up
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
level_id INTEGER NOT NULL references levels(id),
user_id INTEGER NOT NULL references users(id),
comment TEXT NOT NULL,
percent INTEGER,
created_at TEXT NOT NULL DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'now')),
likes INTEGER NOT NULL DEFAULT 0
);
-- +migrate down
DROP TABLE comments;

View File

@ -0,0 +1,31 @@
require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)
return "-1"
end
comment = params["comment"]?
level_id = params["levelID"].to_i
percent = (params["percent"]? || "nil").to_i?
if percent && (percent < 0 || percent > 100)
return "-1"
end
if comment && comment != ""
# todo: cap comment size
comment_value = Base64.decode_string comment # usual b64, surprisingly
next_id = (DATABASE.scalar("select max(id) from comments").as(Int64 | Nil) || 0) + 1
DATABASE.exec("insert into comments (id, level_id, user_id, comment, percent) values (?, ?, ?, ?, ?)", next_id, level_id, user_id, comment_value, percent)
return "1"
else
return "-1"
end
}

View File

@ -0,0 +1,67 @@
require "uri"
include CrystalGauntlet
comments_per_page = 10
CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
level_id = params["levelID"].to_i
comment_offset = (params["page"]? || "0").to_i * comments_per_page
amount = DATABASE.scalar("select count(*) from comments where level_id = ?", level_id)
# todo: account comment view (help)
# todo: handle binaryVersion < 32
users_str = [] of String
DATABASE.query("select comments.id, comment, comments.created_at, likes, percent, user_id, users.username, users.udid, users.account_id, users.icon_type, users.color1, users.color2, users.cube, users.ship, users.ball, users.ufo, users.wave, users.robot, users.spider, users.special from comments left join users on users.id == user_id where level_id = ? order by #{params["mode"]? == 1 ? "likes" : "comments.created_at"} desc limit #{comments_per_page} offset #{comment_offset}", level_id) do |rs|
rs.each do
id = rs.read(Int32)
comment = rs.read(String)
created_at = rs.read(String)
likes = rs.read(Int32)
percent = rs.read(Int32 | Nil)
user_id = rs.read(Int32)
username = rs.read(String | Nil)
udid = rs.read(String | Nil)
account_id = rs.read(Int32 | Nil)
icon_type = rs.read(Int32)
color1 = rs.read(Int32)
color2 = rs.read(Int32)
icon_value = [rs.read(Int32), rs.read(Int32), rs.read(Int32), rs.read(Int32), rs.read(Int32), rs.read(Int32), rs.read(Int32)][icon_type]
special = rs.read(Int32)
users_str << Format.fmt_comment({
2 => comment,
3 => user_id,
4 => likes,
5 => 0,
7 => likes < -3,
9 => Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC),
6 => id,
10 => percent || 0,
#12 => "0,0,0", # todo: badge
11 => "0:1",
1 => username || "-",
7 => 1,
9 => icon_value,
10 => color1,
11 => color2,
14 => icon_type,
15 => special,
16 => account_id || udid
})
end
end
search_meta = "#{amount}:#{comment_offset}:#{comments_per_page}"
[users_str.join("|"), search_meta].join("#")
}