add apisocket messages to chat history

This commit is contained in:
HF 2022-01-15 11:49:57 +01:00
parent de83b3e06e
commit 10353c2056
6 changed files with 47 additions and 23 deletions

View File

@ -30,7 +30,7 @@ const floatStyle = {
top: '50%',
left: '50%',
transform: 'translate(-50%,-50%)',
}
};
/*
* autoload: Load captcha immediately and autofocus input textbox

View File

@ -7,7 +7,7 @@
import Sequelize from 'sequelize';
import logger from './logger';
import { RegUser, Message, Channel } from '../data/models';
import { Message, Channel } from '../data/models';
const MAX_BUFFER_TIME = 120000;
@ -59,9 +59,11 @@ class ChatMessageBuffer {
flag,
) {
Message.create({
name,
flag,
message,
cid,
uid,
message,
});
Channel.update({
lastMessage: Sequelize.literal('CURRENT_TIMESTAMP'),
@ -83,20 +85,11 @@ class ChatMessageBuffer {
static async getMessagesFromDatabase(cid, limit = 200) {
const messagesModel = await Message.findAll({
include: [
{
model: RegUser,
as: 'user',
foreignKey: 'uid',
attributes: [
'id',
'name',
'flag',
],
},
],
attributes: [
'message',
'uid',
'name',
'flag',
],
where: { cid },
limit,
@ -108,10 +101,10 @@ class ChatMessageBuffer {
while (i > 0) {
i -= 1;
const {
name,
message,
'user.name': name,
'user.flag': flag,
'user.id': uid,
flag,
uid,
} = messagesModel[i];
messages.push([
name,

View File

@ -13,7 +13,12 @@ import { cheapDetector } from './isProxy';
import { DailyCron } from '../utils/cron';
import ttags from './ttag';
import { CHAT_CHANNELS, EVENT_USER_NAME, INFO_USER_NAME } from './constants';
import {
CHAT_CHANNELS,
EVENT_USER_NAME,
INFO_USER_NAME,
APISOCKET_USER_NAME,
} from './constants';
export class ChatProvider {
constructor() {
@ -23,6 +28,7 @@ export class ChatProvider {
this.enChannelId = 0;
this.infoUserId = 1;
this.eventUserId = 1;
this.apiSocketUserId = 1;
this.caseCheck = /^[A-Z !.]*$/;
this.cyrillic = /[\u0436-\u043B]'/;
this.filters = [
@ -156,6 +162,20 @@ export class ChatProvider {
raw: true,
});
this.eventUserId = eventUser[0].id;
name = APISOCKET_USER_NAME;
const apiSocketUser = await RegUser.findOrCreate({
attributes: [
'id',
],
where: { name },
defaults: {
name,
verified: 3,
email: 'event@example.com',
},
raw: true,
});
this.apiSocketUserId = apiSocketUser[0].id;
this.clearOldMessages();
DailyCron.hook(this.clearOldMessages);
}

View File

@ -96,3 +96,4 @@ export const MAX_CHAT_MESSAGES = 100;
export const EVENT_USER_NAME = 'event';
export const INFO_USER_NAME = 'info';
export const APISOCKET_USER_NAME = 'apisocket';

View File

@ -2,8 +2,6 @@
*
* Database layout for Chat Message History
*
* @flow
*
*/
import DataType from 'sequelize';
@ -19,6 +17,18 @@ const Message = Model.define('Message', {
primaryKey: true,
},
name: {
type: `${DataType.CHAR(32)} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci`,
defaultValue: 'mx',
allowNull: false,
},
flag: {
type: DataType.CHAR(2),
defaultValue: 'xx',
allowNull: false,
},
message: {
type: `${DataType.CHAR(200)} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci`,
allowNull: false,

View File

@ -239,11 +239,11 @@ class APISocketServer {
}
if (command === 'chat') {
const [name, id, msg, country, channelId] = packet;
const uid = id || 1;
const uid = id || chatProvider.apiSocketUserId;
/*
* do not send message back up ws that sent it
*/
socketEvents.broadcastChatMessage(
chatProvider.broadcastChatMessage(
name,
msg,
channelId,