/*! For license information please see build.js.LICENSE.txt */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("ChatMessages",[],e):"object"==typeof exports?exports.ChatMessages=e():t.ChatMessages=e()}(this,(()=>(()=>{var t={666:t=>{var e=function(t){"use strict";var e,r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=e&&e.prototype instanceof v?e:v,a=Object.create(o.prototype),i=new P(n||[]);return a._invoke=function(t,e,r){var n=l;return function(o,a){if(n===p)throw new Error("Generator is already running");if(n===y){if("throw"===o)throw a;return M()}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=k(i,r);if(c){if(c===d)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===l)throw n=y,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=p;var s=h(t,e,r);if("normal"===s.type){if(n=r.done?y:f,s.arg===d)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n=y,r.method="throw",r.arg=s.arg)}}}(t,r,i),a}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l="suspendedStart",f="suspendedYield",p="executing",y="completed",d={};function v(){}function g(){}function m(){}var b={};s(b,a,(function(){return this}));var w=Object.getPrototypeOf,x=w&&w(w(N([])));x&&x!==r&&n.call(x,a)&&(b=x);var j=m.prototype=v.prototype=Object.create(b);function L(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function _(t,e){function r(o,a,i,c){var s=h(t[o],t,a);if("throw"!==s.type){var u=s.arg,l=u.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,i,c)}),(function(t){r("throw",t,i,c)})):e.resolve(l).then((function(t){u.value=t,i(u)}),(function(t){return r("throw",t,i,c)}))}c(s.arg)}var o;this._invoke=function(t,n){function a(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(a,a):a()}}function k(t,r){var n=t.iterator[r.method];if(n===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=e,k(t,r),"throw"===r.method))return d;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var o=h(n,t.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,d;var a=o.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,d):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,d)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function N(t){if(t){var r=t[a];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,i=function r(){for(;++o=0;--a){var i=this.tryEntries[a],c=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(s&&u){if(this.prev=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),O(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;O(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:N(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),d}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}},e={};function r(n){var o=e[n];if(void 0!==o)return o.exports;var a=e[n]={exports:{}};return t[n](a,a.exports,r),a.exports}r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var n={};return(()=>{"use strict";function t(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,r){if(t){if("string"==typeof t)return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=r.call(a,"catchLoc"),s=r.call(a,"finallyLoc");if(c&&s){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),_(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;_(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}function a(t,e,r,n,o,a,i){try{var c=t[a](i),s=c.value}catch(t){return void r(t)}c.done?e(s):Promise.resolve(s).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function c(t){a(i,n,o,c,s,"next",t)}function s(t){a(i,n,o,c,s,"throw",t)}c(void 0)}))}}function c(t){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},c(t)}function s(t,e){for(var r=0;rl}),r(666);const l={name:"chat-messages",builder:function(e,r){var n={type:"object",properties:{"@context":{type:"string",default:"https://kosmos.org/ns/v2/chat-channel"},"@id":{type:"string",required:!0},"@type":{type:"string",default:"ChatChannel",enum:["ChatChannel"]},service:{type:"object",properties:{domain:{type:"string",required:!0},protocol:{type:"string",required:!0}}},name:{type:"string",required:!0},type:{type:"string",required:!0,enum:["room","person"]},today:{type:"object",properties:{"@id":{type:"string",pattern:"^[0-9]{4}/[0-9]{2}/[0-9]{2}$",required:!0},"@type":{type:"string",default:"ChatLog",pattern:"^ChatLog$"},messageType:{type:"string",default:"InstantMessage",pattern:"^InstantMessage$"},previous:{type:"string",pattern:"^[0-9]{4}/[0-9]{2}/[0-9]{2}$"},next:{type:"string",pattern:"^[0-9]{4}/[0-9]{2}/[0-9]{2}$"},messages:{type:"array",required:!0,items:{type:"object",properties:{date:{type:"string",format:"date-time"},user:{type:"string"},text:{type:"string"},type:"string",default:"text",enum:["text","join","leave","action"]}}}}}},required:[]};e.declareType("daily-archive","https://kosmos.org/ns/v2/chat-channel",n),r.declareType("daily-archive","https://kosmos.org/ns/v2/chat-channel",n);var a={type:"object",properties:{"@context":{type:"string",default:"https://kosmos.org/ns/v2/chat-channel-meta"},"@id":{type:"string"},"@type":{type:"string",default:"ChatChannelMeta",enum:["ChatChannelMeta"]},first:{type:"string",pattern:"^[0-9]{4}/[0-9]{2}/[0-9]{2}$"},last:{type:"string",pattern:"^[0-9]{4}/[0-9]{2}/[0-9]{2}$"}},required:["@id","first","last"]};e.declareType("daily-archive-meta","https://kosmos.org/ns/v2/chat-channel-meta",a),r.declareType("daily-archive-meta","https://kosmos.org/ns/v2/chat-channel-meta",a);var l=function(){function n(t){if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,n),t.isPublic=t.isPublic||!1,t.channelType=t.channelType||"room","object"!==c(t))throw"options must be an object";if("object"!==c(t.service)||"string"!=typeof t.service.protocol||"string"!=typeof t.service.domain)throw'service must be an object containing at least service "protocol" and "domain"';if("string"!=typeof t.channelName)throw"channelName must be a string";if(!(t.date instanceof Date))throw"date must be a date object";if("boolean"!=typeof t.isPublic)throw"isPublic must be a boolean value";var o;if(this.service=t.service,this.channelName=t.channelName,this.channelType=t.channelType,this.date=t.date,this.isPublic=t.isPublic||!1,this.parsedDate={year:(o=this.date).getUTCFullYear(),month:u(o.getUTCMonth()+1),day:u(o.getUTCDate())},this.dateId=this.parsedDate.year+"/"+this.parsedDate.month+"/"+this.parsedDate.day,"room"===this.channelType){var a=this.channelName.replace(/^#/,"");this.channelPath="".concat(this.service.domain,"/channels/").concat(a)}else this.channelPath="".concat(this.service.domain,"/users/").concat(this.channelName);this.path="".concat(this.channelPath,"/").concat(this.dateId),this.metaPath="".concat(this.channelPath,"/meta"),this.client=this.isPublic?r:e,this.previous=t.previous,this.next=t.next}var a,l,f,p,y,d,v,g;return a=n,l=[{key:"addMessage",value:function(t){var e=this;return this.isPublic&&!this.channelName.match(/^#/)?Promise.resolve(!1):(t.type=t.type||"text",this.client.getObject(this.path).then((function(r){return"object"===c(r)?e._updateDocument(r,t):e._createDocument(t)})))}},{key:"addMessages",value:function(t,e){var r=this;return this.isPublic&&!this.channelName.match(/^#/)?Promise.resolve(!1):(e=e||!1,t.forEach((function(t){t.type=t.type||"text"})),e?this._createDocument(t):this.client.getObject(this.path).then((function(e){return"object"===c(e)?r._updateDocument(e,t):r._createDocument(t)})))}},{key:"remove",value:function(){return this.client.remove(this.path)}},{key:"_updateDocument",value:(g=i(o().mark((function t(e,r){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.debug("[chat-messages] Updating archive document"),Array.isArray(r)?r.forEach((function(t){e.today.messages.push(t)})):e.today.messages.push(r),t.abrupt("return",this._sync(e));case 3:case"end":return t.stop()}}),t,this)}))),function(t,e){return g.apply(this,arguments)})},{key:"_createDocument",value:(v=i(o().mark((function t(e){var r,n;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(console.debug("[chat-messages] Creating new archive document"),r=this._buildArchiveObject(),Array.isArray(e)?e.forEach((function(t){r.today.messages.push(t)})):r.today.messages.push(e),!this.previous&&!this.next){t.next=8;break}this.previous&&(r.today.previous=this.previous),this.next&&(r.today.next=this.next),t.next=12;break;case 8:return t.next=10,this._updatePreviousArchive();case 10:"object"===c(n=t.sent)&&(r.today.previous=n.today["@id"]);case 12:return t.next=14,this._sync(r);case 14:return t.next=16,this._updateArchiveMetaDocument();case 16:return t.abrupt("return");case 17:case"end":return t.stop()}}),t,this)}))),function(t){return v.apply(this,arguments)})},{key:"_buildArchiveObject",value:function(){var t=this.channelName.replace(/^#/,""),e={"@id":"chat-messages/"+this.service.domain+"/channels/"+t+"/","@type":"ChatChannel",service:this.service,name:this.channelName,type:this.channelType,today:{"@id":this.dateId,"@type":"ChatLog",messageType:"InstantMessage",messages:[]}};return"IRC"===this.service.protocol&&(this.channelName.match(/^#/)||(e["@id"]="chat-messages/"+this.service.domain+"/users/"+this.channelName+"/")),e}},{key:"_updatePreviousArchive",value:function(){var t=this;return this._findPreviousArchive().then((function(e){if("object"===c(e)&&e.today){e.today.next=t.dateId;var r=t.path.substring(0,t.path.length-t.dateId.length)+e.today["@id"];return t.client.storeObject("daily-archive",r,e).then((function(){return console.debug("[chat-messages] Previous archive written to remote storage at",r),e}))}return console.debug("[chat-messages] Previous archive not found"),!1}))}},{key:"_findPreviousArchive",value:function(){var e=this,r=this.path.substring(0,this.path.length-2),n=this.path.substring(0,this.path.length-5),o=this.path.substring(0,this.path.length-10);return this.client.getListing(r).then((function(a){var i=Object.keys(a).map((function(t){return parseInt(t)})).map((function(t){return t0){var c=u(Math.max.apply(Math,t(i)).toString());return e.client.getObject(r+c)}return e.client.getListing(n).then((function(r){var a=Object.keys(r).map((function(t){return parseInt(t.substr(0,2))})).map((function(t){return t0){var i=u(Math.max.apply(Math,t(a)).toString());return e.client.getListing(n+i+"/").then((function(r){var o=Object.keys(r).map((function(t){return parseInt(t)})),a=u(Math.max.apply(Math,t(o)).toString());return e.client.getObject(n+i+"/"+a)}))}return e.client.getListing(o).then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t.substr(0,4))})).map((function(t){return t0){var a=Math.max.apply(Math,t(n)).toString();return e.client.getListing(o+a+"/").then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t.substr(0,2))})),i=u(Math.max.apply(Math,t(n)).toString());return e.client.getListing(o+a+"/"+i+"/").then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t)})),c=u(Math.max.apply(Math,t(n)).toString());return e.client.getObject(o+a+"/"+i+"/"+c)}))}))}return!1}))}))}))}},{key:"_updateArchiveMetaDocument",value:(d=i(o().mark((function t(){var e;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.client.getObject(this.metaPath);case 2:if("object"===c(e=t.sent)){t.next=5;break}return t.abrupt("return",this._createArchiveMetaDocument());case 5:if(!(Date.parse(e.last.replace(/\//g,"-"))