2016-04-07 09:20:35 +00:00
package v1
import (
"database/sql"
2017-01-14 17:06:16 +00:00
"zxq.co/ripple/rippleapi/common"
2016-04-07 09:20:35 +00:00
)
type friendData struct {
userData
IsMutual bool ` json:"is_mutual" `
}
2016-04-16 16:05:24 +00:00
type friendsGETResponse struct {
common . ResponseBase
Friends [ ] friendData ` json:"friends" `
}
2016-04-07 09:20:35 +00:00
// FriendsGET is the API request handler for GET /friends.
// It retrieves an user's friends, and whether the friendship is mutual or not.
2016-04-16 16:05:24 +00:00
func FriendsGET ( md common . MethodData ) common . CodeMessager {
2016-04-07 09:20:35 +00:00
var myFrienders [ ] int
2016-04-08 17:05:54 +00:00
myFriendersRaw , err := md . DB . Query ( "SELECT user1 FROM users_relationships WHERE user2 = ?" , md . ID ( ) )
2016-04-07 09:20:35 +00:00
if 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 09:20:35 +00:00
}
defer myFriendersRaw . Close ( )
for myFriendersRaw . Next ( ) {
var i int
err := myFriendersRaw . Scan ( & i )
if err != nil {
2016-04-08 17:05:54 +00:00
md . Err ( err )
2016-04-07 09:20:35 +00:00
continue
}
myFrienders = append ( myFrienders , i )
}
if err := myFriendersRaw . Err ( ) ; err != nil {
2016-04-08 17:05:54 +00:00
md . Err ( err )
2016-04-07 09:20:35 +00:00
}
2016-10-16 14:45:42 +00:00
myFriendsQuery := `
2016-07-03 19:55:03 +00:00
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-04-09 21:58:27 +00:00
users_stats . username_aka ,
2016-07-06 21:43:43 +00:00
users_stats . country
2016-04-07 09:20:35 +00:00
FROM users_relationships
LEFT JOIN users
ON users_relationships . user2 = users . id
LEFT JOIN users_stats
ON users_relationships . user2 = users_stats . id
WHERE users_relationships . user1 = ?
2016-10-16 14:45:42 +00:00
`
2016-04-07 09:20:35 +00:00
2016-10-16 14:45:42 +00:00
myFriendsQuery += common . Sort ( md , common . SortConfiguration {
Allowed : [ ] string {
"id" ,
"username" ,
"latest_activity" ,
} ,
Default : "users.id asc" ,
Table : "users" ,
} ) + "\n"
results , err := md . DB . Query ( myFriendsQuery + common . Paginate ( md . Query ( "p" ) , md . Query ( "l" ) , 100 ) , md . ID ( ) )
2016-04-07 09:20:35 +00:00
if 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 09:20:35 +00:00
}
var myFriends [ ] friendData
defer results . Close ( )
for results . Next ( ) {
newFriend := friendPuts ( md , results )
for _ , uid := range myFrienders {
if uid == newFriend . ID {
newFriend . IsMutual = true
break
}
}
myFriends = append ( myFriends , newFriend )
}
if err := results . Err ( ) ; err != nil {
2016-04-08 17:05:54 +00:00
md . Err ( err )
2016-04-07 09:20:35 +00:00
}
2016-04-07 09:44:46 +00:00
2016-04-16 16:05:24 +00:00
r := friendsGETResponse { }
2016-04-07 09:44:46 +00:00
r . Code = 200
2016-04-16 16:05:24 +00:00
r . Friends = myFriends
return r
2016-04-07 09:20:35 +00:00
}
func friendPuts ( md common . MethodData , row * sql . Rows ) ( user friendData ) {
var err error
2016-07-06 21:43:43 +00:00
err = row . Scan ( & user . ID , & user . Username , & user . RegisteredOn , & user . Privileges , & user . LatestActivity , & user . UsernameAKA , & user . Country )
2016-04-07 09:20:35 +00:00
if err != nil {
2016-04-08 17:05:54 +00:00
md . Err ( err )
2016-04-07 09:20:35 +00:00
return
}
return
}
2016-04-07 10:21:54 +00:00
2016-04-16 16:05:24 +00:00
type friendsWithResponse struct {
common . ResponseBase
2016-04-07 10:21:54 +00:00
Friends bool ` json:"friend" `
Mutual bool ` json:"mutual" `
}
// FriendsWithGET checks the current user is friends with the one passed in the request path.
2016-04-16 16:05:24 +00:00
func FriendsWithGET ( md common . MethodData ) common . CodeMessager {
2016-06-13 19:48:09 +00:00
var r friendsWithResponse
2016-04-07 10:21:54 +00:00
r . Code = 200
2016-08-15 17:59:46 +00:00
uid := common . Int ( md . Query ( "id" ) )
2016-06-13 19:48:09 +00:00
if uid == 0 {
return r
2016-04-07 10:21:54 +00:00
}
2016-06-13 20:40:01 +00:00
err := md . DB . QueryRow ( "SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ? LIMIT 1), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ? LIMIT 1)" , md . ID ( ) , uid , md . ID ( ) , uid ) . Scan ( & r . Friends , & r . Mutual )
2016-04-08 16:18:42 +00:00
if err != sql . ErrNoRows && 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 10:21:54 +00:00
}
2016-04-16 16:05:24 +00:00
if ! r . Friends {
r . Mutual = false
}
return r
2016-04-07 10:21:54 +00:00
}
2016-04-08 17:05:54 +00:00
2016-11-16 17:03:47 +00:00
// FriendsAddPOST adds an user to the friends.
func FriendsAddPOST ( md common . MethodData ) common . CodeMessager {
var u struct {
User int ` json:"user" `
}
md . RequestData . Unmarshal ( & u )
return addFriend ( md , u . User )
2016-04-08 17:05:54 +00:00
}
2016-04-16 16:05:24 +00:00
func addFriend ( md common . MethodData , u int ) common . CodeMessager {
2016-04-08 17:05:54 +00:00
if md . ID ( ) == u {
2016-04-16 16:05:24 +00:00
return common . SimpleResponse ( 406 , "Just so you know: you can't add yourself to your friends." )
2016-04-08 17:05:54 +00:00
}
if ! userExists ( md , u ) {
2016-04-16 16:05:24 +00:00
return common . SimpleResponse ( 404 , "I'd also like to be friends with someone who doesn't even exist (???), however that's NOT POSSIBLE." )
2016-04-08 17:05:54 +00:00
}
var (
relExists bool
isMutual bool
)
err := md . DB . QueryRow ( "SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ?), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ?)" , md . ID ( ) , u , md . ID ( ) , u ) . Scan ( & relExists , & isMutual )
if err != nil && err != sql . ErrNoRows {
md . Err ( err )
2016-04-16 16:05:24 +00:00
return Err500
2016-04-08 17:05:54 +00:00
}
if ! relExists {
_ , err := md . DB . Exec ( "INSERT INTO users_relationships(user1, user2) VALUES (?, ?)" , md . User . UserID , u )
if err != nil {
md . Err ( err )
2016-04-16 16:05:24 +00:00
return Err500
2016-04-08 17:05:54 +00:00
}
}
2016-04-16 16:05:24 +00:00
var r friendsWithResponse
2016-04-08 17:05:54 +00:00
r . Code = 200
2016-04-16 16:05:24 +00:00
r . Friends = true
r . Mutual = isMutual
return r
2016-04-08 17:05:54 +00:00
}
// userExists makes sure an user exists.
func userExists ( md common . MethodData , u int ) ( r bool ) {
2016-08-27 10:04:12 +00:00
err := md . DB . QueryRow ( "SELECT EXISTS(SELECT 1 FROM users WHERE id = ? AND " +
md . User . OnlyUserPublic ( true ) + ")" , u ) . Scan ( & r )
2016-04-08 17:05:54 +00:00
if err != nil && err != sql . ErrNoRows {
md . Err ( err )
}
return
}
2016-04-08 17:23:52 +00:00
2016-11-16 17:03:47 +00:00
// FriendsDelPOST deletes an user's friend.
func FriendsDelPOST ( md common . MethodData ) common . CodeMessager {
var u struct {
User int ` json:"user" `
}
md . RequestData . Unmarshal ( & u )
return delFriend ( md , u . User )
2016-04-08 17:23:52 +00:00
}
2016-04-16 16:05:24 +00:00
func delFriend ( md common . MethodData , u int ) common . CodeMessager {
2016-04-08 17:23:52 +00:00
_ , err := md . DB . Exec ( "DELETE FROM users_relationships WHERE user1 = ? AND user2 = ?" , md . ID ( ) , u )
if err != nil {
md . Err ( err )
return Err500
}
2016-04-16 16:05:24 +00:00
r := friendsWithResponse {
Friends : false ,
Mutual : false ,
2016-04-08 17:23:52 +00:00
}
2016-04-16 16:05:24 +00:00
r . Code = 200
return r
2016-04-08 17:23:52 +00:00
}