From aae4c5de504573b3ce3df198f93044d55a132f1e Mon Sep 17 00:00:00 2001 From: Howl Date: Thu, 7 Apr 2016 12:21:54 +0200 Subject: [PATCH] Add GET /friends/with/:id --- app/start.go | 1 + app/v1/friend.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/start.go b/app/start.go index 11b7112..3cc27e7 100644 --- a/app/start.go +++ b/app/start.go @@ -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)) } } diff --git a/app/v1/friend.go b/app/v1/friend.go index 0740a2e..f1c2c40 100644 --- a/app/v1/friend.go +++ b/app/v1/friend.go @@ -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 +}