<commit_before>import {isEmpty} from 'lodash';

import StatsUtil from '../stats/util';
import Metrics from '../metrics';
import {eventType, trigger, mediaType, error} from '../metrics/config';
import Media from '../media';
import LoggerProxy from '../common/logs/logger-proxy';
import WebRTCStats from '../stats/index';
import {
  INTENT_TO_JOIN,
  MODERATOR_TRUE,
  MODERATOR_FALSE,
  _JOINED_,
  ICE_STATE,
  STATS,
  EVENT_TRIGGERS
} from '../constants';
import Trigger from '../common/events/trigger-proxy';
import IntentToJoinError from '../common/errors/intent-to-join';
import JoinMeetingError from '../common/errors/join-meeting';
import ParameterError from '../common/errors/parameter';
import PermissionError from '../common/errors/permission';

const MeetingUtil = {};

MeetingUtil.parseLocusJoin = (response) => {
  const parsed = {};

  // First todo: add check for existance
  parsed.locus = response.body.locus;
  parsed.mediaConnections = response.body.mediaConnections;
  parsed.locusUrl = parsed.locus.url;
  parsed.locusId = parsed.locus.url.split('/').pop();
  parsed.selfId = parsed.locus.self.id;

  // we need mediaId before making roap calls
  parsed.mediaConnections.forEach((mediaConnection) => {
    if (mediaConnection.mediaId) {
      parsed.mediaId = mediaConnection.mediaId;
    }
  });

  return parsed;
};

MeetingUtil.remoteUpdateAudioVideo = (audioMuted, videoMuted, meeting) => {
  if (!meeting) {
    return Promise.reject(new ParameterError('You need a meeting object.'));
  }
  const localMedias = Media.generateLocalMedias(meeting.mediaId, audioMuted, videoMuted);

  if (isEmpty(localMedias)) {
    return Promise.reject(new ParameterError('You need a media id on the meeting to change remote audio.'));
  }

  Metrics.postEvent({event: eventType.MEDIA_REQUEST, meeting});

  return meeting.meetingRequest.remoteAudioVideoToggle({
    locusUrl: meeting.locusUrl,
    selfId: meeting.selfId,
    localMedias,
    deviceUrl: meeting.deviceUrl,
    correlationId: meeting.correlationId
  }).then((response) => {
    Metrics.postEvent({event: eventType.MEDIA_RESPONSE, meeting});
    meeting.locusInfo.onFullLocus(response.body.locus);
  });
};

MeetingUtil.checkShare = (meeting) => {
  if ((meeting.wirelessShare && meeting.isSharing) || meeting.isSharing) {
    return meeting.stopShare();
  }

  return Promise.resolve();
};

// TODO: have locus ignore hostPin if they find out it's my claimed PMR
/*
for my claimed PMR
passing an "undefined" hostPin was causing the issue.
if I don't include hostPin in the first request, locus figures out it's my PMR
and sends a 200 after the first request
so we must clean up the hostPin to stop the double request
*/
MeetingUtil.cleanOptions = (options, info, owner, userId) => {
  if (MeetingUtil.hasOwner(info)) {
    if (MeetingUtil.isOwnerSelf(owner, userId)) {
      if (options.hostPin) {
        delete options.hostPin;
      }
      options.moderator = MODERATOR_TRUE;
    }
    else {
      options.moderator = MODERATOR_FALSE;
    }
  }

  return options;
};

MeetingUtil.hasOwner = (info) => info && info.owner;

MeetingUtil.isOwnerSelf = (owner, selfId) => owner === selfId;

MeetingUtil.isPinOrGuest = (err) => {
  if (err && err.body && err.body.errorCode === INTENT_TO_JOIN) {
    return true;
  }

  return false;
};<commit_msg>fix(meeting): enforce moderator as true only if user sets hostPin<commit_after>import {isEmpty} from 'lodash';

import StatsUtil from '../stats/util';
import Metrics from '../metrics';
import {eventType, trigger, mediaType, error} from '../metrics/config';
import Media from '../media';
import LoggerProxy from '../common/logs/logger-proxy';
import WebRTCStats from '../stats/index';
import {
  INTENT_TO_JOIN,
  MODERATOR_TRUE,
  _JOINED_,
  ICE_STATE,
  STATS,
  EVENT_TRIGGERS
} from '../constants';
import Trigger from '../common/events/trigger-proxy';
import IntentToJoinError from '../common/errors/intent-to-join';
import JoinMeetingError from '../common/errors/join-meeting';
import ParameterError from '../common/errors/parameter';
import PermissionError from '../common/errors/permission';

const MeetingUtil = {};

MeetingUtil.parseLocusJoin = (response) => {
  const parsed = {};

  // First todo: add check for existance
  parsed.locus = response.body.locus;
  parsed.mediaConnections = response.body.mediaConnections;
  parsed.locusUrl = parsed.locus.url;
  parsed.locusId = parsed.locus.url.split('/').pop();
  parsed.selfId = parsed.locus.self.id;

  // we need mediaId before making roap calls
  parsed.mediaConnections.forEach((mediaConnection) => {
    if (mediaConnection.mediaId) {
      parsed.mediaId = mediaConnection.mediaId;
    }
  });

  return parsed;
};

MeetingUtil.remoteUpdateAudioVideo = (audioMuted, videoMuted, meeting) => {
  if (!meeting) {
    return Promise.reject(new ParameterError('You need a meeting object.'));
  }
  const localMedias = Media.generateLocalMedias(meeting.mediaId, audioMuted, videoMuted);

  if (isEmpty(localMedias)) {
    return Promise.reject(new ParameterError('You need a media id on the meeting to change remote audio.'));
  }

  Metrics.postEvent({event: eventType.MEDIA_REQUEST, meeting});

  return meeting.meetingRequest.remoteAudioVideoToggle({
    locusUrl: meeting.locusUrl,
    selfId: meeting.selfId,
    localMedias,
    deviceUrl: meeting.deviceUrl,
    correlationId: meeting.correlationId
  }).then((response) => {
    Metrics.postEvent({event: eventType.MEDIA_RESPONSE, meeting});
    meeting.locusInfo.onFullLocus(response.body.locus);
  });
};

MeetingUtil.checkShare = (meeting) => {
  if ((meeting.wirelessShare && meeting.isSharing) || meeting.isSharing) {
    return meeting.stopShare();
  }

  return Promise.resolve();
};

// TODO: have locus ignore hostPin if they find out it's my claimed PMR
/*
for my claimed PMR
passing an "undefined" hostPin was causing the issue.
if I don't include hostPin in the first request, locus figures out it's my PMR
and sends a 200 after the first request
so we must clean up the hostPin to stop the double request
*/
MeetingUtil.cleanOptions = (options, info, owner, userId) => {
  if (MeetingUtil.hasOwner(info)) {
    if (MeetingUtil.isOwnerSelf(owner, userId)) {
      if (options.hostPin) {
        delete options.hostPin;
        options.moderator = MODERATOR_TRUE;
      }
    }
  }

  return options;
};

MeetingUtil.hasOwner = (info) => info && info.owner;

MeetingUtil.isOwnerSelf = (owner, selfId) => owner === selfId;

MeetingUtil.isPinOrGuest = (err) => {
  if (err && err.body && err.body.errorCode === INTENT_TO_JOIN) {
    return true;
  }

  return false;
};
{'fraction_non_alphanumeric': 0.12792629512158285, 'fraction_numerical': 0.0009062075215224287, 'mean_word_length': 4.039573820395738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '7150224', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2061, 'n_words': 527}