{"size": 1398, "ext": "js", "max_stars_repo_path": "src/client/networking/websocket/handlers/SUBSCRIBER_UPDATE.js", "max_stars_repo_name": "dawalters1/wolf.js", "max_stars_repo_stars_event_min_datetime": "2021-07-22T18:30:44.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T17:45:45.000Z", "max_issues_repo_path": "src/client/networking/websocket/handlers/SUBSCRIBER_UPDATE.js", "max_issues_repo_name": "dawalters1/wolf.js", "max_issues_repo_issues_event_min_datetime": "2021-06-08T00:10:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T19:14:26.000Z", "max_forks_repo_path": "src/client/networking/websocket/handlers/SUBSCRIBER_UPDATE.js", "max_forks_repo_name": "dawalters1/wolf.js", "max_forks_repo_forks_event_min_datetime": "2021-06-10T15:59:34.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-08T11:06:09.000Z"}
{"max_stars_count": 4, "max_issues_count": 35, "max_forks_count": 3, "avg_line_length": 29.125, "max_line_length": 149, "alphanum_fraction": 0.6559370529}
const SubscriberObject = require('../../../../models/SubscriberObject');
const { Events } = require('../../../../constants');
const patch = require('../../../../utils/Patch');

module.exports = async (api, body) => {
  let subscriber = await api.subscriber()._subscribers.find((subscriber) => subscriber.id === body.id);

  if (!subscriber || subscriber.hash === body.hash) {
    return Promise.resolve();
  }

  const old = new SubscriberObject(api, Object.assign({}, subscriber));

  subscriber = await api.subscriber().getById(body.id, true);

  if (subscriber.id === api.currentSubscriber.id) {
    api._currentSubscriber = subscriber;
  }

  const groups = await api.group()._groups.filter((group) => group.subscribers && group.subscribers.some((subscriber) => subscriber.id === body.id));

  groups.forEach(group => {
    const subscriber = group.subscribers.find((subscriber) => subscriber.id === body.id);

    if (subscriber) {
      patch(subscriber.additionalInfo, subscriber);
    }
  });

  const contact = await api.contact()._contacts.find((contact) => contact.id === body.id);

  if (contact) {
    patch(contact.additionalInfo, subscriber);
  }

  const blocked = api.blocked()._blocked.find((blocked) => blocked.id === body.id);

  if (blocked) {
    patch(blocked.additionalInfo, subscriber);
  }

  return api.emit(
    Events.SUBSCRIBER_UPDATE,
    old,
    subscriber
  );
};
