2 lines
8.4 KiB
JavaScript
2 lines
8.4 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ChatMessages=e():t.ChatMessages=e()}(self,(function(){return(()=>{"use strict";var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function n(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return r(t,e);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)?r(t,e):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 r(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function a(t){return(a="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 i(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function s(t){return 1===(t=String(t)).length&&(t="0"+t),t}t.d(e,{default:()=>o});const o={name:"chat-messages",builder:function(t,e){var r={type:"object",properties:{"@context":{type:"string",default:"https://kosmos.org/ns/v1",enum:["https://kosmos.org/ns/v1"]},"@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:[]};return t.declareType("daily-archive","https://kosmos.org/ns/v1",r),e.declareType("daily-archive","https://kosmos.org/ns/v1",r),{exports:{DailyArchive:function(){function r(n){if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,r),n.isPublic=n.isPublic||!1,n.channelType=n.channelType||"room","object"!==a(n))throw"options must be an object";if("object"!==a(n.service)||"string"!=typeof n.service.protocol||"string"!=typeof n.service.domain)throw'service must be an object containing at least service "protocol" and "domain"';if("string"!=typeof n.channelName)throw"channelName must be a string";if(!(n.date instanceof Date))throw"date must be a date object";if("boolean"!=typeof n.isPublic)throw"isPublic must be a boolean value";var i;if(this.service=n.service,this.channelName=n.channelName,this.channelType=n.channelType,this.date=n.date,this.isPublic=n.isPublic||!1,this.parsedDate={year:(i=this.date).getUTCFullYear(),month:s(i.getUTCMonth()+1),day:s(i.getUTCDate())},this.dateId=this.parsedDate.year+"/"+this.parsedDate.month+"/"+this.parsedDate.day,"room"===this.channelType){var o=this.channelName.replace(/#/,"");this.path="".concat(this.service.domain,"/channels/").concat(o,"/").concat(this.dateId)}else this.path="".concat(this.service.domain,"/users/").concat(this.channelName,"/").concat(this.dateId);this.client=this.isPublic?e:t,this.previous=n.previous,this.next=n.next}var o,c;return o=r,(c=[{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(n){return"object"===a(n)?e._updateDocument(n,t):e._createDocument(t)})))}},{key:"addMessages",value:function(t,e){var n=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"===a(e)?n._updateDocument(e,t):n._createDocument(t)})))}},{key:"remove",value:function(){return this.client.remove(this.path)}},{key:"_updateDocument",value:function(t,e){return console.debug("[chat-messages] Updating archive document",t),Array.isArray(e)?e.forEach((function(e){t.today.messages.push(e)})):t.today.messages.push(e),this._sync(t)}},{key:"_createDocument",value:function(t){var e=this;console.debug("[chat-messages] Creating new archive document");var n=this._buildArchiveObject();return Array.isArray(t)?t.forEach((function(t){n.today.messages.push(t)})):n.today.messages.push(t),this.previous||this.next?(this.previous&&(n.today.previous=this.previous),this.next&&(n.today.next=this.next),this._sync(n)):this._updatePreviousArchive().then((function(t){return"object"===a(t)&&(n.today.previous=t.today["@id"]),e._sync(n)}))}},{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"===a(e)&&e.today){e.today.next=t.dateId;var n=t.path.substring(0,t.path.length-t.dateId.length)+e.today["@id"];return t.client.storeObject("daily-archive",n,e).then((function(){return console.debug("[chat-messages] Previous archive written to remote storage",n,e),e}))}return console.debug("[chat-messages] Previous archive not found"),!1}))}},{key:"_findPreviousArchive",value:function(){var t=this,e=this.path.substring(0,this.path.length-2),r=this.path.substring(0,this.path.length-5),a=this.path.substring(0,this.path.length-10);return this.client.getListing(e).then((function(i){var o=Object.keys(i).map((function(t){return parseInt(t)})).map((function(e){return e<parseInt(t.parsedDate.day)?e:null})).filter((function(t){return null!=t}));if(o.length>0){var c=s(Math.max.apply(Math,n(o)).toString());return t.client.getObject(e+c)}return t.client.getListing(r).then((function(e){var i=Object.keys(e).map((function(t){return parseInt(t.substr(0,2))})).map((function(e){return e<parseInt(t.parsedDate.month)?e:null})).filter((function(t){return null!=t}));if(i.length>0){var o=s(Math.max.apply(Math,n(i)).toString());return t.client.getListing(r+o+"/").then((function(e){var a=Object.keys(e).map((function(t){return parseInt(t)})),i=s(Math.max.apply(Math,n(a)).toString());return t.client.getObject(r+o+"/"+i)}))}return t.client.getListing(a).then((function(e){var r=Object.keys(e).map((function(t){return parseInt(t.substr(0,4))})).map((function(e){return e<parseInt(t.parsedDate.year)?e:null})).filter((function(t){return null!=t}));if(r.length>0){var i=Math.max.apply(Math,n(r)).toString();return t.client.getListing(a+i+"/").then((function(e){var r=Object.keys(e).map((function(t){return parseInt(t.substr(0,2))})),o=s(Math.max.apply(Math,n(r)).toString());return t.client.getListing(a+i+"/"+o+"/").then((function(e){var r=Object.keys(e).map((function(t){return parseInt(t)})),c=s(Math.max.apply(Math,n(r)).toString());return t.client.getObject(a+i+"/"+o+"/"+c)}))}))}return!1}))}))}))}},{key:"_sync",value:function(t){return console.debug("[chat-messages] Writing archive object",t),this.client.storeObject("daily-archive",this.path,t).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}))}}])&&i(o.prototype,c),r}(),privateClient:t,publicClient:e}}}};return e.default})()}));
|
|
//# sourceMappingURL=build.js.map
|