I am working on site where i need to update the status of user when he comments on anyone's status

I am using status comments rules integration module to do that .

i have rule that when user comments on status , add status to users wall "user just commented on others status "

rule works fine if i dont save new status on commentor's wall ( just empty rule with only one action "load status" (I am loading the commented status ) .

but when i add action of "save a staus" , it posts double comment. on status .

I am also using ajax-comments module ( just additional information )

also attaching the screenshot for it.

Comments

mrwhizkid’s picture

Had the same problem running a few different triggered rules. Solved it by going into "load status" and changing the name of the "Machine readable variable name". Just change it to something else or add something to it.

I think there was a conflict there with another "load status" in another triggered rule somewhere.

mrwhizkid’s picture

OK. I take that back (sheepish grin). Of course, when I changed the machine label, I also needed to change the variable in the next action, which I forgot to do which means the rule didn't run at all which means that the problem seem solved.

So, I have the same problem. The same comment appears twice when I have a rule based on the comments submodule.

capellic’s picture

I am having the same double comment problem.

My rule sends an email to the status author when anybody comments when the commenter UID does NOT match the status UID.

1. I load the status given the token: [comment:comment-status-id]
2. Then I load a user account given the token from step 1: [status_loaded:sender-uid].
3. Then I send an email to the "loaded user"

The duplicate is being caused by step 2 -- loading a user. Not sure why this event would trigger a double posting or if this is a red herring.

icecreamyou’s picture

Status: Active » Postponed (maintainer needs more info)

It would help if someone could export the rule, paste it into a text file, and attach it to a comment so I can import it and see what you're doing

mrwhizkid’s picture

StatusFileSize
new2.48 KB

Hi Icecream -- here's the rule I'm using. It's very basic...I stripped all of the conditions out for testing. Just a load status and appbar action.

Thanks

arsenalpilgrim’s picture

I am having the same problem of duplicated comments. My rule is:

User saves a comment on a new status:

Rule element 1:
Load Status using the [comment:comment-status-id]
Machine readable variable name is: status_loaded_from_comment

Rule element 2:
Send a mail to an arbitrary mail address
Recipient:[status_loaded_from_comment:user-mail]

The email does work correctly. When I place a comment, it is duplicated (not just a display/view issue), new comment numbers are assigned.

I did notice the Save a Facebook Style Status immediately follows the Load Status in "Select Action to Add". Coincidence?

Thanks,
Richard

arsenalpilgrim’s picture

Any progress? I'm scheduled to go live this week.

mrwhizkid’s picture

StatusFileSize
new3.83 KB

I'm not sure if the previous rule that I uploaded was the right one. Please test this one out. It sends an email out to the owner of the status when a comment is made on that status. I just tested it. It sends the email out but the comment is duplicated because of the load action.

And it definitely is the load actions that are causing problems here.

mrwhizkid’s picture

I used Rules debugging on the rule above, and here is what it comes up with:

0 ms "User saves a new comment on a status" has been invoked.
0.111 ms Executing the rule "test comment" on rule set "User saves a new comment on a status"
49.906 ms Action execution: "Load a status"
50.462 ms Successfully added the new variable "Loaded status"
139.123 ms "User saves a new comment on a status" has been invoked.
139.236 ms Not executing the rule "test comment" on rule set "User saves a new comment on a status" to prevent recursion.
139.307 ms Evaluation of "User saves a new comment on a status" has been finished.
169.957 ms Action execution: "Load a user account"
173.006 ms Successfully added the new variable "Loaded user"
206.264 ms Action execution: "Send a mail to an arbitrary mail address"
238.895 ms Evaluation of "User saves a new comment on a status" has been finished.

As you can see, right after 'Successfully added the new variable "Loaded status"', we get a ' "User saves a new comment on a status" has been invoked' again! Then the next line says "Not executing the rule "test comment" on rule set "User saves a new comment on a status" to prevent recursion" which means that somehow Rules knows it shouldn't be running the rule again...

Strange. I've been going through the module files but I can't figure out why this is happening.

icecreamyou’s picture

Interesting. On first pass I don't see anything that would be causing this either. I don't have time this week to reproduce or debug so I appreciate your digging into it. If you can manage to identify the cause I will fix as soon as I get a chance.

GolDRoger’s picture

Same problem here,

Load Status -> [comment:comment-status-id]

when I try to Set an Alert in Application Toolbar

if I keep (under UID) user i can see the message but if i change to [status_loaded:recipient-id] i don't recieve messages and I see duplicates, but if a load an user and I use loaded user token work.

Unlucky with double messages :/

GolDRoger’s picture

StatusFileSize
new2.78 KB

I've found the solution, after severals try now it's working for me without duplicates.

Simply if we can't do this with normal tokens and rules actions, we build what we need with php :)

So follow this instructions

First create your rule (Triggered Rule) User saves a new comment on a Status

In DO

STEP 1:

Load a Status and in ID put [comment:comment-status-id]

Label: loaded status

Machine readable variable name: *

status_loaded

STEP 2:

Exegute custom PHP code

and inside paste this:


<?php

// first we need to load two users, the creator of the status and the user who commented on this status

$status_creator = user_load($status_loaded->sender); // by user load function i take the UID of the creator of the status, grabbing it from the previous load status
$comment_creator = user_load($comment->uid); // now it's turn to load the UID of the user who created the comment on this status

// i've used user load because the $status_loaded and $comment don't have in memory the user name or other details like mail, but only the ID, so for grabbing all the informations we need to use user_load

// after having all the informations we can start the e-mail sending

$to = $status_creator->mail; // here we set who recieve the notification, specifying his mail

$comment_author = $comment_creator->name; // this is the name of the comment author
$recipient = $status_creator->name; // this is the name of the status creator

// mail is composed and sended

$settings = array();

$settings['to'] = $to;
$settings['subject'] = "You got new message from $comment_author";
$settings['message'] = "Hello $recipient, $comment_author commented your status";

rules_action_mail($settings);

?>

Ok you are ready to recieve your mails without duplicates :D

Hope this help :D let me know if works for you :)

GolDRoger’s picture

What can be interesting now it's to have a list of people who commented the status, and send a mail to every person of a new post and not only the owner of the status. For example owner reply to a comment of a user who commented his status

mrwhizkid’s picture

How about something like this?? I know it's for appbar, but it could be modified, I think.

from: http://drupal.org/node/786144

However, this code was

<?php
global $user; //@todo: really should use $uid instead of $user->uid here
  if (!module_exists('appbar')) {
    return;
  }
  $user_link = theme('username', user_load(array('uid' => $uid)));
  $status_link = url('statuses/'. $sid);
  $status = facebook_status_load($sid); // where $sid is the status ID
  $result = db_query("SELECT uid FROM {fbssc} WHERE sid = %d", $sid);

  $uids = array();
  while ($account = db_fetch_object($result)) {
    if ($account->uid != $status->sender && ($account->uid != $status->recipient || $status->type != 'user')) { // Exclude the sender and recipient of the status
      if ($account->uid != $GLOBALS['user']->uid) { // Exclude the person who posted the comment
        if (!in_array($account->uid, $uids)) { // No repeats
          $uids[] = $account->uid;
        appbar_set_message(t('A comment has been made on a mini blog or wall post that you also commented on. <a href="!message">Click here </a> to view.',
        array('!user' => $user_link, '!message' => $status_link)), $account->uid);
        }
      }
    }
  }
?>
GolDRoger’s picture

Interesting, when I have some time I'll try to make a sistem like this to send mails :)

The rule I previously posted it's working for you?

It's a provisory solution, because if u try to add some other actions after or before the Exegute custom php code the comment is posted twice.

So I think that someone who understand better php than me should focus on find the bug :)

GolDRoger’s picture

If someone has the need, just under

rules_action_mail($settings);

It's possible to add a send to appbar, just add:

$comment_appbar = " $comment_author has commented your Status";

appbar_set_message($comment_appbar, $uid = $status_loaded->sender);
GolDRoger’s picture

Forgot to mention that if you would know what an array stores add a Show a configurable message on the site action and inside put

<?php drupal_set_message(print_r($variable, TRUE)); ?>

This will return all the items contained in the array. :)

mrwhizkid’s picture

Great! It works. I think this will be just fine until something the token issue is fixed. I have spent hours trying to debug that and I can't get to the bottom of it.

Just two question:

1. How can I put a link in the email so that the user can click on it and see their status which was commented on?

2. In the appbar alert, how can I link to that status?

Thanks for your help!

GolDRoger’s picture

Honestly I don't know :( I'm trying to achieve this because I need too to print out the status link in mails and in appbar.

But at the moment I don't know how to dom, because text now are passed to the mail function in an array, and unlucky I don't know a better method. Using a print we can instert some html tags

mrwhizkid’s picture

I have spent hours trying to figure out the problem here and I am completely lost. There's got to be a problem with token integration here somewhere but I just can't quite figure it out.

If anyone has any ideas out there, please share them. I really need this feature.

icecreamyou’s picture

It's not immediately obvious to me why this problem happens, but for anyone debugging, I would probably start by doing a stack trace1 in fbss_comments_save_comment(). That will show you what code saves the comment so that we can walk backwards and figure out what is being erroneously called.

1 If you have Devel installed, use dsm(debug_backtrace()); otherwise use drupal_set_message(print_r(debug_backtrace(), TRUE));

mrwhizkid’s picture

Hi IceCreamYou. I have never done a stacktrace before. I looked through Drupal for a guide but there doesn't seem to be much out there.

Where should I print the <?php drupal_set_message(print_r(debug_backtrace(), TRUE)); ?> exactly?

icecreamyou’s picture

Just put it at the top of the function you want to trace, without the <?php ?> tags.

What a stack trace does is print out a list of the functions that were called to get to where you are in the code. For example if you had this:

function a() {
  b();
}

function b() {
  c();
}

function c() {
  drupal_set_message(print_r(debug_backtrace(), TRUE));
}

...then you would see that c() was called by b() and b() was called by a().

The point of doing this is to see where fbss_comments_save_comment() gets called erroneously so that we can keep that from happening.

mrwhizkid’s picture

StatusFileSize
new8.42 KB

OK. Thanks for the info. I did a trace on the comments save function in the rules submodule of the comments submodule.

function fbss_comments_rules_fbss_comments_after_save($comment, $edit) {
  if ($edit) {
    rules_invoke_event('fbss_comments_edit', $comment);
  }
  else {
global $user; if ($user->uid==24) drupal_set_message(print_r(debug_backtrace(), TRUE));
    rules_invoke_event('fbss_comments_save', $comment);
  }

Here is what I got when I tried to leave a comment. Please note that the comment was "try again". Please see the attachment.

There was lots of code but this was the code immediately following the place where I made the comment. I can't quite make sense of it but it...can anyone else?

P.S. "24" happens to be my UID. I was doing this on a production site and I didn't want anyone else to see all of that code.

icecreamyou’s picture

Status: Postponed (maintainer needs more info) » Fixed

That's only a very small part of the backtrace.

I debugged this myself, and here's what was happening:

  1. A user submits a status comment form.
  2. The status comment form submit handler is run.
    1. The status comment is saved.
    2. The Rules event is invoked.
      1. The status object is loaded.
      2. Tokens for the status object are retrieved.
        1. The themed status update form is rendered.
          1. The themed comment form is rendered.
            1. The form submit handler is executed again, because it hasn't completed the first time it was run yet so $form_state['submitted'] hasn't been set yet -- and the whole thing starts over.

The solution was to add a static variable to the submit handler to check if it has already been called in that request. If it has, we don't try to save the comment again. I'm not 100% sure that this solves every possible corner case, but I haven't been able to break it, so I'm satisfied with this approach.

Committed fix to dev.

mrwhizkid’s picture

It works! Thanks so much!

mrwhizkid’s picture

Oops...duplicate. Sorry.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.