forked from ppfun/pixelplanet
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
27b8076178 | |||
bcd1511bb4 |
|
@ -3,12 +3,9 @@
|
||||||
* it just buffers the msot recent 200 messages for each channel
|
* it just buffers the msot recent 200 messages for each channel
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import Sequelize from 'sequelize';
|
import { storeMessage, getMessagesForChannel } from '../data/sql/Message';
|
||||||
import logger from './logger';
|
|
||||||
|
|
||||||
import { Message, Channel } from '../data/sql';
|
const MAX_BUFFER_TIME = 600000;
|
||||||
|
|
||||||
const MAX_BUFFER_TIME = 120000;
|
|
||||||
|
|
||||||
class ChatMessageBuffer {
|
class ChatMessageBuffer {
|
||||||
constructor(socketEvents) {
|
constructor(socketEvents) {
|
||||||
|
@ -24,12 +21,12 @@ class ChatMessageBuffer {
|
||||||
|
|
||||||
async getMessages(cid, limit = 30) {
|
async getMessages(cid, limit = 30) {
|
||||||
if (limit > 200) {
|
if (limit > 200) {
|
||||||
return ChatMessageBuffer.getMessagesFromDatabase(cid, limit);
|
return getMessagesForChannel(cid, limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
let messages = this.buffer.get(cid);
|
let messages = this.buffer.get(cid);
|
||||||
if (!messages) {
|
if (!messages) {
|
||||||
messages = await ChatMessageBuffer.getMessagesFromDatabase(cid);
|
messages = await getMessagesForChannel(cid, limit);
|
||||||
this.buffer.set(cid, messages);
|
this.buffer.set(cid, messages);
|
||||||
}
|
}
|
||||||
this.timestamps.set(cid, Date.now());
|
this.timestamps.set(cid, Date.now());
|
||||||
|
@ -39,18 +36,15 @@ class ChatMessageBuffer {
|
||||||
cleanBuffer() {
|
cleanBuffer() {
|
||||||
const curTime = Date.now();
|
const curTime = Date.now();
|
||||||
const toDelete = [];
|
const toDelete = [];
|
||||||
this.timestamps.forEach((cid, timestamp) => {
|
this.timestamps.forEach((timestamp, cid) => {
|
||||||
if (curTime > timestamp + MAX_BUFFER_TIME) {
|
if (curTime > timestamp + MAX_BUFFER_TIME) {
|
||||||
toDelete.push(cid);
|
toDelete.push(cid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
toDelete.forEach((cid) => {
|
toDelete.forEach((cid) => {
|
||||||
this.buffer.delete(cid);
|
|
||||||
this.timestamps.delete(cid);
|
this.timestamps.delete(cid);
|
||||||
|
this.buffer.delete(cid);
|
||||||
});
|
});
|
||||||
logger.info(
|
|
||||||
`Cleaned ${toDelete.length} channels from chat message buffer`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async broadcastChatMessage(
|
async broadcastChatMessage(
|
||||||
|
@ -64,20 +58,7 @@ class ChatMessageBuffer {
|
||||||
if (message.length > 200) {
|
if (message.length > 200) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Message.create({
|
storeMessage(name, flag, message, cid, uid);
|
||||||
name,
|
|
||||||
flag,
|
|
||||||
message,
|
|
||||||
cid,
|
|
||||||
uid,
|
|
||||||
});
|
|
||||||
Channel.update({
|
|
||||||
lastMessage: Sequelize.literal('CURRENT_TIMESTAMP'),
|
|
||||||
}, {
|
|
||||||
where: {
|
|
||||||
id: cid,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
/*
|
/*
|
||||||
* goes through socket events and then comes
|
* goes through socket events and then comes
|
||||||
* back at addMessage
|
* back at addMessage
|
||||||
|
@ -110,45 +91,6 @@ class ChatMessageBuffer {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getMessagesFromDatabase(cid, limit = 200) {
|
|
||||||
const messagesModel = await Message.findAll({
|
|
||||||
attributes: [
|
|
||||||
'message',
|
|
||||||
'uid',
|
|
||||||
'name',
|
|
||||||
'flag',
|
|
||||||
[
|
|
||||||
Sequelize.fn('UNIX_TIMESTAMP', Sequelize.col('createdAt')),
|
|
||||||
'ts',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
where: { cid },
|
|
||||||
limit,
|
|
||||||
order: [['createdAt', 'DESC']],
|
|
||||||
raw: true,
|
|
||||||
});
|
|
||||||
const messages = [];
|
|
||||||
let i = messagesModel.length;
|
|
||||||
while (i > 0) {
|
|
||||||
i -= 1;
|
|
||||||
const {
|
|
||||||
message,
|
|
||||||
uid,
|
|
||||||
name,
|
|
||||||
flag,
|
|
||||||
ts,
|
|
||||||
} = messagesModel[i];
|
|
||||||
messages.push([
|
|
||||||
name,
|
|
||||||
message,
|
|
||||||
flag,
|
|
||||||
uid,
|
|
||||||
ts,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ChatMessageBuffer;
|
export default ChatMessageBuffer;
|
||||||
|
|
|
@ -160,7 +160,7 @@ export class MailProvider {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
await reguser.update({
|
await reguser.update({
|
||||||
mailVerified: true,
|
verified: true,
|
||||||
verificationReqAt: null,
|
verificationReqAt: null,
|
||||||
});
|
});
|
||||||
return reguser.name;
|
return reguser.name;
|
||||||
|
|
|
@ -13,17 +13,17 @@ export default async function getMe(user, lang = 'default') {
|
||||||
const userdata = await user.getUserData();
|
const userdata = await user.getUserData();
|
||||||
// sanitize data
|
// sanitize data
|
||||||
const {
|
const {
|
||||||
name, mailVerified,
|
name, verified,
|
||||||
} = userdata;
|
} = userdata;
|
||||||
if (!name) userdata.name = null;
|
if (!name) userdata.name = null;
|
||||||
const messages = [];
|
const messages = [];
|
||||||
if (USE_MAILER && name && !mailVerified) {
|
if (USE_MAILER && name && !verified) {
|
||||||
messages.push('not_verified');
|
messages.push('not_verified');
|
||||||
}
|
}
|
||||||
if (messages.length > 0) {
|
if (messages.length > 0) {
|
||||||
userdata.messages = messages;
|
userdata.messages = messages;
|
||||||
}
|
}
|
||||||
delete userdata.mailVerified;
|
delete userdata.verified;
|
||||||
|
|
||||||
userdata.canvases = getLocalicedCanvases(lang);
|
userdata.canvases = getLocalicedCanvases(lang);
|
||||||
userdata.channels = {
|
userdata.channels = {
|
||||||
|
|
|
@ -244,7 +244,7 @@ class User {
|
||||||
return {
|
return {
|
||||||
...data,
|
...data,
|
||||||
name: null,
|
name: null,
|
||||||
mailVerified: false,
|
verified: false,
|
||||||
blockDm: false,
|
blockDm: false,
|
||||||
priv: false,
|
priv: false,
|
||||||
mailreg: false,
|
mailreg: false,
|
||||||
|
@ -260,7 +260,7 @@ class User {
|
||||||
return {
|
return {
|
||||||
...data,
|
...data,
|
||||||
name: regUser.name,
|
name: regUser.name,
|
||||||
mailVerified: regUser.mailVerified,
|
verified: regUser.verified,
|
||||||
blockDm: regUser.blockDm,
|
blockDm: regUser.blockDm,
|
||||||
priv: regUser.priv,
|
priv: regUser.priv,
|
||||||
totalPixels,
|
totalPixels,
|
||||||
|
|
|
@ -60,6 +60,9 @@ async function cleanBans() {
|
||||||
},
|
},
|
||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
if (!expiredIPs.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const ips = [];
|
const ips = [];
|
||||||
for (let i = 0; i < expiredIPs.length; i += 1) {
|
for (let i = 0; i < expiredIPs.length; i += 1) {
|
||||||
ips.push(expiredIPs[i].ip);
|
ips.push(expiredIPs[i].ip);
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { DataTypes } from 'sequelize';
|
import Sequelize, { DataTypes } from 'sequelize';
|
||||||
import sequelize from './sequelize';
|
import sequelize from './sequelize';
|
||||||
import Channel from './Channel';
|
import Channel from './Channel';
|
||||||
import RegUser from './RegUser';
|
import RegUser from './RegUser';
|
||||||
|
@ -17,18 +17,6 @@ const Message = sequelize.define('Message', {
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
name: {
|
|
||||||
type: `${DataTypes.CHAR(32)} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
|
||||||
defaultValue: 'mx',
|
|
||||||
allowNull: false,
|
|
||||||
},
|
|
||||||
|
|
||||||
flag: {
|
|
||||||
type: DataTypes.CHAR(2),
|
|
||||||
defaultValue: 'xx',
|
|
||||||
allowNull: false,
|
|
||||||
},
|
|
||||||
|
|
||||||
message: {
|
message: {
|
||||||
type: `${DataTypes.CHAR(200)} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
type: `${DataTypes.CHAR(200)} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
@ -55,4 +43,66 @@ Message.belongsTo(RegUser, {
|
||||||
onDelete: 'cascade',
|
onDelete: 'cascade',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export async function storeMessage(
|
||||||
|
name,
|
||||||
|
flag,
|
||||||
|
message,
|
||||||
|
cid,
|
||||||
|
uid,
|
||||||
|
) {
|
||||||
|
await Channel.update({
|
||||||
|
lastMessage: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||||
|
}, {
|
||||||
|
where: {
|
||||||
|
id: cid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return Message.create({
|
||||||
|
name,
|
||||||
|
flag,
|
||||||
|
message,
|
||||||
|
cid,
|
||||||
|
uid,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getMessagesForChannel(cid, limit) {
|
||||||
|
const models = await Message.findAll({
|
||||||
|
attributes: [
|
||||||
|
'message',
|
||||||
|
'uid',
|
||||||
|
],
|
||||||
|
include: {
|
||||||
|
model: RegUser,
|
||||||
|
as: 'user',
|
||||||
|
foreignKey: 'uid',
|
||||||
|
attributes: ['name', 'flag'],
|
||||||
|
},
|
||||||
|
where: { cid },
|
||||||
|
limit,
|
||||||
|
order: [['createdAt', 'DESC']],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
const messages = [];
|
||||||
|
let i = models.length;
|
||||||
|
while (i > 0) {
|
||||||
|
i -= 1;
|
||||||
|
const {
|
||||||
|
message,
|
||||||
|
uid,
|
||||||
|
'user.name': name,
|
||||||
|
'user.flag': flag,
|
||||||
|
ts,
|
||||||
|
} = models[i];
|
||||||
|
messages.push([
|
||||||
|
name,
|
||||||
|
message,
|
||||||
|
flag,
|
||||||
|
uid,
|
||||||
|
ts,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
export default Message;
|
export default Message;
|
||||||
|
|
|
@ -38,7 +38,7 @@ const RegUser = sequelize.define('User', {
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
// null if external oauth authentification
|
// null if only ever used external oauth
|
||||||
password: {
|
password: {
|
||||||
type: DataTypes.CHAR(60),
|
type: DataTypes.CHAR(60),
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
|
@ -51,9 +51,9 @@ const RegUser = sequelize.define('User', {
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
},
|
},
|
||||||
|
|
||||||
// mail and Minecraft verified
|
// currently just mail verification
|
||||||
verified: {
|
verified: {
|
||||||
type: DataTypes.TINYINT,
|
type: DataTypes.BOOLEAN,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
|
@ -98,10 +98,6 @@ const RegUser = sequelize.define('User', {
|
||||||
updatedAt: false,
|
updatedAt: false,
|
||||||
|
|
||||||
getterMethods: {
|
getterMethods: {
|
||||||
mailVerified() {
|
|
||||||
return this.verified & 0x01;
|
|
||||||
},
|
|
||||||
|
|
||||||
blockDm() {
|
blockDm() {
|
||||||
return this.blocks & 0x01;
|
return this.blocks & 0x01;
|
||||||
},
|
},
|
||||||
|
@ -112,11 +108,6 @@ const RegUser = sequelize.define('User', {
|
||||||
},
|
},
|
||||||
|
|
||||||
setterMethods: {
|
setterMethods: {
|
||||||
mailVerified(num) {
|
|
||||||
const val = (num) ? (this.verified | 0x01) : (this.verified & ~0x01);
|
|
||||||
this.setDataValue('verified', val);
|
|
||||||
},
|
|
||||||
|
|
||||||
blockDm(num) {
|
blockDm(num) {
|
||||||
const val = (num) ? (this.blocks | 0x01) : (this.blocks & ~0x01);
|
const val = (num) ? (this.blocks | 0x01) : (this.blocks & ~0x01);
|
||||||
this.setDataValue('blocks', val);
|
this.setDataValue('blocks', val);
|
||||||
|
|
|
@ -56,7 +56,7 @@ export default async (req, res) => {
|
||||||
|
|
||||||
await user.regUser.update({
|
await user.regUser.update({
|
||||||
email,
|
email,
|
||||||
mailVerified: false,
|
verified: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const host = getHostFromRequest(req);
|
const host = getHostFromRequest(req);
|
||||||
|
|
|
@ -15,8 +15,8 @@ export default async (req, res) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, email, mailVerified } = user.regUser;
|
const { name, email, verified } = user.regUser;
|
||||||
if (mailVerified) {
|
if (verified) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.json({
|
res.json({
|
||||||
errors: ['You are already verified.'],
|
errors: ['You are already verified.'],
|
||||||
|
|
|
@ -77,7 +77,7 @@ app.use(routes);
|
||||||
// ip config
|
// ip config
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// sync sql models
|
// sync sql models
|
||||||
sequelize.sync({ alter: { drop: false } })
|
sequelize.sync({ alter: { drop: true } })
|
||||||
// connect to redis
|
// connect to redis
|
||||||
.then(connectRedis)
|
.then(connectRedis)
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user