• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CMU-17313Q / fall23-nodebb-inshallah-a / 6239420468

19 Sep 2023 06:18PM UTC coverage: 77.199% (+0.01%) from 77.188%
6239420468

push

github

web-flow
Merge pull request #59 from CMU-17313Q/Adding-isAnonymous-if-conditions

Adding is anonymous if conditions

9091 of 13356 branches covered (0.0%)

Branch coverage included in aggregate %.

20876 of 25462 relevant lines covered (81.99%)

2463.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.34
/src/socket.io/posts.js
1
'use strict';
2

3
const validator = require('validator');
4✔
4

5
const db = require('../database');
4✔
6
const posts = require('../posts');
4✔
7
const privileges = require('../privileges');
4✔
8
const plugins = require('../plugins');
4✔
9
const meta = require('../meta');
4✔
10
const topics = require('../topics');
4✔
11
const user = require('../user');
4✔
12
const notifications = require('../notifications');
4✔
13
const utils = require('../utils');
4✔
14
const events = require('../events');
4✔
15

16
const SocketPosts = module.exports;
4✔
17

18
require('./posts/votes')(SocketPosts);
4✔
19
require('./posts/tools')(SocketPosts);
4✔
20

21
SocketPosts.getRawPost = async function (socket, pid) {
4✔
22
    const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
×
23
    if (!canRead) {
×
24
        throw new Error('[[error:no-privileges]]');
×
25
    }
26

27
    const postData = await posts.getPostFields(pid, ['content', 'deleted']);
×
28
    if (postData.deleted) {
×
29
        throw new Error('[[error:no-post]]');
×
30
    }
31
    postData.pid = pid;
×
32
    const result = await plugins.hooks.fire('filter:post.getRawPost', { uid: socket.uid, postData: postData });
×
33
    return result.postData.content;
×
34
};
35

36
SocketPosts.getPostSummaryByIndex = async function (socket, data) {
4✔
37
    if (data.index < 0) {
×
38
        data.index = 0;
×
39
    }
40
    let pid;
41
    if (data.index === 0) {
×
42
        pid = await topics.getTopicField(data.tid, 'mainPid');
×
43
    } else {
44
        pid = await db.getSortedSetRange(`tid:${data.tid}:posts`, data.index - 1, data.index - 1);
×
45
    }
46
    pid = Array.isArray(pid) ? pid[0] : pid;
×
47
    if (!pid) {
×
48
        return 0;
×
49
    }
50

51
    const topicPrivileges = await privileges.topics.get(data.tid, socket.uid);
×
52
    if (!topicPrivileges['topics:read']) {
×
53
        throw new Error('[[error:no-privileges]]');
×
54
    }
55

56
    const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false });
×
57
    posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
×
58
    return postsData[0];
×
59
};
60

61
SocketPosts.getPostSummaryByPid = async function (socket, data) {
4✔
62
    if (!data || !data.pid) {
×
63
        throw new Error('[[error:invalid-data]]');
×
64
    }
65
    const { pid } = data;
×
66
    const tid = await posts.getPostField(pid, 'tid');
×
67
    const topicPrivileges = await privileges.topics.get(tid, socket.uid);
×
68
    if (!topicPrivileges['topics:read']) {
×
69
        throw new Error('[[error:no-privileges]]');
×
70
    }
71

72
    const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false });
×
73
    posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
×
74
    return postsData[0];
×
75
};
76

77
SocketPosts.getCategory = async function (socket, pid) {
4✔
78
    return await posts.getCidByPid(pid);
×
79
};
80

81
SocketPosts.getPidIndex = async function (socket, data) {
4✔
82
    if (!data) {
×
83
        throw new Error('[[error:invalid-data]]');
×
84
    }
85
    return await posts.getPidIndex(data.pid, data.tid, data.topicPostSort);
×
86
};
87

88
SocketPosts.getReplies = async function (socket, pid) {
4✔
89
    if (!utils.isNumber(pid)) {
×
90
        throw new Error('[[error:invalid-data]]');
×
91
    }
92
    const { topicPostSort } = await user.getSettings(socket.uid);
×
93
    const pids = await posts.getPidsFromSet(`pid:${pid}:replies`, 0, -1, topicPostSort === 'newest_to_oldest');
×
94

95
    let [postData, postPrivileges] = await Promise.all([
×
96
        posts.getPostsByPids(pids, socket.uid),
97
        privileges.posts.get(pids, socket.uid),
98
    ]);
99
    postData = await topics.addPostData(postData, socket.uid);
×
100
    postData.forEach((postData, index) => posts.modifyPostByPrivilege(postData, postPrivileges[index]));
×
101
    postData = postData.filter((postData, index) => postData && postPrivileges[index].read);
×
102
    postData = await user.blocks.filter(socket.uid, postData);
×
103
    return postData;
×
104
};
105

106
SocketPosts.accept = async function (socket, data) {
4✔
107
    await canEditQueue(socket, data, 'accept');
×
108
    const result = await posts.submitFromQueue(data.id);
×
109
    if (result && socket.uid !== parseInt(result.uid, 10)) {
×
110
        await sendQueueNotification('post-queue-accepted', result.uid, `/post/${result.pid}`);
×
111
    }
112
    await logQueueEvent(socket, result, 'accept');
×
113
};
114

115
SocketPosts.reject = async function (socket, data) {
4✔
116
    await canEditQueue(socket, data, 'reject');
×
117
    const result = await posts.removeFromQueue(data.id);
×
118
    if (result && socket.uid !== parseInt(result.uid, 10)) {
×
119
        await sendQueueNotification('post-queue-rejected', result.uid, '/');
×
120
    }
121
    await logQueueEvent(socket, result, 'reject');
×
122
};
123

124
async function logQueueEvent(socket, result, type) {
125
    const eventData = {
×
126
        type: `post-queue-${result.type}-${type}`,
127
        uid: socket.uid,
128
        ip: socket.ip,
129
        content: result.data.content,
130
        targetUid: result.uid,
131
    };
132
    if (result.type === 'topic') {
×
133
        eventData.cid = result.data.cid;
×
134
        eventData.title = result.data.title;
×
135
    } else {
136
        eventData.tid = result.data.tid;
×
137
    }
138
    if (result.pid) {
×
139
        eventData.pid = result.pid;
×
140
    }
141
    await events.log(eventData);
×
142
}
143

144
SocketPosts.notify = async function (socket, data) {
4✔
145
    await canEditQueue(socket, data, 'notify');
×
146
    const result = await posts.getFromQueue(data.id);
×
147
    if (result) {
×
148
        await sendQueueNotification('post-queue-notify', result.uid, `/post-queue/${data.id}`, validator.escape(String(data.message)));
×
149
    }
150
};
151

152
async function canEditQueue(socket, data, action) {
153
    const canEditQueue = await posts.canEditQueue(socket.uid, data, action);
×
154
    if (!canEditQueue) {
×
155
        throw new Error('[[error:no-privileges]]');
×
156
    }
157
}
158

159
async function sendQueueNotification(type, targetUid, path, notificationText) {
160
    const notifData = {
×
161
        type: type,
162
        nid: `${type}-${targetUid}-${path}`,
163
        bodyShort: notificationText ? `[[notifications:${type}, ${notificationText}]]` : `[[notifications:${type}]]`,
×
164
        path: path,
165
    };
166
    if (parseInt(meta.config.postQueueNotificationUid, 10) > 0) {
×
167
        notifData.from = meta.config.postQueueNotificationUid;
×
168
    }
169
    const notifObj = await notifications.create(notifData);
×
170
    await notifications.push(notifObj, [targetUid]);
×
171
}
172

173
SocketPosts.editQueuedContent = async function (socket, data) {
4✔
174
    if (!data || !data.id || (!data.content && !data.title && !data.cid)) {
×
175
        throw new Error('[[error:invalid-data]]');
×
176
    }
177
    await posts.editQueuedContent(socket.uid, data);
×
178
    if (data.content) {
×
179
        return await plugins.hooks.fire('filter:parse.post', { postData: data });
×
180
    }
181
    return { postData: data };
×
182
};
183

184
require('../promisify')(SocketPosts);
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc