Matrix.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Component, createRef } from "inferno";
  2. import TabView from "./TabView";
  3. import ListView from "./ListView";
  4. import Header from "./ui/Header";
  5. import SoftKey from "./ui/SoftKey";
  6. import TextListItem from "./ui/TextListItem";
  7. import About from "./About";
  8. import DMsView from "./DMsView";
  9. import colors from "KaiUI/src/theme/colors.scss";
  10. import requestFunc from "./requestFunc";
  11. import CallScreen from "./CallScreen";
  12. import personIcon from "./person_icon.png";
  13. import { pushSubscribe } from "./utils";
  14. import * as localforage from "localforage";
  15. import * as matrixcs from "matrix-js-sdk";
  16. function makeCallScreen(call, softKeyRef, callEndCb, incoming) {
  17. const client = window.mClient;
  18. const roomCall = client.getRoom(call.roomId);
  19. const theOther = client.getUser(roomCall.guessDMUserId());
  20. const avatarUrl = theOther.avatarUrl;
  21. const displayName = theOther.displayName || theOther.userId;
  22. let avatarSrc;
  23. if (avatarUrl) {
  24. avatarSrc = matrixcs.getHttpUriForMxc(
  25. client.getHomeserverUrl(),
  26. avatarUrl,
  27. 180,
  28. 180,
  29. "scale"
  30. );
  31. } else {
  32. avatarSrc = personIcon;
  33. }
  34. return (
  35. <CallScreen
  36. name={displayName}
  37. avatarSrc={avatarSrc}
  38. call={call}
  39. softKeyRef={softKeyRef}
  40. incoming={incoming}
  41. callEndCb={callEndCb}
  42. />
  43. );
  44. }
  45. class Matrix extends Component {
  46. handleTabIndexChange = (tabIndex) => {
  47. this.setState({ currentTab: tabIndex });
  48. };
  49. placeCall = (roomId) => {
  50. let call = matrixcs.createNewMatrixCall(window.mClient, roomId);
  51. let audio = new Audio();
  52. audio.mozAudioChannelType = "telephony";
  53. call.setRemoteAudioElement(audio);
  54. call.placeVoiceCall();
  55. this.callScreen = makeCallScreen(
  56. call,
  57. this.softKeyRef,
  58. this.callEnd,
  59. false
  60. );
  61. this.setState({ isCall: true });
  62. };
  63. centerCb = () => {
  64. this.headerRef.current.setState({
  65. blink: !this.headerRef.current.state.blink,
  66. });
  67. };
  68. onTabChange = (currentTab) => {
  69. this.currentTab = this.tabLabels[currentTab];
  70. };
  71. openRoom = (roomId) => {
  72. console.log("OPENING", roomId);
  73. };
  74. callEnd = (err) => {
  75. if (err) {
  76. alert("Call ended with Error!");
  77. }
  78. this.callScreen = null;
  79. this.setState({ isCall: false });
  80. };
  81. constructor(props) {
  82. super(props);
  83. const { login } = props;
  84. window.mClient = matrixcs.createClient({
  85. request: requestFunc,
  86. userId: login.user_id,
  87. accessToken: login.access_token,
  88. deviceId: login.device_id,
  89. baseUrl: login.well_known["m.homeserver"].base_url,
  90. identityServer:
  91. login.well_known["m.identity_server"] &&
  92. login.well_known["m.identity_server"].base_url,
  93. });
  94. this.tabLabels = ["People", "Rooms", "Invites", "Settings", "About"];
  95. this.headerRef = createRef();
  96. this.header = (
  97. <Header
  98. text={"Welcome!"}
  99. backgroundColor={colors.headerPurple}
  100. ref={this.headerRef}
  101. />
  102. );
  103. this.softKeyRef = createRef();
  104. this.softKey = (
  105. <SoftKey
  106. centerText={"blink"}
  107. centerCb={this.centerCb}
  108. ref={this.softKeyRef}
  109. />
  110. );
  111. this.callScreen = null;
  112. pushSubscribe();
  113. this.state = { isCall: false, syncDone: false };
  114. }
  115. componentWillMount() {
  116. const client = window.mClient;
  117. client.on("RoomMember.membership", (event, member) => {
  118. const myId = window.mClient.getUserId();
  119. if (member.membership === "invite" && member.userId === myId) {
  120. window.mClient
  121. .joinRoom(member.roomId)
  122. .then(() => alert(`Auto joined ${member.name}`))
  123. .catch(() => {});
  124. }
  125. });
  126. client.once("sync", (state, prevState, res) => {
  127. this.setState({ syncDone: true });
  128. });
  129. client.on("Call.incoming", (call) => {
  130. console.log("INCOMING", call);
  131. call.once("state", (state) => {
  132. if (this.state.isCall || call.type !== "voice") {
  133. call.reject();
  134. } else {
  135. this.callScreen = makeCallScreen(
  136. call,
  137. this.softKeyRef,
  138. this.callEnd,
  139. true
  140. );
  141. this.setState({ isCall: true });
  142. }
  143. });
  144. });
  145. client.on("Session.logged_out", () => {
  146. console.log("Logging out"); // FIXME
  147. localforage.removeItem("login").then(() => {
  148. window.location = window.location; // eslint-disable-line no-self-assign
  149. });
  150. });
  151. client.startClient({ initialSyncLimit: 3, lazyLoadMembers: true });
  152. }
  153. render() {
  154. if (!this.state.syncDone) {
  155. return (
  156. <div>
  157. {this.header}
  158. <div>Please wait I am syncing your account</div>
  159. <footer>{this.softKey}</footer>
  160. </div>
  161. );
  162. }
  163. if (this.state.isCall) {
  164. console.log("CALL SCREEN IS ", this.callScreen);
  165. return (
  166. <div>
  167. {this.header}
  168. {this.callScreen}
  169. <footer>{this.softKey}</footer>
  170. </div>
  171. );
  172. }
  173. return (
  174. <div>
  175. {this.header}
  176. <TabView
  177. tabLabels={this.tabLabels}
  178. onTabChange={this.onTabChange}
  179. $HasNonKeyedChildren
  180. >
  181. <DMsView
  182. updateFocusRoomCb={this.updateFocusDM}
  183. softKeyRef={this.softKeyRef}
  184. openRoom={this.openRoom}
  185. placeCall={this.placeCall}
  186. />
  187. <ListView cursor={0}>
  188. <TextListItem primary={"a room"} />
  189. <TextListItem primary={"another room"} />
  190. </ListView>
  191. <ListView cursor={0}>
  192. <TextListItem primary={"an invite"} />
  193. </ListView>
  194. <ListView cursor={0}>
  195. <TextListItem primary={"some options"} />
  196. </ListView>
  197. <About softKeyRef={this.softKeyRef} />
  198. </TabView>
  199. <footer $HasVNodeChildren>{this.softKey}</footer>
  200. </div>
  201. );
  202. }
  203. }
  204. export default Matrix;