Initial commit
This commit is contained in:
15
app/v1/404.go
Normal file
15
app/v1/404.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/osuripple/api/common"
|
||||
)
|
||||
|
||||
// Handle404 handles requests with no implemented handlers.
|
||||
func Handle404(c *gin.Context) {
|
||||
c.IndentedJSON(404, common.Response{
|
||||
Code: 404,
|
||||
Message: "Oh dear... that API request could not be found! Perhaps the API is not up-to-date? Either way, have a surprise!",
|
||||
Data: surpriseMe(),
|
||||
})
|
||||
}
|
13
app/v1/errors.go
Normal file
13
app/v1/errors.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/osuripple/api/common"
|
||||
)
|
||||
|
||||
// Boilerplate errors
|
||||
var (
|
||||
Err500 = common.Response{
|
||||
Code: 0,
|
||||
Message: "An error occurred. Try again, perhaps?",
|
||||
}
|
||||
)
|
116
app/v1/ping.go
Normal file
116
app/v1/ping.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/osuripple/api/common"
|
||||
)
|
||||
|
||||
var rn = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
var kaomojis = [...]string{
|
||||
"Σ(ノ°▽°)ノ",
|
||||
"( ƅ°ਉ°)ƅ",
|
||||
"ヽ( ・∀・)ノ",
|
||||
"˭̡̞(◞⁎˃ᆺ˂)◞*✰",
|
||||
"(p^-^)p",
|
||||
"(ノ^∇^)ノ゚",
|
||||
"ヽ(〃・ω・)ノ",
|
||||
"(۶* ‘ꆚ’)۶”",
|
||||
"(。>ω<)。",
|
||||
"(ノ。≧◇≦)ノ",
|
||||
"ヾ(。・ω・)シ",
|
||||
"(ノ・д・)ノ",
|
||||
".+:。(ノ・ω・)ノ゙",
|
||||
"Σ(*ノ´>ω<。`)ノ",
|
||||
"ヾ(〃^∇^)ノ♪",
|
||||
"\(@ ̄∇ ̄@)/",
|
||||
"\(^▽^@)ノ",
|
||||
"ヾ(@^▽^@)ノ",
|
||||
"(((\(@v@)/)))",
|
||||
"\(*T▽T*)/",
|
||||
"\(^▽^)/",
|
||||
"\(T∇T)/",
|
||||
"ヽ( ★ω★)ノ",
|
||||
"ヽ(;▽;)ノ",
|
||||
"ヾ(。◕ฺ∀◕ฺ)ノ",
|
||||
"ヾ(@† ▽ †@)ノ",
|
||||
"ヾ(@^∇^@)ノ",
|
||||
"ヾ(@^▽^@)ノ",
|
||||
"ヾ(@^▽^@)ノ",
|
||||
"ヾ(@゜▽゜@)ノ",
|
||||
"(.=^・ェ・^=)",
|
||||
"((≡^⚲͜^≡))",
|
||||
"(^・o・^)ノ”",
|
||||
"(^._.^)ノ",
|
||||
"(^人^)",
|
||||
"(=;ェ;=)",
|
||||
"(=`ω´=)",
|
||||
"(=`ェ´=)",
|
||||
"(=´∇`=)",
|
||||
"(=^・^=)",
|
||||
"(=^・ェ・^=)",
|
||||
"(=^‥^=)",
|
||||
"(=TェT=)",
|
||||
"(=xェx=)",
|
||||
"\(=^‥^)/’`",
|
||||
"~(=^‥^)/",
|
||||
"└(=^‥^=)┐",
|
||||
"ヾ(=゚・゚=)ノ",
|
||||
"ヽ(=^・ω・^=)丿",
|
||||
"d(=^・ω・^=)b",
|
||||
"o(^・x・^)o",
|
||||
"V(=^・ω・^=)v",
|
||||
"(⁎˃ᆺ˂)",
|
||||
"(,,^・⋏・^,,)",
|
||||
}
|
||||
|
||||
var randomSentences = [...]string{
|
||||
"Proudly sponsored by Kirotuso!",
|
||||
"The brace is on fire!",
|
||||
"deverupa ga daisuki!",
|
||||
"It works!!!!",
|
||||
"Feelin' groovy!",
|
||||
"sudo rm -rf /",
|
||||
"Hi! I'm Flowey! Flowey the flower!",
|
||||
"Ripple devs are actually cats",
|
||||
"Support Howl's fund for buying a power supply for his SSD",
|
||||
}
|
||||
|
||||
func surpriseMe() string {
|
||||
return randomSentences[rn.Intn(len(randomSentences))] + " " + kaomojis[rn.Intn(len(kaomojis))]
|
||||
}
|
||||
|
||||
type pingData struct {
|
||||
ID int `json:"user_id"`
|
||||
Privileges int `json:"privileges"`
|
||||
}
|
||||
|
||||
// Ping is a message to check with the API that we are logged in, and know what are our privileges.
|
||||
func Ping(md common.MethodData) (r common.Response) {
|
||||
r.Code = 200
|
||||
if md.User.UserID == 0 {
|
||||
r.Message = "You have not given us a token, so we don't know who you are! But you can still login with /api/v1/login " + kaomojis[rn.Intn(len(kaomojis))]
|
||||
} else {
|
||||
r.Message = surpriseMe()
|
||||
}
|
||||
r.Data = pingData{
|
||||
ID: md.User.UserID,
|
||||
Privileges: int(md.User.Privileges),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SurpriseMe generates cute cats.
|
||||
//
|
||||
// ... Yes.
|
||||
func SurpriseMe(md common.MethodData) (r common.Response) {
|
||||
r.Code = 200
|
||||
cats := make([]string, 100)
|
||||
for i := 0; i < 100; i++ {
|
||||
cats[i] = surpriseMe()
|
||||
}
|
||||
r.Data = cats
|
||||
return
|
||||
}
|
39
app/v1/privileges.go
Normal file
39
app/v1/privileges.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/osuripple/api/common"
|
||||
)
|
||||
|
||||
type privilegesData struct {
|
||||
PrivilegeRead bool `json:"read"`
|
||||
PrivilegeReadConfidential bool `json:"read_confidential"`
|
||||
PrivilegeWrite bool `json:"write"`
|
||||
PrivilegeManageBadges bool `json:"manage_badges"`
|
||||
PrivilegeBetaKeys bool `json:"beta_keys"`
|
||||
PrivilegeManageSettings bool `json:"manage_settings"`
|
||||
PrivilegeViewUserAdvanced bool `json:"view_user_advanced"`
|
||||
PrivilegeManageUser bool `json:"manage_user"`
|
||||
PrivilegeManageRoles bool `json:"manage_roles"`
|
||||
PrivilegeManageAPIKeys bool `json:"manage_api_keys"`
|
||||
PrivilegeBlog bool `json:"blog"`
|
||||
}
|
||||
|
||||
// PrivilegesGET returns an explaination for the privileges, telling the client what they can do with this token.
|
||||
func PrivilegesGET(md common.MethodData) (r common.Response) {
|
||||
// This code sucks.
|
||||
r.Code = 200
|
||||
r.Data = privilegesData{
|
||||
PrivilegeRead: md.User.Privileges.HasPrivilegeRead(),
|
||||
PrivilegeReadConfidential: md.User.Privileges.HasPrivilegeReadConfidential(),
|
||||
PrivilegeWrite: md.User.Privileges.HasPrivilegeWrite(),
|
||||
PrivilegeManageBadges: md.User.Privileges.HasPrivilegeManageBadges(),
|
||||
PrivilegeBetaKeys: md.User.Privileges.HasPrivilegeBetaKeys(),
|
||||
PrivilegeManageSettings: md.User.Privileges.HasPrivilegeManageSettings(),
|
||||
PrivilegeViewUserAdvanced: md.User.Privileges.HasPrivilegeViewUserAdvanced(),
|
||||
PrivilegeManageUser: md.User.Privileges.HasPrivilegeManageUser(),
|
||||
PrivilegeManageRoles: md.User.Privileges.HasPrivilegeManageRoles(),
|
||||
PrivilegeManageAPIKeys: md.User.Privileges.HasPrivilegeManageAPIKeys(),
|
||||
PrivilegeBlog: md.User.Privileges.HasPrivilegeBlog(),
|
||||
}
|
||||
return
|
||||
}
|
82
app/v1/user.go
Normal file
82
app/v1/user.go
Normal file
@@ -0,0 +1,82 @@
|
||||
// Package v1 implements the first version of the Ripple API.
|
||||
package v1
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/osuripple/api/common"
|
||||
)
|
||||
|
||||
type userData struct {
|
||||
ID int `json:"id"`
|
||||
Username string `json:"username"`
|
||||
UsernameAKA string `json:"username_aka"`
|
||||
RegisteredOn time.Time `json:"registered_on"`
|
||||
Rank int `json:"rank"`
|
||||
LatestActivity time.Time `json:"latest_activity"`
|
||||
Country string `json:"country"`
|
||||
Badges []int `json:"badges"`
|
||||
}
|
||||
|
||||
// UserGET is the API handler for GET /user/:id
|
||||
func UserGET(md common.MethodData) (r common.Response) {
|
||||
var err error
|
||||
var uid int
|
||||
uidStr := md.C.Param("id")
|
||||
if uidStr == "self" {
|
||||
uid = md.User.UserID
|
||||
} else {
|
||||
uid, err = strconv.Atoi(uidStr)
|
||||
if err != nil {
|
||||
r.Code = 400
|
||||
r.Message = fmt.Sprintf("%s ain't a number", uidStr)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
user := userData{}
|
||||
|
||||
registeredOn := int64(0)
|
||||
latestActivity := int64(0)
|
||||
var badges string
|
||||
var showcountry bool
|
||||
err = md.DB.QueryRow("SELECT users.id, users.username, register_datetime, rank, latest_activity, users_stats.username_aka, users_stats.badges_shown, users_stats.country, users_stats.show_country FROM users LEFT JOIN users_stats ON users.id=users_stats.id WHERE users.id=?", uid).Scan(
|
||||
&user.ID, &user.Username, ®isteredOn, &user.Rank, &latestActivity, &user.UsernameAKA, &badges, &user.Country, &showcountry)
|
||||
switch {
|
||||
case err == sql.ErrNoRows:
|
||||
r.Code = 404
|
||||
r.Message = "No such user was found!"
|
||||
return
|
||||
case err != nil:
|
||||
md.C.Error(err)
|
||||
r = Err500
|
||||
return
|
||||
}
|
||||
|
||||
user.RegisteredOn = time.Unix(registeredOn, 0)
|
||||
user.LatestActivity = time.Unix(latestActivity, 0)
|
||||
|
||||
badgesSl := strings.Split(badges, ",")
|
||||
for _, badge := range badgesSl {
|
||||
if badge != "" && badge != "0" {
|
||||
// We are ignoring errors because who really gives a shit if something's gone wrong on our end in this
|
||||
// particular thing, we can just silently ignore this.
|
||||
nb, err := strconv.Atoi(badge)
|
||||
if err == nil && nb != 0 {
|
||||
user.Badges = append(user.Badges, nb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !showcountry {
|
||||
user.Country = "XX"
|
||||
}
|
||||
|
||||
r.Code = 200
|
||||
r.Data = user
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user