|
| 1 | +import Component from '@ember/component'; |
| 2 | +import { action, computed } from '@ember/object'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import classic from 'ember-classic-decorator'; |
| 5 | +import FormMixin from 'open-event-frontend/mixins/form'; |
| 6 | +import { protocolLessValidUrlPattern } from 'open-event-frontend/utils/validators'; |
| 7 | +import { allSettled } from 'rsvp'; |
| 8 | + |
| 9 | + |
| 10 | +@classic |
| 11 | +export default class VideoroomForm extends Component.extend(FormMixin) { |
| 12 | + @tracked jitsiButtonLoading = false; |
| 13 | + @tracked loading = false; |
| 14 | + |
| 15 | + @computed('data.stream.rooms.[]') |
| 16 | + get room() { |
| 17 | + return this.data.stream.rooms.toArray()[0]; |
| 18 | + } |
| 19 | + |
| 20 | + @action |
| 21 | + setRoom(room) { |
| 22 | + this.data.stream.rooms = [room]; |
| 23 | + this.data.stream.name = room.name; |
| 24 | + } |
| 25 | + |
| 26 | + getValidationRules() { |
| 27 | + window.$.fn.form.settings.rules.checkVideoRoomsLength = () => { |
| 28 | + return this.data.stream.rooms.length > 0; |
| 29 | + }; |
| 30 | + const validationRules = { |
| 31 | + inline : true, |
| 32 | + delay : false, |
| 33 | + on : 'blur', |
| 34 | + |
| 35 | + fields: { |
| 36 | + name: { |
| 37 | + rules: [ |
| 38 | + { |
| 39 | + type : 'empty', |
| 40 | + prompt : this.l10n.t('Please enter a name') |
| 41 | + } |
| 42 | + ] |
| 43 | + }, |
| 44 | + url: { |
| 45 | + rules: [ |
| 46 | + { |
| 47 | + type : 'empty', |
| 48 | + prompt : this.l10n.t('Please enter a url') |
| 49 | + }, |
| 50 | + { |
| 51 | + type : 'regExp', |
| 52 | + value : protocolLessValidUrlPattern, |
| 53 | + prompt : this.l10n.t('Please enter a valid url') |
| 54 | + } |
| 55 | + ] |
| 56 | + }, |
| 57 | + rooms: { |
| 58 | + rules: [ |
| 59 | + { |
| 60 | + type : 'checkVideoRoomsLength', |
| 61 | + prompt : this.l10n.t('Please select a room') |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + return validationRules; |
| 69 | + } |
| 70 | + |
| 71 | + get randomIdentifier() { |
| 72 | + return Math.random().toString(36).replace(/[^a-z]+/g, ''); |
| 73 | + } |
| 74 | + |
| 75 | + generateMeetingInformation(phoneNumbers, pin) { |
| 76 | + return `To join your meeting, dial one of these numbers and then enter the pin.\n\nPIN: ${pin}\n\n` |
| 77 | + + Object.entries(phoneNumbers).map(([country, numbers]) => `${country}: ${numbers.join(', ')}\n`).join(''); |
| 78 | + } |
| 79 | + |
| 80 | + @action |
| 81 | + async addJitsi() { |
| 82 | + const { event } = this.data; |
| 83 | + const { id, name } = this.data.stream; |
| 84 | + const identifier = [event.identifier, 'stream', name?.replace(/[^a-z0-9\.]/gi, '')?.toLowerCase(), id ?? this.randomIdentifier].filter(Boolean).join('-'); |
| 85 | + |
| 86 | + this.data.stream.set('url', 'https://meet.jit.si/eventyay/' + identifier); |
| 87 | + |
| 88 | + this.jitsiButtonLoading = true; |
| 89 | + |
| 90 | + try { |
| 91 | + const [phoneNumbers, pin] = (await allSettled([ |
| 92 | + this.loader.load(`https://api.jitsi.net/phoneNumberList?conference=${identifier}@conference.eventyay.meet.jit.si`, { isExternal: true }), |
| 93 | + this.loader.load(`https://api.jitsi.net/conferenceMapper?conference=${identifier}@conference.eventyay.meet.jit.si`, { isExternal: true }) |
| 94 | + ])).map(promise => promise.value); |
| 95 | + |
| 96 | + this.data.stream.additionalInformation = this.generateMeetingInformation(phoneNumbers.numbers, pin.id); |
| 97 | + } catch (e) { |
| 98 | + this.notify.error(this.l10n.t('An unexpected error has occurred.')); |
| 99 | + } |
| 100 | + |
| 101 | + this.jitsiButtonLoading = false; |
| 102 | + } |
| 103 | + |
| 104 | + @action |
| 105 | + submit(event) { |
| 106 | + event.preventDefault(); |
| 107 | + this.onValid(async() => { |
| 108 | + try { |
| 109 | + this.loading = true; |
| 110 | + await this.data.stream.save(); |
| 111 | + this.notify.success(this.l10n.t('Your stream has been saved'), |
| 112 | + { |
| 113 | + id: 'stream_save' |
| 114 | + }); |
| 115 | + this.router.transitionTo('events.view.videoroom', this.data.event.id); |
| 116 | + } catch (e) { |
| 117 | + console.error('Error while saving session', e); |
| 118 | + this.notify.error(this.l10n.t('Oops something went wrong. Please try again'), |
| 119 | + { |
| 120 | + id: 'stream_save_error' |
| 121 | + }); |
| 122 | + } finally { |
| 123 | + this.loading = false; |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments