Files
NodeBB/src/messaging/edit.js

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-12-15 14:10:32 +02:00
'use strict';
const db = require('../database');
2020-01-23 22:19:15 -05:00
const meta = require('../meta');
const user = require('../user');
const plugins = require('../plugins');
const activitypub = require('../activitypub');
const privileges = require('../privileges');
const utils = require('../utils');
2015-12-15 14:10:32 +02:00
2020-01-23 22:19:15 -05:00
const sockets = require('../socket.io');
2015-12-15 14:10:32 +02:00
module.exports = function (Messaging) {
Messaging.editMessage = async (uid, mid, roomId, content) => {
await Messaging.checkContent(content);
const isPublic = parseInt(await db.getObjectField(`chat:room:${roomId}`, 'public'), 10) === 1;
const raw = await Messaging.getMessageField(mid, 'content');
if (raw === content) {
return;
}
const payload = await plugins.hooks.fire('filter:messaging.edit', {
content: content,
edited: Date.now(),
});
2015-12-15 14:10:32 +02:00
if (!String(payload.content).trim()) {
throw new Error('[[error:invalid-chat-message]]');
}
await Messaging.setMessageFields(mid, payload);
// Propagate this change to users in the room
Chat refactor (#11779) * first part of chat refactor remove per user chat zsets & store all mids in chat:room:<roomId>:mids reverse uids in getUidsInRoom * feat: create room button public groups wip * feat: public rooms create chats:room zset chat room deletion * join socket.io room * get rid of some calls that load all users in room * dont load all users when loadRoom is called * mange room users infinitescroll dont load all members in api call * IS for user list ability to change groups field for public rooms update groups field if group is renamed * test: test fixes * wip * keep 150 messages * fix extra awaits fix dupe code in chat toggleReadState * unread state for public rooms * feat: faster push unread * test: spec * change base to harmony * test: lint fixes * fix language of chat with message * add 2 methods for perf messaging.getTeasers and getUsers(roomIds) instead of loading one by one * refactor: cleaner conditional * test fix upgrade script fix save timestamp of room creation in room object * set progress.total * don't check for guests/spiders * public room unread fix * add public unread counts * mark read on send * ignore instead of throwing * doggy.gif * fix: restore delete * prevent entering chat rooms with meta.enter * fix self message causing mark unread * ability to sort public rooms * dont init sortable on mobile * move chat-loaded class to core * test: fix spec * add missing keys * use ajaxify * refactor: store some refs * fix: when user is deleted remove from public rooms as well * feat: change how unread count is calculated * get rid of cleaned content get rid of mid * add help text * test: fix tests, add back mid to prevent breaking change * ability to search members of chat rooms * remove * derp * perf: switch with partial data fix tests * more fixes if user leaves a group leave public rooms is he is no longer part of any of the groups that have access fix the cache key used to get all public room ids dont allow joining chat socket.io room if user is no longer part of group * fix: lint * fix: js error when trying to delete room after switching * add isRoomPublic
2023-07-12 13:03:54 -04:00
const messages = await Messaging.getMessagesData([mid], uid, roomId, true);
if (messages[0]) {
const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`;
sockets.in(roomName).emit('event:chats.edit', {
messages: messages,
});
if (!isPublic && utils.isNumber(messages[0].fromuid)) {
activitypub.out.update.privateNote(messages[0].fromuid, messages[0]);
}
}
plugins.hooks.fire('action:messaging.edit', {
message: { ...messages[0], content: payload.content },
});
2017-12-11 10:53:29 -05:00
};
const canEditDelete = async (messageId, uid, type) => {
let durationConfig = '';
2017-12-11 10:53:29 -05:00
if (type === 'edit') {
durationConfig = 'chatEditDuration';
} else if (type === 'delete') {
durationConfig = 'chatDeleteDuration';
}
2021-12-20 11:30:43 -05:00
const exists = await Messaging.messageExists(messageId);
if (!exists) {
throw new Error('[[error:invalid-mid]]');
}
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(uid);
if (meta.config.disableChat) {
throw new Error('[[error:chat-disabled]]');
} else if (!isAdminOrGlobalMod && meta.config.disableChatMessageEditing) {
throw new Error('[[error:chat-message-editing-disabled]]');
2015-12-15 14:10:32 +02:00
}
const userData = await user.getUserFields(uid, ['banned']);
if (userData.banned) {
throw new Error('[[error:user-banned]]');
}
const canChat = await privileges.global.can(['chat', 'chat:privileged'], uid);
if (!canChat.includes(true)) {
throw new Error('[[error:no-privileges]]');
}
2015-12-15 14:10:32 +02:00
const messageData = await Messaging.getMessageFields(messageId, ['fromuid', 'timestamp', 'system']);
if (isAdminOrGlobalMod && !messageData.system) {
return;
}
2020-01-23 22:19:15 -05:00
const chatConfigDuration = meta.config[durationConfig];
if (chatConfigDuration && Date.now() - messageData.timestamp > chatConfigDuration * 1000) {
2021-02-03 23:59:08 -07:00
throw new Error(`[[error:chat-${type}-duration-expired, ${meta.config[durationConfig]}]]`);
}
2024-10-17 11:23:08 -04:00
if (String(messageData.fromuid) === String(uid) && !messageData.system) {
return;
}
2021-02-03 23:59:08 -07:00
throw new Error(`[[error:cant-${type}-chat-message]]`);
};
2017-12-11 10:53:29 -05:00
Messaging.canEdit = async (messageId, uid) => await canEditDelete(messageId, uid, 'edit');
Messaging.canDelete = async (messageId, uid) => await canEditDelete(messageId, uid, 'delete');
2023-08-28 15:57:30 -04:00
Messaging.canPin = async (roomId, uid) => {
const [isAdmin, isGlobalMod, inRoom, isRoomOwner] = await Promise.all([
user.isAdministrator(uid),
user.isGlobalModerator(uid),
Messaging.isUserInRoom(uid, roomId),
Messaging.isRoomOwner(uid, roomId),
]);
if (!isAdmin && !isGlobalMod && (!inRoom || !isRoomOwner)) {
throw new Error('[[error:no-privileges]]');
}
};
2017-02-18 02:30:48 -07:00
};