/**
 * Records group activity items in GES for all activity except:
 *  - group forum posts (handled in ass_group_notification_forum_posts())
 *  - created and joined group entries (irrelevant)
 *
 * You can do more fine-grained activity filtering with the
 * 'ass_block_group_activity_types' filter.
 */
function ass_group_notification_activity($content)
{
    global $bp;
    $type = $content->type;
    $component = $content->component;
    $sender_id = $content->user_id;
    // get group activity update replies to work (there is no group id passed in $content, but we can get it from $bp)
    if ($type == 'activity_comment' && bp_is_groups_component() && $component == 'activity') {
        $component = 'groups';
    }
    // at this point we only want group activity, perhaps later we can make a function and interface for personal activity...
    if ($component != 'groups') {
        return;
    }
    // if you want to conditionally block certain activity types from appearing,
    // use the filter below
    if (false === apply_filters('ass_block_group_activity_types', true, $type, $content)) {
        return;
    }
    if (!ass_registered_long_enough($sender_id)) {
        return;
    }
    $group_id = $content->item_id;
    $action = ass_clean_subject($content->action);
    if ($type == 'activity_comment') {
        // if it's an group activity comment, reset to the proper group id and append the group name to the action
        // this will need to be filtered for plugins manually adding group activity comments
        $group_id = bp_get_current_group_id();
        $action = ass_clean_subject($content->action) . ' ' . __('in the group', 'bp-ass') . ' ' . bp_get_current_group_name();
    }
    $action = apply_filters('bp_ass_activity_notification_action', $action, $content);
    // get the group object
    // if the group is already set in the $bp global use that, otherwise get the group
    $group = groups_get_current_group() ? groups_get_current_group() : groups_get_group('group_id=' . $group_id);
    /* Subject & Content */
    $blogname = '[' . get_blog_option(BP_ROOT_BLOG, 'blogname') . ']';
    $subject = apply_filters('bp_ass_activity_notification_subject', $action . ' ' . $blogname, $action, $blogname);
    $the_content = apply_filters('bp_ass_activity_notification_content', $content->content, $content, $action, $group);
    $the_content = ass_clean_content($the_content);
    /* If it's an activity item, switch the activity permalink to the group homepage rather than the user's homepage */
    $activity_permalink = isset($content->primary_link) && $content->primary_link != bp_core_get_user_domain($content->user_id) ? $content->primary_link : bp_get_group_permalink($group);
    // If message has no content (as in the case of group joins, etc), we'll use a different
    // $message template
    if (empty($the_content)) {
        $message = sprintf(__('%1$s

To view or reply, log in and go to:
%2$s

---------------------
', 'bp-ass'), $action, $activity_permalink);
    } else {
        $message = sprintf(__('%1$s

"%2$s"

To view or reply, log in and go to:
%3$s

---------------------
', 'bp-ass'), $action, $the_content, $activity_permalink);
    }
    // get subscribed users for the group
    $subscribed_users = groups_get_groupmeta($group_id, 'ass_subscribed_users');
    // this is used if a user is subscribed to the "Weekly Summary" option.
    // the weekly summary shouldn't record everything, so we have a filter:
    //
    // 'ass_this_activity_is_important'
    //
    // this hook can be used by plugin authors to record important activity items
    // into the weekly summary
    // @see ass_default_weekly_summary_activity_types()
    $this_activity_is_important = apply_filters('ass_this_activity_is_important', false, $type);
    // cycle through subscribed users
    foreach ((array) $subscribed_users as $user_id => $group_status) {
        //echo '<p>uid: ' . $user_id .' | gstat: ' . $group_status ;
        $self_notify = '';
        // Does the author want updates of their own forum posts?
        if ($type == 'bbp_topic_create' || $type == 'bbp_reply_create') {
            if ($user_id == $sender_id) {
                $self_notify = ass_self_post_notification($user_id);
                // Author does not want notifications of their own posts
                if (!$self_notify) {
                    continue;
                }
            }
            // If this is an activity comment, and the $user_id is the user who is being replied
            // to, check to make sure that the user is not subscribed to BP's native activity
            // reply notifications
        } elseif ('activity_comment' == $type) {
            // First, look at the immediate parent
            $immediate_parent = new BP_Activity_Activity($content->secondary_item_id);
            // Don't send the bp-ass notification if the user is subscribed through BP
            if ($user_id == $immediate_parent->user_id && 'no' != bp_get_user_meta($user_id, 'notification_activity_new_reply', true)) {
                continue;
            }
            // We only need to check the root parent if it's different from the
            // immediate parent
            if ($content->secondary_item_id != $content->item_id) {
                $root_parent = new BP_Activity_Activity($content->item_id);
                // Don't send the bp-ass notification if the user is subscribed through BP
                if ($user_id == $root_parent->user_id && 'no' != bp_get_user_meta($user_id, 'notification_activity_new_reply', true)) {
                    continue;
                }
            }
        }
        $send_it = false;
        // Self-notification email for bbPress posts
        if ($self_notify === true) {
            $send_it = true;
            $group_status = 'self_notify';
            // notification settings link
            $settings_link = trailingslashit(bp_core_get_user_domain($user_id) . bp_get_settings_slug()) . 'notifications/';
            // set notice
            $notice = __('You are currently receiving notifications for your own posts.', 'bp-ass');
            $notice .= "\n\n" . sprintf(__('To disable these notifications please log in and go to: %s', 'bp-ass'), $settings_link);
            $notice .= "\n" . __('Once you are logged in, uncheck "Receive notifications of your own posts?".', 'bp-ass');
            // User is subscribed to "All Mail"
            // OR user is subscribed to "New Topics" (bbPress 2)
        } elseif ($group_status == 'supersub' || $group_status == 'sub' && $type == 'bbp_topic_create') {
            // if someone is signed up for all email and they post a group update, they should not receive an email
            if ('activity_update' == $type && $sender_id === $user_id) {
                continue;
            }
            $send_it = true;
            $settings_link = ass_get_login_redirect_url(trailingslashit(bp_get_group_permalink($group) . 'notifications'), $group_status);
            $notice = __('Your email setting for this group is: ', 'bp-ass') . ass_subscribe_translate($group_status);
            $notice .= "\n" . sprintf(__('To change your email setting for this group, please log in and go to: %s', 'bp-ass'), $settings_link);
            $notice .= "\n\n" . ass_group_unsubscribe_links($user_id);
        }
        // if we're good to send, send the email!
        if ($send_it) {
            // One last chance to filter the message content
            $user_message = apply_filters('bp_ass_activity_notification_message', $message . $notice, array('message' => $message, 'notice' => $notice, 'user_id' => $user_id, 'subscription_type' => $group_status, 'content' => $the_content, 'settings_link' => !empty($settings_link) ? $settings_link : ''));
            // Get the details for the user
            $user = bp_core_get_core_userdata($user_id);
            // Send the email
            if ($user->user_email) {
                wp_mail($user->user_email, $subject, $user_message);
            }
        }
        // otherwise, user is subscribed to "Daily Digest" so record item in digest!
        // OR user is subscribed to "Weekly Summary" and activity item is important
        // enough to be recorded
        if ($group_status == 'dig' || $group_status == 'sum' && $this_activity_is_important) {
            ass_digest_record_activity($content->id, $user_id, $group_id, $group_status);
        }
    }
    //echo '<p>Subject: ' . $subject;
    //echo '<pre>'; print_r( $message ); echo '</pre>';
}
/**
 * Records group activity items in GES for all activity except:
 *  - group forum posts (handled in ass_group_notification_forum_posts())
 *  - created and joined group entries (irrelevant)
 *
 * You can do more fine-grained activity filtering with the
 * 'ass_block_group_activity_types' filter.
 */
function ass_group_notification_activity($content)
{
    global $bp;
    $type = $content->type;
    $component = $content->component;
    $sender_id = $content->user_id;
    // get group activity update replies to work (there is no group id passed in $content, but we can get it from $bp)
    if ($type == 'activity_comment' && bp_is_groups_component() && $component == 'activity') {
        $component = 'groups';
    }
    // at this point we only want group activity, perhaps later we can make a function and interface for personal activity...
    if ($component != 'groups') {
        return;
    }
    // if you want to conditionally block certain activity types from appearing,
    // use the filter below
    if (false === apply_filters('ass_block_group_activity_types', true, $type, $content)) {
        return;
    }
    if (!ass_registered_long_enough($sender_id)) {
        return;
    }
    $group_id = $content->item_id;
    $action = ass_clean_subject($content->action);
    if ($type == 'activity_comment') {
        // if it's an group activity comment, reset to the proper group id and append the group name to the action
        // this will need to be filtered for plugins manually adding group activity comments
        $group_id = bp_get_current_group_id();
        $action = ass_clean_subject($content->action) . ' ' . __('in the group', 'bp-ass') . ' ' . bp_get_current_group_name();
    }
    $action = apply_filters('bp_ass_activity_notification_action', $action, $content);
    // get the group object
    // if the group is already set in the $bp global use that, otherwise get the group
    $group = groups_get_current_group() ? groups_get_current_group() : groups_get_group('group_id=' . $group_id);
    /* Subject & Content */
    $blogname = '[' . get_blog_option(BP_ROOT_BLOG, 'blogname') . ']';
    $subject = apply_filters('bp_ass_activity_notification_subject', $action . ' ' . $blogname, $action, $blogname);
    $the_content = apply_filters('bp_ass_activity_notification_content', $content->content, $content);
    $the_content = ass_clean_content($the_content);
    /* If it's an activity item, switch the activity permalink to the group homepage rather than the user's homepage */
    $activity_permalink = isset($content->primary_link) && $content->primary_link != bp_core_get_user_domain($content->user_id) ? $content->primary_link : bp_get_group_permalink($group);
    // If message has no content (as in the case of group joins, etc), we'll use a different
    // $message template
    if (empty($the_content)) {
        $message = sprintf(__('%s

To view or reply, log in and go to:
%s

---------------------
', 'bp-ass'), $action, $activity_permalink);
    } else {
        $message = sprintf(__('%s

"%s"

To view or reply, log in and go to:
%s

---------------------
', 'bp-ass'), $action, $the_content, $activity_permalink);
    }
    // get subscribed users for the group
    $subscribed_users = groups_get_groupmeta($group_id, 'ass_subscribed_users');
    // this is used if a user is subscribed to the "Weekly Summary" option.
    // the weekly summary shouldn't record everything, so we have a filter:
    //
    // 'ass_this_activity_is_important'
    //
    // this hook can be used by plugin authors to record important activity items
    // into the weekly summary
    // @see ass_default_weekly_summary_activity_types()
    $this_activity_is_important = apply_filters('ass_this_activity_is_important', false, $type);
    // cycle through subscribed users
    foreach ((array) $subscribed_users as $user_id => $group_status) {
        //echo '<p>uid: ' . $user_id .' | gstat: ' . $group_status ;
        // Does the author want updates of his own posts?
        if ($user_id == $sender_id) {
            if (!ass_self_post_notification()) {
                continue;
            }
        }
        // If this is an activity comment, and the $user_id is the user who is being replied
        // to, check to make sure that the user is not subscribed to BP's native activity
        // reply notifications
        if ('activity_comment' == $type) {
            // First, look at the immediate parent
            $immediate_parent = new BP_Activity_Activity($content->secondary_item_id);
            // Don't send the bp-ass notification if the user is subscribed through BP
            if ($user_id == $immediate_parent->user_id && 'no' != get_user_meta($user_id, 'notification_activity_new_reply', true)) {
                continue;
            }
            // We only need to check the root parent if it's different from the
            // immediate parent
            if ($content->secondary_item_id != $content->item_id) {
                $root_parent = new BP_Activity_Activity($content->item_id);
                // Don't send the bp-ass notification if the user is subscribed through BP
                if ($user_id == $root_parent->user_id && 'no' != get_user_meta($user_id, 'notification_activity_new_reply', true)) {
                    continue;
                }
            }
        }
        // User is subscribed to "All Mail"
        // OR user is subscribed to "New Topics" (bbPress 2) so send email about this item now!
        if ($group_status == 'supersub' || $group_status == 'sub' && $type == 'bbp_topic_create') {
            /* Content footer */
            $footer = ass_group_unsubscribe_links($user_id);
            $notice = "\n" . __('Your email setting for this group is: ', 'bp-ass') . ass_subscribe_translate($group_status);
            $user = bp_core_get_core_userdata($user_id);
            if ($user->user_email) {
                wp_mail($user->user_email, $subject, $message . $footer . $notice);
            }
            // Send the email
            //echo '<br>EMAIL: ' . $user->user_email . "<br>";
            // User is subscribed to "Daily Digest" so record item in digest!
            // OR user is subscribed to "Weekly Summary" and activity item is important
            // enough to be recorded
        } elseif ($group_status == 'dig' || $group_status == 'sum' && $this_activity_is_important) {
            ass_digest_record_activity($content->id, $user_id, $group_id, $group_status);
            //echo '<br>DIGEST: ' . $user_id . "<br>";
        }
    }
    //echo '<p>Subject: ' . $subject;
    //echo '<pre>'; print_r( $message ); echo '</pre>';
}