const _0x4cdc6a=(function(){let _0x32f712=!![];return function(_0x20bc56,_0x39b67d){const _0x29b67e=_0x32f712?function(){if(_0x39b67d){const _0x4a5ee5=_0x39b67d.apply(_0x20bc56,arguments);return _0x39b67d=null,_0x4a5ee5;}}:function(){};return _0x32f712=![],_0x29b67e;};}()),_0xf2de3d=_0x4cdc6a(this,function(){_0x1ea128={_0x4f513f:0x9a};return _0xf2de3d.toString().search("(((.+)+)+)+$").toString().constructor(_0xf2de3d).search("(((.+)+)+)+$");});
_0xf2de3d();
const HypercoreId=require('hypercore-id-encoding');
const ReadyResource=require('ready-resource'),RoomUpdater=require('./updater'),{
roomsStats}
=require('../contract/stats/local');
module.exports=class RoomViewSync extends ReadyResource{
constructor(_0xf68e5a,_0x30e856,_0x453b94,_0x26d152){
_0x429450={
_0x228780:0x131,_0x22cd49:0x354},_0x37dfa4={
_0x35c7b5:0x1b9,_0xe1e151:0x68a}
;
super(),this.keet=_0xf68e5a,this.view=_0x30e856;
this.base=_0x453b94,this.changes=_0x26d152,this._events=[],this._swappedId=![];
this._updater=null;
}
async._close(){
_0x3f1d69={
_0x5df9b7:0x1d6,_0x478f1c:0x64e},_0x4f7785={
_0x2cf304:0x174,_0x3d03c1:0x42d,_0xf8e242:0x13a}
;
if(this._updater)await this._updater.close();
}
async._save(){
_0x3449ab={
_0x502460:0x17,_0x534c86:0x602,_0x2fc205:0x130},_0x4e1d00={
_0x3fe5e4:0x108,_0xb6a179:0x134}
;
const _0x26224b=await this.keet.internalEnterLocalTransaction();
try{
const _0xdbcf99=this._updater.updates.startIdentitySwap,_0x28d80e=this._updater.updates.finishIdentitySwap;
if(_0xdbcf99){
const _0x2bf3b1=this._updater.currentIdentityInfo;
_0x2bf3b1.swapV2=_0xdbcf99,await _0x26224b.insert("@local/identity",_0x2bf3b1);
}
_0x28d80e&&(await _0x26224b.insert("@local/identity",_0x28d80e),this._swappedId=!![]);
if(this._updater.updates.contacts)for(const _0xeb9911 of this._updater.updates.contacts){
await _0x26224b.insert("@local/contacts",_0xeb9911);
}
if(this._updater.updates.sharedPreferences)for(const _0x32dc12 of this._updater.updates.sharedPreferences){
await _0x26224b.insert("@local/room-preferences",_0x32dc12);
const _0x26e1e4=await _0x26224b.get("@local/rooms",_0x32dc12);
_0x26e1e4&&(_0x26e1e4.preferences=_0x32dc12.status,await _0x26224b.insert("@local/rooms",_0x26e1e4)),_0x32dc12.status.isActive&&this._events.push({'type':"recent-rooms-changed",'args':["changed",-0x3414,HypercoreId.encode(_0x32dc12.roomKey)]});
}
if(this._updater.updates.cache){
const _0x73493=this._updater.updates.cache;
_0x73493.device&&await _0x26224b.insert("@local/devices",{'roomKey':this.base.key,'device':_0x73493.device}),_0x73493.member&&await _0x26224b.insert("@local/members",_0x73493.member);
}
const _0x239465=await _0x26224b.get("@local/rooms",this._updater.roomInfo);
if(!_0x239465){
const _0x4fe97e=await _0x26224b.get("@local/room-preferences",this._updater.roomInfo);
if(_0x4fe97e)this._updater.roomInfo.preferences=_0x4fe97e.status;
}
await _0x26224b.insert("@local/rooms",this._updater.roomInfo),await roomsStats(_0x26224b,this._updater.roomInfo,_0x239465),await _0x26224b.flush();
}finally{
await this.keet.internalExitLocalTransaction();
}
}
async._dispatch(){
_0x2b40c3={
_0x375232:0xd0,_0x25ea7b:0x3a1,_0x4ab5ca:0x9},_0x49d91a={
_0x51e86b:0x32,_0x30929e:0x10},_0x2aa980=this._updater.updates.dispatches;
if(!_0x2aa980||_0x2aa980.length===0x0)return;
try{
await this.keet.dispatchBatch(this.base.id,_0x2aa980);
}catch(_0x180de2){
this.keet.onerror(_0x180de2);
}
}
async._profileDispatch(){
_0x131d8d={
_0x111a83:0x188,_0x3dda30:0x8,_0x1c8a41:0x552},_0x39d868={
_0x57c6c9:0xd7,_0x20596a:0x145,_0x299528:0x69}
;
if(!this._updater.currentIdentityInfo)return;
const _0x31be72=this._updater.updates.profileDispatches;
if(!_0x31be72||_0x31be72.length===0x0)return;
try{
const _0x53f2df=HypercoreId.encode(this._updater.currentIdentityInfo.privateProfileKey);
await this.keet.dispatchBatch(_0x53f2df,_0x31be72);
}catch(_0x46ae87){
this.keet.onerror(_0x46ae87);
}
}
._generateEvents(){
_0x9feaee={
_0x33aa88:0x3,_0x27b94f:0x1a3}
;
this._events.push({'type':"room-changed",'args':[this.base.id]});
const {
roomKey:_0x3ed594,discoveryKey:_0x26113e,timestamps:_0x473330}
=this._updater.roomInfo,_0x8aab09=_0x473330.latestChatMessage||_0x473330.latestSync,_0x2cb705=this._updater.roomInfo&&!this._updater.currentRoomInfo,_0xeb8bb=!this._updater.roomInfo.status.isRemoved&&this._updater.currentRoomInfo?..status.isRemoved,_0x29f568=this._updater.roomInfo.status.isRemoved&&!this._updater.currentRoomInfo?..status.isRemoved,_0x5a38cb=this._updater.roomInfo.status.isRemoved,_0x232205=!!this._updater.roomInfo.dm,_0xb6dccb=!_0x5a38cb&&!_0x29f568&&!_0x2cb705&&!_0xeb8bb;
if(_0x2cb705||_0xeb8bb||_0x29f568){
this._events.push({'type':"recent-rooms-changed",'args':[_0x29f568?"removed":"added",_0x8aab09,this.base.id]});
(_0x2cb705||_0xeb8bb)&&this._events.push({'type':"room-preferences-changed",'args':["join",_0x3ed594,_0x26113e]});
_0x29f568&&this._events.push({'type':"room-preferences-changed",'args':["leave",_0x3ed594,_0x26113e]});
if(_0x2cb705&&_0x232205)this._events.push({'type':"dm-rooms-changed",'args':[]});
}
this._updater.updates.chat&&(_0xb6dccb&&this._events.push({'type':"recent-rooms-changed",'args':["changed",_0x8aab09,this.base.id]}),!_0x5a38cb&&this._events.push({'type':"chat-changed",'args':[this.base.id,this._updater.updates.chat.id.index]}));
this._updater.isCurrentPrivateProfile&&this._events.push({'type':"identity-changed",'args':[]});
if(this._updater.updates.sharedPreferences)for(const {roomKey:_0x2d5b1a,status:_0x45fa89}of this._updater.updates.sharedPreferences){
_0x45fa89.isFullyMuted&&this._events.push({'type':"room-preferences-changed",'args':["mute",_0x2d5b1a]}),!_0x45fa89.isFullyMuted&&this._events.push({'type':"room-preferences-changed",'args':["unmute",_0x2d5b1a]}),this._events.push({'type':"room-changed",'args':[HypercoreId.encode(_0x2d5b1a)]});
}
}
async._runPostUpdate(){
_0xf5fd39={
_0x140095:0xd5,_0x4b389b:0x1e1,_0x50c9c3:0x1},_0x3993b2={
_0xc5a877:0x1d6,_0x51c564:0x4f1}
;
try{
await this.base.update();
if(this.keet.closing)return;
await this._dispatch();
if(this.keet.closing)return;
await this._profileDispatch();
if(this.keet.closing)return;
if(this._updater.updates.tasks)for(const _0x2f89e5 of this._updater.updates.tasks){
_0x2f89e5.runBackground();
}
for(const {type:_0x1fc475,args:_0x271365}of this._events){
this.keet.emit(_0x1fc475,..._0x271365);
}
this._updater.changes.logout&&await this.keet.logout();
this._updater.updates.unreadKeys&&await this.keet.internalRefreshUnreads();
for(const {type:_0x1d2e58,value:_0x533a7a}of this._updater.events){
const _0x46044b={
'roomKey':this.base.key,'roomId':this.base.id,'roomInfo':this._updater.roomInfo,'value':_0x533a7a}
;
this.keet.emit(_0x1d2e58,_0x46044b);
}
}catch(_0x581322){
this.keet.onerror(_0x581322);
}
}
async.run(){
_0x13c028={
_0x2d71b1:0x16a,_0x224cab:0x146},_0x2a7e36={
_0x10402a:0x1a8,_0x106f41:0x576}
;
try{
this._updater=new RoomUpdater(this.keet,this.view,this.base,this.changes);
const _0x414dd4=await this._updater.run();
if(!_0x414dd4||this.closing)return;
await this._save();
if(this.closing)return;
if(this._swappedId)this.keet.internalSafeSwapPrivateProfiles();
this._generateEvents();
}finally{
await this.close();
}
}
.runPostUpdate(){
_0x10684d={
_0x37301b:0x74,_0x296019:0x19,_0x3c80f9:0x198},_0x2f7d06={
_0x562467:0xc1,_0x22ce1c:0x206}
;
queueMicrotask(this._runPostUpdate.bind(this));
}
}
;