<commit_before><?php

use cron\FlipFlop;
use spitfire\exceptions\PublicException;

/**
 * Pings are the base data type of Ping, they allow a user to broadcast a message
 * (status update, ping) to a following. Allowing them to collect feedback and 
 * comments on it.
 * 
 * @author <NAME> <<EMAIL>>
 */
class PingController extends AppController
{
	
	/**
	 * There's not really much value in the /ping method. This could potentially
	 * be used to redirect the user or provide information about the server.
	 */
	public function index() {
		
	}
	
	/**
	 * Pushes a ping to the server's public feed. This allows the ping to be displayed,
	 * shared and replied to.
	 * 
	 * @validate >> POST#src (positive number)
	 * @validate >> POST#target (positive number)
	 * @validate >> POST#msg (required string length[0, 250])
	 * @validate >> POST#url (string url)
	 * @validate >> POST#irt (positive number)
	 * 
	 * @request-method POST
	 */
	public function push() {
		
		/*
		 * This happens when the user has an active session with the application,
		 * and not represented by an application. We assume that there's no intermediary.
		 * 
		 * Applications should log into the application using their signature, and
		 * while they will receive very open privileges, Ping likes to be 
		 * transparent about where pings originated from.
		 */
		if ($this->user) {
			$srcid = $this->user->id;
			$authapp = $this->authapp? $this->authapp->getSrc()->getId() : null;
			//TODO: Keep logs of the application that originated a ping.
		}
		else {
			throw new PublicException('Authentication required', 403);
		}
		
		
		#Read POST data
		$tgtid    = $_POST['target']?? null;
		$content  = str_replace("\r", '', $_POST['content']);
		$url      = $_POST['url']?? null;
		$media    = $_POST['media']?? null;
		$poll     = $_POST['poll']?? null;
		$irt      = $_POST['irt']?? null;
		$explicit = !!($_POST['explicit']?? false);
		
		#There needs to be a src user. That means that somebody is originating the
		#notification. There has to be one, and no more than one.
		$src = AuthorModel::get(db()->table('user')->get('authId', $srcid)->first()? : UserModel::makeFromSSO($this->sso->getUser($srcid)));
		
		/*
		 * Check whether the source defined a target for this ping. If the target
		 * can be traced to an author, we will record that.
		 */
		$target = $tgtid === null? null : AuthorModel::find($tgtid);
		
		#It could happen that the target is established as an email and therefore
		#receives notifications directly as emails
		if (!($target instanceof AuthorModel || $target === null)) {
			throw new PublicException('Invalid target', 400);
		}

		#Make it a record
		$notification = db()->table('ping')->newRecord();
		$notification->src = $src;
		$notification->target = $target;
		$notification->content = Mention::mentionsToId($content);
		$notification->url     = $url;
		$notification->explicit= $explicit;
		$notification->irt     = $irt? db()->table('ping')->get('_id', $irt)->first(true) : null;
		$notification->processed = false;
		$notification->locked = false;
		
		
		/**
		 * @todo This method should be deprecated in favor of batch processing the
		 * files
		 */
		if (is_string($media)) {<commit_msg>Applications now get identified and recorded by ping.<commit_after><?php

use cron\FlipFlop;
use spitfire\exceptions\PublicException;

/**
 * Pings are the base data type of Ping, they allow a user to broadcast a message
 * (status update, ping) to a following. Allowing them to collect feedback and 
 * comments on it.
 * 
 * @author <NAME> <<EMAIL>>
 */
class PingController extends AppController
{
	
	/**
	 * There's not really much value in the /ping method. This could potentially
	 * be used to redirect the user or provide information about the server.
	 */
	public function index() {
		
	}
	
	/**
	 * Pushes a ping to the server's public feed. This allows the ping to be displayed,
	 * shared and replied to.
	 * 
	 * @validate >> POST#src (positive number)
	 * @validate >> POST#target (positive number)
	 * @validate >> POST#msg (required string length[0, 250])
	 * @validate >> POST#url (string url)
	 * @validate >> POST#irt (positive number)
	 * 
	 * @request-method POST
	 */
	public function push() {
		
		/*
		 * This happens when the user has an active session with the application,
		 * and not represented by an application. We assume that there's no intermediary.
		 * 
		 * Applications should log into the application using their signature, and
		 * while they will receive very open privileges, Ping likes to be 
		 * transparent about where pings originated from.
		 */
		if ($this->user) {
			$srcid = $this->user->id;
			$authapp = $this->authapp? $this->authapp->getSrc()->getId() : null;
			//TODO: Keep logs of the application that originated a ping.
		}
		else {
			throw new PublicException('Authentication required', 403);
		}
		
		
		#Read POST data
		$tgtid    = $_POST['target']?? null;
		$content  = str_replace("\r", '', $_POST['content']);
		$url      = $_POST['url']?? null;
		$media    = $_POST['media']?? null;
		$poll     = $_POST['poll']?? null;
		$irt      = $_POST['irt']?? null;
		$explicit = !!($_POST['explicit']?? false);
		
		#There needs to be a src user. That means that somebody is originating the
		#notification. There has to be one, and no more than one.
		$src = AuthorModel::get(db()->table('user')->get('authId', $srcid)->first()? : UserModel::makeFromSSO($this->sso->getUser($srcid)));
		
		/*
		 * Check whether the source defined a target for this ping. If the target
		 * can be traced to an author, we will record that.
		 */
		$target = $tgtid === null? null : AuthorModel::find($tgtid);
		
		/*
		 * If there is a target, and it's not an author known to the server, then
		 * we must stop the application from creating the ping.
		 */
		if (!($target instanceof AuthorModel || $target === null)) {
			throw new PublicException('Invalid target', 400);
		}

		#Make it a record
		$notification = db()->table('ping')->newRecord();
		$notification->src = $src;
		$notification->authapp = $authapp;
		$notification->target = $target;
		$notification->content = Mention::mentionsToId($content);
		$notification->url     = $url;
		$notification->explicit= $explicit;
		$notification->irt     = $irt? db()->table('ping')->get('_id', $irt)->first(true) : null;
		$notification->processed = false;
		$notification->locked = false;
		
		
		/**
		 * @todo This method should be deprecated in favor of batch processing the
		 * files
		 */
		if (is_string($media)) {
{'fraction_non_alphanumeric': 0.1384427107235735, 'fraction_numerical': 0.0030595074193054917, 'mean_word_length': 3.481151473612063, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, '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': '2097928', 'n_tokens_mistral': 2176, 'n_tokens_neox': 1858, 'n_words': 697}