Skip to content

Instantly share code, notes, and snippets.

@goto-bus-stop
Last active May 25, 2017 17:19
Show Gist options
  • Save goto-bus-stop/b135baa3b2daa74d130eb12b5e265f38 to your computer and use it in GitHub Desktop.
Save goto-bus-stop/b135baa3b2daa74d130eb12b5e265f38 to your computer and use it in GitHub Desktop.
miniplug, but minified using babili.
import{EventEmitter as a}from'events';import b from'debug';import c from'create-error-class';import d from'bluebird';import{unescape as e}from'plug-message-split';import f from'got';import g from'plug-login';import h from'plug-socket';function i(a){return(b=[]).concat.apply(b,a);var b}function j(a,b){return void 0===b&&(b='id'),'object'==typeof a?a[b]:a}function k(a,b){void 0===b&&(b='id');const c=Array.isArray(medias)?medias:[medias];return c.map((a)=>j(a,b))}function l(a){return new Date(`${a} UTC`)}function m(a){for(var b=[],c=arguments.length-1;0<c--;)b[c]=arguments[c+1];return function(){for(var c=[],d=arguments.length;d--;)c[d]=arguments[d];return a.apply(void 0,b.concat(c))}}const n={NONE:0,DJ:1,BOUNCER:2,MANAGER:3,COHOST:4,HOST:5,AMBASSADOR:3,ADMIN:5},o={YOUTUBE:1,SOUNDCLOUD:2},p={HOUR:'h',DAY:'d',PERMA:'f'},q={SPAMMING:1,VERBAL_ABUSE:2,OFFENSIVE_PLAYS:3,GENRE:4,ATTITUDE:5},r={SHORT:'s',MEDIUM:'m',LONG:'l'},s={VIOLATING_RULES:1,VERBAL_ABUSE:2,SPAMMING:3,LANGUAGE:4,ATTITUDE:5};var t=Object.freeze({ROLE:n,MEDIA_SOURCE:o,BAN_DURATION:p,BAN_REASON:q,MUTE_DURATION:r,MUTE_REASON:s});function u(a,b){this.message=a.body.data&&a.body.data[0]||b.message,this.status=a.body.status,this.response=a,this.cause=b}function v(a,b){u.call(this,a,b),this.message='You don\'t have enough Plug Points to unlock this.'}const w=c('RequestError',u),x=c('BadLoginError',u),y=c('NotAuthorizedError',u),z=c('NotFoundError',u),A=c('NoValidPlaylistError',u),B=c('NotEnoughPPError',v),C={requestError:w,badLogin:x,notAuthorized:y,notFound:z,noValidPlaylist:A},D={RequestError:w,BadLoginError:x,NotAuthorizedError:y,NotFoundError:z,NoValidPlaylistError:A,NotEnoughPPError:B};function E(a,b){var c=a.body,d=c.status,e=c.data;if('requestError'===d&&e&&'fundsPP'===e[0])return new B(a,b);const f=C[d];return f?new f(a,b):(u.call(b,a,b),b)}const F=b('miniplug:backoff');function G(a){function b(){g+=1,h=Math.min(h+e,f),F('queued',g,h)}function c(){g-=1,0==g&&(h=0),F('unqueued',g,h)}var e=a.increment,f=a.max;let g=0,h=0,i=d.resolve();return(a)=>function(){for(var d=[],e=arguments.length;e--;)d[e]=arguments[e];b(),F('calling args',d.length);const f=i.then(()=>a.apply(void 0,d));return i=f.return(null).catch(()=>null).delay(h).tap(c),f}}function H(a,b){const c=Object.assign({},{toJSON:()=>a},b),d=Object.create(c);return Object.assign(d,a),d}function I(a,b){return b.un&&(b.un=e(b.un)),b.id=b.cid,b.message=e(b.message),b.user=a.user(b.uid)||a.wrapUser({id:b.uid,username:b.un}),H(b,{own:()=>a.me().id===b.uid,chat:a.chat,reply:m(a.chat,`@${b.un}`),emote:m(a.chat,`/me @${b.un}`),delete:m(a.deleteChat,b.cid),getUser(){const c=a.user&&a.user(b.uid);return c?d.resolve(c):a.getUser(b.uid)}})}function J(a,b){const c=l(b.timestamp),d={id:b.id,media:a.wrapMedia(null,b.media),room:a.wrapRoom(b.room),user:a.wrapUser(b.user),time:c,timestamp:c,score:b.score};return H(d,{getUser:m(a.getUser,b.user.id)})}function K(a,b){return b.name=b.id,delete b.id,H(b,{})}function L(a,b,c){return H(c,{update:m(a.updateMedia,b,c.id),delete:()=>a.deleteMedia(b,[c.id])})}function M(a,b){if(b.id=+b.id,b.timestamp=l(b.timestamp),'levelUp'===b.action&&(b.level=+b.value),'gift'===b.action){const a=b.value.split('\u2800');b.from=a[0],b.amount=+a[1]}return'custom'===b.action&&(b.message=b.value),H(b,{acknowledge:m(a.acknowledgeNotification,b.id)})}function N(a,b){return H(b,{delete:m(a.deletePlaylist,b.id),activate:m(a.activatePlaylist,b.id),rename:m(a.renamePlaylist,b.id),shuffle:m(a.shufflePlaylist,b.id),getMedia:()=>a.getMedia(b.id).map(m(a.wrapMedia,b.id)).tap(()=>{}),insert:(c,d)=>{return void 0===d&&(d=!0),a.insertMedia(b.id,c,d)},move:(c,d)=>a.moveMedia(b.id,c,d)})}function O(a,b){return b.welcome&&(b.welcome=e(b.welcome)),b.description&&(b.description=e(b.description)),b.isFavorite=b.favorite,delete b.favorite,H(b,{getHost:m(a.getUser,b.hostID),join:m(a.join,b.slug),favorite:m(a.favoriteRoom,b.id),unfavorite:m(a.unfavoriteRoom,b.id)})}function P(a,b){return H(b,{purchase:m(a.purchase,b.id)})}function Q(a,b){b.username=b.guest?null:e(b.username||'');const c=`@${b.username}`;return H(b,{hasPermission:(a)=>b.role>=a||b.gRole>=a,hasGlobalPermission:(a)=>b.gRole>=a,chat:m(a.chat,c),emote:m(a.chat,`/me ${c}`),add:m(a.addDJ,b.id),move:m(a.moveDJ,b.id),remove:m(a.removeDJ,b.id),skip:m(a.skipDJ,b.id),befriend:m(a.befriend,b.id),rejectRequest:m(a.rejectFriendRequest,b.id),ignore:m(a.ignore,b.id),unignore:m(a.unignore,b.id),mute:m(a.mute,b.id),unmute:m(a.unmute,b.id),ban:m(a.ban,b.id),unban:m(a.unban,b.id),getHistory:m(a.getUserHistory,b.id),mention:()=>c,toString:()=>c})}class R extends Array{}function S(a,b){const c=Object.assign(new R,{contains:(a)=>c.some((b)=>b.id===j(a)),positionOf:(a)=>c.findIndex((b)=>b.id===j(a)),toJSON:()=>b});return b.forEach((b)=>{c.push(a.user(b)||a.wrapUser({id:b}))}),c}function T(){return(a)=>{Object.assign(a,{wrapMessage:m(I,a),wrapHistoryEntry:m(J,a),wrapInventoryProduct:m(K,a),wrapMedia:m(L,a),wrapNotification:m(M,a),wrapPlaylist:m(N,a),wrapRoom:m(O,a),wrapStoreProduct:m(P,a),wrapUser:m(Q,a),wrapWaitlist:m(S,a)})}}const U=b('miniplug:http');function V(a){return a=Object.assign({},{backoff:(a)=>a},a),(b)=>{const c=a.backoff((c,d)=>b.connected.tap(()=>U(d.method,c,d.body||d.query)).then((b)=>f(`${a.host}/_/${c}`,Object.assign({},{headers:{cookie:b.cookie,'content-type':'application/json'},json:!0},d,{body:d.body?JSON.stringify(d.body):void 0}))).then((a)=>{if('ok'!==a.body.status)throw new Error(a.body.data.length?a.body.data[0]:a.body.status);return a.body.data}).catch((a)=>{if(a&&a.response)throw E(a.response,a);throw a}));b.request=c,b.post=(a,b)=>c(a,{method:'post',body:b}),b.get=(a,b)=>c(a,{method:'get',query:b}),b.put=(a,b)=>c(a,{method:'put',body:b}),b.del=(a,b)=>c(a,{method:'delete',body:b})}}const W=b('miniplug:connect'),X=b('miniplug:ws');function Y(a){return void 0===a&&(a={}),(b)=>{const c={host:a.host,authToken:!0};b.connect=function(a){W('connecting',a.email);const e=d.resolve(a.email?g.user(a.email,a.password,c):g.guest(c)),f=h();f.setMaxListeners(100),f.on('action',(a,b)=>{X(a,b)});const i=e.then((a)=>new d((c,d)=>{f.auth(a.token),f.once('error',d),b.isConnected=!0,b.emit('login');const e=b.getMe();f.once('ack',()=>{c({cookie:a.cookie}),f.removeListener('error',d),e.then((a)=>b.emit('connected',a))})})).catch((a)=>{throw b.emit('error',a),a});return b.ws=f,b.connected=i,i}}}const Z=b('miniplug:users'),$=0;function _(){const a=Symbol('Guests count'),b=Symbol('Users'),c=Symbol('Me');return(e)=>{function f(c){Z('join',c.id),c.guest?(e[a]+=1,e.emit('guestJoin')):(c=e.wrapUser(c),e[b].push(c),e.emit('userJoin',c))}function g(c){if(Z('leave',c),c===$)0<e[a]&&(e[a]-=1),e.emit('guestLeave');else{const a=e[b].findIndex((a)=>a.id===c);if(-1!==a){const c=e[b][a];e[b].splice(a,1),e.emit('userLeave',c)}}}function h(a){const b=e.user(a.i);if(b){delete a.i;const c=Object.keys(a).reduce((a,c)=>{return a[c]=b[c],a},{});Object.assign(b,a),e.emit('userUpdate',b,c)}}function j(a){a.u.forEach((b)=>{h({i:b.i,role:b.p}),e.emit('modStaff',{moderator:e.user(a.mi)||e.wrapUser({id:a.mi,username:a.m}),user:e.user(b.i)||e.wrapUser({id:b.i,username:b.n}),role:b.p})})}e[a]=0,e[b]=[],e.on('connected',(a)=>{e[c]=e.wrapUser(a),e.ws.on('userJoin',f),e.ws.on('userLeave',g),e.ws.on('userUpdate',h),e.ws.on('modStaff',j)}),e.on('roomState',(c)=>{var d=c.users,f=c.meta.guests;e[b]=d.map(e.wrapUser),e[a]=f});const k=(a)=>(d)=>{return e[c]&&d===e[c][a]?e[c]:e[b].find((b)=>b[a]===d)},l=k('id'),n=k('username'),o=m(e.put,'users/settings'),p=m(e.get,'users/me/transactions');Object.assign(e,{me:()=>e[c],user:l,userByName:n,users:()=>e[b],guests:()=>e[a],getMe:()=>e.get('users/me').get(0).then(e.wrapUser),getUser:(a)=>e.get(`users/${a}`).get(0).then(e.wrapUser),getUsers:()=>{for(var a=[],b=arguments.length;b--;)a[b]=arguments[b];return e.post('users/bulk',{ids:i(a)}).map(e.wrapUser)},saveSettings:o,setAvatar:(a)=>e.put('users/avatar',{id:a}),setBadge:(a)=>e.put('users/badge',{id:a}),setBlurb:(a)=>e.put('profile/blurb',{blurb:a}),setLanguage:(a)=>e.put('users/language',{language:a}),getTransactions:p,validateUsername:(a)=>e.get(`users/validate/${encodeURIComponent(a)}`).get(0).catch(()=>d.reject(new Error('Username unavailable.')))})}}function aa(){return(a)=>{const b=m(a.get,'ignores');Object.assign(a,{getIgnoredUsers:b,ignore:(b)=>a.post('ignores',{id:b}).get(0),unignore:(b)=>a.del(`ignores/${b}`)})}}function ba(){return(a)=>{function b(b){a.emit('modMute',{moderator:a.user(b.mi)||a.wrapUser({id:b.mi,username:b.m}),user:a.user(b.i)||a.wrapUser({id:b.i,username:b.t}),reason:b.r,duration:b.d})}const c=m(a.get,'mutes');Object.assign(a,{getMutes:c,mute:(b,c,d)=>{return void 0===c&&(c=r.SHORT),void 0===d&&(d=s.VIOLATING_RULES),a.post('mutes',{userID:b,duration:c,reason:d})},unmute:(b)=>a.del(`mutes/${b}`)}),a.on('connected',()=>{a.ws.on('modMute',b)})}}function ca(){return(a)=>{function b(b){a.emit('ban',{duration:b.d,reason:b.r})}function c(b){a.emit('modBan',{moderator:a.user(b.mi)||a.wrapUser({id:b.mi}),username:b.t,duration:b.d})}const d=m(a.get,'bans');Object.assign(a,{getBans:d,ban:(b,c,d)=>{return void 0===c&&(c=p.HOUR),void 0===d&&(d=q.SPAMMING),a.post('bans/add',{userID:b,reason:d,duration:c}).get(0)},unban:(b)=>a.del(`bans/${b}`)}),a.on('connected',()=>{a.ws.on('ban',b),a.ws.on('modBan',c)})}}function da(){const a=Symbol('Notifications');return(b)=>{function c(c){b[a].push(c),b.emit('notify',b.wrapNotification(c))}function d(a){b.emit('earn',{xp:a.xp,pp:a.pp,level:a.level})}b[a]=[],b.on('connected',(e)=>{b[a]=e&&e.notifications||[],b.ws.on('notify',c),b.ws.on('earn',d)}),Object.assign(b,{notifications:()=>b[a].map(b.wrapNotification),getNotifications:()=>b.getMe().then((a)=>a.notifications||[]).tap((c)=>{b[a]=c}).map(b.wrapNotification),acknowledgeNotification:(c)=>b.del(`notifications/${c}`).tap(()=>{b[a]=b[a].filter((a)=>a.id!==+c)})})}}function ea(a){void 0===a&&(a={});const b=Symbol('History entry');return(a)=>{function c(c){const e=d();if(!c||!c.m)return a[b]=null,void a.emit('advance',null,e);var f=c.h,g=c.m,h=c.c,i=c.p,j=c.t;j=l(j),a[b]={id:f,user:a.user(h)||a.wrapUser({id:h}),media:g,playlistId:i,time:j,timestamp:j},a[b].dj=a[b].user,a.emit('advance',d(),e)}const d=()=>a[b];a.on('roomState',(c)=>{const d=l(c.playback.startTime);a[b]={id:c.playback.historyID,dj:a.user(c.booth.currentDJ),media:c.playback.media,playlistId:c.playback.playlistID,time:d,timestamp:d}}),a.on('connected',()=>{a.ws.on('advance',c)}),Object.assign(a,{skipDJ:(b,c)=>a.post('booth/skip',{userID:b,historyID:c}),skipMe:m(a.post,'booth/skip/me')}),Object.assign(a,{historyEntry:d,dj:()=>a[b]?a[b].dj:null,media:()=>a[b]?a[b].media:null})}}const fa=b('miniplug:waitlist');function ga(){const a=Symbol('Waitlist');return(b)=>{function c(c){fa('update',c);const d=b.waitlist();b[a]=b.wrapWaitlist(c),b.emit('waitlistUpdate',b.waitlist(),d)}function d(a){a&&a.d?c(a.d):c([])}function e(a){b.emit('modAddDj',{moderator:b.user(a.mi)||b.wrapUser({id:a.mi}),username:a.t,cycle:a.m})}function f(a){b.emit('modMoveDj',{moderator:b.user(a.mi)||b.wrapUser({id:a.mi}),username:a.u,movedFrom:a.o,movedTo:a.n})}function g(a){b.emit('modRemoveDj',{moderator:b.user(a.mi)||b.wrapUser({id:a.mi}),username:a.t,inBooth:a.d})}function h(a){b.emit('modSkip',b.user(a.mi)||b.wrapUser({id:a.mi,username:a.m}))}function i(a){var c=a.f,d=a.mi;b.emit('waitlistCycle',{shouldCycle:c,user:b.user(d)})}function j(a){var d=a.f,e=a.c,g=a.mi;const h=b.user(g);b.emit('waitlistLock',{locked:d,cleared:!!e,user:h}),e&&b.emit('waitlistClear',{user:h})}function k(a){b.emit('skip',b.user(a)||b.wrapUser({id:a}))}b[a]=b.wrapWaitlist([]),b.on('roomState',(c)=>{b[a]=b.wrapWaitlist(c.booth.waitingDJs),b.emit('waitlistUpdate',b.waitlist(),[])}),b.on('connected',()=>{b.ws.on('djListUpdate',c),b.ws.on('advance',d),b.ws.on('modAddDJ',e),b.ws.on('modMoveDJ',f),b.ws.on('modRemoveDJ',g),b.ws.on('modSkip',h),b.ws.on('djListCycle',i),b.ws.on('djListLocked',j),b.ws.on('skip',k)});const l=(a)=>{return void 0===a&&(a=!0),b.put('booth/cycle',{shouldCycle:a})};Object.assign(b,{waitlist:()=>b[a],joinWaitlist:m(b.post,'booth'),leaveWaitlist:m(b.del,'booth'),setCycle:l,enableCycle:m(l,!0),disableCycle:m(l,!1),setLock:(a,c)=>{return void 0===a&&(a=!0),void 0===c&&(c=!1),b.put('booth/lock',{isLocked:a,removeAllDJs:c})},lockWaitlist:(a)=>{return void 0===a&&(a=!1),b.setLock(!0,a)},unlockWaitlist:()=>b.setLock(!1,!1),addDJ:(a)=>b.post('booth/add',{id:a}),moveDJ:(a,c)=>b.post('booth/move',{userID:a,position:c}),removeDJ:(a)=>b.del(`booth/remove/${a}`)})}}function ha(){return(a)=>{const b=()=>a.get('users/me/history').map(a.wrapHistoryEntry),c=(b)=>a.get(`profile/${b}`).get(0),d=(b)=>c(b).get('history').map(a.wrapHistoryEntry);Object.assign(a,{getRoomHistory:()=>a.get('rooms/history').map(a.wrapHistoryEntry),getUserHistory:(c)=>{return void 0===c&&(c=a.me().id),a.me().id===c?b():d(c)}})}}const ia=b('miniplug:chat');function ja(a){return a=Object.assign({},{timeout:7e3,backoff:(a)=>a},a),(b)=>{function c(a){ia('chat',a.uid,a.un,a.message),b.emit('chat',b.wrapMessage(a))}function e(a){var d=a.c,e=a.mi;ia('chatDelete',e,d),b.emit('chatDelete',{cid:d,user:b.user(e)})}function f(a){b.emit('gifted',{sender:a.s,recipient:a.r})}b.on('connected',()=>{b.ws.on('chat',c),b.ws.on('chatDelete',e),b.ws.on('gifted',f)});const g=a.backoff(function(){for(var c=[],e=arguments.length;e--;)c[e]=arguments[e];const f=b.ws,g=c.join(' ');return ia('send',g),f.chat(g),new d((c,d)=>{const e=setTimeout(()=>{b.removeListener('chat',f),d(new Error('Waiting for chat message to come back timed out.'))},a.timeout),f=(a)=>{ia('is same?',a.own(),g,a.message),a.own()&&a.message===g&&(b.removeListener('chat',f),clearTimeout(e),c(a))};b.on('chat',f)})}),h=m(g,'/me');Object.assign(b,{deleteChat:(a)=>b.del(`chat/${a}`),chat:g,emote:h})}}function ka(){return(a)=>{function b(b){f().each((c)=>{e(c.username)===e(b)&&a.emit('friendRequest',a.wrapUser(c))})}function c(b){d().each((c)=>{c.username===e(b)&&a.emit('friendAccept',c)})}a.on('connected',()=>{a.ws.on('friendRequest',b),a.ws.on('friendAccept',c)});const d=()=>a.get('friends').map(a.wrapUser),f=m(a.get,'friends/invites');Object.assign(a,{getFriends:d,befriend:(b)=>a.post('friends',{id:b}),unfriend:(b)=>a.del(`friends/${b}`),getFriendRequests:f,rejectFriendRequest:(b)=>a.put('friends/ignore',{id:b})})}}const la=b('miniplug:rooms');function ma(){const a=Symbol('Current room');return(b)=>{function c(c,d,e){b.ws.on(c,(f,g)=>{const h=b.user(f.u),i=f[d];la(c,h&&h.id,i),b[a]&&b[a].slug===g&&(b[a][e]=i),b.emit(c,i,h),b.emit('roomUpdate',{[e]:i},h)})}b[a]=null,b.on('roomState',(c)=>{b[a]=b.wrapRoom(c.meta)}),b.on('connected',()=>{c('roomNameUpdate','n','name'),c('roomDescriptionUpdate','d','description'),c('roomWelcomeUpdate','w','welcome'),c('roomMinChatLevelUpdate','m','minChatLevel')});const e=()=>b[a],f=()=>b.get('rooms/state').get(0).tap(b.emit.bind(b,'roomState')),g=(a)=>{return e()?b.post('rooms/update',a):d.reject(new Error('You are not currently in a room.'))};Object.assign(b,{room:e,getRooms:(a,c,d)=>{return void 0===a&&(a=''),void 0===c&&(c=0),void 0===d&&(d=50),b.get('rooms',{q:a,page:c,limit:d}).map(b.wrapRoom)},getFavorites:(a,c,d)=>{return void 0===a&&(a=''),void 0===c&&(c=0),void 0===d&&(d=50),b.get('rooms/favorites',{q:a,page:c,limit:d}).map(b.wrapRoom)},getMyRooms:()=>b.get('rooms/me').map(b.wrapRoom),createRoom:(a,c)=>{return void 0===c&&(c=!1),b.post('rooms',{name:a,private:c}).get(0)},validateRoomName:(a)=>b.get(`rooms/validate/${encodeURIComponent(a)}`).get(0).catch(()=>d.reject(new Error('Room name unavailable.'))),setRoomWelcome:(a)=>g({welcome:a}),setRoomDescription:(a)=>g({description:a}),setRoomName:(a)=>g({name:a}),setRoomMinChatLevel:(a)=>g({minChatLevel:a}),favoriteRoom:(a)=>b.post('rooms/favorites',{id:a}),unfavoriteRoom:(a)=>b.del(`rooms/favorites/${a}`),join:(a)=>b.post('rooms/join',{slug:a}).then(f),getRoomState:f})}}function na(){return(a)=>{a.on('connected',()=>{a.ws.on('playlistCycle',(b)=>{a.emit('playlistCycle',b)})}),Object.assign(a,{getPlaylists:()=>a.get('playlists').map(a.wrapPlaylist),getActivePlaylist:()=>a.get('playlists').filter((a)=>a.active).get(0).then(a.wrapPlaylist),createPlaylist:(b)=>a.post('playlists',{name:b}),deletePlaylist:(b)=>a.del(`playlists/${b}`),activatePlaylist:(b)=>a.put(`playlists/${b}/activate`),renamePlaylist:(b,c)=>a.put(`playlists/${b}/rename`,{name:c}),shufflePlaylist:(b)=>a.put(`playlists/${b}/shuffle`),getMedia:(b)=>a.get(`playlists/${b}/media`).map(m(a.wrapMedia,b)),updateMedia:(b,c,d,e)=>a.put(`playlists/${b}/media/update`,{id:c,author:d,title:e}).get(0),moveMedia:(b,c,d)=>a.put(`playlists/${b}/media/move`,{ids:k(c),beforeID:j(d)}),insertMedia:(b,c,d)=>{return void 0===d&&(d=!0),a.post(`playlists/${b}/media/insert`,{media:c,append:d})},deleteMedia:(b,c)=>a.post(`playlists/${b}/media/delete`,{ids:k(c)})})}}function oa(){return(a)=>{const b=(b,c)=>{return void 0===c&&(c='all'),a.get(`store/products/${b}/${c}`).map(a.wrapStoreProduct)},c=m(b,'avatars'),e=m(b,'badges'),f=m(b,'misc'),g=(b)=>a.get(`store/inventory/${b}`).map(a.wrapInventoryProduct),h=m(g,'avatars'),i=m(g,'badges'),k=()=>f('username').get(0).get('id');Object.assign(a,{getProducts:b,getStoreAvatars:c,getStoreBadges:e,getStoreMisc:f,getInventory:g,getOwnedAvatars:h,getOwnedBadges:i,purchase:(b)=>a.post('store/purchase',{id:j(b)}).get(0),purchaseNameChange:(b)=>d.props({id:k(),username:b}).then(m(a.post,'store/purchase/username'))})}}function pa(){return(a)=>{function b(b){a.emit('systemMessage',b)}function c(b){a.emit('maintenanceAlert',b)}function d(){a.emit('maintenance')}function e(){a.emit('systemUpdate')}a.on('connected',()=>{a.ws.on('plugMessage',b),a.ws.on('plugMaintenanceAlert',c),a.ws.on('plugMaintenance',d),a.ws.on('plugUpdate',e)})}}function qa(a){void 0===a&&(a={});const b=Symbol('Votes'),c=Symbol('Grabs');return function(a){function d(c){var d=c.i,e=c.v;a[b].push({uid:d,vote:e});const f=a.user(d)||a.wrapUser({id:d});a.emit('vote',{user:f,vote:e})}function e(b){a[c].push(b);const d=a.user(b);a.emit('grab',d)}a[b]=[],a[c]=[],a.on('advance',()=>{a[b]=[],a[c]=[]}),a.on('connected',()=>{a.ws.on('vote',d),a.ws.on('grab',e)}),a.on('roomState',(d)=>{a[c]=Object.keys(d.grabs),a[b]=Object.keys(d.votes).map((a)=>({uid:a,vote:d.votes[a]}))}),a.score=()=>{const d=a[b].reduce((a,b)=>Object.assign(a,{[b.uid]:b.vote}),{});let e=0,f=0;return Object.keys(d).forEach((a)=>{1===d[a]?e++:-1===d[a]&&f++}),{positive:e,negative:f,grabs:a[c].length,listeners:a.users().length+a.guests()}};const f=(b)=>a.post('votes',{historyID:a.historyEntry().id,direction:b});Object.assign(a,{grab:(b,c)=>a.post('grabs',{playlistID:b,historyID:c}).get(0),vote:f,woot:m(f,1),meh:m(f,-1)})}}Object.assign(ta,Object.assign({},{httpPlugin:V,connectPlugin:Y,usersPlugin:_,ignoresPlugin:aa,mutesPlugin:ba,bansPlugin:ca,notificationsPlugin:da,boothPlugin:ea,waitlistPlugin:ga,historyPlugin:ha,chatPlugin:ja,friendsPlugin:ka,roomsPlugin:ma,playlistsPlugin:na,storePlugin:oa,systemPlugin:pa,votePlugin:qa},t,D));const ra=b('miniplug:miniplug'),sa={host:'https://plug.dj',connect:!0};function ta(b){function c(a){return a(e),e}void 0===b&&(b={});const d=new a;d.setMaxListeners(100);const e=Object.create(d);b=Object.assign({},sa,b);const f=b.host.replace(/\/+$/,'');return e.on('login',()=>ra('authenticated')),e.on('connected',()=>ra('connected')),e.use=c,c(T()),c(V({host:f,backoff:G({increment:200,max:2200})})),c(Y({host:f})),c(_()),c(aa()),c(ba()),c(ca()),c(da()),c(ea()),c(ga()),c(ha()),c(ja({backoff:G({increment:70,max:700})})),c(ka()),c(ma()),c(na()),c(oa()),c(pa()),c(qa()),Object.assign(e,{getNews:m(e.get,'news')}),b.connect&&e.connect(b),e}export default ta;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment