Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import _ from 'lodash';
  4. import { uuid } from '/lib/util';
  5. import { store } from 'store';
  6.  
  7. class UrbitApi {
  8. setAuthTokens(authTokens) {
  9. this.authTokens = authTokens;
  10. this.bindPaths = [];
  11. }
  12.  
  13. // keep default bind to hall, since its bind procedure more complex for now AA
  14. bind(path, method, ship = this.authTokens.ship, appl = "hall", success, fail) {
  15. this.bindPaths = _.uniq([...this.bindPaths, path]);
  16.  
  17. window.subscriptionId = window.urb.subscribe(ship, appl, path,
  18. (err) => {
  19. fail(err);
  20. },
  21. (event) => {
  22. success({
  23. data: event,
  24. from: {
  25. ship,
  26. path
  27. }
  28. });
  29. },
  30. (err) => {
  31. fail(err);
  32. });
  33. }
  34.  
  35. addPendingMessage(data) {
  36. store.setState({
  37. pendingMessages: store.pendingMessages.append(data)
  38. });
  39. }
  40.  
  41. hall(data) {
  42. this.action("hall", "hall-action", data);
  43. }
  44.  
  45. chat(lis) {
  46. this.action("chat", "chat-action", {
  47. actions: {
  48. lis
  49. }
  50. });
  51. }
  52.  
  53. action(appl, mark, data) {
  54. return new Promise((resolve, reject) => {
  55. window.urb.poke(ship, appl, mark, data,
  56. (json) => {
  57. resolve(json);
  58. },
  59. (err) => {
  60. reject(err);
  61. });
  62. });
  63. }
  64.  
  65. notify(aud, bool) {
  66. this.hall({
  67. notify: {
  68. aud,
  69. pes: !!bool ? 'hear' : 'gone'
  70. }
  71. });
  72. }
  73.  
  74. permit(cir, aud, message) {
  75. this.hall({
  76. permit: {
  77. nom: cir,
  78. sis: aud,
  79. inv: true
  80. }
  81. });
  82.  
  83. if (message) {
  84. this.invite(cir, aud);
  85. }
  86. }
  87.  
  88. unpermit(cir, ship) {
  89. /*
  90. * lol, never send an unpermit to yourself.
  91. * it puts your ship into an infinite loop.
  92. * */
  93. if (ship === window.ship) {
  94. return;
  95. }
  96. this.hall({
  97. permit: {
  98. nom: cir,
  99. sis: [ship],
  100. inv: false
  101. }
  102. });
  103. }
  104.  
  105. invite(cir, aud) {
  106. let audInboxes = aud.map((aud) => `~${aud}/i`);
  107. let inviteMessage = {
  108. aud: audInboxes,
  109. ses: [{
  110. inv: {
  111. inv: true,
  112. cir: `~${window.ship}/${cir}`
  113. }
  114. }]
  115. };
  116.  
  117. this.hall({
  118. phrase: inviteMessage
  119. });
  120. }
  121.  
  122. source(nom, sub) {
  123. this.hall({
  124. source: {
  125. nom: "inbox",
  126. sub: sub,
  127. srs: [nom]
  128. }
  129. })
  130. }
  131.  
  132. delete(nom) {
  133. this.hall({
  134. delete: {
  135. nom,
  136. why: ''
  137. }
  138. })
  139. }
  140.  
  141. read(nom, red) {
  142. this.hall({
  143. read: {
  144. nom,
  145. red
  146. }
  147. })
  148. }
  149. }
  150.  
  151. export let api = new UrbitApi();
  152. window.api = api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement