solved Creating a module to change from the Follow model to the Friend model

Elise
Elise
@elise
8 years ago
249 posts
Ning uses the Facebook/Friend model:
1) I send you a friend request
2) You accept
3) we can both send each other private notes

Jamroom uses the Twitter/Follower model
1) I send you a Follow request
2) You accept. You can send me private notes
3) You sent me a Follow request.
4) I accept. I can send you private notes.

Both models are great but my Ning members are used to the second one.

So I am trying to write a module that will immediately authorise both party when a request is accepted.

I am following the documentation here:
https://www.jamroom.net/the-jamroom-network/documentation/jamroom-developers-guide/26/creating-a-module


I went to Follow + Private Note + User module > Info, hoping to find an event that related to the Follow request being sent, so I can have a listener. But I can't find it.

Developer mode is enabled.

Where should I look for this event?

updated by @elise: 02/23/17 04:32:56PM
paul
@paul
8 years ago
4,325 posts
The event you need to listen for is the 'db_update_item' event. If that event is updating the Follower datastore with 'follow_active' = 1 (ie. the follow has been accepted) then you can reciprocate by adding another entry to the Follower datastore where the 'followee is following the follower'!
That explanation may not mean a lot to you at the moment but 'datastores' are the basis of all item storage within Jamroom. They are MySql items and are accessed with php function calls within the custom listener module.


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
So that event would not be listed on the info tab of a specific module? Since datastore events are common to all?

How do I find the original call that makes the datastore update?
paul
@paul
8 years ago
4,325 posts
Datastore events are common to all modules so listed in the Core module Info tab.
The actual 'update' that you are listening and filtering for occurs in the jrFollower module index.php script, in function 'view_jrFollower_approve' at about line 300
(Be sure not to edit or change anything when looking at these module php scripts as things easily break!!)


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
Yes, I am sweating heavily as I try to do this, being deathly afraid to break something :)
Thanks, I found the datastore call and I am looking into other similar listeners to see how they do it.
Elise
Elise
@elise
8 years ago
249 posts
lolyes, i posted that URL higher in the discussion. That's what I am following :)
Elise
Elise
@elise
8 years ago
249 posts
When a bare bone module has been created (modules/xxxFriend) with an /include.php and /index.php files, should it show up in the ACP? Or is that only for jrModules?
paul
@paul
8 years ago
4,325 posts
It should show up in the designated category, providing you have built the meta and init functions correctly.
You should be able to do this with just an include.php file with the above functions and the listener function in that. Paste it here if you want so that we can check it over.


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
<?php
/**
 * @copyright xxxxxx.
 */

// make sure we are not being called directly
defined('APP_DIR') or exit();

/**
 * meta
 */
function xxFriend_meta(){
    $_tmp = array(
        'name'        => 'Friend',
        'url'         => 'friend',
        'version'     => '0.0.1',
        'developer'   => 'Elise, ©' . strftime('%Y'),
        'description' => 'When a follow request is accepted, the user follows the requester back.',
        'category'    => 'profiles'
    );
    return $_tmp;
}

/**
 * init
 */
function xxFriend_init(){
    //register event listeners
    jrCore_register_event_listener('jrCore', 'db_update_item', 'xxFriend_follow_request_authorized_listener');

    return true;
}

/**
 * Listens for when a user accepts a Follow request
 * Update the user to immediately follow the requester back 
 * @param $_data array Array of information from trigger
 * @param $_user array Current user
 * @param $_conf array Global Config
 * @param $_args array additional parameters passed in by trigger caller
 * @param $event string Triggered Event name
 * @return array
 */
function xxFriend_follow_request_authorized_listener($_data, $_user, $_conf, $_args, $event)
{ return $_data; }

updated by @elise: 09/10/16 12:00:25PM
Elise
Elise
@elise
8 years ago
249 posts
OOooo there's error messages in the dashboard Activity Log.
[EliseLebeau] invalid module: xxxFriend - required meta function does not exist

Ooops one too many xx.
Elise
Elise
@elise
8 years ago
249 posts
Ok, I have achieved integrity and I can see my tiny module. It's cute.

Now I'm guessing I need to test if the event has the follow_active = 1 data in it. On to figure that out :)
Elise
Elise
@elise
8 years ago
249 posts
Is that enough (or too much) to ensure I am only responding to the db_update_item that relates to a follower being authorized?


function xxFriend_follow_request_authorized_listener($_data, $_user, $_conf, $_args, $event)
{ if ($_args['module'] == 'jrFollower' && jrCore_checktype($_args['option'], 'follow') && jrCore_checktype($_data['__ajax'], '1') && //profile_id isset($_data['_1']) && jrCore_checktype($_data['_1'], 'number_nz') && //requester user_id isset($_data['_1']) && jrCore_checktype($_data['_2'], 'number_nz')) { return $_data;
Elise
Elise
@elise
8 years ago
249 posts
So the better version (that compiles)
function xxFriend_follow_request_authorized_listener($_data, $_user, $_conf, $_args, $event)
{ if ($_args['module'] == 'jrFollower' && isset($_args['option']) && $_args['option'] == 'follow' && isset($_data['__ajax']) && $_args['__ajax'] == '1' && //profile_id isset($_data['_1']) && jrCore_checktype($_data['_1'], 'number_nz') && //requester user_id isset($_data['_1']) && jrCore_checktype($_data['_2'], 'number_nz')) { } return $_data; }

I still don't know how to look at th incoming data or print it out so I can see what variable I have to switch out.
paul
@paul
8 years ago
4,325 posts
To see the values of php variables when developing a module, use 'fdebug'. This will dump the values into the debug log which you can view in your ACP=>Dashboard=>Activity Log=>Debug Log
So whenever I develop a module listener I always start with this code -

function xxModule_my_listener($_data, $_user, $_conf, $_args, $event)
{ fdebug($_data, $_args); return $_data; }
That way, I can see all the variables I have to play with in the function.

You're doing well so far. Please carry on. Its good to nurture potential Jamroom developers :-)
If I have a bit of time later today I will try coding this listener and post my version here so that you can compare things.
Cheers
Pa


--
Paul Asher - JR Developer and System Import Specialist
paul
@paul
8 years ago
4,325 posts
OK - Here is my include.php file -

<?php
/**
 * @copyright 2016 Talldude Networks, LLC.
 */

// make sure we are not being called directly
defined('APP_DIR') or exit();

/**
 * meta
 */
function jrFriend_meta(){
    $_tmp = array(
        'name'        => 'Followers to Friends',
        'url'         => 'friend',
        'version'     => '0.0.1',
        'developer'   => 'The Jamroom Network, ©' . strftime('%Y'),
        'description' => 'A listener module. Upon follower acceptence, it reciprocates the follow.',
        'category'    => 'custom',
        'requires'    => 'jrFollower',
        'license'     => 'jcl'
    );
    return $_tmp;
}

/**
 * init
 */
function jrFriend_init(){
    // Update Item listener
    jrCore_register_event_listener('jrCore', 'db_update_item', 'jrFriend_db_update_item_listener');

    // Create Item listener
    jrCore_register_event_listener('jrCore', 'db_create_item', 'jrFriend_db_create_item_listener');

    // Register our friend tools
    jrCore_register_module_feature('jrCore', 'tool_view', 'jrFriend', 'approve_set', array('Set Approval', 'Set all site profiles to \'Follower Approve\''));

    return true;
}

//----------------------
// EVENT LISTENERS
//----------------------

/**
 * Update item listener
 * @param $_data array incoming data array of item including original owner profile and user IDs
 * @param $_user array current user info
 * @param $_conf array Global config
 * @param $_args array of new owner profile and user IDs
 * @param $event string Event Trigger name
 * @return array
 */
function jrFriend_db_update_item_listener($_data, $_user, $_conf, $_args, $event)
{ if ($_args['module'] == 'jrFollower' && jrCore_checktype($_args['_item_id'], 'number_nz') && isset($_data['follow_active']) && $_data['follow_active'] == 1) { // Its a follow accept - Let's reciprocate $_rt = jrCore_db_get_item('jrFollower', $_args['_item_id'], true); $_tmp = array( 'follow_active' => 1, 'follow_profile_id' => $_rt['_profile_id'] ); jrCore_db_create_item('jrFollower', $_tmp); } return $_data; } /** * Create item listener * @param $_data array incoming data array of item including original owner profile and user IDs * @param $_user array current user info * @param $_conf array Global config * @param $_args array of new owner profile and user IDs * @param $event string Event Trigger name * @return array */ function jrFriend_db_create_item_listener($_data, $_user, $_conf, $_args, $event)
{ if ($_args['module'] == 'jrProfile' && jrCore_checktype($_args['_item_id'], 'number_nz')) { // Its a profile creation - set follower approve to 'on' $_data['profile_jrFollower_approve'] = 'on'; } return $_data; }
Note that there is a second listener for 'db_create_item'. This looks for new profiles being created and sets the 'follow approve' to 'on' by default.
Also note that there is also a module tool to set all site profiles to follower approve regardless. The code for that tool is in the index.php file below -

<?php
/**
 * @copyright 2016 Talldude Networks, LLC.
 */

// make sure we are not being called directly
defined('APP_DIR') or exit();

//------------------------------
// approve_set
//------------------------------
function view_jrFriend_approve_set($_post, &$_user, &$_conf)
{ jrUser_master_only(); jrCore_page_include_admin_menu(); jrCore_page_admin_tabs('jrFriend'); jrCore_page_banner('Set Follower Approval'); // Form init $_tmp = array( 'submit_value' => 'Set Approvals', 'cancel' => "{$_conf['jrCore_base_url']}/{$_post['module_url']}/admin/tools", 'submit_prompt' => 'Are you sure you want to set all your profiles to Follower Approve?', 'submit_modal' => 'update', 'modal_width' => 800, 'modal_height' => 400, 'modal_note' => 'Please be patient whilst group discussions are alternatively imported' ); jrCore_form_create($_tmp); $_tmp = array( 'name' => 'dummy', 'value' => 'off', 'type' => 'hidden' ); jrCore_form_field_create($_tmp); jrCore_page_display(); } //------------------------------ // approve_set //------------------------------ function view_jrFriend_approve_set_save($_post, &$_user, &$_conf)
{ jrUser_master_only(); jrCore_form_validate($_post); // How many profiles are in the system? $pcnt = jrCore_db_get_datastore_item_count('jrProfile'); jrCore_form_modal_notice('update', "Setting {$pcnt} profiles to 'Follower Approve'"); // Get all their IDs $_s = array( 'return_item_id_only' => true, 'limit' => $pcnt ); $_rt = jrCore_db_search_items('jrProfile', $_s); if ($_rt && is_array($_rt) && count($_rt) > 0) { $_tmp = array(); foreach ($_rt as $rt) { $_tmp["{$rt}"] = array('profile_jrFollower_approve' => 'on'); } jrCore_db_update_multiple_items('jrProfile', $_tmp); } else { jrCore_form_modal_notice('update', "Error: Profiles not found"); } jrCore_form_modal_notice('complete', "Done"); jrCore_form_result(); }

Cheers
Pa


--
Paul Asher - JR Developer and System Import Specialist

updated by @paul: 09/11/16 04:23:49AM
paul
@paul
8 years ago
4,325 posts
PS - Have moved this thread to the 'developers' forum.

General question to all - Would anyone else be interested in this module? If so, we could get it released officially.


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
paul:
To see the values of php variables when developing a module, use 'fdebug'. This will dump the values into the debug log which you can view in your ACP=>Dashboard=>Activity Log=>Debug Log
So whenever I develop a module listener I always start with this code -
function xxModule_my_listener($_data, $_user, $_conf, $_args, $event)
{ fdebug($_data, $_args); return $_data; }
That way, I can see all the variables I have to play with in the function.
You're doing well so far. Please carry on. Its good to nurture potential Jamroom developers
If I have a bit of time later today I will try coding this listener and post my version here so that you can compare things.
Cheers
Pa

AH! This is an excellent tip. I saw the mention of {debug} in Michael's videos but it wasn't clear when the debug content goes to. Thanks for clarifying.
Elise
Elise
@elise
8 years ago
249 posts
paul:
PS - Have moved this thread to the 'developers' forum.
General question to all - Would anyone else be interested in this module? If so, we could get it released officially.

LOL @strumelia?
Elise
Elise
@elise
8 years ago
249 posts
Thanks for the solution, Paul.

I thought initially the module would consist of calling a method to create a new follower and then another one with reversed ID (MIchael had hinted at this) to close the loop. But I realized last night that it was more than that and it was out of my depth (I'm a Java gal) and gave up.

It was really nice to wake up to a solution!

I would love to see this is a new jrModule so it's maintained as part of the releases.
I'll update my test module and let you know how to goes.
Elise
Elise
@elise
8 years ago
249 posts
paul:
Note that there is a second listener for 'db_create_item'. This looks for new profiles being created and sets the 'follow approve' to 'on' by default.
Also note that there is also a module tool to set all site profiles to follower approve regardless. The code for that tool is in the index.php file below -

follow approve means that all followers request need to be approved right? are you doing this in case someone has unchecked that option in their profile and they would skip by the Friend module? Or another reason I don't see?
paul
@paul
8 years ago
4,325 posts
Quote: follow approve means that all followers request need to be approved right? are you doing this in case someone has unchecked that option in their profile and they would skip by the Friend module? Or another reason I don't see?
Yes - Its sets all profiles to have to approve followers. They can subsequently turn that off, of course, but if the do, this module will not kick in and anyone who follows them will be a 'follower' and not a 'friend' (ie. no reciprocation).
Cheers
Pa


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
Oooooops this module is not doing what I had in mind,.
When I installed and Followed someone, they were "automatically" approved. This would let the spammers run wild.

Here are the steps to repro:
1) Login with a member account
2) Follow another member
3) The Follow button turns immediately to "Followed" (not expected, they should have to be authorized first)
4) The email notification went out, as if authorization was required. But the follower was already marked "accepted".

The trigger is when the member "authorizes" the follower right? Not when they click the Follow button?
updated by @elise: 09/11/16 08:46:31AM
Elise
Elise
@elise
8 years ago
249 posts
Oh wait. The flag that sets "Follower Approve" in ACP (I can't remember where it is right now), it must be only for NEW members. Existing members need that tool you wrote to change their flag in bulk. (I think).

I'm catching up. Trying again...
Elise
Elise
@elise
8 years ago
249 posts
Wow, that is SO BEAUTIFUL! Works like a charm.
@paul, you da man!
paul
@paul
8 years ago
4,325 posts
Yep - That tool will make sure everyone is set to approve followers to start with.
So you're ok with the way it works now? The follower will get an email when he/she has been approved so there's no need for anything else to happen imo.


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
Yeah! It's perfect :)
Holly Dilatush
Holly Dilatush
@holly-dilatush
8 years ago
212 posts
We are very interested in this! Is it available?

Thanks in advance,
Holly
paul
@paul
8 years ago
4,325 posts
Holly Dilatush:
We are very interested in this! Is it available?

Thanks in advance,
Holly

Not as a JR module. From the above posts it looks as though Elise has worked on my original code suggestion. Elise - Is the module working ok on your site? If so, do you want to share the final version?


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
Yep it works and I love it. I sponsored the work you did on the module.
Will it still work in JR6? Or do I need to sponsor an upgrade?
Elise
Elise
@elise
8 years ago
249 posts
Here are the files
zip
Archive.zip  •  3KB

michael
@michael
8 years ago
7,692 posts
Elise:....Will it still work in JR6? .....
Should be fine.
paul
@paul
8 years ago
4,325 posts
Elise:
Yep it works and I love it. I sponsored the work you did on the module.
Will it still work in JR6? Or do I need to sponsor an upgrade?
Doh - Forgot that we did it for you !!
Yes, as Michael said, it should work fine on JR6. The module is called 'jrFriend' and I'll try and release it to the marketplace later today.


--
Paul Asher - JR Developer and System Import Specialist
brian
@brian
8 years ago
10,136 posts
The new "Followers to Friends" module has been released in the Marketplace:

https://www.jamroom.net/the-jamroom-network/networkmarket/393/followers-to-friends


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
This sounds like something my members would like and be familiar with from ning.
But I need to know how it might effect my site that has been running without it for over a year now.
I get that all members will get their setting changed to require Follower approval....but:
On profile pages, will their list of 'followers' still look the same? Will any language be automatically changed, or NEED to be changed by us (meaning not optional)... from "follow/follower/following" to "friend" ? For example will members still see their usual list of members on the /follow/following page? And even if we change the profile page TAB label from "followers" to "Friends"...clicking on it will still take you to /(profilename)/follow ...correct?
and all this will not otherwise change or lose all the 2-way connections my members have already established with others, correct?
Thanks for clarifying these things.


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015
brian
@brian
8 years ago
10,136 posts
The Followers to Friends module makes _no_ changes to the followers module at all. All follower pages will remain the same and show as "followers". It would be up to you to make any language changes that you want.

It does provide a tool that will force change your profiles to "Approve followers" so they have to accept the "friend" request before it is reciprocated.

Let me know if that helps.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
Ok- so do you mean that simply by accepting the 'friend' request, that in itself completes the 2 way connection?
And is there a 'decline' or 'ignore' option when a member receives the 'friend' request?


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015
brian
@brian
8 years ago
10,136 posts
Strumelia:
Ok- so do you mean that simply by accepting the 'friend' request, that in itself completes the 2 way connection?
And is there a 'decline' or 'ignore' option when a member receives the 'friend' request?

The friend request is the same request the user gets to "approve" a follower. There is no "decline" or "ignore".

By accepting it adds the receiving user as a follower to the profile they are accepting the follower request from - i.e. it reciprocates the follow.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
Ok good- so as I understand it, when a member has their settings to approve followers, it eliminates the extra step of them having to go to that person's page and click 'follow', is that right?- they cantake the action to 'accept/follow back' right from the email notification/request?

Oh, and does it ''permanently'' set people to need to approve followers? or can they change it back again?


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015

updated by @strumelia: 11/15/16 01:47:43PM
brian
@brian
8 years ago
10,136 posts
Correct - the follow back is automatic - it's not an option for them - it automatically happens when they accept the follower.

The users CAN change it back so it works like the regular followers model.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
They 'accept' it by clicking somewhere in the request notification email?


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
I think I may particularly need to adjust the language in people's profile page settings where members see/check "Approve followers" and get the associated help message: ("If "Approve Followers" is checked, anyone following your profile will need to be approved by you before becoming a follower of your profile.").
I'll have to incorporate/clarify the 'friends' concept in there somehow, otherwise this could be somewhat confusing to them.

But I think this will all be very good!


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015
brian
@brian
8 years ago
10,136 posts
Nothing has changed in the followers. If a profile is set to APPROVE followers, when they get a new follower they will get an email that tells them they have a new follower, and give them the option to approve the follower. If they approve the new follower, then a reciprocal follow is created that goes the other direction.

It's a super simple setup and does nothing more that what I have outlined.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
Strumelia
Strumelia
@strumelia
8 years ago
3,602 posts
That's very clear to me now and it's just great. Thank you for bearing with me Brian. :)


--
...just another satisfied Jamroom customer.
Migrated from Ning to Jamroom June 2015
Elise
Elise
@elise
8 years ago
249 posts
@paul I think we might also need the same logic to "unfollow" where it unfollows both sides when one side requests it. I'm having a member who is trying to unfriend someone but the other person still shows up as a profile she follows.
Elise
Elise
@elise
8 years ago
249 posts
is BBCode not working?
paul
@paul
8 years ago
4,325 posts
Elise:
@paul I think we might also need the same logic to "unfollow" where it unfollows both sides when one side requests it. I'm having a member who is trying to unfriend someone but the other person still shows up as a profile she follows.

Ha - We didn't fully think this through ;-)
Let me check that out.
Thanks


--
Paul Asher - JR Developer and System Import Specialist
Elise
Elise
@elise
8 years ago
249 posts
LOL, it's organically grown!
paul
@paul
8 years ago
4,325 posts
This is implemented in the next release of the jrFriend module.


--
Paul Asher - JR Developer and System Import Specialist

Tags