2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
!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,(function(){return(()=>{var t={666:t=>{var e=function(t){"use strict";var e,r=Object.prototype,n=r.hasOwnProperty,a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",o=a.asyncIterator||"@@asyncIterator",s=a.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var a=e&&e.prototype instanceof d?e:d,i=Object.create(a.prototype),o=new M(n||[]);return i._invoke=function(t,e,r){var n=l;return function(a,i){if(n===f)throw new Error("Generator is already running");if(n===y){if("throw"===a)throw i;return E()}for(r.method=a,r.arg=i;;){var o=r.delegate;if(o){var s=P(o,r);if(s){if(s===m)continue;return s}}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=f;var c=h(t,e,r);if("normal"===c.type){if(n=r.done?y:p,c.arg===m)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n=y,r.method="throw",r.arg=c.arg)}}}(t,r,o),i}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",p="suspendedYield",f="executing",y="completed",m={};function d(){}function g(){}function v(){}var b={};c(b,i,(function(){return this}));var w=Object.getPrototypeOf,x=w&&w(w(C([])));x&&x!==r&&n.call(x,i)&&(b=x);var j=v.prototype=d.prototype=Object.create(b);function k(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function L(t,e){function r(a,i,o,s){var c=h(t[a],t,i);if("throw"!==c.type){var u=c.arg,l=u.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,o,s)}),(function(t){r("throw",t,o,s)})):e.resolve(l).then((function(t){u.value=t,o(u)}),(function(t){return r("throw",t,o,s)}))}s(c.arg)}var a;this._invoke=function(t,n){function i(){return new e((function(e,a){r(t,n,e,a)}))}return a=a?a.then(i,i):i()}}function P(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,P(t,r),"throw"===r.method))return m;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return m}var a=h(n,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,m;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,m):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function _(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 M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function C(t){if(t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var a=-1,o=function r(){for(;++a<t.length;)if(n.call(t,a))return r.value=t[a],r.done=!1,r;return r.value=e,r.done=!0,r};return o.next=o}}return{next:E}}function E(){return{value:e,done:!0}}return g.prototype=v,c(j,"constructor",v),c(v,"constructor",g),g.displayName=c(v,s,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===g||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,c(t,s,"GeneratorFunction")),t.prototype=Object.create(j),t},t.awrap=function(t){return{__await:t}},k(L.prototype),c(L.prototype,o,(function(){return this})),t.AsyncIterator=L,t.async=function(e,r,n,a,i){void 0===i&&(i=Promise);var o=new L(u(e,r,n,a),i);return t.isGeneratorFunction(r)?o:o.next().then((function(t){return t.done?t.value:o.next()}))},k(j),c(j,s,"Generator"),c(j,i,(function(){return this})),c(j,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=C,M.prototype={constructor:M,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(O),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function a(n,a){return s.type="throw",s.arg=t,r.next=n,a&&(r.method="next",r.arg=e),!!a}for(var i=this.tryEntries.length-1;i>=0;--i){var o=this.tryEntries[i],s=o.completion;if("root"===o.tryLoc)return a("end");if(o.tryLoc<=this.prev){var c=n.call(o,"catchLoc"),u=n.call(o,"finallyLoc");if(c&&u){if(this.prev<o.catchLoc)return a(o.catchLoc,!0);if(this.prev<o.finallyLoc)return a(o.finallyLoc)}else if(c){if(this.prev<o.catchLoc)return a(o.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return a(o.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var a=this.tryEntries[r];if(a.tryLoc<=this.prev&&n.call(a,"finallyLoc")&&this.prev<a.finallyLoc){var i=a;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var o=i?i.completion:{};return o.type=t,o.arg=e,i?(this.method="next",this.next=i.finallyLoc,m):this.complete(o)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),m},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),O(r),m}},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 a=n.arg;O(r)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:C(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),m}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}},e={};function r(n){var a=e[n];if(void 0!==a)return a.exports;var i=e[n]={exports:{}};return t[n](i,i.exports,r),i.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<e;r++)n[r]=t[r];return n}function a(t,e,r,n,a,i,o){try{var s=t[i](o),c=s.value}catch(t){return void r(t)}s.done?e(c):Promise.resolve(c).then(n,a)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var o=t.apply(e,r);function s(t){a(o,n,i,s,c,"next",t)}function c(t){a(o,n,i,s,c,"throw",t)}s(void 0)}))}}function o(t){return(o="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})(t)}function s(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function c(t){return 1===(t=String(t)).length&&(t="0"+t),t}function u(t){return Object.keys(t).map((function(t){return parseInt(t)})).filter((function(t){return!Number.isNaN(t)})).sort()[0]}r.d(n,{default:()=>h}),r(666);const h={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"]};return 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),{exports:{DailyArchive: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"!==o(t))throw"options must be an object";if("object"!==o(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 a;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:(a=this.date).getUTCFullYear(),month:c(a.getUTCMonth()+1),day:c(a.getUTCDate())},this.dateId=this.parsedDate.year+"/"+this.parsedDate.month+"/"+this.parsedDate.day,"room"===this.channelType){var i=this.channelName.replace(/#/,"");this.channelPath="".concat(this.service.domain,"/channels/").concat(i)}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,h,l,p,f,y,m,d;return a=n,(h=[{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"===o(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"===o(e)?r._updateDocument(e,t):r._createDocument(t)})))}},{key:"remove",value:function(){return this.client.remove(this.path)}},{key:"_updateDocument",value:(d=i(regeneratorRuntime.mark((function t(e,r){return regeneratorRuntime.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 d.apply(this,arguments)})},{key:"_createDocument",value:(m=i(regeneratorRuntime.mark((function t(e){var r,n;return regeneratorRuntime.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"===o(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 m.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:[]}};switch(this.service.protocol){case"IRC":this.channelName.match(/^#/)||(e["@id"]="chat-messages/"+this.service.domain+"/users/"+this.channelName+"/")}return e}},{key:"_updatePreviousArchive",value:function(){var t=this;return this._findPreviousArchive().then((function(e){if("object"===o(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),a=this.path.substring(0,this.path.length-10);return this.client.getListing(r).then((function(i){var o=Object.keys(i).map((function(t){return parseInt(t)})).map((function(t){return t<parseInt(e.parsedDate.day)?t:null})).filter((function(t){return null!=t}));if(o.length>0){var s=c(Math.max.apply(Math,t(o)).toString());return e.client.getObject(r+s)}return e.client.getListing(n).then((function(r){var i=Object.keys(r).map((function(t){return parseInt(t.substr(0,2))})).map((function(t){return t<parseInt(e.parsedDate.month)?t:null})).filter((function(t){return null!=t}));if(i.length>0){var o=c(Math.max.apply(Math,t(i)).toString());return e.client.getListing(n+o+"/").then((function(r){var a=Object.keys(r).map((function(t){return parseInt(t)})),i=c(Math.max.apply(Math,t(a)).toString());return e.client.getObject(n+o+"/"+i)}))}return e.client.getListing(a).then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t.substr(0,4))})).map((function(t){return t<parseInt(e.parsedDate.year)?t:null})).filter((function(t){return null!=t}));if(n.length>0){var i=Math.max.apply(Math,t(n)).toString();return e.client.getListing(a+i+"/").then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t.substr(0,2))})),o=c(Math.max.apply(Math,t(n)).toString());return e.client.getListing(a+i+"/"+o+"/").then((function(r){var n=Object.keys(r).map((function(t){return parseInt(t)})),s=c(Math.max.apply(Math,t(n)).toString());return e.client.getObject(a+i+"/"+o+"/"+s)}))}))}return!1}))}))}))}},{key:"_updateArchiveMetaDocument",value:(y=i(regeneratorRuntime.mark((function t(){var e;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.client.getObject(this.metaPath);case 2:if("object"===o(e=t.sent)){t.next=5;break}return t.abrupt("return",this._createArchiveMetaDocument());case 5:if(!(Date.parse(e.last.replace("/","-"))>Date.parse(this.date))){t.next=10;break}return console.debug("[chat-messages]","Updating meta document for channel"),e.last=this.dateId,t.next=10,this.client.storeObject("daily-archive-meta",this.metaPath,e);case 10:return t.abrupt("return");case 11:case"end":return t.stop()}}),t,this)}))),function(){return y.apply(this,arguments)})},{key:"_createArchiveMetaDocument",value:(f=i(regeneratorRuntime.mark((function t(){var e,r,n,a=this;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.debug("[chat-messages]","Creating new meta document for channel"),t.next=3,this._findFirstArchive();case 3:return e=t.sent,r=this.channelName.replace(/#/,""),n={"@id":"chat-messages/".concat(this.service.domain,"/channels/").concat(r,"/meta"),"@type":"ChatChannelMeta",first:e,last:this.dateId},t.abrupt("return",this.client.storeObject("daily-archive-meta",this.metaPath,n).then((function(){return console.debug("[chat-messages]","Meta document written to remote storage")})).catch((function(t){console.log("[chat-messages]","Failed to store ".concat(a.metaPath)),console.error(t)})));case 7:case"end":return t.stop()}}),t,this)}))),function(){return f.apply(this,arguments)})},{key:"_findFirstArchive",value:(p=i(regeneratorRuntime.mark((function t(){var e,r,n,a,i,o,s;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.debug("[chat-messages]","Finding first archive for channel"),t.next=3,this.client.getListing("".concat(this.channelPath,"/"));case 3:return e=t.sent,r=u(e),t.next=7,this.client.getListing("".concat(this.channelPath,"/").concat(r,"/"));case 7:return n=t.sent,a=u(n),t.next=11,this.client.getListing("".concat(this.channelPath,"/").concat(r,"/").concat(c(a),"/"));case 11:return i=t.sent,o=u(i),s="".concat(r,"/").concat(c(a),"/").concat(c(o)),console.debug("[chat-messages]","First is",s),t.abrupt("return",s);case 16:case"end":return t.stop()}}),t,this)}))),function(){return p.apply(this,arguments)})},{key:"_sync",value:(l=i(regeneratorRuntime.mark((function t(e){return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.debug("[chat-messages] Writing archive object with ".concat(e.today.messages.length," messages")),t.abrupt("return",this.client.storeObject("daily-archive",this.path,e).then((function(){return console.debug("[chat-messages] Archive written to remote storage"),!0}),(function(t){return console.warn("[chat-messages] Error trying to store object",t),t})));case 2:case"end":return t.stop()}}),t,this)}))),function(t){return l.apply(this,arguments)})}])&&s(a.prototype,h),n}(),privateClient:e,publicClient:r}}}}})(),n.default})()}));
|
|
//# sourceMappingURL=build.js.map
|