2016-04-03 17:59:27 +00:00
|
|
|
// Package v1 implements the first version of the Ripple API.
|
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
2016-05-17 15:06:15 +00:00
|
|
|
"git.zxq.co/ripple/ocl"
|
2016-04-19 14:07:27 +00:00
|
|
|
"git.zxq.co/ripple/rippleapi/common"
|
2016-04-03 17:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type userData struct {
|
2016-07-06 20:32:30 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
UsernameAKA string `json:"username_aka"`
|
|
|
|
RegisteredOn common.UnixTimestamp `json:"registered_on"`
|
|
|
|
Privileges uint64 `json:"privileges"`
|
|
|
|
LatestActivity common.UnixTimestamp `json:"latest_activity"`
|
|
|
|
Country string `json:"country"`
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 16:32:43 +00:00
|
|
|
// UsersGET is the API handler for GET /users
|
|
|
|
func UsersGET(md common.MethodData) common.CodeMessager {
|
2016-05-14 18:55:42 +00:00
|
|
|
shouldRet, whereClause, param := whereClauseUser(md, "users")
|
2016-05-14 17:51:33 +00:00
|
|
|
if shouldRet != nil {
|
|
|
|
return *shouldRet
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 21:09:28 +00:00
|
|
|
query := `
|
2016-07-03 22:06:23 +00:00
|
|
|
SELECT users.id, users.username, register_datetime, privileges,
|
2016-04-09 22:03:05 +00:00
|
|
|
latest_activity, users_stats.username_aka,
|
2016-07-06 21:43:43 +00:00
|
|
|
users_stats.country
|
2016-04-03 21:09:28 +00:00
|
|
|
FROM users
|
|
|
|
LEFT JOIN users_stats
|
|
|
|
ON users.id=users_stats.id
|
2016-07-03 19:55:03 +00:00
|
|
|
WHERE ` + whereClause + ` AND users.privileges & 1 > 0
|
2016-04-03 21:09:28 +00:00
|
|
|
LIMIT 1`
|
2016-08-15 17:59:46 +00:00
|
|
|
return userPuts(md, md.DB.QueryRowx(query, param))
|
2016-04-03 21:09:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 07:37:11 +00:00
|
|
|
type userPutsUserData struct {
|
|
|
|
common.ResponseBase
|
|
|
|
userData
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
func userPuts(md common.MethodData, row *sqlx.Row) common.CodeMessager {
|
2016-04-03 21:09:28 +00:00
|
|
|
var err error
|
2016-06-14 07:37:11 +00:00
|
|
|
var user userPutsUserData
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-08-15 17:59:46 +00:00
|
|
|
err = row.StructScan(&user.userData)
|
2016-04-03 17:59:27 +00:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2016-04-16 16:05:24 +00:00
|
|
|
return common.SimpleResponse(404, "No such user was found!")
|
2016-04-03 17:59:27 +00:00
|
|
|
case err != nil:
|
2016-04-08 17:05:54 +00:00
|
|
|
md.Err(err)
|
2016-04-16 16:05:24 +00:00
|
|
|
return Err500
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
user.Code = 200
|
|
|
|
return user
|
2016-04-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func badgesToArray(badges string) []int {
|
|
|
|
var end []int
|
2016-04-03 17:59:27 +00:00
|
|
|
badgesSl := strings.Split(badges, ",")
|
|
|
|
for _, badge := range badgesSl {
|
|
|
|
if badge != "" && badge != "0" {
|
2016-06-13 19:48:09 +00:00
|
|
|
nb := common.Int(badge)
|
|
|
|
if nb != 0 {
|
2016-04-07 11:47:42 +00:00
|
|
|
end = append(end, nb)
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 11:47:42 +00:00
|
|
|
return end
|
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-04-05 20:38:33 +00:00
|
|
|
// UserSelfGET is a shortcut for /users/id/self. (/users/self)
|
2016-04-16 16:05:24 +00:00
|
|
|
func UserSelfGET(md common.MethodData) common.CodeMessager {
|
2016-05-14 16:32:43 +00:00
|
|
|
md.C.Request.URL.RawQuery = "id=self&" + md.C.Request.URL.RawQuery
|
|
|
|
return UsersGET(md)
|
2016-04-05 20:38:33 +00:00
|
|
|
}
|
2016-04-07 09:59:38 +00:00
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
type whatIDResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
ID int `json:"id"`
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:59:38 +00:00
|
|
|
// UserWhatsTheIDGET is an API request that only returns an user's ID.
|
2016-04-16 16:05:24 +00:00
|
|
|
func UserWhatsTheIDGET(md common.MethodData) common.CodeMessager {
|
2016-04-07 09:59:38 +00:00
|
|
|
var (
|
2016-07-03 22:06:23 +00:00
|
|
|
r whatIDResponse
|
2016-07-03 19:55:03 +00:00
|
|
|
privileges int
|
2016-04-07 09:59:38 +00:00
|
|
|
)
|
2016-08-15 17:59:46 +00:00
|
|
|
err := md.DB.QueryRow("SELECT id, privileges FROM users WHERE username = ? LIMIT 1", md.Query("name")).Scan(&r.ID, &privileges)
|
2016-07-03 22:06:23 +00:00
|
|
|
if err != nil || ((privileges&common.UserPrivilegePublic) == 0 && !md.User.Privileges.HasPrivilegeViewUserAdvanced()) {
|
2016-04-16 16:05:24 +00:00
|
|
|
return common.SimpleResponse(404, "That user could not be found!")
|
2016-04-07 09:59:38 +00:00
|
|
|
}
|
2016-04-16 16:05:24 +00:00
|
|
|
r.Code = 200
|
|
|
|
return r
|
2016-04-07 09:59:38 +00:00
|
|
|
}
|
2016-04-07 11:47:42 +00:00
|
|
|
|
|
|
|
type modeData struct {
|
|
|
|
RankedScore uint64 `json:"ranked_score"`
|
|
|
|
TotalScore uint64 `json:"total_score"`
|
|
|
|
PlayCount int `json:"playcount"`
|
|
|
|
ReplaysWatched int `json:"replays_watched"`
|
|
|
|
TotalHits int `json:"total_hits"`
|
|
|
|
Level float64 `json:"level"`
|
|
|
|
Accuracy float64 `json:"accuracy"`
|
2016-05-14 19:27:42 +00:00
|
|
|
PP int `json:"pp"`
|
2016-04-07 11:47:42 +00:00
|
|
|
GlobalLeaderboardRank int `json:"global_leaderboard_rank"`
|
|
|
|
}
|
2016-04-16 16:05:24 +00:00
|
|
|
type userFullResponse struct {
|
|
|
|
common.ResponseBase
|
2016-04-07 11:47:42 +00:00
|
|
|
userData
|
|
|
|
STD modeData `json:"std"`
|
|
|
|
Taiko modeData `json:"taiko"`
|
|
|
|
CTB modeData `json:"ctb"`
|
|
|
|
Mania modeData `json:"mania"`
|
|
|
|
PlayStyle int `json:"play_style"`
|
|
|
|
FavouriteMode int `json:"favourite_mode"`
|
2016-04-09 21:58:27 +00:00
|
|
|
Badges []int `json:"badges"`
|
2016-04-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserFullGET gets all of an user's information, with one exception: their userpage.
|
2016-04-16 16:05:24 +00:00
|
|
|
func UserFullGET(md common.MethodData) common.CodeMessager {
|
2016-05-14 18:55:42 +00:00
|
|
|
shouldRet, whereClause, param := whereClauseUser(md, "users")
|
2016-05-14 17:51:33 +00:00
|
|
|
if shouldRet != nil {
|
|
|
|
return *shouldRet
|
|
|
|
}
|
|
|
|
|
2016-04-07 11:47:42 +00:00
|
|
|
// Hellest query I've ever done.
|
|
|
|
query := `
|
|
|
|
SELECT
|
2016-07-03 22:06:23 +00:00
|
|
|
users.id, users.username, users.register_datetime, users.privileges, users.latest_activity,
|
2016-07-03 19:55:03 +00:00
|
|
|
|
2016-07-06 21:43:43 +00:00
|
|
|
users_stats.username_aka, users_stats.badges_shown, users_stats.country,
|
2016-04-07 11:47:42 +00:00
|
|
|
users_stats.play_style, users_stats.favourite_mode,
|
2016-07-03 19:55:03 +00:00
|
|
|
|
2016-04-07 11:47:42 +00:00
|
|
|
users_stats.ranked_score_std, users_stats.total_score_std, users_stats.playcount_std,
|
2016-05-17 15:06:15 +00:00
|
|
|
users_stats.replays_watched_std, users_stats.total_hits_std,
|
2016-05-14 19:27:42 +00:00
|
|
|
users_stats.avg_accuracy_std, users_stats.pp_std, leaderboard_std.position as std_position,
|
2016-07-03 19:55:03 +00:00
|
|
|
|
2016-04-07 11:47:42 +00:00
|
|
|
users_stats.ranked_score_taiko, users_stats.total_score_taiko, users_stats.playcount_taiko,
|
2016-05-17 15:06:15 +00:00
|
|
|
users_stats.replays_watched_taiko, users_stats.total_hits_taiko,
|
2016-05-14 19:27:42 +00:00
|
|
|
users_stats.avg_accuracy_taiko, users_stats.pp_taiko, leaderboard_taiko.position as taiko_position,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
|
|
|
users_stats.ranked_score_ctb, users_stats.total_score_ctb, users_stats.playcount_ctb,
|
2016-05-17 15:06:15 +00:00
|
|
|
users_stats.replays_watched_ctb, users_stats.total_hits_ctb,
|
2016-05-14 19:27:42 +00:00
|
|
|
users_stats.avg_accuracy_ctb, users_stats.pp_ctb, leaderboard_ctb.position as ctb_position,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
|
|
|
users_stats.ranked_score_mania, users_stats.total_score_mania, users_stats.playcount_mania,
|
2016-05-17 15:06:15 +00:00
|
|
|
users_stats.replays_watched_mania, users_stats.total_hits_mania,
|
2016-05-14 19:27:42 +00:00
|
|
|
users_stats.avg_accuracy_mania, users_stats.pp_mania, leaderboard_mania.position as mania_position
|
2016-04-07 11:47:42 +00:00
|
|
|
|
|
|
|
FROM users
|
|
|
|
LEFT JOIN users_stats
|
|
|
|
ON users.id=users_stats.id
|
|
|
|
LEFT JOIN leaderboard_std
|
|
|
|
ON users.id=leaderboard_std.user
|
|
|
|
LEFT JOIN leaderboard_taiko
|
|
|
|
ON users.id=leaderboard_taiko.user
|
|
|
|
LEFT JOIN leaderboard_ctb
|
|
|
|
ON users.id=leaderboard_ctb.user
|
|
|
|
LEFT JOIN leaderboard_mania
|
|
|
|
ON users.id=leaderboard_mania.user
|
2016-07-03 19:55:03 +00:00
|
|
|
WHERE ` + whereClause + ` AND users.privileges & 1 > 0
|
2016-04-07 11:47:42 +00:00
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
// Fuck.
|
2016-04-16 16:05:24 +00:00
|
|
|
r := userFullResponse{}
|
2016-04-07 11:47:42 +00:00
|
|
|
var (
|
2016-08-18 13:15:22 +00:00
|
|
|
badges string
|
2016-04-07 11:47:42 +00:00
|
|
|
)
|
2016-05-14 17:51:33 +00:00
|
|
|
err := md.DB.QueryRow(query, param).Scan(
|
2016-07-06 20:32:30 +00:00
|
|
|
&r.ID, &r.Username, &r.RegisteredOn, &r.Privileges, &r.LatestActivity,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-08-18 13:15:22 +00:00
|
|
|
&r.UsernameAKA, &badges, &r.Country,
|
2016-04-16 16:05:24 +00:00
|
|
|
&r.PlayStyle, &r.FavouriteMode,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
&r.STD.RankedScore, &r.STD.TotalScore, &r.STD.PlayCount,
|
2016-05-17 15:06:15 +00:00
|
|
|
&r.STD.ReplaysWatched, &r.STD.TotalHits,
|
2016-05-14 19:27:42 +00:00
|
|
|
&r.STD.Accuracy, &r.STD.PP, &r.STD.GlobalLeaderboardRank,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
&r.Taiko.RankedScore, &r.Taiko.TotalScore, &r.Taiko.PlayCount,
|
2016-05-17 15:06:15 +00:00
|
|
|
&r.Taiko.ReplaysWatched, &r.Taiko.TotalHits,
|
2016-05-14 19:27:42 +00:00
|
|
|
&r.Taiko.Accuracy, &r.Taiko.PP, &r.Taiko.GlobalLeaderboardRank,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
&r.CTB.RankedScore, &r.CTB.TotalScore, &r.CTB.PlayCount,
|
2016-05-17 15:06:15 +00:00
|
|
|
&r.CTB.ReplaysWatched, &r.CTB.TotalHits,
|
2016-05-14 19:27:42 +00:00
|
|
|
&r.CTB.Accuracy, &r.CTB.PP, &r.CTB.GlobalLeaderboardRank,
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
&r.Mania.RankedScore, &r.Mania.TotalScore, &r.Mania.PlayCount,
|
2016-05-17 15:06:15 +00:00
|
|
|
&r.Mania.ReplaysWatched, &r.Mania.TotalHits,
|
2016-05-14 19:27:42 +00:00
|
|
|
&r.Mania.Accuracy, &r.Mania.PP, &r.Mania.GlobalLeaderboardRank,
|
2016-04-07 11:47:42 +00:00
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2016-04-16 16:05:24 +00:00
|
|
|
return common.SimpleResponse(404, "That user could not be found!")
|
2016-04-07 11:47:42 +00:00
|
|
|
case err != nil:
|
2016-04-08 17:05:54 +00:00
|
|
|
md.Err(err)
|
2016-04-16 16:05:24 +00:00
|
|
|
return Err500
|
2016-04-07 11:47:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:05:24 +00:00
|
|
|
r.Badges = badgesToArray(badges)
|
2016-04-07 11:47:42 +00:00
|
|
|
|
2016-05-17 15:06:15 +00:00
|
|
|
for _, m := range []*modeData{&r.STD, &r.Taiko, &r.CTB, &r.Mania} {
|
|
|
|
m.Level = ocl.GetLevelPrecise(int64(m.TotalScore))
|
|
|
|
}
|
|
|
|
|
2016-04-07 11:47:42 +00:00
|
|
|
r.Code = 200
|
2016-04-16 16:05:24 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type userpageResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
Userpage string `json:"userpage"`
|
2016-04-07 11:47:42 +00:00
|
|
|
}
|
2016-04-07 12:42:17 +00:00
|
|
|
|
|
|
|
// UserUserpageGET gets an user's userpage, as in the customisable thing.
|
2016-04-16 16:05:24 +00:00
|
|
|
func UserUserpageGET(md common.MethodData) common.CodeMessager {
|
2016-05-14 18:55:42 +00:00
|
|
|
shouldRet, whereClause, param := whereClauseUser(md, "users_stats")
|
|
|
|
if shouldRet != nil {
|
|
|
|
return *shouldRet
|
|
|
|
}
|
2016-04-16 16:05:24 +00:00
|
|
|
var r userpageResponse
|
2016-05-14 18:55:42 +00:00
|
|
|
err := md.DB.QueryRow("SELECT userpage_content FROM users_stats WHERE "+whereClause+" LIMIT 1", param).Scan(&r.Userpage)
|
2016-04-07 12:42:17 +00:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2016-05-14 18:55:42 +00:00
|
|
|
return common.SimpleResponse(404, "No such user!")
|
2016-04-07 12:42:17 +00:00
|
|
|
case err != nil:
|
2016-04-08 17:05:54 +00:00
|
|
|
md.Err(err)
|
2016-04-16 16:05:24 +00:00
|
|
|
return Err500
|
2016-04-07 12:42:17 +00:00
|
|
|
}
|
|
|
|
r.Code = 200
|
2016-04-16 16:05:24 +00:00
|
|
|
return r
|
2016-04-07 12:42:17 +00:00
|
|
|
}
|
2016-05-14 17:51:33 +00:00
|
|
|
|
2016-05-14 18:55:42 +00:00
|
|
|
func whereClauseUser(md common.MethodData, tableName string) (*common.CodeMessager, string, interface{}) {
|
2016-05-14 17:51:33 +00:00
|
|
|
switch {
|
2016-08-15 17:59:46 +00:00
|
|
|
case md.Query("id") == "self":
|
2016-05-14 18:55:42 +00:00
|
|
|
return nil, tableName + ".id = ?", md.ID()
|
2016-08-15 17:59:46 +00:00
|
|
|
case md.Query("id") != "":
|
|
|
|
id, err := strconv.Atoi(md.Query("id"))
|
2016-05-14 17:51:33 +00:00
|
|
|
if err != nil {
|
|
|
|
a := common.SimpleResponse(400, "please pass a valid user ID")
|
|
|
|
return &a, "", nil
|
|
|
|
}
|
2016-05-14 18:55:42 +00:00
|
|
|
return nil, tableName + ".id = ?", id
|
2016-08-15 17:59:46 +00:00
|
|
|
case md.Query("name") != "":
|
|
|
|
return nil, tableName + ".username = ?", md.Query("name")
|
2016-05-14 17:51:33 +00:00
|
|
|
}
|
|
|
|
a := common.SimpleResponse(400, "you need to pass either querystring parameters name or id")
|
|
|
|
return &a, "", nil
|
|
|
|
}
|
2016-05-14 20:43:47 +00:00
|
|
|
|
|
|
|
type userLookupResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
Users []lookupUser `json:"users"`
|
|
|
|
}
|
|
|
|
type lookupUser struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserLookupGET does a quick lookup of users beginning with the passed
|
|
|
|
// querystring value name.
|
|
|
|
func UserLookupGET(md common.MethodData) common.CodeMessager {
|
|
|
|
name := strings.NewReplacer(
|
|
|
|
"%", "\\%",
|
|
|
|
"_", "\\_",
|
2016-05-17 13:35:18 +00:00
|
|
|
"\\", "\\\\",
|
2016-08-15 17:59:46 +00:00
|
|
|
).Replace(md.Query("name"))
|
2016-05-14 20:43:47 +00:00
|
|
|
if name == "" {
|
|
|
|
return common.SimpleResponse(400, "please provide an username to start searching")
|
|
|
|
}
|
2016-05-17 15:40:30 +00:00
|
|
|
name = "%" + name + "%"
|
2016-07-03 19:55:03 +00:00
|
|
|
rows, err := md.DB.Query("SELECT users.id, users.username FROM users WHERE username LIKE ? AND privileges & 1 > 0 LIMIT 25", name)
|
2016-05-14 20:43:47 +00:00
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
|
|
|
|
var r userLookupResponse
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var l lookupUser
|
|
|
|
err := rows.Scan(&l.ID, &l.Username)
|
|
|
|
if err != nil {
|
|
|
|
continue // can't be bothered to handle properly
|
|
|
|
}
|
|
|
|
r.Users = append(r.Users, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Code = 200
|
|
|
|
return r
|
|
|
|
}
|