<commit_before>'use strict';

import { Express, Request, Response } from 'express';

import { sendError } from '../shared/functions';

module.exports = userRoutes;

/** REST endpoints for user management. */
function userRoutes(app: Express, db) {

	app.route('/api/user/:username/follow')
		.post(follow)
		.delete(unfollow);

	app.route('/api/user/:username/upsert')
		.post(upsert);

	app.route('/api/user/:username')
		.get(get)
		.patch(patch);

	const aqlQuery = require('arangojs').aqlQuery;

	/**
	 * Endpoint to select a user by its given username.
	 * 
	 * Example:
	 * GET /api/user/andre
	 */
	function get(req: Request, res: Response): void {
		const myself = aqlQuery`
		for u in user
			filter u._id == ${req['user']._id}
			let followers = first(
				for v in 1 inbound u graph 'socialGraph'
				collect with count into followers
				return followers
			),
			following = first(
				for v in 1 outbound u graph 'socialGraph'
				collect with count into following
				return following
			)
			return merge(u, {me: true, followers, following})
		`;

		const notMyself = aqlQuery`
		for u in user
			filter u._key == ${req.params.username}
			let followed = 0 < length(
				for f in follow
				filter f._from == ${req['user']._id} and f._to == u._id
				return 1
			)
			return merge(u, { me: false, followed })
		`;

		console.log('query', req['user']._key, req.params.username);

		db.query(req['user']._key === req.params.username ? myself : notMyself)
			.then(cursor => cursor.all())
			.then(values => res.json(values[0]))
			.catch(err => sendError(err, res));
	}

	/**
	 * Endpoint to create or update a user.
	 * 
	 * Example:
	 * POST /api/user/andre/upsert
	 * { "_key": "andre", "name": "<NAME>" ... }<commit_msg>users can now follow each other for real<commit_after>'use strict';

import { Express, Request, Response } from 'express';

import { sendError } from '../shared/functions';

module.exports = userRoutes;

/** REST endpoints for user management. */
function userRoutes(app: Express, db) {

	app.route('/api/user/:username/follow')
		.post(follow)
		.delete(unfollow);

	app.route('/api/user/:username/upsert')
		.post(upsert);

	app.route('/api/user/:username')
		.get(get)
		.patch(patch);

	const aqlQuery = require('arangojs').aqlQuery;

	/**
	 * Endpoint to select a user by its given username.
	 * 
	 * Example:
	 * GET /api/user/andre
	 */
	function get(req: Request, res: Response): void {
		const myself = aqlQuery`
		for u in user
			filter u._id == ${req['user']._id}
			let followers = first(
				for v in 1 inbound u graph 'socialGraph'
				collect with count into followers
				return followers
			),
			following = first(
				for v in 1 outbound u graph 'socialGraph'
				collect with count into following
				return following
			)
			return merge(u, {me: true, followers, following})
		`;

		const notMyself = aqlQuery`
		for u in user
			filter u._key == ${req.params.username}
			let followed = 0 < length(
				for f in follow
				filter f._from == ${req['user']._id} and f._to == u._id
				return 1
			)
			return merge(u, { me: false, followed })
		`;

		db.query(req['user']._key === req.params.username ? myself : notMyself)
			.then(cursor => cursor.all())
			.then(values => res.json(values[0]))
			.catch(err => sendError(err, res));
	}

	/**
	 * Endpoint to create or update a user.
	 * 
	 * Example:
	 * POST /api/user/andre/upsert
	 * { "_key": "andre", "name": "<NAME>" ... }
{'fraction_non_alphanumeric': 0.15440535039255598, 'fraction_numerical': 0.002907822041291073, 'mean_word_length': 3.1148325358851676, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 11, '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': '3369265', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1132, 'n_words': 328}