show users currently online in a topic

This commit is contained in:
or-else
2018-01-20 17:24:34 -08:00
parent e5c98ba2d1
commit 6a2681ab56
2 changed files with 78 additions and 4 deletions
+10
View File
@@ -882,6 +882,16 @@ table.permission-editor td.checkbox {
margin-right: 1rem; margin-right: 1rem;
} }
#topic-users .avatar-box {
display: inline-block;
width: 1.8rem;
min-width: 1.8rem;
height: 1.8rem;
min-height: 1.8rem;
font-size: 1rem;
margin-right: 0.2rem;
}
/* Scrollable panel where the messages are shown */ /* Scrollable panel where the messages are shown */
#messages-panel { #messages-panel {
position: relative; position: relative;
+68 -4
View File
@@ -916,6 +916,38 @@
}); });
/* END ChipInput */ /* END ChipInput */
/* BEGIN GroupSubs: a list of group subscribers currently online */
var GroupSubs = React.createClass({
getInitialState: function() {
return {
onlineSubs: this.props.subscribers ? this.props.subscribers : []
};
},
componentWillReceiveProps: function(nextProps) {
this.setState({onlineSubs: nextProps.subscribers ? nextProps.subscribers : []});
},
render: function() {
var usersOnline = [];
this.state.onlineSubs.map(function(sub) {
usersOnline.push(
<div className="avatar-box">
<LetterTile
topic={sub.user}
avatar={makeImageUrl(sub.public ? sub.public.photo : null)}
title={sub.public ? sub.public.fn : null}
key={sub.user} />
</div>
);
});
return (
<div id="topic-users">{usersOnline}</div>
);
}
});
/* END GroupSubs */
/* BEGIN Login: a login form */ /* BEGIN Login: a login form */
var LoginView = React.createClass({ var LoginView = React.createClass({
getInitialState: function() { getInitialState: function() {
@@ -2619,6 +2651,7 @@
getInitialState: function() { getInitialState: function() {
return { return {
messages: [], messages: [],
onlineSubs: [],
topic: '', topic: '',
title: '', title: '',
avatar: null, avatar: null,
@@ -2656,7 +2689,7 @@
}, },
propsChange: function(props) { propsChange: function(props) {
if (!props || !props.topic) { if (!props || !props.topic) {
this.setState({messages: [], topic: null}); this.setState({messages: [], onlineSubs: [], topic: null});
this.leave(); this.leave();
return; return;
} }
@@ -2671,15 +2704,24 @@
var topic = Tinode.getTopic(props.topic); var topic = Tinode.getTopic(props.topic);
if (props.topic != this.state.topic) { if (props.topic != this.state.topic) {
var msgs = []; var msgs = [];
var subs = [];
// Bind the new topic to component. // Bind the new topic to component.
topic.onData = this.handleNewMessage; topic.onData = this.handleNewMessage;
topic.onInfo = this.handleInfoReceipt; topic.onInfo = this.handleInfoReceipt;
topic.onMetaDesc = this.handleDescChange; topic.onMetaDesc = this.handleDescChange;
topic.onSubsUpdated = this.handleSubsUpdated; topic.onSubsUpdated = this.handleSubsUpdated;
topic.onPres = this.handleSubsUpdated;
// Unbind the previous topic from this component. // Unbind the previous topic from this component.
this.leave(); this.leave();
this.handleDescChange(topic); this.handleDescChange(topic);
topic.subscribers(function(sub) {
if (sub.online) {
subs.push(sub);
}
});
topic.messages(function(msg) { topic.messages(function(msg) {
if (!msg.deleted) { if (!msg.deleted) {
msgs = msgs.concat(msg); msgs = msgs.concat(msg);
@@ -2688,6 +2730,7 @@
this.setState({ this.setState({
messages: msgs, messages: msgs,
onlineSubs: subs,
topic: props.topic, topic: props.topic,
imagePreview: null, imagePreview: null,
scrollPosition: 0 scrollPosition: 0
@@ -2726,6 +2769,7 @@
oldTopic.onInfo = undefined; oldTopic.onInfo = undefined;
oldTopic.onMetaDesc = undefined; oldTopic.onMetaDesc = undefined;
oldTopic.onSubsUpdated = undefined; oldTopic.onSubsUpdated = undefined;
oldTopic.onPres = undefined;
} }
} }
}, },
@@ -2775,9 +2819,24 @@
} }
}, },
handleSubsUpdated: function(uids) { handleSubsUpdated: function() {
// console.log("topic subscriptions updated"); if (this.state.topic) {
var subs = [];
var myId = this.props.myUserId;
var topic = Tinode.getTopic(this.state.topic);
topic.subscribers(function(sub) {
console.log("processing sub", sub);
if (sub.online && sub.user != myId) {
console.log("sub added", sub);
subs.push(sub);
}
});
this.setState({onlineSubs: subs});
console.log("subs in list:", subs);
}
console.log("topic subscriptions updated");
}, },
handleNewMessage: function(msg) { handleNewMessage: function(msg) {
// Regenerate messages list // Regenerate messages list
var topic = Tinode.getTopic(this.state.topic); var topic = Tinode.getTopic(this.state.topic);
@@ -2806,6 +2865,7 @@
this.setState(newState); this.setState(newState);
}, },
handleInfoReceipt: function(info) { handleInfoReceipt: function(info) {
switch (info.what) { switch (info.what) {
case "kp": { case "kp": {
@@ -2948,7 +3008,11 @@
<div id="topic-title" className="panel-title">{this.state.title}</div> <div id="topic-title" className="panel-title">{this.state.title}</div>
<div id="topic-last-seen">{lastSeen}</div> <div id="topic-last-seen">{lastSeen}</div>
</div> </div>
<div id="topic-users"></div> {groupTopic ?
<GroupSubs
subscribers={this.state.onlineSubs} /> :
<div id="topic-users" />
}
<div> <div>
<a href="#" onClick={this.handleContextClick}> <a href="#" onClick={this.handleContextClick}>
<i className="material-icons">more_vert</i> <i className="material-icons">more_vert</i>