<commit_before>'use strict';

var _ = require('lodash');
var glimpse = require('glimpse');
var util = require('lib/util');
var messageProcessor = require('./util/request-message-processor');

(function() {
	var deepLinkRequestId;
	var deepLinkUserId;
	var detailSubscription;
	var summarySubscription;
	
	var checkUrl = function() {
		var requestId = util.getQueryStringParam('requestId');
		if (requestId) {
			deepLinkRequestId = requestId;
			
			if (util.getQueryStringParam('follow')) {
				detailSubscription = glimpse.on('data.request.detail.found', foundRequestDetail);
			}
			
			glimpse.emit('shell.request.summary.selected', { requestId: requestId })
		}
	};
	
	// NOTE: assumes that user message is going to be in detail response we get 
	var foundRequestDetail = function(foundRequest) {
		var request = foundRequest.allRequestsIndex[deepLinkRequestId];
		if (request) {
			glimpse.off(detailSubscription);
			
			var userMessage = messageProcessor.getTypePayloadItem(request, 'user-identification');
			if (userMessage) {
				deepLinkUserId = userMessage.userId;
				
				summarySubscription = glimpse.on('data.request.summary.found', foundRequestSummary);
			}
		}
	};
	
	var foundRequestSummary = function(foundRequest) {
		_.each(foundRequest.affectedRequests, function(request) {
			var userMessage = messageProcessor.getTypePayloadItem(request, 'user-identification');
			if (userMessage && userMessage.userId == deepLinkUserId) {
				// TODO: need to some sort of check to see that this request happened after the last one
				var endMessage = messageProcessor.getTypePayloadItem(request, 'end-request');
				if (endMessage && endMessage.responseContentType && endMessage.responseContentType.indexOf('text/html') > -1) {
					glimpse.emit('shell.request.summary.selected', { requestId: request.id });
				}
			}
		});
	};
	
	var clearRequest = function() {
		if (deepLinkRequestId) {
			glimpse.off(summarySubscription);
			
			deepLinkRequestId = null;
			deepLinkUserId = null;
		}
	};
	
	glimpse.on('shell.request.ready', checkUrl);
	glimpse.on('shell.request.detail.closed', clearRequest);
})();<commit_msg>Update follow mode to deal with only following newer requests<commit_after>'use strict';

var _ = require('lodash');
var moment = require('moment');
var glimpse = require('glimpse');
var util = require('lib/util');
var messageProcessor = require('./util/request-message-processor');

(function() {
	var deepLinkRequestId;
	var deepLinkUserId;
	var deepLinkRequestStartTime;
	var detailSubscription;
	var summarySubscription;
	
	var checkUrl = function() {
		var requestId = util.getQueryStringParam('requestId');
		if (requestId) {
			deepLinkRequestId = requestId;
			
			if (util.getQueryStringParam('follow')) {
				detailSubscription = glimpse.on('data.request.detail.found', foundRequestDetail);
			}
			
			glimpse.emit('shell.request.summary.selected', { requestId: requestId })
		}
	};
	
	// NOTE: assumes that user message is going to be in detail response we get 
	var foundRequestDetail = function(foundRequest) {
		var request = foundRequest.allRequestsIndex[deepLinkRequestId];
		if (request) {
			glimpse.off(detailSubscription);
			
			// TODO: possibly race condition here... need to look into it more
			if (request._userId && request._requestStartTime) {
				deepLinkUserId = request._userId;
				deepLinkRequestStartTime = request._requestStartTime;
				
				summarySubscription = glimpse.on('data.request.summary.found', foundRequestSummary);
			}
		}
	};
	
	var foundRequestSummary = function(foundRequests) {
		_.each(foundRequests.affectedRequests, function(request) {
			// if its the same user, and this new requet happens after this one and its of document type
			if (request._userId == deepLinkUserId 
				&& moment(request._requestStartTime).isAfter(deepLinkUserId)
				&& request._responseContentCategory && request._responseContentCategory.document) {
					glimpse.emit('shell.request.summary.selected', { requestId: request.id });
			}
		});
	};
	
	var clearRequest = function() {
		if (deepLinkRequestId) {
			glimpse.off(summarySubscription);
			
			deepLinkRequestStartTime = null;
			deepLinkRequestId = null;
			deepLinkUserId = null;
		}
	};
	
	glimpse.on('shell.request.ready', checkUrl);
	glimpse.on('shell.request.detail.closed', clearRequest);
})();
{'fraction_non_alphanumeric': 0.11982419616007402, 'fraction_numerical': 0.0002313208420078649, 'mean_word_length': 4.49428208386277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6587300', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1273, 'n_words': 276}