Notify Users When They get Tagged in Activity Stream Window

Idea created by Shijin Krishna on Nov 1, 2015
    Active
    Score230

    As we know Sugar 7.x has the option to tag Users while creating a new post in Activity Stream window out of the box.But unfortunately there is no in built functionality to notify the user once he/she get tagged.

     

    Don’t worry! In this article we are going to see how can we send an email alert or create a notification in Sugar notification list for a user when he or she get tagged in Activity Stream by another Sugar user.

    tagged.png

     

    In the above image user Venkat has been tagged by Administrator under the Activity Stream window of a lead.See below Venkat’s Notification Indicator.

    tag_notification.png

    Once you click on it, system will take you to the notification detail view.

     

    taged_notification_detail.png

     

     

    In description you can see the comment user has wrote during the time of tagging.By clicking the Lead’s name user can directly navigate to the activity stream window under the lead’s record and update his/her comments.

     

    Let’s start implementing this.

     

    Step 1

      Copy file ~clients/base/views/activitystream-omnibar.js to custom/clients/base/views/activitystream-omnibar.js and modify the addPost method as below

     

    addPost: function() {

            var self = this,

                parentId = this.context.parent.get("model").id,

                parentType = this.context.parent.get("model").module,

                attachments = this.$('.activitystream-pending-attachment'),

                $submitButton = this.$('button.addPost'),

                bean;

            if (parentType == 'Home' || parentType == 'Activities') {

                parentType = null;

                parentId = null;

            }

            var payload = {

                activity_type: "post",

                parent_id: parentId || null,

                parent_type: parentType,

                data: {}

            };

            if (!$submitButton.hasClass('disabled')) {

                payload.data = this.getPost();

                if (payload.data.value && (payload.data.value.length > 0)) {

                    $submitButton.addClass('disabled');

                    bean = app.data.createBean('Activities');

                    bean.save(payload, {

                        success: function(model) {

    //Custom code added to make an api call to notify the tagged user.

                var comment=$.trim(model.get('data')['value'].split(']').pop());

                var users=model.get('data')['tags'];

                var module_name=(!_.isUndefined(model.get('data')['object'])) ? model.get('data')['object']['module'] : '';

                var record_id=(!_.isUndefined(model.get('data')['object'])) ? model.get('data')['object']['id'] : '';

                data={users:users,comment:comment,module_name:module_name,record_id:record_id};

                app.api.call('create',app.api.buildURL('Notifications/createNotification'),data,{

                 

                })

                            self.$('div.sayit').empty().trigger('change').focus();

                            model.set('picture', app.user.get('picture'));

                            self.collection.add(model);

                            self.context.trigger('activitystream:post:prepend', model);

                        },

                        complete: function() {

                            $submitButton.removeClass('disabled');

                        },

                        showAlerts: true

                    });

                }

                this.trigger("attachments:process");

            }

        },

     

    Step 2

     

    Now create a new file in the following path createTagNotificationApi.php

    custom/modules/Notifications/clients/base/api/createTagNotificationApi.php

    and add the below code

    <?php

      if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

      class createTagNotificationApi extends SugarApi{

        public function registerApiRest(){

          return array(

            'createNotification' => array(

              'reqType' => 'POST',

              'path' => array('Notifications','createNotification'),

              'pathVars' => array(),

              'method' => 'createNotification',

              'shortHelp' => 'Custom Api to create a notification for a user whenever he is getting taged in activity stream',

              'longHelp' => '',

            ),

          ); 

        }

        public function createNotification($api,$args){

          foreach($args['users'] as $user){

            global $app_list_strings;

            global $current_user;

            $notification= BeanFactory::newBean("Notifications");

            $notification->assigned_user_id=$user['id'];

            $notification->severity="alert";

            $notification->is_read=0;

            $notification->deleted=0;

            $notification->created_by=$current_user->id;

            $notification->modified_user_id=$current_user->id;

            $notification->description=$args['comment'];

            $notification->parent_type=$args['module_name'];

            $notification->parent_id=$args['record_id'];

            $notification->name=trim($current_user->first_name." ".$current_user->last_name)." has mentioned you in his/her new post";

            $notification->save();

          }

          return "success";

        }

      }

    ?>

    Note : To send an email alert you can customize the createNotification() method with the custom code to send an email.Primary email id of the user can be fetched as below.

    $userBean = BeanFactory::getBean(Users,$user['id']);

    $primary_email = $userBean->email1;

     

    Step 3

     

    Navigate to Admin > Repair > Quick Repair and Rebuild

     

     

    Thanks!