xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / watch.inc.php (3817a0503eeea342c350fc4565823a9af367e465) (4,804B) (mode 100644) [raw]
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/user.inc.php");
require_once($INC . "/prof.inc.php");

$rg_watch_error = "";

function rg_watch_set_error($str)
{
	global $rg_watch_error;
	$rg_watch_error = $str;
	rg_log($str);
}

function rg_watch_error()
{
	global $rg_watch_error;
	return $rg_watch_error;
}


/*
 * Returns a watched entry
 */
$rg_watch_load_cache = array();
function rg_watch_load($db, $type, $uid, $obj_id1, $obj_id2)
{
	global $rg_watch_load_cache;

	$key = $type . "-" . $uid . "-" . $obj_id1 . "-" . $obj_id2;
	if (isset($rg_watch_load_cache[$key]))
		return $rg_watch_load_cache[$key];

	rg_prof_start("watch_load");
	rg_log_enter("watch_load: type=$type uid=$uid obj_id=$obj_id1/$obj_id2");

	$ret = FALSE;
	while (1) {
		$params = array("uid" => $uid,
			"obj_id1" => $obj_id1,
			"obj_id2" => $obj_id2);
		if (strcmp($type, "bug") == 0) {
			$sql = "SELECT 1 FROM watch_bug"
				. " WHERE uid = @@uid@@"
				. " AND repo_id = @@obj_id1@@"
				. " AND bug_id = @@obj_id2@@";
		} else if (strcmp($type, "repo") == 0) {
			$sql = "SELECT 1 FROM watch_repo"
				. " WHERE uid = @@uid@@"
				. " AND repo_id = @@obj_id1@@";
		} else {
			rg_internal_error("Invalid watch type!");
			break;
		}
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;

		$rows = rg_sql_num_rows($res);
		rg_sql_free_result($res);

		$ret = $rows > 0 ? 1 : 0;
		$rg_watch_load_cache[$key] = $ret;
		break;
	}

	rg_log_exit();
	rg_prof_end("watch_load");
	return $ret;
}

/*
 * Add somebody to the watch list
 */
$rg_watch_add_state = array();
function rg_watch_add($db, $type, $uid, $obj_id1, $obj_id2)
{
	global $rg_watch_add_state;

	// If watch already added, skip.
	$key = $type . "-" . $uid . "-" . $obj_id1 . "-" . $obj_id2;
	if (isset($rg_watch_add_state[$key]))
		return $rg_watch_add_state[$key];

	rg_prof_start("watch_add");
	rg_log_enter("watch_add type=$type, uid=$uid obj_id=$obj_id1/$obj_id2");

	$ret = FALSE;
	while (1) {
		$r = rg_watch_load($db, $type, $uid, $obj_id1, $obj_id2);
		if ($r === FALSE)
			break;
		if ($r === 1) { // already in watch list
			$ret = TRUE;
			break;
		}

		$params = array("uid" => $uid,
			"obj_id1" => $obj_id1,
			"obj_id2" => $obj_id2);

		if (strcmp($type, "bug") == 0) {
			$sql = "INSERT INTO watch_bug (uid, repo_id, bug_id)"
				. " VALUES (@@uid@@, @@obj_id1@@, @@obj_id2@@)";
		} else if (strcmp($type, "repo") == 0) {
			$sql = "INSERT INTO watch_repo (uid, repo_id)"
				. " VALUES (@@uid@@, @@obj_id1@@)";
		} else {
			rg_internal_error("Invalid watch type!");
			break;
		}
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;
		rg_sql_free_result($res);

		$ret = TRUE;
		break;
	}

	$rg_watch_add_state[$key] = $ret;

	rg_log_exit();
	rg_prof_end("watch_add");
	return $ret;
}

/*
 * Delete somebody from the watch list
 */
function rg_watch_del($db, $type, $login_uid, $obj_id1, $obj_id2)
{
	rg_prof_start("watch_del");
	rg_log_enter("watch_del type=$type, login_uid=$login_uid obj_id=$obj_id1/$obj_id2");

	$ret = FALSE;
	while (1) {
		$params = array("login_uid" => $login_uid,
			"obj_id1" => $obj_id1,
			"obj_id2" => $obj_id2);

		if (strcmp($type, "bug") == 0) {
			$sql = "DELETE FROM watch_bug"
				. " WHERE uid = @@login_uid@@"
				. " AND repo_id = @@obj_id1@@"
				. " AND bug_id = @@obj_id2@@";
		} else if (strcmp($type, "repo") == 0) {
			$sql = "DELETE FROM watch_repo"
				. " WHERE uid = @@login_uid@@"
				. " AND repo_id = @@obj_id1@@";
		} else {
			rg_internal_error("Invalid watch type!");
			break;
		}
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;
		rg_sql_free_result($res);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end("watch_del");
	return $ret;
}

/*
 * Returns a list of uids by type and obj_id
 */
function rg_watch_load_by_obj_id($db, $type, $obj_id1, $obj_id2)
{
	rg_prof_start("watch_load_by_obj_id");
	rg_log_enter("watch_load_by_obj_id: type=$type obj_id=$obj_id1/$obj_id2");

	$ret = FALSE;
	while (1) {
		$params = array("obj_id1" => $obj_id1,
			"obj_id2" => $obj_id2);

		if (strcmp($type, "bug") == 0) {
			$sql = "SELECT uid FROM watch_bug"
				. " WHERE repo_id = @@obj_id1@@"
				. " AND bug_id = @@obj_id2@@";
		} else if (strcmp($type, "repo") == 0) {
			$sql = "SELECT uid FROM watch_repo"
				. " WHERE repo_id = @@obj_id1@@";
		} else {
			rg_internal_error("Invalid watch type!");
			break;
		}
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;

		$ret = array();
		while (($row = rg_sql_fetch_array($res))) {
			$ret[] = $row['uid'];
		}
		rg_sql_free_result($res);

		break;
	}

	rg_log_exit();
	rg_prof_end("watch_load_by_obj_id");
	return $ret;
}

?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master