您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tmsg_u.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. var mhndlr_opt_null = {
  2. from: undefined,
  3. handler: "onAll",
  4. id: null,
  5. name: null,
  6. // "http://jitsi.org/jitmeet"
  7. ns: null,
  8. options: {matchBareFromJid: false, ignoreNamespaceFragment: false},
  9. type: null,
  10. user: true,
  11. cstr:"test"
  12. };
  13. var mhndlr_opt_x = {
  14. cstr:"cmx",
  15. ns:"corner_mx",
  16. handler:mhndlr,
  17. }
  18. var dlog = nop
  19. // var dlog = nop
  20. // dlog = clog
  21. function attr_info(n){
  22. var k,v
  23. var ret ={}
  24. for ([k,v] of Object.entries(n.attributes)){
  25. ret[v.name]= v.value
  26. }
  27. return ret
  28. }
  29. function remove_handlers_cmx(_stropheConn) {
  30. // var state = window.glob_vhook.fns.getState()
  31. // var conference_0 = state["features/base/conference"]
  32. // var conference = conference_0.conference
  33. var k,v
  34. for (v of _stropheConn.handlers){
  35. if (v.cstr == "cmx"){
  36. _stropheConn.removeHandlers.push(v)
  37. }
  38. }
  39. // var _stropheConn = conference.xmpp.connection._stropheConn
  40. }
  41. function add_handler_m(handler) {
  42. // clog("add_handler_m...")
  43. // var state = window.glob_vhook.fns.getState()
  44. var state = APP.store.getState()
  45. var conference_0 = state["features/base/conference"]
  46. var conference = conference_0.conference
  47. var _stropheConn = conference.xmpp.connection._stropheConn
  48. var phx = _stropheConn.handlers[0].__proto__;
  49. var hxa=Object.create(phx)
  50. Object.assign(hxa,mhndlr_opt_null,handler)
  51. remove_handlers_cmx(_stropheConn)
  52. _stropheConn.addHandlers.push(hxa)
  53. return hxa
  54. }
  55. function mhndlr_rld_dflt(a0,a1,a2,a3,a4){
  56. clog("mhndlr... dflt")
  57. return true
  58. }
  59. function mhndlr(a0,a1,a2,a3,a4){
  60. setTimeout(window.mhndlr_rld.bind({a:"fake_that"},this,a0,a1,a2,a3,a4),1)
  61. return true
  62. }
  63. function init_mxu(){
  64. add_handler_m(mhndlr_opt_x)
  65. }
  66. function mhndlr_rld(that,m){
  67. try {
  68. var n = m.querySelector(`code`)
  69. if (!n){return}
  70. var parsed = JSON.parse(n.innerHTML)
  71. var ai = attr_info(m)
  72. var id = ai.from.split("/").pop()
  73. var o = parsed.payload
  74. if (window.mhndlr_rld_dbg){
  75. mhndlr_rld_dbg({m,that,n,parsed,ai,o,arguments})
  76. }
  77. // clog("mhndlr_rld:",parsed.type)
  78. switch (parsed.type){
  79. case "set_msg":
  80. pf._set_recv(o.path,o.key,o.val,id)
  81. glob_u.fns.msto_evh ? glob_u.fns.msto_evh(o,parsed,ai,id) : 0
  82. // glob_u.fns.msto_evh()
  83. break;
  84. case "event":
  85. dlog("~event~",parsed)
  86. if (window.dispatch_events){
  87. dispatch_events({parsed,ai,m,that})
  88. }
  89. break;
  90. case "msg_test":
  91. dlog("msg_test",parsed)
  92. break;
  93. case "db_sync_request":
  94. // tmsgx3({type:"db_sync_response",payload:pf.root})
  95. tmsgx3({type:"db_sync_response",payload:pf.root},{to:ai.from})
  96. // dlog("db_sync_request",parsed)
  97. clog("db_sync_request",parsed)
  98. break;
  99. case "db_sync_response":
  100. if (id == glob_u.data.local_id){
  101. dlog( "db_sync_response ...")
  102. break
  103. }
  104. if (glob_u.prom.init_db_resolve){
  105. glob_u.prom.init_db_resolve({aaa:"dsr",pl:parsed.payload})
  106. }
  107. clog("db_sync_response",ai.from == glob_u.data.local_id,ai.from,parsed)
  108. break;
  109. }
  110. } catch (err) {
  111. clog("mhndlr err:",err)
  112. }
  113. }
  114. function tmsgx3(data,opt_arg={},attrs={}){
  115. // return
  116. // clog("tmsgx3",{data,opt_arg,attrs})
  117. var dflt_opt = {
  118. tag:"code",
  119. // type:"chat",
  120. to:window.APP.conference._room.room.roomjid,
  121. }
  122. var opt = Object.assign({},dflt_opt,opt_arg)
  123. // clog("T:",data,opt,opt.type)
  124. if (!opt.type){
  125. // clog("NO TYPE")
  126. opt.to == dflt_opt.to ? opt.type = "groupchat" : opt.type = "chat"
  127. }
  128. // clog("T:",data,opt,opt.type)
  129. var ns = {xmlns:"corner_mx"}
  130. var msg = $build("message",{ to: opt.to,
  131. type: opt.type,
  132. // from: "nnnx@conference.jfidev.com/3db7bf15",
  133. // xtra: "nnnx@conference.jfidev.com/3db7bf15",
  134. });
  135. var payload
  136. if (typeof(data) == "string"){
  137. payload=data
  138. } else {
  139. payload=JSON.stringify(data)
  140. }
  141. Object.assign({},ns,attrs)
  142. msg.c(opt.tag, payload,Object.assign({},ns,attrs)).up();
  143. // console.log("zzzz")
  144. APP.conference._room.room.connection.send(msg)
  145. }
  146. isProxy = Symbol("isProxy")
  147. tlu = {
  148. object:"",
  149. string:true,
  150. number:true,
  151. undefined:true,
  152. boolean:true,
  153. }
  154. function match(mx,m,o){
  155. var k,v,k2,v2
  156. var r = 0
  157. for ([k,v] of Object.entries(m || {})){
  158. v2 = o[k]
  159. if (typeof(v) == "string" && typeof(v2) == "string") {
  160. r += !(v==v2)
  161. } else if (Array.isArray(v) && Array.isArray(v2)){
  162. r += !(isSubset_arr(v,v2))
  163. } else if (Array.isArray(v) && typeof(v2) == "string"){
  164. // r += !(isSubset_arr(v2,v))
  165. r += !v.includes(v2)
  166. } else if (Array.isArray(v2) && typeof(v) == "string"){
  167. r += !(v2.includes(v))
  168. }
  169. if (r){
  170. return !r
  171. }
  172. }
  173. return !r
  174. }
  175. function isSubset(subset,superset){
  176. for (var elem of subset) {
  177. if (!superset.includes(elem)) {
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. function isSubset_arr(subset,superset){
  184. for (var elem of subset) {
  185. if (Array.isArray(elem)){
  186. if (!new Set(elem).intersection(superset).size){
  187. return false
  188. }
  189. } else if (!superset.includes(elem)) {
  190. return false;
  191. }
  192. }
  193. return true;
  194. }
  195. function type_info(o){
  196. try {
  197. var t
  198. var flags = {
  199. leaf:false,
  200. deep_not_prx:false,
  201. prx:false,
  202. isObj:false,
  203. isArr:false,
  204. els:false,
  205. }
  206. t = typeof(o)
  207. flags.t=t
  208. // window?.glob_mx?.flags?.type_info_dbg ? clog("type_info") : 0
  209. if (!(o === null)){
  210. flags.isArr = Array.isArray(o)
  211. flags.isObj = Object.prototype == o.__proto__
  212. }
  213. if (tlu[t] === true || o === null){
  214. flags.leaf = true
  215. } else if (t == "object" && o.__prx){
  216. flags.prx = true
  217. } else if (t == "object" && (flags.isArr || flags.isObj)){
  218. flags.prx = false
  219. flags.deep_not_prx = true
  220. } else {
  221. clog("ELSE")
  222. flags.els = true
  223. }
  224. return flags
  225. // !window?.glob_mx?.flags?.type_info_dbg ? clog("type_info") : 0
  226. } catch (err){
  227. clog("type_info err:",err,{flags,o})
  228. }
  229. // clog(t,flags)
  230. }
  231. function deep_prop_v0(o,p){
  232. try {
  233. var arr = jc(p)
  234. var ret = o
  235. while (arr.length){
  236. // clog("deep_prop",arr,ret)
  237. ret = ret[arr.shift()]
  238. }
  239. // clog("deep_prop ret",ret)
  240. return ret
  241. } catch(err){console.error("DEEP_PROP ERR:",err)}
  242. }
  243. delete Proxy_Factory
  244. {
  245. // TODO:JFI remove fn
  246. function init_participant(path,key,val){
  247. //
  248. // is this a real function
  249. // console.log("init_db_local _set_pub?",path)
  250. var path_join = path.join(".") + "." + key
  251. clog("_set_pub_log",path_join,{path,key,val})
  252. // console.trace("_set_pub_trv",path_join)
  253. if (path == "participants" && path.length == 1){
  254. // console.trace("init_db_local _set_pub")
  255. // console.log("init_db_local _set_pub")
  256. }
  257. }
  258. class Proxy_Factory {
  259. constructor(o,n="dflt"){
  260. this.db = o
  261. this.n = n
  262. this.hidden_props = ["add_proxy","_set","walker","walker_start","prxy","_sett","_sett2"]
  263. this._pub_cb =[]
  264. this.__prx = true
  265. this.SymPrx = Symbol("SymPrx")
  266. // clog("pfx",{o,n,that:this})
  267. }
  268. init(o){
  269. this.root = o
  270. }
  271. sync(o){
  272. this.root = jc(o.root)
  273. o._pub_cb.push(this._set_recv.bind(this))
  274. }
  275. get prxy(){
  276. return this
  277. }
  278. _set(o,k,v,prx,a0){
  279. this.obj[k]=prx
  280. }
  281. _sett(o,k,v,prx,a0){
  282. dlog("_SETT",this,arguments)
  283. }
  284. _sett2(o,k,v,prx,a0){
  285. dlog("_SETT2",this,arguments)
  286. }
  287. set_pub(obj, prop, val,receiver){
  288. this._set_pub(obj.__path_arr, prop, jx(val))
  289. }
  290. _set_pub(path,key,val){
  291. var k,v
  292. // init_participant(path,key,val)
  293. var arg = {
  294. "type":"set_msg",
  295. "payload":{path,key,val},}
  296. tmsgx3(arg,
  297. // {},
  298. )
  299. for (v of this._pub_cb){
  300. v(path,key,val)
  301. }
  302. }
  303. _set_recv(path,prop,val,id){
  304. // return
  305. /*
  306. if (id == glob_u.data.local_id){
  307. clog("_set_recv:local_id",{path,prop,val,id})
  308. return
  309. }
  310. */
  311. var p = path
  312. var nprop = deep_prop_v0(this.root,p)
  313. var dpth = nprop.__dpth
  314. if (tlu[typeof(val)]){
  315. nprop._set(nprop,prop,val,val)
  316. } else {
  317. nprop._set(nprop,prop,val,new Proxy(val,this))
  318. nprop[prop].add_proxy(nprop,prop,val,dpth+1)
  319. }
  320. // var tf = {}
  321. // this.walker(nprop[prop],tf,dpth+2)
  322. this.walker(nprop[prop],{},dpth+2)
  323. }
  324. set(obj, prop, val,receiver){
  325. // dlog("prx_db set ",this,obj, prop, val,receiver,val.__prx)
  326. // clog("prx_db set ",{that:this,obj, prop, val,receiver},val.__prx)
  327. // clog("prx_db set ",obj.__path_arr)
  328. if (obj[prop] === val){
  329. return Reflect.set(...arguments)
  330. }
  331. this.set_pub(obj, prop, val,receiver)
  332. this.event_proc("set")
  333. if ("__prx" == prop){
  334. this.__prx=val
  335. return this.__prx
  336. }
  337. if (typeof(val) === "object" && val != null && !(val.__prx)){
  338. var ret = Reflect.set(...arguments)
  339. this.set_obj(obj, prop, val,receiver)
  340. return ret
  341. }
  342. return Reflect.set(...arguments)
  343. }
  344. get(obj, prop,receiver){
  345. // dlog("prx_db get ...",prop==isProxy,this,obj, prop,receiver)
  346. if (this.hidden_props.includes(prop)){
  347. switch (prop){
  348. case "add_proxy":
  349. return this.add_proxy.bind(this)
  350. break;
  351. case "prxy":
  352. return this
  353. case "_set":
  354. return this._set.bind({that:this,obj, prop,receiver})
  355. case "_sett":
  356. return this._sett.bind({that:this,obj, prop,receiver})
  357. case "_sett2":
  358. return this._sett2.bind({that:this})
  359. case "walker_start":
  360. return this.walker_start
  361. case "walker":
  362. return this.walker
  363. break;
  364. }
  365. }
  366. if (isProxy == prop){ return true }
  367. if ("__prx" == prop){ return this.__prx }
  368. return Reflect.get(...arguments)
  369. }
  370. add_proxy(o,k,v,dpth){
  371. // clog("~add_proxy~",o,k,v,dpth)
  372. // clog("~add_proxy~",o.__prx,o,k,v,dpth,xpath)
  373. var xpath = o.__path
  374. var apath = o.__path_arr
  375. this.event_proc("add_proxy")
  376. if (!apath){
  377. apath=[]
  378. }
  379. if (!xpath){
  380. xpath="ROOT!"
  381. }
  382. Object.defineProperty(v,"__path_arr",Object.assign({value:apath.concat(k),},this.baseProp))
  383. Object.defineProperty(v,"__path",Object.assign({value:xpath+":"+k,},this.baseProp))
  384. Object.defineProperty(v,"__dpth",Object.assign({value:dpth,},this.baseProp))
  385. // Object.defineProperty(v,"__dbg",Object.assign({value:{},},this.baseProp))
  386. }
  387. set_obj(obj, prop, val,receiver){
  388. var dpth = obj.__dpth
  389. var tf = {}
  390. this.walker(receiver,tf,dpth)
  391. }
  392. walker(o,tfo,dpth=0){
  393. if (dpth > 7){
  394. console.error("MAX DEPTH")
  395. return
  396. }
  397. var k,v
  398. var tf
  399. for ([k,v] of Object.entries(o)){
  400. tf = type_info(v)
  401. if (tf.deep_not_prx){
  402. o._set(o,k,v,new Proxy(v,this.prxy))
  403. o[k].add_proxy(o,k,v,dpth)
  404. this.walker(o[k],tf,dpth+1)
  405. }
  406. }
  407. }
  408. walker_start(o,dpth){
  409. var tf = type_info(o)
  410. o.add_proxy({},[],o,-1)
  411. this.walker(o,tf,0)
  412. }
  413. event_proc(e){}
  414. }
  415. window.Proxy_Factory = Proxy_Factory
  416. Proxy_Factory.prototype.baseProp={
  417. writable: true,
  418. // enumerable: true,
  419. enumerable: false,
  420. configurable: true,
  421. }
  422. }
  423. function arr_match(m,o){
  424. var k,v
  425. var r = 0
  426. for ([k,v] of Object.entries(m || {})){
  427. if (o[k] != v.valueOf()){
  428. r += 1
  429. return !r
  430. }
  431. // for ([k,v] in m){
  432. // case
  433. // dlog(k,v)
  434. }
  435. return !r
  436. }
  437. permissions_def = {
  438. participants:{
  439. m:["participants"],
  440. fn(set_scope,match){
  441. // clog("FN....",match.fp,match.fp[1] == window.glob_mx.local_id)
  442. // if (match.fp[1] != window.glob_u.data.local_id)
  443. if (match.fp[1] != window.glob_u.data.local_id && 0)
  444. throw "permission err"
  445. return
  446. },
  447. }
  448. }
  449. //
  450. // msto.my_data.prxy.__proto__.__proto__.set.call(msto.my_data.prxy,msto.participants,"abc2",{})
  451. permissions_def__set = {
  452. participants:{
  453. m:["participants"],
  454. fn(set_scope,match){
  455. dlog("FN....",match.fp,match.fp[1] == window.glob_u.data.local_id)
  456. if (match.fp[1] != window.glob_u.data.local_id)
  457. // throw "permission err"
  458. return
  459. },
  460. }
  461. }
  462. {
  463. class Proxy_Permissions extends Proxy_Factory {
  464. constructor(a,a1){
  465. super(...arguments)
  466. }
  467. set(obj, prop, val,receiver){
  468. // clog("PRX_PERM",obj.__path_arr,prop)
  469. // clog("PRX_PERM",{obj, prop, val,receiver})
  470. var k,v
  471. var r
  472. var matched
  473. var fp = obj.__path_arr.concat(prop)
  474. for ([k,v] of Object.entries(permissions_def)){
  475. matched = arr_match(v.m,fp)
  476. if (matched){
  477. r = v.fn({that:this,obj, prop, val,receiver},{fp,k,v}) || {}
  478. }
  479. // if (r.return)
  480. }
  481. // case
  482. // if (obj.__path_arr && obj.__path_arr.length){}
  483. return super.set(obj, prop, val,receiver)
  484. }
  485. _set(){
  486. // clog("Proxy_Permissions _set")
  487. super._set(...arguments)
  488. }
  489. }
  490. window.Proxy_Permissions = Proxy_Permissions
  491. }