<commit_before><?php
require_once __DIR__."/../config.php";

include_once __DIR__."/".CONFIG_PATH."config.php";
require_once __DIR__."/".SHRUB_PATH."api.php";
require_once __DIR__."/".SHRUB_PATH."note/note.php";
require_once __DIR__."/".SHRUB_PATH."notification/notification.php";
require_once __DIR__."/".SHRUB_PATH."node/node.php";

json_Begin();

const CACHE_KEY_PREFIX = "SH!NOTE!";
const CACHE_TTL = 60;


// Do Actions
$action = json_ArgShift();
switch ( $action ) {
	case 'stats': //note/stats
		json_ValidateHTTPMethod('GET');
		//$event_id = intval(json_ArgGet(0));

		$RESPONSE['ham'] = "true";

		break; // case 'stats': //note/stats
	case 'get': //note/get
		json_ValidateHTTPMethod('GET');

		$ids = explode('+', json_ArgGet(0));

		if ( count($ids) !== 1 ) {
			json_EmitFatalError_BadRequest(null, $RESPONSE);
		}

		$RESPONSE['note'] = noteFlags_Filter(noteComplete_GetByNode($ids[0]), userAuth_GetID());

		break; //case 'get': //note/get		
	case 'getnote': //note/getnote
		// Temporary "Get note by note ID" call. To be renamed when the renaming time comes.
		json_ValidateHTTPMethod('GET');

		$ids = explode('+', json_ArgGet(0));

		// Limit number of notes
		if ( count($ids) > 250 ) {
			json_EmitFatalError_BadRequest("Too many notes", $RESPONSE);
		}
		
		$RESPONSE['notes'] = noteFlags_Filter(noteComplete_Get($ids), userAuth_GetID());

		break; //case 'getnote': //note/getnote
	case 'add': //note/add
		json_ValidateHTTPMethod('POST');

		if ( $user_id = userAuth_GetID() ) {
			$node_id = intval(json_ArgShift());
			if ( empty($node_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}

			$author = $user_id;
			$flags = 0;

			if ( !isset($_POST['parent']) )
				json_EmitFatalError_BadRequest("No parent", $RESPONSE);
			$parent = intval($_POST['parent']);

			if ( !isset($_POST['body']) )
				json_EmitFatalError_BadRequest("No body", $RESPONSE);
			$body = coreSanitize_Body(substr($_POST['body'], 0, 4096));

			$version_tag = "";
			if ( isset($_POST['tag']) )
				$version_tag = coreSlugify_Name($_POST['tag']);

			$parent = intval($_POST['parent']);

			// Load Node
			$node = node_GetById($node_id);


			if ( isset($_POST['anonymous']) && $_POST['anonymous'] ) {
				// Only allow anonymous comments to be posted if the node has "allow-anonymous-comments" meta set.
				
				$meta = nodeMeta_GetByKeyNode('allow-anonymous-comments',$node_id);
				if ( count($meta) == 0 || !$meta[0]['value'] ) {
					json_EmitFatalError_BadRequest("Cannot post anonymous comment: Node doesn't allow anonymous comments.", $RESPONSE);
				}
				
				$flags |= SH_NOTE_FLAG_ANONYMOUS;
			}

			// Check if you have permission to add comment to node
			if ( note_IsNotePublicByNode($node) ) {
				// hack for now
				if ( $parent !== 0 )
					json_EmitFatalError_Permission("Temporary: No children", $RESPONSE);

				$RESPONSE['note'] = note_AddByNode($node['id'], $node['parent'], $author, $parent, $body, $version_tag, $flags);
				
				// Add notifications for users watching this thread
				if ( $RESPONSE['note'] ) {
					$mentions = notification_GetMentionedUsers($body);
					notification_AddForNote($node['id'], $RESPONSE['note'], $author, $mentions);
				}
			}
			else {
				json_EmitFatalError_Permission(null, $RESPONSE);
			}
		}
		else {
			json_EmitFatalError_Permission(null, $RESPONSE);
		}

		break; //case 'add': //note/add

	case 'update': //note/update
		json_ValidateHTTPMethod('POST');

		if ( $user_id = userAuth_GetID() ) {
			$note_id = intval(json_ArgShift());
			if ( empty($note_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}

			$author = $user_id;

			if ( !isset($_POST['node']) )
				json_EmitFatalError_BadRequest("No node", $RESPONSE);
			$node_id = intval($_POST['node']);
			if ( empty($node_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}<commit_msg>Post comment should immediately invalidate node cache.<commit_after><?php
require_once __DIR__."/../config.php";

include_once __DIR__."/".CONFIG_PATH."config.php";
require_once __DIR__."/".SHRUB_PATH."api.php";
require_once __DIR__."/".SHRUB_PATH."note/note.php";
require_once __DIR__."/".SHRUB_PATH."notification/notification.php";
require_once __DIR__."/".SHRUB_PATH."node/node.php";

json_Begin();

const CACHE_KEY_PREFIX = "SH!NOTE!";
const CACHE_TTL = 60;


// Do Actions
$action = json_ArgShift();
switch ( $action ) {
	case 'stats': //note/stats
		json_ValidateHTTPMethod('GET');
		//$event_id = intval(json_ArgGet(0));

		$RESPONSE['ham'] = "true";

		break; // case 'stats': //note/stats
	case 'get': //note/get
		json_ValidateHTTPMethod('GET');

		$ids = explode('+', json_ArgGet(0));

		if ( count($ids) !== 1 ) {
			json_EmitFatalError_BadRequest(null, $RESPONSE);
		}

		$RESPONSE['note'] = noteFlags_Filter(noteComplete_GetByNode($ids[0]), userAuth_GetID());

		break; //case 'get': //note/get		
	case 'getnote': //note/getnote
		// Temporary "Get note by note ID" call. To be renamed when the renaming time comes.
		json_ValidateHTTPMethod('GET');

		$ids = explode('+', json_ArgGet(0));

		// Limit number of notes
		if ( count($ids) > 250 ) {
			json_EmitFatalError_BadRequest("Too many notes", $RESPONSE);
		}
		
		$RESPONSE['notes'] = noteFlags_Filter(noteComplete_Get($ids), userAuth_GetID());

		break; //case 'getnote': //note/getnote
	case 'add': //note/add
		json_ValidateHTTPMethod('POST');

		if ( $user_id = userAuth_GetID() ) {
			$node_id = intval(json_ArgShift());
			if ( empty($node_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}

			$author = $user_id;
			$flags = 0;

			if ( !isset($_POST['parent']) )
				json_EmitFatalError_BadRequest("No parent", $RESPONSE);
			$parent = intval($_POST['parent']);

			if ( !isset($_POST['body']) )
				json_EmitFatalError_BadRequest("No body", $RESPONSE);
			$body = coreSanitize_Body(substr($_POST['body'], 0, 4096));

			$version_tag = "";
			if ( isset($_POST['tag']) )
				$version_tag = coreSlugify_Name($_POST['tag']);

			$parent = intval($_POST['parent']);

			// Load Node
			$node = node_GetById($node_id);


			if ( isset($_POST['anonymous']) && $_POST['anonymous'] ) {
				// Only allow anonymous comments to be posted if the node has "allow-anonymous-comments" meta set.
				
				$meta = nodeMeta_GetByKeyNode('allow-anonymous-comments',$node_id);
				if ( count($meta) == 0 || !$meta[0]['value'] ) {
					json_EmitFatalError_BadRequest("Cannot post anonymous comment: Node doesn't allow anonymous comments.", $RESPONSE);
				}
				
				$flags |= SH_NOTE_FLAG_ANONYMOUS;
			}

			// Check if you have permission to add comment to node
			if ( note_IsNotePublicByNode($node) ) {
				// hack for now
				if ( $parent !== 0 )
					json_EmitFatalError_Permission("Temporary: No children", $RESPONSE);

				$RESPONSE['note'] = note_AddByNode($node['id'], $node['parent'], $author, $parent, $body, $version_tag, $flags);
				
				// Add notifications for users watching this thread
				if ( $RESPONSE['note'] ) {
					$mentions = notification_GetMentionedUsers($body);
					notification_AddForNote($node['id'], $RESPONSE['note'], $author, $mentions);
				}

				// Invalidate the cache for the containing node so we recompute the note count immediately on the site.
				nodeCache_InvalidateById($node['id']);
			}
			else {
				json_EmitFatalError_Permission(null, $RESPONSE);
			}
		}
		else {
			json_EmitFatalError_Permission(null, $RESPONSE);
		}

		break; //case 'add': //note/add

	case 'update': //note/update
		json_ValidateHTTPMethod('POST');

		if ( $user_id = userAuth_GetID() ) {
			$note_id = intval(json_ArgShift());
			if ( empty($note_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}

			$author = $user_id;

			if ( !isset($_POST['node']) )
				json_EmitFatalError_BadRequest("No node", $RESPONSE);
			$node_id = intval($_POST['node']);
			if ( empty($node_id) ) {
				json_EmitFatalError_BadRequest(null, $RESPONSE);
			}
{'fraction_non_alphanumeric': 0.16976127320954906, 'fraction_numerical': 0.004799797903246179, 'mean_word_length': 4.124919093851132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5487449', 'n_tokens_mistral': 3225, 'n_tokens_neox': 2786, 'n_words': 538}