-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunorg.min.js
More file actions
1 lines (1 loc) · 5.65 KB
/
runorg.min.js
File metadata and controls
1 lines (1 loc) · 5.65 KB
1
var RunOrg=MakeRunOrg(jQuery);function MakeRunOrg(j){function p(t,s,r){if(typeof s=="string"){s={id:s}}s=s||{};r.forEach(function(u){t[u]=s[u]||null})}function a(t,r){var s={};p(s,t,r);return s}function q(t){var s=j.Deferred();s.resolve(t);return s}function d(r){if(typeof r=="object"&&r!==null&&"id" in r){return r.id}return r}function e(){return +new Date()}var f;function i(y,t,w,x){var s="?";var r=j.Deferred();var u;var v;w=w||{};if(RunOrg.clock&&f<e()){RunOrg.clock=null}if(typeof t=="string"){t=[t]}t=RunOrg.endpoint+"/db/"+RunOrg.db+"/"+t.join("/");RunOrg.clock&&(w.at=RunOrg.clock);RunOrg.token&&(w.as=RunOrg.as);for(u in w){if(w[u]===null){continue}t+=s+u+"="+w[u];s="&"}v={url:t,dataType:"json",type:y,beforeSend:function(z){RunOrg.token&&z.setRequestHeader("Authorization","RUNORG token="+RunOrg.token)}};if(y>"P"){v.data=JSON.stringify(x);v.contentType="application/json"}j.ajax(v).always(function(C,B,A){var F=("responseJSON" in C)?C:A,E=B=="success";if(E&&F.status<500){var D=F.responseJSON;if("at" in D){var G=RunOrg.clock?JSON.parse(RunOrg.clock):{},z=D.at;for(k in z){if(!(k in G)||G[k]<z[k]){G[k]=z[k]}}RunOrg.clock=JSON.stringify(G);f=e()+60000}if("token" in D&&"self" in D){RunOrg.token=D.token;RunOrg.as=D.self.id}r.resolve(D)}else{r.reject(E?{HTTP:F.status}:{error:B})}});return r.promise()}function n(r){p(this,r,["id","label","gender","pic"])}n.prototype=Object.create({Load:function(){var r=this;return i("GET",["people",this.id]).then(function(s){n.call(r,s);return n.Cache(r)})}});n.Search=function(r,s){s=s||{};s.q=r;return i("GET","people/search",a(s,["q","limit"])).then(function(t){return t.list.map(n.Cache)})};n.List=function(r){r=r||{};return i("GET","people",a(r,["limit","offset"])).then(function(s){return s.list.map(n.Cache)})};n.Cache=function(s){var r=new n(s);c[r.id]=r;return r};var c={};n.Get=function(r){if(r in c){return promised(c[r])}return new n(r).Load()};function m(r){p(this,r,["id","label","count","audience","access"])}m.prototype=Object.create({Create:function(){var r=this;return i("POST","groups",{},a(this,["id","label","audience"])).then(function(s){m.call(r,s);return r})},List:function(r){r=r||{};return i("GET",["groups",this.id],a(r,["limit","offset"])).then(function(s){return s.list.map(n.Cache)})},Load:function(){var r=this;return i("GET",["groups",this.id,"info"]).then(function(s){m.call(r,s);return r})},Save:function(){return i("PUT",["groups",this.id,"info"],{},a(this,["label","audience"]))},Delete:function(){return i("DELETE",["groups",this.id])},Add:function(r){return this.AddMany([r])},Remove:function(r){return this.RemoveMany([r])},AddMany:function(r){return i("POST",["groups",this.id,"add"],{},r.map(d))},RemoveMany:function(r){return i("POST",["groups",this.id,"remove"],{},r.map(d))},});m.List=function(r){r=r||{};return i("GET","groups",a(r,["limit","offset"])).then(function(s){return s.list.map(function(t){return new m(t)})})};var l={Persona:function(r){return i("POST","people/auth/persona",{},{assertion:r}).then(function(s){return n.Cache(s.self)})},HMAC:function(r){r=a(r,["id","expires","key","proof"]);return i("POST","people/auth/hmac",{},r).then(function(s){return n.Cache(s.self)})}};function o(r){p(this,r,["id","key"])}o.prototype=Object.create({Create:function(){var r=this;return i("POST","keys",{},{key:this.key,hash:"SHA-1",encoding:"hex"}).then(function(s){o.call(r,s);return r})}});function b(r){p(this,r,["id"])}b.prototype=Object.create({Owner:function(){return i("GET",["tokens",this.id]).then(function(r){return n.Cache(r.self)})},Delete:function(){return i("DELETE",["tokens",this.id])}});function g(t){p(this,t,["id","chat","author","time","body","track","custom","tree"]);var r=this.tree=this.tree||{count:0,top:[]};var s=this.chat;if(r.top.length>0){r.top=r.top.map(function(u){u.chat=s;return new g(u)})}}g.prototype=Object.create({Load:function(){var r=this;return i("GET",["chat",this.chat,"posts",this.id]).then(function(s){g.call(r,s.info);s.people.forEach(n.Cache);return r})},Delete:function(){return i("DELETE",["chat",this.chat,"posts",this.id])},Reply:function(s){var r=this;s=a(s,["body","custom"]);s.reply=this.id;return i("POST",["chat",this.chat,"posts"],{},s).then(function(t){return new h.Post({body:s.body,custom:s.custom||null,id:t.id,chat:r.chat})})},Track:function(r){return i("POST",["chat",this.chat,"posts",this.id,"track"],{},r)},Replies:function(s){var r=this;s=a(s||{},["limit","offset"]);s.under=r.id;return i("GET",["chat",this.chat,"posts"],s).then(function(t){t.people.forEach(n.Cache);return t.posts.map(function(u){u.chat=r.chat;return new h.Post(u)})})}});function h(r){p(this,r,["id","subject","custom","audience","count","last","access","track"])}h.prototype=Object.create({Load:function(){var r=this;return i("GET",["chat",this.id]).then(function(s){h.call(r,s.info);return r})},Save:function(){return i("PUT",["chat",this.id],{},a(this,["subject","custom","audience"]))},Create:function(){var r=this;return i("POST","chat",{},a(this,["subject","custom","audience"])).then(function(s){r.id=s.id;return r})},Delete:function(){return i("DELETE",["chat",this.id])},Track:function(r){return i("POST",["chat",this.id,"track"],{},r)},Posts:function(s){var r=this;s=s||{};return i("GET",["chat",this.id,"posts"],a(s,["limit","offset"])).then(function(t){t.people.forEach(n.Cache);return t.posts.map(function(u){u.chat=r.id;return new h.Post(u)})})},Post:function(s){var r=this;return i("POST",["chat",this.id,"posts"],{},a(s,["body","custom"])).then(function(t){return new h.Post({body:s.body,custom:s.custom||null,id:t.id,chat:r.id})})}});h.List=function(r){r=r||{};return i("GET","chat",a(r,["limit","offset"])).then(function(s){return s.list.map(function(t){return new h(t)})})};h.Post=g;return{Person:n,Group:m,Auth:l,Key:o,Token:b,Chat:h}};