简体   繁体   中英

Using Pusher to push messages to friends only

I have set up basic Pusher functionality on my Rails app based on a model Post, however the Pusher updates are sent to all users whereas I want the updates to only be seen by users who are 'connected / friends' with the sender.

Is this best resolved in the view using javascript or ruby? From the controller I am sending to the view via Pusher the @post.user_id, however I can't seem to get it to work when implementing the below:

Post Controller

def index

    # Get the current user's friends
    @friends = current_user.friends.all


    end

end

def create
    @post = current_user.posts.new(post_params)
    @puserid = @post.user_id
    @postuser = User.find_by_id(@puserid)
    @userslug = @postuser.slug
    @ids = current_user.friends.pluck(:id) << current_user.id

    if @post.save
        #flash[:success] = "You have created a new post"
        redirect_to posts_path

        Pusher['worklink'].trigger('update', {

            postcontent: @post.content,
            userid: @post.user_id,
            userslug: @userslug,
            userfirstname: @postuser.firstname,
            userlastname: @postuser.lastname,
            postid: @post.id

            })



    else
        render "new"
    end
end

Post index view

<script src="https://js.pusher.com/2.2/pusher.min.js"></script>

<script type="text/javascript">
var pusher = new Pusher('<%= Pusher.key %>'); // uses your API KEY
var channel = pusher.subscribe('worklink');

channel.bind('update', function(data) {

var prependLink = "<a href='/profile/" + data.userslug + "'>" + data.userfirstname + " " + data.userlastname + " " + "</a><span class='red small'>New</span>";

var user = data.userid;

$("#comments").prepend(

    "<% if @friends.include?(" + user + ") %><div class='post_box'><div class='post'><p class='username'><b>" + prependLink + "</b></p><p class='post_content'>" + data.postcontent + "</p><div class='post_attributes'><ul><div class='right'><li class='small bold'>0&nbsp;</li><li class='x'><a data-method='post' href='/home/" + data.postid + "/likes' rel='nofollow'><img alt='Tick2' src='/assets/tick2.png'></a></li></div><li class='post_time'>less than a minute ago</li></ul></div></div></div><% end %>");


$('.post p:contains("http://")').html(function(index, html) {
var url = html.match(/(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g);

$.each(url, function(i, v) {
    html = html.replace(v, '<a href="' + v + '" target="_blank">' + v + '</a>');        
});

return html;
});

});
</script>

There are three different channels type ( http://pusher.com/docs/client_api_guide/client_channels ):

  • Private
  • Public
  • Presence

The right one for you I guess is presence, it requires authentication before subscribe where you can control whether to let subscribe or not.

Also, you can refer to this code for more functional example: https://github.com/tarnfeld/PusherChat-Rails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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