const ReadyResource=require('ready-resource'),HypercoreId=require('hypercore-id-encoding'),safetyCatch=require('safety-catch'),b4a=require('b4a'),{
KeetContractError}
=require('../contract/errors'),{
contract:{
hyperschema:{
getEnum:getContractEnum}
}
}
=require('../../spec'),RoomType=getContractEnum("@keet/room-type"),{
restoreAvatar,restoreFile}
=require('../contract/avatars'),{
getMemberForDeviceKey}
=require('../contract/query'),LATEST_CHAT_MESSAGES_LIMIT=0x14;
module.exports=class RoomLoader extends ReadyResource{
constructor(_0xd89a4d,_0xe89f10,_0x4e9b59){
_0x442534={
_0x2bd5c4:0x145,_0x1190d2:0x371},_0x1e0a44={
_0x39ef88:0x137,_0x433156:0x45,_0x44e227:0x11c}
;
super(),this.local=_0xd89a4d,this.view=_0xe89f10,this.base=_0x4e9b59,this.shared=-0x7cc,this.syncTime=null,this.output=null,this.input=null,this.inputBlobs=null,this.core=null,this._loadingLocalPrivateProfileInfo=null,this._loadingLocalIdentityInfo=null,this._loadingLocalRoomInfo=null,this._loadingLatestMessages=null,this._loadingLatestMembers=null,this._loadingDigest=null,this._loadingConfig=null,this._loadingCapabilities=null,this._loadingIdentity=null,this._loadingPrimaryThread=null;
this._loadingLastMessage=null;
this._loadingDMMembers=null,this._loadingReadLengths=null,this._loadingChatChanges=null,this._loadingContactChanges=null,this._loadingSharedRoomChanges=null,this._loadingPendingInvitations=null,this.ready().catch(safetyCatch);
}
._close(){
_0x1b2df5={
_0x2d950c:0x32,_0x1643cf:0x14},_0xb72758={
_0x13ba36:0x28d,_0x27c833:0x1ac}
;
return Promise.all([this.input.close(),this.output.close(),this.inputBlobs.close()]);
}
async._open(){
_0x17e8cc={
_0x4a3cc7:0x147,_0xe972a2:0x43e,_0x20f981:0x18},_0x4f000b={
_0x3f652c:0x132,_0x57d9f3:0x24d,_0x4ca5b1:0xe}
;
this.view.db.update();
this.output=this.local.snapshot(),this.input=this.view.db.snapshot(),this.inputBlobs=this.view.blobs.snapshot();
this.core=this.input.engine.db.core,this.syncTime=Date.now();
}
async._getChangeRange(_0x15e3f7){
_0x171b98={
_0x56d4ae:0x47a,_0x2137d9:0x1a1},_0x5376f9={
_0x416719:0x138,_0x24677b:0x1e5},_0x4b74ee=await this.input.findOne(_0x15e3f7,{'reverse':!![],'checkout':this.shared});
return _0x4b74ee?{
'gt':{
'clock':_0x4b74ee.clock}
}
:{
}
;
}
async._runLoadConfig(){
_0x2d8118={
_0x22a2f8:0xb6,_0x481a1a:0x1aa,_0x1ca466:0x157},[_0x449d38,_0x1c3ecc,_0x26a390,_0x1b2dd4]=await Promise.all([this.loadLocalRoomInfo(),this.loadDigest(),this.loadSelfMember(),this.input.get("@keet/room-config")]);
let _0x2551ea=_0x1b2dd4?..title||null,_0x22105e=_0x1b2dd4?..avatar||null;
if(_0x26a390&&_0x1c3ecc?..settings.roomType===RoomType.DirectMessage){
const _0x5c3983=await this.input.find("@keet/members").toArray();
for(const _0x150ba4 of _0x5c3983){
if(b4a.equals(_0x26a390.key,_0x150ba4.key))continue;
_0x2551ea=_0x150ba4.profile.displayName,_0x22105e=_0x150ba4.profile.avatar;
break;
}
}
if(_0x22105e){
const _0x3cf185=_0x449d38?..config?..avatar?..small?..hash,_0x990b89=_0x22105e?..small?..hash;
_0x3cf185&&_0x990b89&&b4a.equals(_0x3cf185,_0x990b89)?_0x1b2dd4.avatar=_0x449d38.config.avatar:await restoreAvatar(this.inputBlobs,_0x22105e);
}
return{
..._0x1b2dd4,'title':_0x2551ea,'avatar':_0x22105e}
;
}
async._runLoadSelfMember(){
_0x27208b={
_0x310909:0x197,_0x313b24:0x1c,_0x6cc709:0xc1},_0x767f51={
_0x18a610:0x1f2,_0x15c000:0x407,_0x3b04b7:0x1d7}
;
const _0x1b18de=await getMemberForDeviceKey(this.input,this.base.local.key);
if(!_0x1b18de)return null;
const _0x546600=await this.input.get("@keet/member-proofs",{'key':_0x1b18de.key}),_0xde01da={
..._0x1b18de,'proofs':_0x546600}
;
if(!_0xde01da?..profile.avatar)return _0xde01da;
return await restoreAvatar(this.inputBlobs,_0xde01da.profile.avatar),_0xde01da;
}
async._runLoadLastMessage(){
_0xaab97d={
_0x16c328:0x14a,_0x1a7b09:0x355,_0x18e43f:0xb9},_0x20c45f={
_0x1a2360:0x11d,_0x5049cf:0x6a},_0x50a4fd=await this.loadPrimaryThread();
if(!_0x50a4fd||_0x50a4fd.count===-0x28a0)return null;
const _0x4e71d7={
'thread':0x0,'index':_0x50a4fd.count-(0x1)}
;
return this.loadMessage(_0x4e71d7);
}
async._runLoadDMMembers(){
_0x346d35={
_0x489652:0x6c,_0xc55403:0x3dd,_0x442d4b:0x49},_0x532ff0={
_0x10a2ee:0x1ad,_0x1f2a1d:0x3f3,_0x4ff8a1:0x1e6},[_0x4fd3bc,_0x50d9f7]=await Promise.all([this.loadDigest(),this.loadSelfMember()]);
if(!_0x4fd3bc||!_0x50d9f7||_0x4fd3bc.settings.roomType!==RoomType.DirectMessage)return[];
const _0x496416=await this.input.find("@keet/members").toArray();
const _0x5a1621=[];
for(const _0x112660 of _0x496416){
if(b4a.equals(_0x112660.key,_0x50d9f7.key))continue;
_0x5a1621.push(_0x112660.key);
}
return _0x5a1621;
}
async._runLoadChatChanges(){
_0x5ebb08={
_0x435461:0x3,_0x16f108:0x1b1},[_0x1f4eee,_0x1b90e2]=await Promise.all([this.loadLocalRoomInfo(),this.loadLastMessage()]),_0x1cebae=_0x1f4eee?..lastMessage?..oplog.key;
const _0x2292a1=_0x1f4eee?..lastMessage?..oplog.seq,_0x4a71e1=_0x1b90e2?..oplog.key,_0x2f1a23=_0x1b90e2?..oplog.seq;
let _0x59b702=![];
if(!_0x1cebae)_0x59b702=!![];
else{
const _0x3ae3bd=b4a.equals(_0x1cebae,_0x4a71e1),_0x7a36dd=_0x2292a1===_0x2f1a23;
_0x59b702=!(_0x3ae3bd&&_0x7a36dd);
}
return{
'lastMessage':_0x1b90e2,'changed':_0x59b702}
;
}
async._runLoadLatestMessages(){
_0x306b21={
_0x513ea8:0x8,_0x21d274:0x30},_0x877b59={
_0xf9b83:0x1d9}
;
const _0x52cbe9=await this.loadLocalIdentityInfo();
if(_0x52cbe9&&b4a.equals(this.base.key,_0x52cbe9.privateProfileKey))return[];
return this.input.find("@keet/chat",{'gte':{'id':{'thread':0x0}},'lte':{'id':{'thread':0x0}},'limit':LATEST_CHAT_MESSAGES_LIMIT,'reverse':!![]}).toArray();
}
async._loadFiles(_0x2639a5){
_0x591c0a={
_0x5195ee:0x482,_0xeda870:0x444,_0x380744:0x4a2,_0x521e50:0x467,_0x3beb0b:0x1c7,_0x57052e:0x1a9,_0x127d59:0x19b},_0x379ba2={
_0x68af44:0x44,_0x2ffdfa:0x64,_0x40533c:0x120},_0x581bf7={
_0x26248d:0x380,_0x270b18:0x8d},_0x158093={
_0xa9001:0xad,_0xc22930:0x20},_0x1d715c=await Promise.all(_0x2639a5.map(_0x595aba=>this.input.get("@keet/files",{'id':_0x595aba})));
return await Promise.all(_0x1d715c.map(async _0x54a864=>{return await restoreFile(this.inputBlobs,_0x54a864.file.preview),_0x54a864.file;}));
}
async._loadMemberForDeviceKey(_0x569d43,_0x132221,_0x278f5a){
_0x4499e5={
_0xe58997:0x1d6,_0x30489e:0x6f},_0x274fbc={
_0x4e76d2:0x150,_0x9bef9f:0x139},_0x219172=await getMemberForDeviceKey(this.input,_0x278f5a);
if(!_0x219172)return null;
const _0x46b70b=HypercoreId.encode(_0x219172.key);
if(_0x569d43.has(_0x46b70b))return;
_0x569d43.add(_0x46b70b);
if(_0x219172.avatar)await restoreAvatar(this.inputBlobs,_0x219172.avatar);
_0x132221.push(_0x219172);
}
async._runLoadLatestMembers(){
_0x4ce819={
_0x3e5d9f:0xb6,_0x39462b:0x140},_0x8d5a5d={
_0x593b04:0x93,_0x205ab5:0xb,_0x102515:0x1c1},_0x4b5bca=await this.loadLatestMessages();
if(this.closing||!_0x4b5bca.length)return[];
const _0x4d9a63=[],_0xdaf735=[],_0x4428e4=new Set(),_0x58e15f=new Set();
for(const _0x16e76d of _0x4b5bca){
const _0x141552=HypercoreId.encode(_0x16e76d.oplog.key);
if(_0x4428e4.has(_0x141552))continue;
_0x4428e4.add(_0x141552),_0xdaf735.push(this._loadMemberForDeviceKey(_0x58e15f,_0x4d9a63,_0x16e76d.oplog.key));
}
return await Promise.all(_0xdaf735),_0x4d9a63;
}
async._runLoadSharedRoomChanges(){
_0x12d9e4={
_0x219fb8:0x1dc,_0x435f6f:0xcc,_0x117a2a:0x5c},_0x185b82={
_0x4690a9:0x1bb,_0x5b4413:0x47f,_0x4c6010:0xdd},_0x476e44=await this._getChangeRange("@keet-private-id/rooms-by-clock"),_0x3c745a=[];
for await(const _0x628c43 of this.input.find("@keet-private-id/rooms-by-clock",_0x476e44)){
_0x3c745a.push(_0x628c43);
}
return _0x3c745a;
}
async._runLoadContactChanges(){
_0x4864f7={
_0xbce568:0x242,_0x4d91a8:0xc7},_0x3a1c1c={
_0x2c685c:0x18d,_0x52bb53:0x194,_0x1f3ff1:0x14b},_0x38c3a7=await this._getChangeRange("@keet-private-id/contacts-by-clock"),_0x1524fb=[];
for await(const _0x401fe6 of this.input.find("@keet-private-id/contacts-by-clock",_0x38c3a7)){
_0x1524fb.push(_0x401fe6);
}
return _0x1524fb;
}
._runLoadPendingInvitations(){
_0x160a68={
_0x522c4b:0x601},_0xc2b40={
_0x375c82:0x9c,_0x5adf6b:0x38d,_0x1363c2:0x6}
;
return this.input.find("@keet-private-id/pending-invitations").toArray();
}
async._runLoadLocalPrivateProfileInfo(){
_0x31bdc6={
_0x78f08d:0x1e8,_0x599b6e:0x28c,_0x4cf2ce:0x1e9},_0x3c65cd={
_0x2aebad:0x1c4,_0x51b241:0x364}
;
const _0x3d7501=await this.loadLocalIdentityInfo();
if(this.closing||!_0x3d7501)return null;
return this.output.get("@local/rooms",{'roomKey':_0x3d7501.privateProfileKey});
}
._runLoadReadLengths(){
_0x5cfee1={
_0x853a61:0x3f,_0x158ad7:0x616,_0x592c35:0xb2},_0x4129b6={
_0x41b124:0x5d,_0x2f99b4:0xf1,_0x426a59:0x5e}
;
return this.input.find("@keet/read-lengths").toArray();
}
.loadLocalRoomInfo(){
_0x489150={
_0x5cd122:0x2c1},_0x11d6e1={
_0x5a1ab6:0x1b5}
;
if(this._loadingLocalRoomInfo)return this._loadingLocalRoomInfo;
this._loadingLocalRoomInfo=this.output.get("@local/rooms",{'roomKey':this.base.key}),this._loadingLocalRoomInfo.catch(safetyCatch);
return this._loadingLocalRoomInfo;
}
.loadLocalPrivateProfileInfo(){
_0x525bfa={
_0x5af667:0x167},_0x51f889={
_0x139686:0x188}
;
if(this._loadingLocalPrivateProfileInfo)return this._loadingLocalPrivateProfileInfo;
this._loadingLocalPrivateProfileInfo=this._runLoadLocalPrivateProfileInfo();
return this._loadingLocalPrivateProfileInfo.catch(safetyCatch),this._loadingLocalPrivateProfileInfo;
}
.loadLocalIdentityInfo(){
_0x45e947={
_0x24da05:0xbb,_0x2abe1d:0x309,_0x5f2d31:0x44},_0x7e7db8={
_0x2aefdb:0xc1,_0x5aa9bf:0x4}
;
if(this._loadingLocalIdentityInfo)return this._loadingLocalIdentityInfo;
this._loadingLocalIdentityInfo=this.output.get("@local/identity"),this._loadingLocalIdentityInfo.catch(safetyCatch);
return this._loadingLocalIdentityInfo;
}
.loadDigest(){
_0x422c66={
_0x5333ab:0x1dd},_0x37b3bb={
_0x4e5a2b:0x1a,_0x251059:0x5,_0x4674c3:0xbd}
;
if(this._loadingDigest)return this._loadingDigest;
this._loadingDigest=this.input.get("@keet/system-digest"),this._loadingDigest.catch(safetyCatch);
return this._loadingDigest;
}
.loadConfig(){
_0x71f5b8={
_0x23fa57:0x4df,_0x8adee:0x1e0},_0x26dae2={
_0x2e77fc:0x5ef}
;
if(this._loadingConfig)return this._loadingConfig;
this._loadingConfig=this._runLoadConfig();
return this._loadingConfig.catch(safetyCatch),this._loadingConfig;
}
.loadCapabilities(){
_0x253174={
_0x3ffadd:0x123,_0x4d9478:0x3ad},_0x5d479c={
_0x34c06c:0x1b1,_0x400020:0x76,_0x389df1:0x170}
;
if(this._loadingCapabilities)return this._loadingCapabilities;
this._loadingCapabilities=this.input.get("@keet/room-capabilities");
return this._loadingCapabilities.catch(safetyCatch),this._loadingCapabilities;
}
.loadPrimaryThread(){
_0x1e8e8a={
_0x1a9320:0x102,_0x3c63ad:0x1e1},_0x3ee1eb={
_0x1bf9ba:0x281,_0x28214b:0x1ed}
;
if(this._loadingPrimaryThread)return this._loadingPrimaryThread;
return this._loadingPrimaryThread=this.input.get("@keet/threads",{'id':0x0}),this._loadingPrimaryThread.catch(safetyCatch),this._loadingPrimaryThread;
}
.loadSelfDevice(){
_0x44b3e3={
_0x9bfc86:0x0,_0x5177d3:0x169},_0x249e38={
_0x5cd5a0:0x1a2,_0x51192c:0x1bf}
;
if(this._loadingSelfDevice)return this._loadingSelfDevice;
this._loadingSelfDevice=this.input.get("@keet/devices",{'key':this.base.local.key});
return this._loadingSelfDevice.catch(safetyCatch),this._loadingSelfDevice;
}
.loadSelfMember(){
_0x3482b1={
_0x4b78ef:0x1da,_0x4b7287:0xba},_0x401870={
_0x4e4e63:0x13a,_0x4e8c74:0x3e6,_0x5e8066:0x13a}
;
if(this._loadingSelfMember)return this._loadingSelfMember;
this._loadingSelfMember=this._runLoadSelfMember();
this._loadingSelfMember.catch(safetyCatch);
return this._loadingSelfMember;
}
.loadLastMessage(){
_0x50d248={
_0x20a84a:0x84,_0x55ca33:0x126},_0x2c9c90={
_0x10f15b:0x308}
;
if(this._loadingLastMessage)return this._loadingLastMessage;
return this._loadingLastMessage=this._runLoadLastMessage(),this._loadingLastMessage.catch(safetyCatch),this._loadingLastMessage;
}
.loadDMMembers(){
_0x24ed6b={
_0x46f367:0x153,_0x196693:0xc8,_0x5021a6:0x1bf},_0x403c55={
_0x2c0f73:0xe3,_0x4ea498:0x2ba,_0x386e0f:0x34}
;
if(this._loadingDMMembers)return this._loadingDMMembers;
this._loadingDMMembers=this._runLoadDMMembers(),this._loadingDMMembers.catch(safetyCatch);
return this._loadingDMMembers;
}
.loadIdentity(){
_0x21ab07={
_0x3ed5e4:0x16b,_0x4932a4:0x57},_0x5df369={
_0x52a4e3:0x2b}
;
if(this._loadingIdentity)return this._loadingIdentity;
return this._loadingIdentity=this.input.get("@keet-private-id/identity"),this._loadingIdentity.catch(safetyCatch),this._loadingIdentity;
}
.loadChatChanges(){
_0x348a3f={
_0x266314:0x24f,_0x3f9fd5:0x125},_0x59ef6f={
_0x564b18:0x66,_0x3ecaea:0x37,_0x57abb6:0x65}
;
if(this._loadingChatChanges)return this._loadingChatChanges;
this._loadingChatChanges=this._runLoadChatChanges();
return this._loadingChatChanges.catch(safetyCatch),this._loadingChatChanges;
}
.loadContactChanges(){
_0x56a36b={
_0x119135:0x1ac,_0xc792f7:0x143,_0x1ae37b:0x141},_0x2a1588={
_0xba7ea2:0xb7,_0x1a63a8:0x3cf,_0x3605dd:0x15e}
;
if(this._loadingContactChanges)return this._loadingContactChanges;
return this._loadingContactChanges=this._runLoadContactChanges(),this._loadingContactChanges.catch(safetyCatch),this._loadingContactChanges;
}
.loadSharedRoomChanges(){
_0x13add5={
_0x2f364a:0x13f,_0x409f22:0x156},_0x40c176={
_0x2bd522:0x171,_0x438a5b:0x19f,_0x3c8429:0xb5}
;
if(this._loadingSharedRoomChanges)return this._loadingSharedRoomChanges;
return this._loadingSharedRoomChanges=this._runLoadSharedRoomChanges(),this._loadingSharedRoomChanges.catch(safetyCatch),this._loadingSharedRoomChanges;
}
.loadPendingInvitations(){
_0x43a78b={
_0x22fe35:0x37,_0x37bd38:0x399,_0x582411:0x16f},_0x1cae30={
_0x230d41:0xed,_0x3b737a:0xf7}
;
if(this._loadingPendingInvitations)return this._loadingPendingInvitations;
return this._loadingPendingInvitations=this._runLoadPendingInvitations(),this._loadingPendingInvitations.catch(safetyCatch),this._loadingPendingInvitations;
}
.loadLatestMessages(){
_0x38f75a={
_0x17b593:0x102,_0x323193:0x357,_0x4de242:0x1a1},_0x12db34={
_0x372198:0x1ae,_0x44fb88:0xcc,_0x319020:0x1a6}
;
if(this._loadingLatestMessages)return this._loadingLatestMessages;
this._loadingLatestMessages=this._runLoadLatestMessages();
return this._loadingLatestMessages.catch(safetyCatch),this._loadingLatestMessages;
}
.loadLatestMembers(){
_0x505540={
_0x1e0994:0x89,_0x6d9bdc:0xdb,_0x38ff07:0x44},_0x4a2a1d={
_0x42a885:0x203,_0xb83e3b:0x1d8}
;
if(this._loadingLatestMembers)return this._loadingLatestMembers;
this._loadingLatestMembers=this._runLoadLatestMembers();
return this._loadingLatestMembers.catch(safetyCatch),this._loadingLatestMembers;
}
.loadReadLengths(){
_0x4f0ad1={
_0x281594:0xe8,_0x4d8e2c:0x340,_0x22787a:0x110},_0x26df41={
_0x2facfa:0xb2,_0x39d8dd:0x568,_0x3e9bc8:0xf1}
;
if(this._loadingReadLengths)return this._loadingReadLengths;
this._loadingReadLengths=this._runLoadReadLengths();
return this._loadingReadLengths.catch(safetyCatch),this._loadingReadLengths;
}
async.loadMessage(_0x2673fd){
_0x52f9da={
_0x7a414a:0x1d5,_0x4be0ec:0x182,_0x5e1105:0x5a},_0x3efead={
_0x2cc1f6:0x141},[_0x1ae991,_0x559040]=await Promise.all([this.input.get("@keet/chat",{'id':_0x2673fd}),this.input.get("@keet/reaction-digests",{'id':_0x2673fd})]);
if(!_0x1ae991)return null;
const _0x1953d0=_0x1ae991.message?..files,_0x33c6e3=!!_0x1953d0&&_0x1953d0.length>0x0,[_0x15181b,_0x20ea1c,_0x506cab]=await Promise.all([this.input.get("@keet/devices",{'key':_0x1ae991.oplog.key}),getMemberForDeviceKey(this.input,_0x1ae991.oplog.key),_0x33c6e3?this._loadFiles(_0x1953d0):null]);
if(!_0x20ea1c)throw KeetContractError.CORRUPT("Last\x20chat\x20message does\x20not\x20have a member");
if(_0x20ea1c.profile.avatar)await restoreAvatar(this.inputBlobs,_0x20ea1c.profile.avatar);
if(_0x506cab)_0x1ae991.message.files=_0x506cab;
return{
'message':_0x1ae991,'reactions':_0x559040,'device':_0x15181b,'member':_0x20ea1c}
;
}
}
;