I would like to be able to call a javascript function when someone posts a comment. Is this possible? I searched the documentation on disqus and I didn't see anything about this. There is a setting for enabling this under disqus settings.

Comments

kate’s picture

Status: Active » Fixed

In case anyone else has this question.... I asked disqus support for the answer and they pointed me to the help page that describes how you can capture 'Disqus commenting activity':

http://docs.disqus.com/help/60/

robloach’s picture

Interesting, would be neat to add this to some kind of Drupal JavaScript hook or something.....

robloach’s picture

Title: Trigger action when someone posts a comment? » JavaScript Trigger action when someone posts a comment?
Version: 6.x-1.7 » 7.x-1.x-dev
Category: support » feature
Priority: Normal » Minor
Status: Fixed » Active
bryancasler’s picture

subscribe

sean_a’s picture

Status: Active » Needs review
StatusFileSize
new25.81 KB

I couldn't get a patch made via git but here is the first stab at this. Posting on behalf of my team member Jennifer.

Changed files are disqus.module and disqus.js

changes/testing welcome!

This is for 6.x branch

sean_a’s picture

StatusFileSize
new3.7 KB

ok I think I patched it right. (attached) please review either patch or .zip file in previous comment

bryancasler’s picture

sean_a is the patch in #6 for the 6.x branch or 7.x branch?

sean_a’s picture

6.x- sorry I realize this is a 7.x feature request but there wasn't another discussion about this feature

patrickroma’s picture

Is there also a working version of this for D7? This is really cool!

attheshow’s picture

StatusFileSize
new3.34 KB

Here's a patch for 7.x-1.x-dev with the code from #6 improved a bit and refactored for D7. Works nicely for our site.

attheshow’s picture

One note: When using the patch from #10 above, you need to probably give both anonymous users and authenticated users the "Notify Disqus node author" permission. This will allow the notifications to be sent out if you wish.

attheshow’s picture

You'd also need to clear your cache after applying the patch. There are changes to both the Javascript and menu callbacks.

attheshow’s picture

StatusFileSize
new3.33 KB

Updated patched. I cleaned up a couple of whitespace issues.

prashant.c’s picture

StatusFileSize
new4.52 KB

I tried the patch posted by #10 with 7.x-1.x-dev but it had some issues and not notifying the node author.
Made modifications

1. Modified code in diqus.js file.
2. It was not fetching node id sent by drupal_add_js() function arguments in disqus.module file.
3. 2 arguments were coming in 'identifier' array. Splitted the node id from it.

Now the patch is working with both 7.x-1.x-dev and 7.x-1.9.

Please review the patch.

slashrsm’s picture

JS callback support has been already added to current 7.x-1.x-dev. See http://drupalcode.org/project/disqus.git/blob/refs/heads/7.x-1.x:/disqus... for more info.

joel_osc’s picture

This is really fantastic functionality. Does anyone have some sample code for this? Specifically, I would like to tie my disqus commenting into flag (follow) so users are automatically set to follow nodes when they leave a comment - like drupal.org.

freakalis’s picture

Issue summary: View changes
StatusFileSize
new5.51 KB

Here is a new patch based on the previous patches but using the new builtin JS Callback support. I also stops logged in Drupal users from getting notified when they are commenting on a node that they created themself.

slashrsm’s picture

  1. +++ b/sites/all/modules/disqus/disqus.js
    +++ b/sites/all/modules/disqus/disqus.js
    @@ -82,6 +82,11 @@ Drupal.behaviors.disqus = {
    
    @@ -82,6 +82,11 @@ Drupal.behaviors.disqus = {
               cache: false
             });
           }
    +      ¶
    +      // Ajax notify node author
    +      notifyAuthor = function (comment) {
    +        $.post('/disqus/notifyauthor/' + Drupal.settings.disqus.identifier, { 'id' : comment.id, 'comment' : comment.text }, null);
    +      }
         });
    

    This shouldn't live inside Drupal.behaviours, but something like Drupal.plupload.notifyAuthor.

  2. +++ b/sites/all/modules/disqus/disqus.admin.inc
    @@ -195,6 +201,7 @@ function disqus_admin_settings() {
    +  ¶
       // Make sure the validation is called to handle the custom sso logo.
    
    @@ -84,6 +84,12 @@ function disqus_admin_settings() {
    @@ -195,6 +201,7 @@ function disqus_admin_settings() {
    
    @@ -195,6 +201,7 @@ function disqus_admin_settings() {
           ),
         ),
       );
    ...
       $form['#submit'][] = 'disqus_admin_settings_submit';
       return system_settings_form($form);
    diff --git a/sites/all/modules/disqus/disqus.js b/sites/all/modules/disqus/disqus.js
    

    Whitespace.

  3. diff --git a/sites/all/modules/disqus/.cvsignore b/sites/all/modules/disqus/.cvsignore
    deleted file mode 100644
    index 3a4edf6..0000000
    --- a/sites/all/modules/disqus/.cvsignore
    +++ /dev/null
    @@ -1 +0,0 @@
    -.project
    diff --git a/sites/all/modules/disqus/disqus.admin.inc b/sites/all/modules/disqus/disqus.admin.inc
    

    We can kill .csvignore entirely.

  4. +++ b/sites/all/modules/disqus/disqus.module
    @@ -141,6 +149,15 @@ function disqus_element_post_render($children, &$element) {
    +  // Notify author on new comments
    +  if(variable_get('disqus_notify_author', FALSE)) {
    +    $element['#disqus']['callbacks'] = array(
    +      'onNewComment' => array(
    +        'notifyAuthor',
    +      ),
    +    );
    +  }
    +  ¶
       /**
    

    Drupal./.../.notifyAuthor?

    Also whitespace in last line.

  5. +++ b/sites/all/modules/disqus/disqus.module
    @@ -1041,3 +1058,71 @@ function disqus_sso_user_data($account = NULL) {
    +  // Don't notify if logged in user is commenting it's own node/user or notify_email is missing
    +  if($notify_email && ($user->mail != $notify_email)) {
    +    ¶
    +    // Send e-mail.
    +    $params['from'] = variable_get('site_mail', '');
    +    $recipient = $author->mail;
    +    $params['subject'] = t('New comment at !title', array('!title' => $node->title));
    +    ¶
    +    $body = t('Comment: !comment', array('!comment' => check_plain($_POST['comment'])))."\n";
    +    $body .= "\n\n".t('The comment can be seen at !view_url', array('!view_url' => $view_url));
    +    $params['body'] = $body;
    +    ¶
    

    We probably want email content to be configurable.

  6. +++ b/sites/all/modules/disqus/disqus.module
    @@ -1041,3 +1058,71 @@ function disqus_sso_user_data($account = NULL) {
    +    }
    +  }
    +}
    \ No newline at end of file
     
    

    Newline and few more whitespace issues all around the patch.

  7. +++ b/sites/all/modules/disqus/disqus.module
    @@ -81,6 +81,14 @@ function disqus_menu() {
    +  $items['disqus/notifyauthor/%/%'] = array(
    +    'title' => 'Please wait',
    +    'description' => 'Once the user posts a Disqus comment, their browser is redirected here in order to have Drupal send a notification to the node author.',
    +    'access arguments' => array('view disqus comments'),
    +    'page callback' => 'disqus_notify_author_ajax',
    +    'page arguments' => array(2, 3),
    +    'type' => MENU_CALLBACK,
    +  );
       return $items;
    

    This will be world open in 90% cases. Can we implement this a bit more securely? Token check or something like this?

slashrsm’s picture

Status: Needs review » Needs work
freakalis’s picture

Status: Needs work » Needs review
StatusFileSize
new6.75 KB

New patch fixing the problems reported in comment #18.

E-mail is now configurable with Token support at settings page. Added a CSRF token.

slashrsm’s picture

Status: Needs review » Needs work
  1. +++ b/disqus/disqus.module
    @@ -1065,3 +1084,87 @@ function disqus_sso_user_data($account = NULL) {
    +  // Anonymous users get a less secure token, since it must be the same for all
    +  // anonymous users on the entire site to work with page caching.
    +  return ($GLOBALS['user']->uid) ? drupal_get_token($content_id) : md5(drupal_get_private_key() . $content_id);
    +}
    +
    

    Does this provide any security at all (for anon)?

  2. +++ b/disqus/disqus.module
    @@ -1065,3 +1084,87 @@ function disqus_sso_user_data($account = NULL) {
    +function disqus_check_token($token, $content_id) {
    +  return disqus_get_token($content_id) == $token;
    +}
    \ No newline at end of file
    

    Newline still missing.

freakalis’s picture

I tried using only drupal_get_token() but that did't work for anonymous users because they don't have a session. I found this token solution in Flag module. Do you have a better solution that doesn't require a session for every user?

slashrsm’s picture

I believe the only secure way is to create a session for anon users.

zonesny’s picture

Thanks @freakalis and @slahrsm for all of the great work on this feature. Can someone please clarify the status of the patch in #20? Does it need to be tested further by the community, or will it be committed, etc.?

girishmuraly’s picture

+++ b/disqus/disqus.module
@@ -141,6 +149,17 @@ function disqus_element_post_render($children, &$element) {
+  // Notify author on new comments
+  if(variable_get('disqus_notify_author', FALSE)) {
+    $element['#disqus']['callbacks'] = array(
+      'onNewComment' => array(
+        'Drupal.disqus.notifyAuthor',
+      ),
+    );

If any custom/contrib module has prepended a new #post_render function to 'disqus' and set an 'onNewComment' callback, then this code is wiping it out. How about using
$element['#disqus']['callbacks']['onNewComment][] = 'Drupal.disqus.notifyAuthor';
instead?

Also, it seems possible to completely implement callbacks in custom modules by implementing new #post_renders like above, so is it worth adding all this to this module?

slashrsm’s picture

@zonesny: This patch is not ready to be committed (as per #21 and #23).

#25 raised a fair question. I'd say that we can add this, but it must be done in a secure, simple and reliable way.

girishmuraly’s picture

@slashrsm perhaps it can be a submodule, like in https://drupal.org/comment/8692701#comment-8692701?

slashrsm’s picture

That also works.

vgutekunst’s picture

Hi,

i use 7.x-1.10+7-dev and the patch #20 but the email notification doesnt work? Any help with that? Isnt the patch working with 7.x-1.10+7-dev?

kind regards,

vgutekunst’s picture

It seems Token dont works correct in this case. [node:author:mail] dont work thats why the email notification doesnt work! Any help i this case'?

vgutekunst’s picture

any progress in the meanwhile'''?

karamveersingh’s picture

hi guys!

I have integrated Disqus-Version: 7.x-1.12 and core feature working fine, but I am looking -> notify to Drupal-Article author when someone posted comments.

Please suggest me above which patch I can use for full-fill my requirement?

karamveersingh’s picture

any update ?