<commit_before>import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  Input,
  ViewChild,
} from '@angular/core';

import { Session } from '../../../services/session';
import { Client } from '../../../services/api';
import { AuthModalService } from '../../../modules/auth/modal/auth-modal.service';
import { ExperimentsService } from '../../../modules/experiments/experiments.service';
import { FriendlyCaptchaComponent } from '../../../modules/captcha/friendly-catpcha/friendly-captcha.component';
import { FormToastService } from '../../services/form-toast.service';
import { ActivityService } from '../../../modules/newsfeed/activity/activity.service';

@Component({
  selector: 'minds-button-thumbs-up',
  inputs: ['_object: object'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: 'thumbs-up.component.html',
  styleUrls: [`thumbs-up.component.ng.scss`],
})
export class ThumbsUpButton {
  changesDetected: boolean = false;
  object = {
    guid: null,
    owner_guid: null,
    'thumbs:up:user_guids': [],
  };

  /**
   * Will display the friendly captcha component and complete puzzle if true
   */
  public showFriendlyCaptcha = false;

  /**
   * In progress state, eg. captcha working or api saving
   */
  @Input() inProgress = false;

  @Input() iconOnly = false;

  @ViewChild(FriendlyCaptchaComponent)
  friendlyCaptchaEl: FriendlyCaptchaComponent;

  constructor(
    public session: Session,
    public client: Client,
    private authModal: AuthModalService,
    private cd: ChangeDetectorRef,
    private experiments: ExperimentsService,
    private toast: FormToastService,
    private activityService: ActivityService
  ) {}

  set _object(value: any) {
    if (!value) return;
    this.object = value;
    if (!this.object['thumbs:up:user_guids'])
      this.object['thumbs:up:user_guids'] = [];
  }

  /**
   * Called when a mouse click / tap is made
   * @param e
   * @returns void
   */
  onClick(e: MouseEvent): void {
    if (this.isFriendlyCaptchaFeatureEnabled() && !this.has()) {
      this.showFriendlyCaptcha = true;
      this.inProgress = true;
    } else {
      this.submit();
    }
  }

  /**
   * Called when friendly captcha returns value
   * @param solution
   */
  onFriendlyCaptchaComplete(solution: string): void {
    this.submit(solution);
  }

  /**
   * Submits the vote (or cancels it)
   * @param solution
   * @returns Promise<void>
   */
  async submit(solution?: string): Promise<void> {
    this.inProgress = true;
    if (!this.session.isLoggedIn()) {
      const user = await this.authModal.open();
      if (!user) return;
    }
    let data = {};

    if (this.isFriendlyCaptchaFeatureEnabled()) {
      data = {
        puzzle_solution: solution,
      };
    }

    try {
      await this.client.put('api/v1/thumbs/' + this.object.guid + '/up', data);
    } catch (e) {
      this.toast.error(e?.message ?? 'An unknown error has occurred');
    }

    this.showFriendlyCaptcha = false;
    this.inProgress = false;

    if (!this.has()) {
      this.object['thumbs:up:user_guids'] = [
        this.session.getLoggedInUser().guid,
      ];
      this.object['thumbs:up:count']++;
    } else {
      for (let key in this.object['thumbs:up:user_guids']) {
        if (
          this.object['thumbs:up:user_guids'][key] ===
          this.session.getLoggedInUser().guid
        )
          delete this.object['thumbs:up:user_guids'][key];
      }
      this.object['thumbs:up:count']--;
    }

    this.activityService.entity$.next(this.object);

    this.cd.detectChanges();
  }
<commit_msg>(fix): Fixing bug with activity service update on upvotes minds/front#5665<commit_after>import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  Input,
  ViewChild,
} from '@angular/core';

import { Session } from '../../../services/session';
import { Client } from '../../../services/api';
import { AuthModalService } from '../../../modules/auth/modal/auth-modal.service';
import { ExperimentsService } from '../../../modules/experiments/experiments.service';
import { FriendlyCaptchaComponent } from '../../../modules/captcha/friendly-catpcha/friendly-captcha.component';
import { FormToastService } from '../../services/form-toast.service';
import { ActivityService } from '../../../modules/newsfeed/activity/activity.service';

@Component({
  selector: 'minds-button-thumbs-up',
  inputs: ['_object: object'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: 'thumbs-up.component.html',
  styleUrls: [`thumbs-up.component.ng.scss`],
})
export class ThumbsUpButton {
  changesDetected: boolean = false;
  object = {
    guid: null,
    owner_guid: null,
    'thumbs:up:user_guids': [],
  };

  /**
   * Will display the friendly captcha component and complete puzzle if true
   */
  public showFriendlyCaptcha = false;

  /**
   * In progress state, eg. captcha working or api saving
   */
  @Input() inProgress = false;

  @Input() iconOnly = false;

  @ViewChild(FriendlyCaptchaComponent)
  friendlyCaptchaEl: FriendlyCaptchaComponent;

  constructor(
    public session: Session,
    public client: Client,
    private authModal: AuthModalService,
    private cd: ChangeDetectorRef,
    private experiments: ExperimentsService,
    private toast: FormToastService,
    private activityService: ActivityService
  ) {}

  set _object(value: any) {
    if (!value) return;
    this.object = value;
    if (!this.object['thumbs:up:user_guids'])
      this.object['thumbs:up:user_guids'] = [];
  }

  /**
   * Called when a mouse click / tap is made
   * @param e
   * @returns void
   */
  onClick(e: MouseEvent): void {
    if (this.isFriendlyCaptchaFeatureEnabled() && !this.has()) {
      this.showFriendlyCaptcha = true;
      this.inProgress = true;
    } else {
      this.submit();
    }
  }

  /**
   * Called when friendly captcha returns value
   * @param solution
   */
  onFriendlyCaptchaComplete(solution: string): void {
    this.submit(solution);
  }

  /**
   * Submits the vote (or cancels it)
   * @param solution
   * @returns Promise<void>
   */
  async submit(solution?: string): Promise<void> {
    this.inProgress = true;
    if (!this.session.isLoggedIn()) {
      const user = await this.authModal.open();
      if (!user) return;
    }
    let data = {};

    if (this.isFriendlyCaptchaFeatureEnabled()) {
      data = {
        puzzle_solution: solution,
      };
    }

    try {
      await this.client.put('api/v1/thumbs/' + this.object.guid + '/up', data);
    } catch (e) {
      this.toast.error(e?.message ?? 'An unknown error has occurred');
    }

    this.showFriendlyCaptcha = false;
    this.inProgress = false;

    if (!this.has()) {
      this.object['thumbs:up:user_guids'] = [
        this.session.getLoggedInUser().guid,
      ];
      this.object['thumbs:up:count']++;
    } else {
      for (let key in this.object['thumbs:up:user_guids']) {
        if (
          this.object['thumbs:up:user_guids'][key] ===
          this.session.getLoggedInUser().guid
        )
          delete this.object['thumbs:up:user_guids'][key];
      }
      this.object['thumbs:up:count']--;
    }

    if (
      this.object['type'] === 'activity' ||
      this.object['type'] === 'object'
    ) {
      this.activityService.entity$.next(this.object);
    }

    this.cd.detectChanges();
  }

{'fraction_non_alphanumeric': 0.1502520092630432, 'fraction_numerical': 0.0008173273395995096, 'mean_word_length': 2.953688745288099, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1367963', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2290, 'n_words': 474}