diff --git a/commons_follow_node/commons_follow_node.module b/commons_follow_node/commons_follow_node.module index b43251e..c8648ff 100644 --- a/commons_follow_node/commons_follow_node.module +++ b/commons_follow_node/commons_follow_node.module @@ -86,3 +86,36 @@ function commons_follow_node_commons_follow_get_following_uids(&$following_uids, $following_uids[$this_result->uid] = $this_result->uid; } } + +/** + * Implements hook_node_insert(). + */ +function commons_follow_node_node_insert($node) { + // The owner of the content automatically follows the content + commons_follow_node_follow_node($node->nid, $node->type, $node->uid); +} + +/** + * Implements hook_comment_insert(). + */ +function commons_follow_node_comment_insert($comment) { + // The user who comment a node automatically follows that content + $content_type = str_replace('comment_node_', '', $comment->node_type); + commons_follow_node_follow_node($comment->nid, $content_type, $comment->uid); +} + +/** + * Let a user to follow a node. + * + * @see commons_follow_node_comment_insert + * @see commons_follow_node_node_insert + */ +function commons_follow_node_follow_node($nid, $content_type, $user_id = NULL) { + $flag = flag_get_flag('commons_follow_node'); + + // Check if this content type use the "Individual nodes" flag + if (in_array($content_type, $flag->types)) { + $account = isset($user_id) ? user_load($user_id) : NULL; + $flag->flag('flag', $nid, $account); + } +}