简体   繁体   English

如何在设计用户之间建立友谊并通过api获取每个用户的朋友列表?

[英]How to create friendships between devise users and get the list of friends of each user through api?

I use devise for authentication in my web application and my web application also responds to iPhone applications. 我在Web应用程序中使用devise进行身份验证,并且Web应用程序也响应iPhone应用程序。 So far I have done only authentication and so when the iPhone app sends the username and password as 到目前为止,我只完成了身份验证,因此当iPhone应用程序将用户名和密码发送为

http://localhost:3000/login.json  -d "{'user' : { 'username' : 'sample@yahoo.co.in','password' : 'password'}}"

The response would be 响应将是

{"user":{"authentication_token":"LayzZw3s3gsNMFRJp1JR","email":"sample@yahoo.co.in","username":"sample@yahoo.co.in"}} {“用户”:{“ authentication_token”:“ LayzZw3s3gsNMFRJp1JR”,“电子邮件”:“ sample@yahoo.co.in”,“用户名”:“ sample@yahoo.co.in”}}

But now I need to create friendships between user from iPhone application ie, the user have to send friend request to another user through API and the another user should receive a request 但是现在我需要在iPhone应用程序的用户之间建立友谊,即该用户必须通过API向另一个用户发送好友请求,而另一个用户应该收到一个请求

After the user accepts the request the user should be added to the friend list of both users and the friends of the user should be displayed when the user needs by calling API 用户接受请求后,应将用户添加到两个用户的好友列表中,并在需要时通过调用API显示该用户的好友

How can I do this? 我怎样才能做到这一点?

This has very little if nothing to do with authentication. 这与身份验证没有任何关系。

What you need is a FriendshipRequest model that belongs_to both a "asking" user and a "asked" other user. 您需要的是一个FriendshipRequest模型,该模型既属于“询问”用户,也属于“询问”其他用户。 When a user want to request someone to be his friend, he just creates a FriendshipRequest in your db. 当用户想要请求某人成为他的朋友时,他只是在您的数据库中创建了FriendshipRequest When the other user consults his page, he sees that friendship request and can either accept it (create a new Friendship object in the db, that belongs_to both users) or discard it (destroy the FriendshipRequest record or mark it as rejected for the other user to see). 当其他用户查询他的页面时,他看到该友谊请求并可以接受它(在db中创建一个新的Friendship对象,属于两个用户)或丢弃它(销毁FriendshipRequest记录或将其标记为对其他用户拒绝)查看)。

all in all, you can even use only a Friendship model with an accepted boolean flag. 总而言之,您甚至只能使用带有accepted布尔标志的Friendship模型。 The rest is classic rails. 其余的是经典的轨道。

As m_x said this has nothing to do with authentication. 正如m_x所说,这与身份验证无关。

What you should create is a self referential associon between users with an has many trhough relationship. 您应该创建的是具有许多穿越关系的用户之间的自我参照关联。

An user has many friends trough friendship (and in the same time has many inverse_friends). 一个用户有很多通过友谊的朋友(同时有很多inverse_friends)。

This is very well explained in railscast #163 http://railscasts.com/episodes/163-self-referential-association 在railscast#163 http://railscasts.com/episodes/163-self-referential-association中对此进行了很好的解释。

In your scenario the inverse_friend should be considered as "friends request" and once a user has both a friend and an inverse friend relation with another user the "friendship" should be considered accepted. 在您的方案中,应该将inverse_friend视为“朋友请求”,并且一旦用户与另一个用户同时拥有朋友和反向朋友关系,就应将“友谊”视为已接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM