<commit_before><?php // routes.php

ini_set("memory_limit", "256M");    // this is a horrible way to do this and I apologise

use Symfony\Component\HttpFoundation\Request;
use Purplapp\Adn\NumberCollection;

// helpers
$redirector = function ($name) use ($app) {
    return function () use ($name, $app) {
        return $app->redirect($app->path($name));
    };
};

$renderer = function ($name, array $data = []) use ($app) {
    return function () use ($name, $data, $app) {
        return $app->render($name, $data);
    };
};

/**
 * generates a simple controller that renders templates for GET requests, binds
 * it to the provided name, and sets up a redirect for legacy URLs that use the
 * filename.php convention
 *
 * @param string $path
 * @param string $name
 */
$simplePage = function ($path, $name) use ($app, $renderer, $redirector) {
    $app->get($path, $renderer($name . ".twig"))->bind($name);

    if (substr($path, -1) === "/") {
        $path = $path . "/index";
    }

    $app->get($path . ".php", $redirector($name));
};

$simplePage("/", "index");
$simplePage("/tools", "tools");
$simplePage("/donate", "donate");
$simplePage("/about", "about");
$simplePage("/legal/privacy", "privacy");
$simplePage("/legal/terms", "terms");

$app->get("/signin.php", $redirector("signin"));
$app->get("/signin", function () use ($app) {
    if ($app["adn.user"]) {
        return $app->redirect($app->path("index"));
    }

    return $app->render("signin.twig");
})->bind("signin");

$app->get("/signout", function () use ($app) {
    $app["session"]->invalidate();

    return $app->redirect($app->path("index"));
})->bind("signout");

$app->get("/adn/callback", function (Request $req) use ($app) {
    $response = $app["adn.client"]->getAccessToken($req->get("code"));

    $app["session"]->set("access_token", $response->access_token);
    $app["session"]->set("user", $response->token->user);

    return $app->redirect($app->path("index"));
});

$app->get("/account/mention.php", $redirector("account_mention"));
$app->get("/account/mention", function (Request $req) use ($app) {
    if (!$app["adn.user"]) {
        return $app->render("unauth_message.twig");
    }

    if (!$req->get("id1") && !$req->get("id2")) {
        return $app->render("user_first_mention_form.twig");
    }

    $client = $app["adn.client"];

    $left  = $req->get("id1") ?: "me";
    $right = $req->get("id2") ?: "me";

    $leftData  = $client->getUser($left);
    $rightData = $client->getUser($right);

    $leftByRightParams = [
        "mentions"                 => $leftData->username,
        "creator_id"               => $rightData->id,
        "count"                    => -1,
    ];

    $rightByLeftParams = [
        "mentions"                 => $rightData->username,
        "creator_id"               => $leftData->id,
        "count"                    => -1,
    ];

    $rightByLeft = $client->searchPosts($rightByLeftParams)->tail();
    $leftByRight = $client->searchPosts($leftByRightParams)->tail();

    return $app->render(
        "user_first_mention.twig",
        compact("leftData", "rightData", "leftByRight", "rightByLeft")
    );
})->bind("account_mention");

$app->get("/user.php", $redirector("account_user"));
$app->get("/pca.php", $redirector("account_user"));
$app->get("/account/user.php", $redirector("account_user"));
$app->get("/account/user", function (Request $req) use ($app) {
    if (!$app["adn.user"]) {
        return $app->render("unauth_message.twig");
    }

    $client = $app["adn.client"];

    if ($req->get("id")) {
        $user = $client->getUser($req->get("id"), ["include_user_annotations" => true]);
    } else {
        $user = $client->getAuthorizedUser(["include_user_annotations" => true]);
    }

    $firstOpts = ["count" => -1, "include_deleted" => false];
    $lastOpts  = ["count" => 1,  "include_deleted" => false];

    $firstPost    = $client->getUserPosts($user, $firstOpts)->head();
    $lastPost     = $client->getUserPosts($user, $lastOpts)->tail();

    $firstMention = $client->getUserMentions($user, $firstOpts)->head();
    $lastMention  = $client->getUserMentions($user, $lastOpts)->tail();

    $niceRank = $client->getUserNiceRank($user)->head();

    if ($req->get("id")) {
        $currentUser = $client->getAuthorizedUser();
        if ($req->get("id") == $currentUser->username) {
            $token = $client->getUserToken();
        } else {
            $token = "";
        }
    } else {
        $token = $client->getUserToken();
    }   

    if ($req->get("id")) {
        $currentUser = $client->getAuthorizedUser();
        if ($req->get("id") == $currentUser->username) {
            $unreadBroadcastChannels = $client->getUnreadBroadcastChannels();
            $unreadPMChannels = $client->getUnreadPMChannels();
            $unreadBroadcast = $unreadBroadcastChannels->count();
            $unreadPM = $unreadPMChannels->count();
        } else {
            $unreadBroadcast = "";
            $unreadPM = "";
        }
    } else {
        $unreadBroadcastChannels = $client->getUnreadBroadcastChannels();
        $unreadPMChannels = $client->getUnreadPMChannels();
        $unreadBroadcast = $unreadBroadcastChannels->count();
        $unreadPM = $unreadPMChannels->count();
    }   

    return $app->render("account_user.twig", [<commit_msg>(for @ciarand) comment code for getting user token<commit_after><?php // routes.php

ini_set("memory_limit", "256M");    // this is a horrible way to do this and I apologise

use Symfony\Component\HttpFoundation\Request;
use Purplapp\Adn\NumberCollection;

// helpers
$redirector = function ($name) use ($app) {
    return function () use ($name, $app) {
        return $app->redirect($app->path($name));
    };
};

$renderer = function ($name, array $data = []) use ($app) {
    return function () use ($name, $data, $app) {
        return $app->render($name, $data);
    };
};

/**
 * generates a simple controller that renders templates for GET requests, binds
 * it to the provided name, and sets up a redirect for legacy URLs that use the
 * filename.php convention
 *
 * @param string $path
 * @param string $name
 */
$simplePage = function ($path, $name) use ($app, $renderer, $redirector) {
    $app->get($path, $renderer($name . ".twig"))->bind($name);

    if (substr($path, -1) === "/") {
        $path = $path . "/index";
    }

    $app->get($path . ".php", $redirector($name));
};

$simplePage("/", "index");
$simplePage("/tools", "tools");
$simplePage("/donate", "donate");
$simplePage("/about", "about");
$simplePage("/legal/privacy", "privacy");
$simplePage("/legal/terms", "terms");

$app->get("/signin.php", $redirector("signin"));
$app->get("/signin", function () use ($app) {
    if ($app["adn.user"]) {
        return $app->redirect($app->path("index"));
    }

    return $app->render("signin.twig");
})->bind("signin");

$app->get("/signout", function () use ($app) {
    $app["session"]->invalidate();

    return $app->redirect($app->path("index"));
})->bind("signout");

$app->get("/adn/callback", function (Request $req) use ($app) {
    $response = $app["adn.client"]->getAccessToken($req->get("code"));

    $app["session"]->set("access_token", $response->access_token);
    $app["session"]->set("user", $response->token->user);

    return $app->redirect($app->path("index"));
});

$app->get("/account/mention.php", $redirector("account_mention"));
$app->get("/account/mention", function (Request $req) use ($app) {
    if (!$app["adn.user"]) {
        return $app->render("unauth_message.twig");
    }

    if (!$req->get("id1") && !$req->get("id2")) {
        return $app->render("user_first_mention_form.twig");
    }

    $client = $app["adn.client"];

    $left  = $req->get("id1") ?: "me";
    $right = $req->get("id2") ?: "me";

    $leftData  = $client->getUser($left);
    $rightData = $client->getUser($right);

    $leftByRightParams = [
        "mentions"                 => $leftData->username,
        "creator_id"               => $rightData->id,
        "count"                    => -1,
    ];

    $rightByLeftParams = [
        "mentions"                 => $rightData->username,
        "creator_id"               => $leftData->id,
        "count"                    => -1,
    ];

    $rightByLeft = $client->searchPosts($rightByLeftParams)->tail();
    $leftByRight = $client->searchPosts($leftByRightParams)->tail();

    return $app->render(
        "user_first_mention.twig",
        compact("leftData", "rightData", "leftByRight", "rightByLeft")
    );
})->bind("account_mention");

$app->get("/user.php", $redirector("account_user"));
$app->get("/pca.php", $redirector("account_user"));
$app->get("/account/user.php", $redirector("account_user"));
$app->get("/account/user", function (Request $req) use ($app) {
    if (!$app["adn.user"]) {
        return $app->render("unauth_message.twig");
    }

    $client = $app["adn.client"];

    if ($req->get("id")) {
        $user = $client->getUser($req->get("id"), ["include_user_annotations" => true]);
    } else {
        $user = $client->getAuthorizedUser(["include_user_annotations" => true]);
    }

    $firstOpts = ["count" => -1, "include_deleted" => false];
    $lastOpts  = ["count" => 1,  "include_deleted" => false];

    $firstPost    = $client->getUserPosts($user, $firstOpts)->head();
    $lastPost     = $client->getUserPosts($user, $lastOpts)->tail();

    $firstMention = $client->getUserMentions($user, $firstOpts)->head();
    $lastMention  = $client->getUserMentions($user, $lastOpts)->tail();

    $niceRank = $client->getUserNiceRank($user)->head();

    $currentUser = $client->getAuthorizedUser();    // set who the current user is

    if ($req->get("id")) {                  // if an ID is being requested as part of the URL
        if ($req->get("id") === $currentUser->username) {   // if the ID being requested is equal to the authorised user's username
            $token = $client->getUserToken();   // get the token
        } else {
            $token = "";    // else there's no token to be set
        }
    } else {
        $token = $client->getUserToken();   // if no user is requested, then it must be the authorised user, hence get the token
    }   

    if ($req->get("id")) {
        if ($req->get("id") === $currentUser->username) {
            $unreadBroadcastChannels = $client->getUnreadBroadcastChannels();
            $unreadPMChannels = $client->getUnreadPMChannels();
            $unreadBroadcast = $unreadBroadcastChannels->count();
            $unreadPM = $unreadPMChannels->count();
        } else {
            $unreadBroadcast = "";
            $unreadPM = "";
        }
    } else {
        $unreadBroadcastChannels = $client->getUnreadBroadcastChannels();
        $unreadPMChannels = $client->getUnreadPMChannels();
        $unreadBroadcast = $unreadBroadcastChannels->count();
        $unreadPM = $unreadPMChannels->count();
    }   

    return $app->render("account_user.twig", [
{'fraction_non_alphanumeric': 0.20777211503458318, 'fraction_numerical': 0.002184200946487077, 'mean_word_length': 3.0927374301675976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 196, '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': '1020109', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3519, 'n_words': 660}