<commit_before>import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import { inject as service } from '@ember/service';
import { merge, values, isEmpty } from 'lodash';

export default Route.extend(ApplicationRouteMixin, {
  session: service(),
  title(tokens) {
    if (!tokens) {
      tokens = [];
    }

    tokens.reverse().push(this.get('settings.appName'));
    return tokens.join(' | ');
  },

  async beforeModel() {
    this._super(...arguments);
    await this.get('authManager').initialize();
    await this.get('settings').initialize();
  },

  async model() {
    let notifications = [];
    if (this.get('session.isAuthenticated')) {
      notifications = await this.get('authManager.currentUser').query('notifications', {
        filter: [
          {
            name : 'is-read',
            op   : 'eq',
            val  : false
          }
        ],
        sort: '-received-at'
      });
    }

    return {
      notifications,
      pages: await this.get('store').query('page', {
        sort: 'index'
      }),
      cookiePolicy     : this.get('settings.cookiePolicy'),
      cookiePolicyLink : this.get('settings.cookiePolicyLink'),
      socialLinks      : await this.get('store').queryRecord('setting', {}),
      eventTypes       : await this.get('store').findAll('event-type'),
      eventLocations   : await this.get('store').findAll('event-location')
    };
  },

  sessionInvalidated() {
    if (!this.get('session.skipRedirectOnInvalidation')) {
      this._super(...arguments);
    }
  },

  sessionAuthenticated() {
    if (this.get('session.previousRouteName')) {
      this.transitionTo(this.get('session.previousRouteName'));
    } else {
      this._super(...arguments);
    }
  },

  /**
   * Merge all params into one param.
   *
   * @param params
   * @return {*}
   * @private
   */
  _mergeParams(params) {
    return merge({}, ...values(params));
  },

  actions: {
    willTransition(transition) {
      transition.then(() => {
        let params = this._mergeParams(transition.params);
        let url;

        // generate doesn't like empty params.
        if (isEmpty(params)) {
          url = transition.router.generate(transition.targetName);
        } else {
          url = transition.router.generate(transition.targetName, params);
        }
        // Do not save the url of the transition to login route.
        if (!url.includes('login')) {
          this.set('session.previousRouteName', url);
        }
      });
    }
  }
});
<commit_msg>Set previousUrl to a URL when user visits link directly<commit_after>import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import { inject as service } from '@ember/service';
import { merge, values, isEmpty } from 'lodash';

export default Route.extend(ApplicationRouteMixin, {
  session: service(),
  title(tokens) {
    if (!tokens) {
      tokens = [];
    }

    tokens.reverse().push(this.get('settings.appName'));
    return tokens.join(' | ');
  },

  async beforeModel(transition) {
    this._super(...arguments);
    await this.get('authManager').initialize();
    await this.get('settings').initialize();
    if (!transition.intent.url.includes('login')) {
      this.set('session.previousRouteName', transition.intent.url);
    }
  },

  async model() {
    let notifications = [];
    if (this.get('session.isAuthenticated')) {
      notifications = await this.get('authManager.currentUser').query('notifications', {
        filter: [
          {
            name : 'is-read',
            op   : 'eq',
            val  : false
          }
        ],
        sort: '-received-at'
      });
    }

    return {
      notifications,
      pages: await this.get('store').query('page', {
        sort: 'index'
      }),
      cookiePolicy     : this.get('settings.cookiePolicy'),
      cookiePolicyLink : this.get('settings.cookiePolicyLink'),
      socialLinks      : await this.get('store').queryRecord('setting', {}),
      eventTypes       : await this.get('store').findAll('event-type'),
      eventLocations   : await this.get('store').findAll('event-location')
    };
  },

  sessionInvalidated() {
    if (!this.get('session.skipRedirectOnInvalidation')) {
      this._super(...arguments);
    }
  },

  sessionAuthenticated() {
    if (this.get('session.previousRouteName')) {
      this.transitionTo(this.get('session.previousRouteName'));
    } else {
      this._super(...arguments);
    }
  },

  /**
   * Merge all params into one param.
   *
   * @param params
   * @return {*}
   * @private
   */
  _mergeParams(params) {
    return merge({}, ...values(params));
  },

  actions: {
    willTransition(transition) {
      transition.then(() => {
        let params = this._mergeParams(transition.params);
        let url;
        // generate doesn't like empty params.
        if (isEmpty(params)) {
          url = transition.router.generate(transition.targetName);
        } else {
          url = transition.router.generate(transition.targetName, params);
        }
        // Do not save the url of the transition to login route.
        if (!url.includes('login')) {
          this.set('session.previousRouteName', url);
        }
      });
    }
  }
});

{'fraction_non_alphanumeric': 0.15520149953139645, 'fraction_numerical': 0.0, 'mean_word_length': 2.4875816993464053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2850989', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1520, 'n_words': 287}