<commit_before>
let primaryTab = false;
let liveEnabled = false;
let havePermission = null;
let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
let lastAction = -1;

const focusTrackerKey = "focus-tracker";
const seenDataKey = "seen-notifications";
const recentUpdateThreshold = 1000 * 60 * 2; // 2 minutes
const idleThresholdTime = 1000 * 10; // 10 seconds
const INVITED_TYPE = 8;
let notificationTagName; // "discourse-notification-popup-" + Discourse.SiteSettings.title;

// Called from an initializer
function init(messageBus) {
  liveEnabled = false;
  mbClientId = messageBus.clientId;

  if (!Discourse.User.current()) {
    return;
  }

  try {
    localStorage.getItem(focusTrackerKey);
  } catch (e) {
    Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.');
    return;
  }



  try {
    if (!Notification) {
      Em.Logger.info('Discourse desktop notifications are disabled - not supported by browser');
      return;
    }
  } catch (e) {
    Em.Logger.info('Discourse desktop notifications are disabled - not defined');
    return;
  }

  if (Notification.permission === "granted") {
    havePermission = true;
  } else if (Notification.permission === "denied") {
    havePermission = false;
    return;
  }

  liveEnabled = true;
  try {
    // Preliminary checks passed, continue with setup
    setupNotifications();
  } catch (e) {
    Em.Logger.error(e);
  }
}

// This function is only called if permission was granted
function setupNotifications() {
  // Load up the current state of the notifications
  const seenData = JSON.parse(localStorage.getItem(seenDataKey));
  let markAllSeen = true;
  if (seenData) {
    const lastUpdatedAt = new Date(seenData.updated_at);
    if (lastUpdatedAt.getTime() + recentUpdateThreshold > new Date().getTime()) {
      // The following conditions are met:
      //  - This is a new Discourse tab
      //  - The seen notification data was updated in the last 2 minutes
      // Therefore, there is no need to reset the data.
      markAllSeen = false;
    }
  }
  if (markAllSeen) {
    Discourse.ajax("/notifications.json?silent=true").then(function(result) {
      updateSeenNotificationDatesFrom(result);
    });
  }<commit_msg>Improve Notification probing<commit_after>
let primaryTab = false;
let liveEnabled = false;
let havePermission = null;
let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
let lastAction = -1;

const focusTrackerKey = "focus-tracker";
const seenDataKey = "seen-notifications";
const recentUpdateThreshold = 1000 * 60 * 2; // 2 minutes
const idleThresholdTime = 1000 * 10; // 10 seconds
const INVITED_TYPE = 8;
let notificationTagName; // "discourse-notification-popup-" + Discourse.SiteSettings.title;

// Called from an initializer
function init(messageBus) {
  liveEnabled = false;
  mbClientId = messageBus.clientId;

  if (!Discourse.User.current()) {
    return;
  }

  try {
    localStorage.getItem(focusTrackerKey);
  } catch (e) {
    Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.');
    return;
  }



  if (!("Notification" in window)) {
    Em.Logger.info('Discourse desktop notifications are disabled - not supported by browser');
    return;
  }

  try {
    if (Notification.permission === "granted") {
      havePermission = true;
    } else if (Notification.permission === "denied") {
      havePermission = false;
      return;
    }
  } catch (e) {
    Em.Logger.warn('Unexpected error, Notification is defined on window but not a responding correctly ' + e);
  }

  liveEnabled = true;
  try {
    // Preliminary checks passed, continue with setup
    setupNotifications();
  } catch (e) {
    Em.Logger.error(e);
  }
}

// This function is only called if permission was granted
function setupNotifications() {
  // Load up the current state of the notifications
  const seenData = JSON.parse(localStorage.getItem(seenDataKey));
  let markAllSeen = true;
  if (seenData) {
    const lastUpdatedAt = new Date(seenData.updated_at);
    if (lastUpdatedAt.getTime() + recentUpdateThreshold > new Date().getTime()) {
      // The following conditions are met:
      //  - This is a new Discourse tab
      //  - The seen notification data was updated in the last 2 minutes
      // Therefore, there is no need to reset the data.
      markAllSeen = false;
    }
  }
  if (markAllSeen) {
    Discourse.ajax("/notifications.json?silent=true").then(function(result) {
      updateSeenNotificationDatesFrom(result);
    });
  }
{'fraction_non_alphanumeric': 0.11086474501108648, 'fraction_numerical': 0.008425720620842573, 'mean_word_length': 3.3881322957198443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '937394', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1295, 'n_words': 385}