Add GET /friends/with/:id

This commit is contained in:
Howl 2016-04-07 12:21:54 +02:00
parent 34593ef428
commit aae4c5de50
2 changed files with 24 additions and 0 deletions

View File

@ -35,6 +35,7 @@ func Start(conf common.Conf, db *sql.DB) {
// ReadConfidential privilege required
gv1.GET("/friends", Method(v1.FriendsGET, db, common.PrivilegeReadConfidential))
gv1.GET("/friends/with/:id", Method(v1.FriendsWithGET, db, common.PrivilegeReadConfidential))
}
}

View File

@ -117,3 +117,26 @@ func friendPuts(md common.MethodData, row *sql.Rows) (user friendData) {
}
return
}
type friendsWithData struct {
Friends bool `json:"friend"`
Mutual bool `json:"mutual"`
}
// FriendsWithGET checks the current user is friends with the one passed in the request path.
func FriendsWithGET(md common.MethodData) (r common.Response) {
r.Code = 200
var d friendsWithData
uid, err := strconv.Atoi(md.C.Param("id"))
if err != nil {
r.Data = d
return
}
md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ? LIMIT 1)", md.User.UserID, uid).Scan(&d.Friends)
if d.Friends {
// Nyo mode: activated
md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ? LIMIT 1)", md.User.UserID, uid).Scan(&d.Mutual)
}
r.Data = d
return
}