{"size": 992, "ext": "js", "max_stars_repo_path": "imports/api/meetings/server.js", "max_stars_repo_name": "LucasLeandro1204/meteor-chat", "max_stars_repo_stars_event_min_datetime": "2019-08-28T22:07:55.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-04T14:30:30.000Z", "max_issues_repo_path": "imports/api/meetings/server.js", "max_issues_repo_name": "LucasLeandro1204/meteor-chat", "max_issues_repo_issues_event_min_datetime": "2018-08-30T14:44:06.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-30T14:59:14.000Z", "max_forks_repo_path": "imports/api/meetings/server.js", "max_forks_repo_name": "LucasLeandro1204/meteor-chat", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 21.1063829787, "max_line_length": 76, "alphanum_fraction": 0.6058467742}
import Meetings from './';
import { Meteor } from 'meteor/meteor';
import {
  // isUserAllowed,
  findOrCreateBySlug as getMeeting
} from './methods';
import {
  deleteByConnection,
  updateOrCreate as addUser
} from '/imports/api/users/methods';

Meteor.methods({
  /**
   * Find or create the meeting.
   * User should already be "registered". If not for some reason, create it.
   *
   * @returns {?string}
   */
  async meeting (slug, name) {
    try {
      const id = await getMeeting(slug);

      /**
       * (Maybe) user can join another meeting having the same connection.
       */
      await addUser({
        meetingId: id,
        connectionId: this.connection.id,
      }, {
        name: name || 'Anonymous',
      });

      return id;
    } catch (e) {
      // throw error for unauthorized or locked meeting.
    }

    return null;
  },
});

Meteor.onConnection((connection) => {
  connection.onClose(async () => {
    await deleteByConnection(connection.id);
  });
});
