|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | + * State vars: |
| 19 | + * 'presence' : string (online|offline|unavailable etc) |
| 20 | + * 'active' : number (ms ago; can be -1) |
| 21 | + */ |
| 22 | + |
| 23 | +'use strict'; |
| 24 | +var MatrixClientPeg = require("../../MatrixClientPeg"); |
| 25 | +var dis = require("../../dispatcher"); |
| 26 | + |
| 27 | +module.exports = { |
| 28 | + componentDidMount: function() { |
| 29 | + var self = this; |
| 30 | + function updateUserState(event, user) { |
| 31 | + if (!self.props.member) { return; } |
| 32 | + |
| 33 | + if (user.userId === self.props.member.userId) { |
| 34 | + self.setState({ |
| 35 | + presence: user.presence, |
| 36 | + active: user.lastActiveAgo |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + MatrixClientPeg.get().on("User.presence", updateUserState); |
| 41 | + this.userPresenceFn = updateUserState; |
| 42 | + |
| 43 | + if (this.props.member) { |
| 44 | + var usr = MatrixClientPeg.get().getUser(this.props.member.userId); |
| 45 | + if (!usr) { |
| 46 | + return; |
| 47 | + } |
| 48 | + this.setState({ |
| 49 | + presence: usr.presence, |
| 50 | + active: usr.lastActiveAgo |
| 51 | + }); |
| 52 | + } |
| 53 | + }, |
| 54 | + |
| 55 | + componentWillUnmount: function() { |
| 56 | + MatrixClientPeg.get().removeListener("User.presence", this.userPresenceFn); |
| 57 | + }, |
| 58 | + |
| 59 | + onChatClick: function() { |
| 60 | + // check if there are any existing rooms with just us and them (1:1) |
| 61 | + // If so, just view that room. If not, create a private room with them. |
| 62 | + var rooms = MatrixClientPeg.get().getRooms(); |
| 63 | + var userIds = [ |
| 64 | + this.props.member.userId, |
| 65 | + MatrixClientPeg.get().credentials.userId |
| 66 | + ]; |
| 67 | + var existingRoomId = null; |
| 68 | + for (var i = 0; i < rooms.length; i++) { |
| 69 | + var members = rooms[i].getJoinedMembers(); |
| 70 | + if (members.length === 2) { |
| 71 | + var hasTargetUsers = true; |
| 72 | + for (var j = 0; j < members.length; j++) { |
| 73 | + if (userIds.indexOf(members[j].userId) === -1) { |
| 74 | + hasTargetUsers = false; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + if (hasTargetUsers) { |
| 79 | + existingRoomId = rooms[i].roomId; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (existingRoomId) { |
| 86 | + dis.dispatch({ |
| 87 | + action: 'view_room', |
| 88 | + room_id: existingRoomId |
| 89 | + }); |
| 90 | + } |
| 91 | + else { |
| 92 | + MatrixClientPeg.get().createRoom({ |
| 93 | + invite: [this.props.member.userId], |
| 94 | + preset: "private_chat" |
| 95 | + }).done(function(res) { |
| 96 | + dis.dispatch({ |
| 97 | + action: 'view_room', |
| 98 | + room_id: res.room_id |
| 99 | + }); |
| 100 | + }, function(err) { |
| 101 | + console.error( |
| 102 | + "Failed to create room: %s", JSON.stringify(err) |
| 103 | + ); |
| 104 | + }); |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + getInitialState: function() { |
| 109 | + return { |
| 110 | + presence: "offline", |
| 111 | + active: -1 |
| 112 | + } |
| 113 | + } |
| 114 | +}; |
| 115 | + |
0 commit comments