Skip to content

Commit 08c16e0

Browse files
committed
Hook up presence/last active up (live updating; no ticker for last active).
1 parent 87dd9e8 commit 08c16e0

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

‎skins/base/views/molecules/MemberInfo.js‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,44 @@ limitations under the License.
1919
var React = require('react');
2020

2121
var MatrixClientPeg = require("../../../../src/MatrixClientPeg");
22-
//var MemberInfoController = require("../../../../src/controllers/molecules/MemberInfo");
22+
var MemberInfoController = require("../../../../src/controllers/molecules/MemberInfo");
2323

2424
module.exports = React.createClass({
2525
displayName: 'MemberInfo',
26-
//mixins: [MemberInfoController],
26+
mixins: [MemberInfoController],
27+
28+
getDuration: function(time) {
29+
if (!time) return;
30+
var t = parseInt(time / 1000);
31+
var s = t % 60;
32+
var m = parseInt(t / 60) % 60;
33+
var h = parseInt(t / (60 * 60)) % 24;
34+
var d = parseInt(t / (60 * 60 * 24));
35+
if (t < 60) {
36+
if (t < 0) {
37+
return "0s";
38+
}
39+
return s + "s";
40+
}
41+
if (t < 60 * 60) {
42+
return m + "m";
43+
}
44+
if (t < 24 * 60 * 60) {
45+
return h + "h";
46+
}
47+
return d + "d ";
48+
},
2749

2850
render: function() {
2951
var power;
3052
if (this.props.member) {
3153
var img = "img/p/p" + Math.floor(20 * this.props.member.powerLevelNorm / 100) + ".png";
3254
power = <img src={ img } className="mx_MemberTile_power" width="48" height="48" alt=""/>;
3355
}
56+
var activeAgo = "unknown";
57+
if (this.state.active >= 0) {
58+
activeAgo = this.getDuration(this.state.active);
59+
}
3460

3561
return (
3662
<div className="mx_MemberInfo">
@@ -41,8 +67,8 @@ module.exports = React.createClass({
4167
width="128" height="128" alt=""/>
4268
</div>
4369
<div className="mx_MemberInfo_field">{this.props.member.userId}</div>
44-
<div className="mx_MemberInfo_field">Presence: {this.props.member.presence}</div>
45-
<div className="mx_MemberInfo_field">Last active: {this.props.member.last_active_ago}</div>
70+
<div className="mx_MemberInfo_field">Presence: {this.state.presence}</div>
71+
<div className="mx_MemberInfo_field">Last active: {activeAgo}</div>
4672
<div className="mx_MemberInfo_button">Start chat</div>
4773
</div>
4874
);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
26+
module.exports = {
27+
componentDidMount: function() {
28+
var self = this;
29+
function updateUserState(event, user) {
30+
if (!self.props.member) { return; }
31+
32+
if (user.userId === self.props.member.userId) {
33+
self.setState({
34+
presence: user.presence,
35+
active: user.lastActiveAgo
36+
});
37+
}
38+
}
39+
MatrixClientPeg.get().on("User.presence", updateUserState);
40+
this.userPresenceFn = updateUserState;
41+
42+
if (this.props.member) {
43+
var usr = MatrixClientPeg.get().getUser(this.props.member.userId);
44+
if (!usr) {
45+
return;
46+
}
47+
this.setState({
48+
presence: usr.presence,
49+
active: usr.lastActiveAgo
50+
});
51+
}
52+
},
53+
54+
componentWillUnmount: function() {
55+
MatrixClientPeg.get().removeListener("User.presence", this.userPresenceFn);
56+
},
57+
58+
getInitialState: function() {
59+
return {
60+
presence: "offline",
61+
active: -1
62+
}
63+
}
64+
};
65+

0 commit comments

Comments
 (0)