2

I have created few activity logs using the Message module. All works ok, but when I try to create a message type for example "username started following you on github". When setting up the rules for this message type, I am not able to understand how to achieve this.

What should happen?

Message must be logged for the "User who ‘Is flagged’" user and at the same time the username token must be replaced by the "User who ‘Flagged it’" user. The problem is that this is not happening.

What is happening?

When the message is logged for "User who is flagged", the username is also replaced by "User who is flagged" user and vice versa. Obviously this is not what is required.

Export of "User Following" message type

{
  "name" : "user_following",
  "description" : "User following",
  "argument_keys" : [],
  "argument" : [],
  "category" : "message_type",
  "data" : {
    "token options" : { "clear" : 0 },
    "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
  },
  "language" : "",
  "arguments" : null,
  "message_text" : { "und" : [
      {
        "value" : "[message:user:name] started following you on [site:name]",
        "format" : "plain_text",
        "safe_value" : "[message:user:name] started following you on [site:name]"
      }
    ]
  },
  "rdf_mapping" : []
}

Rules for above message

{ "rules_started_following" : {
    "LABEL" : "Started_following",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "user" ],
    "REQUIRES" : [ "rules", "flag" ],
    "ON" : { "flag_flagged_follow" : [] },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_type" : "user_following",
            "param_user" : [ "flagged-user" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      }
    ]
  }
}

Note: It is about rules setup for message type when you select data-selectors in rules setup.

Another example is for "xyz mentioned you on github". Same problem goes here. Here username replaced by xyz and it was logged for xyz as well. It should not happen.

If abc user was mentioned by the xyz user, then the above message or notification must be received by the abc user and not the xyz user. Like normally you receive a notification on other websites when someone follows you or mentions you.

Export of "Mentions" message type

{
  "name" : "log_mentions",
  "description" : "Log mentions",
  "argument_keys" : [],
  "argument" : [],
  "category" : "message_type",
  "data" : {
    "token options" : { "clear" : 0 },
    "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
  },
  "language" : "",
  "arguments" : null,
  "message_text" : { "und" : [
      {
        "value" : "[message:user:name] mentioned you",
        "format" : "plain_text",
        "safe_value" : "[message:user:name] mentioned you"
      },
      {
        "value" : "\u003Cp\u003E[message:user:name]\u003C\/p\u003E\r\n",
        "format" : "full_html",
        "safe_value" : "\u003Cp\u003E[message:user:name]\u003C\/p\u003E\n"
      }
    ]
  },
  "rdf_mapping" : []
}

Export of Rules for above message

{ "rules_test_rule" : {
    "LABEL" : "test rule",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "statuses_tags" ],
    "ON" : { "statuses_tags_user_was_tagged" : [] },
    "IF" : [
      { "statuses_tags_has_specific_tag_condition" : { "status" : [ "status" ], "type" : "user", "tag" : "[account:name]" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_type" : "log_mentions",
            "param_user" : [ "account" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-message-mentioning:name" ],
          "value" : "[status:sender]"
        }
      }
    ]
  }
}

Problem: When using it, if I mentioned Pierre in a post, the output is, "Pierre mentioned you" and the message is also logged for Pierre.

Statuses Sub-Modules: I am using all sub-module except following:

Statuses Domain Access, Statuses Mollom, Statuses Notifications and Statuses Userpoints. It is important to tell here that Flag 3 does not work with statuses module, Flag 2 does. Good thing is you do not want flag module to workout this problem. I am using flag 2 as I want the "like" button and follow functionality for status updates.

Note: I created the field of machine name "field_message_mentioning" Label: "mentioning" Type: "Entity Reference (User)" Widget: "Autocomplete". I have tried lot of things and spent hours, but I do not know what I am doing. I just read online and try to apply.

Hope you understood. Please read the whole explanation in the context of rules setup for message type. If there is any confusion, please ask me before answering.

1

Part 1: Rules tokens

Here is a sample of a rule (in Rules export format), for a flag which is used to flag an entity of type "user":

{ "rules_set_the_ever_flagged_with_flagx_flag" : {
    "LABEL" : "Set the ever flagged with FlagX flag",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "flag" ],
    "ON" : { "flag_flagged_flagx" : [] },
    "DO" : [
      { "flag_flaguser" : {
          "flag" : "ever_flagged_with_flagx ",
          "user" : [ "flagged-user" ],
          "flagging_user" : [ "flagging-user" ],
          "permission_check" : "1"
        }
      }
    ]
  }
}

The Rules Action of it, uses these 2 tokens:

  • flagged-user, which you should use for User who ‘Is flagged’.
  • flagging-user, which you should use for User who ‘Flagged it’.

Those 2 tokens should also be available for you to select in your custom rule.

You may also want to look at my answer to "Flag Module: "Never Flagged" Status", which provides more details about my example rule here.

However, the above technique is not going to work in your case, because your question has to do with tokens that you need to be available at the level of the Message module. And that is a completely different story ... Read on for more details on that (and a working solution) ...

Part 2: Message tokens

Your question is about messages created via Rules, but for 2 variations of message types:

  • Messages triggered by the Flag module (after a user got flagged).
  • Messages triggered by the Statuses (Social Microblog) module (and it's Tag related submodule).

But in both case (types of messages), you want to create a message that requires some token that is not provided by the Message module. And even though it seems that status:recipient token might be a possible fit, it is actually NOT what you're looking for (that token is about who the message is to be delivered to, not about who triggered that message to be delivered).

However, since you are using Rules to create your messages to be delivered, you can make the missing tokens available by using the Replacement patterns for each message type. Refer to the answer to What's the purpose of "Replacement tokens" of a Message type? for way more details on that (these Replacement tokens are really a hidden gem about how Rules integrates with Message!).

Part 3: Solution for "User following"

Step 1 - Define replacement tokens

Define a replacement token for this message type that is like so: @following_user (adapt to whatever fits, if you also adapt it in the steps below).

Step 2 - Custom message using these replacement tokens

Within the Message text, use something like so (note the tokens I'm using):

@following_user started following [message:user:name]

Step 3 - Custom rule

Here is how your adapted rule should look like (I mostly just added that extra Rules Action):

{ "rules_somebody_started_following_you" : {
    "LABEL" : "Somebody started following you",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "user" ],
    "REQUIRES" : [ "rules", "flag" ],
    "ON" : { "flag_flagged_follow" : [] },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_type" : "user_following",
            "param_user" : [ "flagged-user" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:arguments:following-user" ],
          "value" : "[site:current-user]"
        }
      }
    ]
  }
}

Part 4: Solution for "Log mentions"

Step 1 - Define replacement tokens

Define 2 replacement tokens for this message type that is like so: @mentioning_user, %mentioning_statuses_text (adapt to whatever fits, if you also adapt it in the steps below).

Note the 2 (instead of just 1) tokens I used here, just to also show how to do that ...

Step 2 - Custom message using these replacement tokens

Within the Message text, use something like so (note the tokens I'm using):

@mentioning_user mentioned [message:user:name] in the statuses message "%mentioning_statuses_text" ...

Note the @ versus % I used in this message text, doesn't matter which one you use, just to show the difference ...

Step 3 - Custom rule

Here is how your adapted rule should look like (I mostly just added those 2 extra Rules Action, 1 for each replacement token):

{ "rules_somebody_mentioned_you" : {
    "LABEL" : "Somebody mentioned you",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "statuses_tags" ],
    "ON" : { "statuses_tags_user_was_tagged" : [] },
    "IF" : [
      { "statuses_tags_has_specific_tag_condition" : { "status" : [ "status" ], "type" : "user", "tag" : "[account:name]" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_type" : "log_mentions",
            "param_user" : [ "account" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:arguments:mentioning-user" ],
          "value" : "[site:current-user]"
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:arguments:mentioning-statuses-text" ],
          "value" : "[status:message-formatted]"
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-message-mentioning:name" ],
          "value" : "[status:sender]"
        }
      }
    ]
  }
}

Note that I did not change anything to the (now) last Rules Action, though IMO you could savely remove that Rules Action (it's not needed to make it work, but maybe you need it for something else). Similarly: I'm not sure what the purpose of that extra "field" is for your message type (possibly obsolete now?).

Part 5: Fruit of your labor

If you make it this far, and implement the above rules and message types in your own site, you should be able to get results similar to my setup ...

Demo of "User following"

  • Scenario:

    Hillary.Trump flags Donald.Clinton

  • Result:

    Message delivered to Donald.Clinton:

    11/09/2016 - 12:05 Hillary.Trump started following Donald.Clinton

Demo of "Log mention"

  • Scenario:

    drupal.admin posts a statuses-msg like "Just saw the answer from [@Pierre.Vriens], and it works like a charm!!!"

  • Result:

    Message delivered to Pierre.Vriens:

    11/09/2016 - 12:22 drupal.admin mentioned Pierre.Vriens in the statuses message "Just saw the answer from [@Pierre.Vriens], and it works like a charm!!!" ...

  • Merci for all the effort. A detailed and good answer. I will try it and let you know as quickly as possible. In the last, you have mentioned that the extra field is obsolete, so should I remove following from the rules export? { "data_set" : { "data" : [ "entity-created:field-message-mentioning:name" ], "value" : "[status:sender]" } – Umair Nov 9 '16 at 16:58
  • one more question. I have to paste these replacement patterns (@mentioning_user, @mentioning_statuses_text) in the box given for optional replacement patterns in the message setup page? OR I can just directly use them in the message text in message setup? – Umair Nov 9 '16 at 17:03
  • Merci for the feedback @Umair . It doesn't hurt to leave that obsolete stuff in for now, so I'd rather first try without any deleting/cleanup (it works fine for me). After you succeed, you can always make a backup of what works (in export format), then do your cleanup to see it still works (and if not restore your backup via import). BTW: your question here intrigued me, also because I'm a huge fan of the (poorly documented but great) Message module. That's why I spend a lot of time documenting what I learned, via my new question/answer (also let me know if you "like" that, if so maybe +1?). – Pierre.Vriens Nov 9 '16 at 17:05
  • @Umair : you must " paste these replacement patterns (@mentioning_user, @mentioning_statuses_text)" ... that is like 50% of the clue to it all (take the time to digest my new question/answer, I bet you'll really like what you'll learn from it). So if you don't "paste" that, it can never work ... PS: I do not like "chat", so maybe remove some obsolete comments around here again if you can, ok? – Pierre.Vriens Nov 9 '16 at 17:09
  • Just tested "following" message type. Great it is working. I was stuck with @following_user replacement pattern because my message type machine name was "user_following". I changed it to "following" – Umair Nov 10 '16 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.