Commit 42105fbf authored by Mark Harding's avatar Mark Harding
Browse files

(feat): move entire process to clientMetaService function

parent cef7c142
Loading
Loading
Loading
Loading
+13 −1
Original line number Original line Diff line number Diff line
@@ -2,6 +2,7 @@ import { Injectable, Injector } from '@angular/core';
import { Location } from '@angular/common';
import { Location } from '@angular/common';
import hashCode from '../../helpers/hash-code';
import hashCode from '../../helpers/hash-code';
import { Session } from '../../services/session';
import { Session } from '../../services/session';
import { Client } from '../../services/api';


let uniqId = 0;
let uniqId = 0;


@@ -23,7 +24,11 @@ export class ClientMetaService {


  protected inherited: boolean = false;
  protected inherited: boolean = false;


  constructor(protected location: Location, protected session: Session) {
  constructor(
    protected location: Location,
    protected session: Session,
    protected client: Client
  ) {
    this.id = ++uniqId;
    this.id = ++uniqId;


    this.timestamp = Date.now();
    this.timestamp = Date.now();
@@ -146,6 +151,13 @@ export class ClientMetaService {
    };
    };
  }
  }


  async recordView(entity) {
    await this.client.post(
      'api/v2/analytics/views/entity/' + entity.guid,
      this.build()
    );
  }

  protected checkInheritance() {
  protected checkInheritance() {
    if (!this.inherited) {
    if (!this.inherited) {
      console.warn(
      console.warn(
+13 −11
Original line number Original line Diff line number Diff line
@@ -6,6 +6,8 @@ import {
  OnDestroy,
  OnDestroy,
  OnInit,
  OnInit,
  ViewChild,
  ViewChild,
  Injector,
  SkipSelf,
} from '@angular/core';
} from '@angular/core';
import { Router } from '@angular/router';
import { Router } from '@angular/router';


@@ -22,6 +24,7 @@ import { optimizedResize } from '../../../utils/optimized-resize';
import { OverlayModalService } from '../../../services/ux/overlay-modal';
import { OverlayModalService } from '../../../services/ux/overlay-modal';
import { ActivityService } from '../../../common/services/activity.service';
import { ActivityService } from '../../../common/services/activity.service';
import { ShareModalComponent } from '../../../modules/modals/share/share';
import { ShareModalComponent } from '../../../modules/modals/share/share';
import { ClientMetaService } from '../../../common/services/client-meta.service';


@Component({
@Component({
  moduleId: module.id,
  moduleId: module.id,
@@ -30,7 +33,7 @@ import { ShareModalComponent } from '../../../modules/modals/share/share';
    class: 'm-blog',
    class: 'm-blog',
  },
  },
  templateUrl: 'view.html',
  templateUrl: 'view.html',
  providers: [ActivityService],
  providers: [ActivityService, ClientMetaService],
})
})
export class BlogView implements OnInit, OnDestroy {
export class BlogView implements OnInit, OnDestroy {
  minds;
  minds;
@@ -98,8 +101,15 @@ export class BlogView implements OnInit, OnDestroy {
    public analyticsService: AnalyticsService,
    public analyticsService: AnalyticsService,
    protected activityService: ActivityService,
    protected activityService: ActivityService,
    private cd: ChangeDetectorRef,
    private cd: ChangeDetectorRef,
    private overlayModal: OverlayModalService
    private overlayModal: OverlayModalService,
    private clientMetaService: ClientMetaService,
    @SkipSelf() injector: Injector
  ) {
  ) {
    this.clientMetaService
      .inherit(injector)
      .setSource('single')
      .setMedium('single');

    this.minds = window.Minds;
    this.minds = window.Minds;
    this.element = _element.nativeElement;
    this.element = _element.nativeElement;
    optimizedResize.add(this.onResize.bind(this));
    optimizedResize.add(this.onResize.bind(this));
@@ -108,7 +118,7 @@ export class BlogView implements OnInit, OnDestroy {
  ngOnInit() {
  ngOnInit() {
    this.isVisible();
    this.isVisible();
    this.context.set('object:blog');
    this.context.set('object:blog');
    this.recordAnalytics();
    this.clientMetaService.recordView(this.blog);
  }
  }


  isVisible() {
  isVisible() {
@@ -219,12 +229,4 @@ export class BlogView implements OnInit, OnDestroy {
  onResize(event: Event) {
  onResize(event: Event) {
    this.calculateLockScreenHeight();
    this.calculateLockScreenHeight();
  }
  }

  recordAnalytics() {
    this.analyticsService.recordView(this.blog, {
      position: -1,
      source: 'single',
      medium: 'single',
    });
  }
}
}
+3 −3
Original line number Original line Diff line number Diff line
@@ -37,8 +37,8 @@ import { FeaturesService } from '../../services/features.service';
import { featuresServiceMock } from '../../../tests/features-service-mock.spec';
import { featuresServiceMock } from '../../../tests/features-service-mock.spec';
import { BlockListService } from '../../common/services/block-list.service';
import { BlockListService } from '../../common/services/block-list.service';
import { ChannelMode } from '../../interfaces/entities';
import { ChannelMode } from '../../interfaces/entities';
import { AnalyticsService } from '../../services/analytics';
import { ClientMetaService } from '../../common/services/client-meta.service';
import { analyticsServiceMock } from '../../../tests/analytics-service-mock.spec';
import { clientMetaServiceMock } from '../../../tests/client-meta-service-mock.spec';


describe('ChannelComponent', () => {
describe('ChannelComponent', () => {
  let comp: ChannelComponent;
  let comp: ChannelComponent;
@@ -107,7 +107,7 @@ describe('ChannelComponent', () => {
        },
        },
        { provide: FeaturesService, useValue: featuresServiceMock },
        { provide: FeaturesService, useValue: featuresServiceMock },
        { provide: BlockListService, useValue: MockService(BlockListService) },
        { provide: BlockListService, useValue: MockService(BlockListService) },
        { provide: AnalyticsService, useValue: analyticsServiceMock },
        { provide: ClientMetaService, useValue: clientMetaServiceMock },
      ],
      ],
    }).compileComponents(); // compile template and css
    }).compileComponents(); // compile template and css
  }));
  }));
+11 −12
Original line number Original line Diff line number Diff line
import { Component, ViewChild } from '@angular/core';
import { Component, ViewChild, SkipSelf, Injector } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';


import { Subscription } from 'rxjs';
import { Subscription } from 'rxjs';
@@ -18,6 +18,7 @@ import { DialogService } from '../../common/services/confirm-leave-dialog.servic
import { BlockListService } from '../../common/services/block-list.service';
import { BlockListService } from '../../common/services/block-list.service';
import { ChannelSortedComponent } from './sorted/sorted.component';
import { ChannelSortedComponent } from './sorted/sorted.component';
import { AnalyticsService } from '../../services/analytics';
import { AnalyticsService } from '../../services/analytics';
import { ClientMetaService } from '../../common/services/client-meta.service';


@Component({
@Component({
  moduleId: module.id,
  moduleId: module.id,
@@ -55,8 +56,14 @@ export class ChannelComponent {
    private context: ContextService,
    private context: ContextService,
    private dialogService: DialogService,
    private dialogService: DialogService,
    private blockListService: BlockListService,
    private blockListService: BlockListService,
    private analyticsService: AnalyticsService
    private clientMetaService: ClientMetaService,
  ) {}
    @SkipSelf() injector: Injector
  ) {
    this.clientMetaService
      .inherit(injector)
      .setSource('single')
      .setMedium('single');
  }


  ngOnInit() {
  ngOnInit() {
    this.title.setTitle('Channel');
    this.title.setTitle('Channel');
@@ -135,7 +142,7 @@ export class ChannelComponent {
          this.addRecent();
          this.addRecent();
        }
        }


        this.recordAnalytics();
        this.clientMetaService.recordView(this.user);
      })
      })
      .catch(e => {
      .catch(e => {
        if (e.status === 0) {
        if (e.status === 0) {
@@ -239,14 +246,6 @@ export class ChannelComponent {
      .splice('recent', 50);
      .splice('recent', 50);
  }
  }


  recordAnalytics() {
    this.analyticsService.recordView(this.user, {
      position: -1,
      source: 'single',
      medium: 'single',
    });
  }

  /**
  /**
   * canDeactivate()
   * canDeactivate()
   * Determines whether a page can be deactivated.
   * Determines whether a page can be deactivated.
+3 −4
Original line number Original line Diff line number Diff line
@@ -61,10 +61,9 @@ export class ActivityAnalyticsOnViewService implements OnDestroy {
        }
        }
      });
      });


    this.scroll$ = this.scroll.listenForView().subscribe(this.checkVisibility);
    this.scroll$ = this.scroll.listenForView().subscribe(() => {

    // Do a check for the initial posts
      this.checkVisibility();
      this.checkVisibility();
    });
  }
  }


  checkVisibility() {
  checkVisibility() {
Loading