Ruby on rails 自动跟踪Facebook好友
我正在尝试让用户在我的应用程序中自动跟随他们的FB好友。我能够获得用户的FB朋友的ID,但我不知道如何进行自动跟踪 我得到了他们朋友的uid(FBID),所以我想我需要在我的应用程序中找到uid与用户fb好友列表uid匹配的用户,然后让用户自动跟踪这些用户。你会怎么做 user.rbRuby on rails 自动跟踪Facebook好友,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在尝试让用户在我的应用程序中自动跟随他们的FB好友。我能够获得用户的FB朋友的ID,但我不知道如何进行自动跟踪 我得到了他们朋友的uid(FBID),所以我想我需要在我的应用程序中找到uid与用户fb好友列表uid匹配的用户,然后让用户自动跟踪这些用户。你会怎么做 user.rb has_many :relationships, foreign_key: "follower_id", dependent: :destroy has_many :followed_users, thro
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.image = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
#gets uid of user's fb friends
def fbfriends
@graph = Koala::Facebook::API.new(oauth_token)
begin
@fbfriends = @graph.get_connections("me", "friends", fields: "id")
@uids = @fbfriends.map{ |v| v.values }.flatten
rescue Koala::Facebook::AuthenticationError => e
redirect_to '/auth/facebook'
end
@friends = User.where(uid: @uids)
end
def fb_user_id
self.fbfriends.map{ |v| v.id }
end
after_create :follow_fbfriends!
def follow_fbfriends!
relationships.create!(followed_id: fb_user_id)
end
用户展示
<%= @user.fb_user_id %>
可能的步骤:
@fbffriends
中,获取用户朋友的UID
s@friends=User。其中(uid:@fbfriends)
将提供所有这些uid存在的用户配置文件@friends
并为追随者设置与该用户和朋友的关系
@fbfFriends
返回的是一个哈希数组,而不仅仅是UID。首先,将UID提取为:
@uids = @fbfriends.map{ |v| v.values }.flatten
#=> ['1654449181']
现在,将此新数组与User.where(uid:@uids)
一起使用,以检索具有这些uid(如果存在)的用户。要设置与用户的关系,您可能需要阅读
编辑2:
在中跟随朋友代码>,您必须迭代获取的好友ID,并为每个好友ID创建关系,如下所示:
def follow_fbfriends!
fb_user_id.each do |friend_id|
relationships.create!(followed_id: friend_id)
end
end
2.我以前试过,但我总是两样都做不到“#您可能需要修改上述代码中的@fbfriends
定义。你能用一些代码和输出编辑你的问题吗?我想看看它是否成功地检索到UID。我更新了帖子。我可以获取UID,但现在的问题是如何在我的应用程序中获取用户ID,然后让用户在我的应用程序中跟随他的朋友。更新了答案。正确提取UID;通过将用户传递给User.where(uid:…
来提取用户
,并设置关系。我认为,现在这很合乎逻辑。要设置关系,您可能需要阅读-
def follow_fbfriends!
fb_user_id.each do |friend_id|
relationships.create!(followed_id: friend_id)
end
end