chat: adding time + own messages

This commit is contained in:
Zener 2020-12-24 09:10:13 +01:00
parent 097ae85a37
commit 09a5f5e519

View File

@ -54,16 +54,20 @@ let ChatRoomsModel = {
function printMessage(msg){
let text = msg.msg.replaceAll('<br/>','\n').replace(new RegExp('<style[^<]*</style>|<[^>]*>','gm'),'');
let datetime = new Date(msg.sendTime * 1000).toLocaleString();
console.info(text);
return m('p.message', text);
return m('p.message', datetime, ' ', text);
}
let ChatLobbyModel = {
const ChatLobbyModel = {
currentLobby: {
lobby_name: '...',
},
messages: [],
users: [],
chatId(action) {
return {type:3,lobby_id:{xstr64:m.route.param('lobby')}};
},
loadLobby () {
loadLobbyDetails(m.route.param('lobby'), detail => {
this.currentLobby = detail;
@ -71,16 +75,13 @@ let ChatLobbyModel = {
userlist => {
if (userlist!== undefined) {
var userMap = userlist.ids.reduce((a,c) => {
a[c.mGroupId] = c;
a[c.mGroupId] = c.mGroupName;
return a;
},{});
var names = detail.gxs_ids.reduce((a,u) => {
var user = userMap[u.key];
return a.concat([user === undefined ? '???' : user.mGroupName])
},[]);
var names = detail.gxs_ids.reduce((a,u) => a.concat(userMap[u.key] || '???') ,[]);
this.users = [];
let lobbyid = m.route.param('lobby');
rs.events[15].chatMessages({type:3,lobby_id:{xstr64:lobbyid}},rs.events[15], l => (this.messages = l.map(printMessage)));
rs.events[15].chatMessages(this.chatId(),rs.events[15], l => (this.messages = l.map(printMessage)));
rs.events[15].notify = chatMessage => {
if (chatMessage.chat_id.type===3 && chatMessage.chat_id.lobby_id.xstr64 === lobbyid) {
this.messages.push(printMessage(chatMessage));
@ -97,12 +98,15 @@ let ChatLobbyModel = {
},
sendMessage(msg, onsuccess) {
rs.rsJsonApiRequest('/rsmsgs/sendChat', {},
() => onsuccess(),
true,
{},
undefined,
() => {
// adding own message to log
rs.events[15].handler({mChatMessage:{chat_id:this.chatId(),msg:msg, sendTime:new Date().getTime()/1000}},rs.events[15]);
onsuccess();
},
true, {}, undefined,
() => '{"id":{"type": 3,"lobby_id":' + m.route.param('lobby') + '}, "msg":' + JSON.stringify(msg) + '}'
)
);
},
}
@ -140,7 +144,7 @@ const SubscribedLobbies = () => {
};
};
let PublicLobbies = () => {
const PublicLobbies = () => {
return {
oninit: () => ChatRoomsModel.loadPublicRooms(),
view: () => m('.widget', [