-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathwebsockets.js
94 lines (80 loc) · 2.61 KB
/
websockets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
const meta = require.main.require('./src/meta');
const privileges = require.main.require('./src/privileges');
const posts = require.main.require('./src/posts');
const topics = require.main.require('./src/topics');
const plugins = require.main.require('./src/plugins');
const Sockets = module.exports;
Sockets.push = async function (socket, pid) {
const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
if (!canRead) {
throw new Error('[[error:no-privileges]]');
}
const postData = await posts.getPostFields(pid, ['content', 'tid', 'uid', 'handle', 'timestamp']);
if (!postData && !postData.content) {
throw new Error('[[error:invalid-pid]]');
}
const [topic, tags, isMain] = await Promise.all([
topics.getTopicDataByPid(pid),
topics.getTopicTags(postData.tid),
posts.isMain(pid),
]);
if (!topic) {
throw new Error('[[error:no-topic]]');
}
const result = await plugins.hooks.fire('filter:composer.push', {
pid: pid,
uid: postData.uid,
handle: parseInt(meta.config.allowGuestHandles, 10) ? postData.handle : undefined,
body: postData.content,
title: topic.title,
thumb: topic.thumb,
tags: tags,
isMain: isMain,
timestamp: postData.timestamp,
});
return result;
};
Sockets.editCheck = async function (socket, pid) {
const isMain = await posts.isMain(pid);
return { titleEditable: isMain };
};
Sockets.renderPreview = async function (socket, content) {
return await plugins.hooks.fire('filter:parse.raw', content);
};
Sockets.renderHelp = async function () {
const helpText = meta.config['composer:customHelpText'] || '';
if (!meta.config['composer:showHelpTab']) {
throw new Error('help-hidden');
}
const parsed = await plugins.hooks.fire('filter:parse.raw', helpText);
if (meta.config['composer:allowPluginHelp'] && plugins.hooks.hasListeners('filter:composer.help')) {
return await plugins.hooks.fire('filter:composer.help', parsed) || helpText;
}
return helpText;
};
Sockets.getFormattingOptions = async function () {
return await require('./library').getFormattingOptions();
};
Sockets.shouldQueue = async function (socket, data) {
if (!data || !data.postData) {
throw new Error('[[error:invalid-data]]');
}
if (socket.uid <= 0) {
return false;
}
let shouldQueue = false;
const { postData } = data;
if (postData.action === 'posts.reply') {
shouldQueue = await posts.shouldQueue(socket.uid, {
tid: postData.tid,
content: postData.content || '',
});
} else if (postData.action === 'topics.post') {
shouldQueue = await posts.shouldQueue(socket.uid, {
cid: postData.cid,
content: postData.content || '',
});
}
return shouldQueue;
};