crystal-gauntlet/src/endpoints/misc/likeItem.cr

66 lines
2.1 KiB
Crystal
Raw Normal View History

2022-12-30 19:07:22 +01:00
require "uri"
require "base64"
require "crypto/bcrypt/password"
include CrystalGauntlet
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
2023-01-02 11:59:37 +01:00
LOG.debug { params.inspect }
2022-12-30 19:07:22 +01:00
if !params.has_key?("itemID")
return "-1"
end
item_id = params["itemID"].to_i
2022-12-31 06:07:12 +01:00
type = (params["type"]? || "1").to_i
2022-12-30 19:07:22 +01:00
table = ""
column = ""
case type
2022-12-31 19:29:51 +01:00
when 1 # level like
2022-12-30 19:07:22 +01:00
table = "levels"
column = "id"
2022-12-31 19:29:51 +01:00
when 2 # level comment like
2023-01-06 17:43:12 +01:00
table = "comments"
2022-12-31 19:29:51 +01:00
column = "id"
when 3 # account comments
2023-01-06 17:43:12 +01:00
table = "account_comments"
column = "id"
2023-01-07 17:31:40 +01:00
else
return "-1"
2022-12-30 19:07:22 +01:00
end
2022-12-31 06:07:12 +01:00
is_like = (params["isLike"]? || "1").to_i
2022-12-30 19:07:22 +01:00
sign = is_like == 1 ? '+' : '-'
2022-12-31 19:29:51 +01:00
2023-01-07 17:31:40 +01:00
ip = IPs.get_real_ip(context.request)
if DATABASE.scalar("select count(*) from ip_actions where action = ? and value = ? and ip = ? limit 1", "like_#{type}", item_id, ip).as?(Int64) == 0
DATABASE.exec("update #{table} set likes = likes #{sign} 1 where #{column} = ?", item_id)
DATABASE.exec("insert into ip_actions (action, value, ip) values (?, ?, ?)", "like_#{type}", item_id, ip)
if type == 1 # level
likes = DATABASE.scalar("select likes from #{table} where #{column} = ?", item_id).as(Int64)
if likes == 100
# notify the creator of 100 likes
account_id, level_name = DATABASE.query_one("select users.account_id, #{table}.name from #{table} left join users on users.id = #{table}.user_id where #{table}.#{column} = ?", item_id, as: {Int32?, String})
if account_id
if DATABASE.scalar("select count(*) from notifications where type = \"like_milestone\" and target = ? and account_id = ?", item_id, account_id).as(Int64) == 0
Notifications.send_notification(account_id, "like_milestone", item_id, {
"level_name" => level_name,
"amount" => likes
})
end
end
end
end
2023-01-07 17:31:40 +01:00
end
2022-12-30 19:07:22 +01:00
"1"
}
2023-01-07 14:34:09 +01:00
CrystalGauntlet.endpoints["/likeGJItem20.php"] = CrystalGauntlet.endpoints["/likeGJItem211.php"]
2023-01-08 08:10:27 +01:00
CrystalGauntlet.endpoints["/likeGJItem.php"] = CrystalGauntlet.endpoints["/likeGJItem211.php"]
2023-01-07 16:25:32 +01:00