thread fix
parent
5798d9030c
commit
84563d1829
|
@ -4,32 +4,34 @@ const check_permission = require('../check_permission');
|
||||||
|
|
||||||
async function create_thread(msg, respond) {
|
async function create_thread(msg, respond) {
|
||||||
// validate inputs
|
// validate inputs
|
||||||
if (typeof msg.name !== 'string') {
|
if (typeof msg.name !== 'string')
|
||||||
return respond({
|
return respond({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'thread name required'
|
message: 'thread name required'
|
||||||
});
|
});
|
||||||
}
|
if (msg.name.length > 200)
|
||||||
if (msg.name.length > 200) {
|
|
||||||
return respond({
|
return respond({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'thread name 200 chars max'
|
message: 'thread name 200 chars max'
|
||||||
});
|
});
|
||||||
}
|
if (!Array.isArray(msg.members) || msg.members.find(m => typeof m?.name !== 'string'))
|
||||||
|
return respond({
|
||||||
|
success: false,
|
||||||
|
message: 'invalid members'
|
||||||
|
});
|
||||||
// add to db
|
// add to db
|
||||||
const insert = await db.query(
|
const thread_id = (await db.query(
|
||||||
'insert into thread (name, creator) values (?, ?) returning id',
|
'insert into thread (name, creator) values (?, ?) returning id',
|
||||||
[msg.name, msg.auth_user.id]
|
[msg.name, msg.auth_user.id]
|
||||||
);
|
)).rows[0].id;
|
||||||
const thread_id = insert.rows[0].id;
|
|
||||||
// set up permissions
|
// set up permissions
|
||||||
if (!msg.permissions || !msg.permissions.view_limited) {
|
if (!msg.permissions?.view_limited) {
|
||||||
await db.query(
|
await db.query(
|
||||||
`insert into permission (thread, type, mutable, permission, value)
|
`insert into permission (thread, type, mutable, permission, value)
|
||||||
values (?, ?, ?, ?, ?)`,
|
values (?, ?, ?, ?, ?)`,
|
||||||
[thread_id, 'everyone', true, 'view', 'true']
|
[thread_id, 'everyone', true, 'view', 'true']
|
||||||
);
|
);
|
||||||
if (!msg.permissions || !msg.permissions.post_limited) {
|
if (!msg.permissions?.post_limited) {
|
||||||
await db.query(
|
await db.query(
|
||||||
`insert into permission (thread, type, mutable, permission, value)
|
`insert into permission (thread, type, mutable, permission, value)
|
||||||
values (?, ?, ?, ?, ?)`,
|
values (?, ?, ?, ?, ?)`,
|
||||||
|
@ -55,9 +57,8 @@ async function create_thread(msg, respond) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// add members
|
// add members
|
||||||
if (Array.isArray(msg.members)) {
|
let members = {};
|
||||||
for (let member of msg.members) {
|
for (let member of msg.members) {
|
||||||
if (!member) continue;
|
|
||||||
// get user id
|
// get user id
|
||||||
const id = await db.query('select id from user where name = ?', [
|
const id = await db.query('select id from user where name = ?', [
|
||||||
member.name,
|
member.name,
|
||||||
|
@ -70,6 +71,7 @@ async function create_thread(msg, respond) {
|
||||||
'insert into member (thread, user) values (?, ?)',
|
'insert into member (thread, user) values (?, ?)',
|
||||||
[thread_id, id.rows[0].id]
|
[thread_id, id.rows[0].id]
|
||||||
);
|
);
|
||||||
|
members[member.name] = true;
|
||||||
if (typeof member.permissions === 'object')
|
if (typeof member.permissions === 'object')
|
||||||
for (let permission in member.permissions)
|
for (let permission in member.permissions)
|
||||||
await db.query(`
|
await db.query(`
|
||||||
|
@ -77,34 +79,36 @@ async function create_thread(msg, respond) {
|
||||||
values (?, ?, ?, ?, ?, ?)`,
|
values (?, ?, ?, ?, ?, ?)`,
|
||||||
[thread_id, 'user', id.rows[0].id, true, permission, member.permissions[permission]]);
|
[thread_id, 'user', id.rows[0].id, true, permission, member.permissions[permission]]);
|
||||||
}
|
}
|
||||||
}
|
let thread = {
|
||||||
if (!msg.permissions || !msg.permissions.view_limited) {
|
id: thread_id,
|
||||||
|
name: msg.name,
|
||||||
|
streams: []
|
||||||
|
};
|
||||||
|
if (!msg.permissions?.view_limited) {
|
||||||
for (let username in vybe.users) {
|
for (let username in vybe.users) {
|
||||||
for (let socket of vybe.users[username].sockets) {
|
for (let socket of vybe.users[username].sockets) {
|
||||||
socket.emit('thread', {
|
socket.emit('thread', {
|
||||||
name: msg.name,
|
...thread,
|
||||||
id: insert.rows[0].id,
|
|
||||||
permissions: {
|
permissions: {
|
||||||
is_member: false,
|
|
||||||
view: true,
|
view: true,
|
||||||
post: !msg.permissions || !msg.permissions.post_limited
|
post: !msg.permissions?.post_limited || members[username],
|
||||||
|
admin: username === msg.auth_user.name
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (Array.isArray(msg.members)) {
|
else {
|
||||||
for (let member of msg.members) {
|
for (let member of msg.members) {
|
||||||
if (!vybe.users[member.name])
|
if (!vybe.users[member.name])
|
||||||
continue;
|
continue;
|
||||||
for (let socket of vybe.users[member.name].sockets) {
|
for (let socket of vybe.users[member.name].sockets) {
|
||||||
socket.emit('thread', {
|
socket.emit('thread', {
|
||||||
name: msg.name,
|
...thread,
|
||||||
id: insert.rows[0].id,
|
|
||||||
permissions: {
|
permissions: {
|
||||||
is_member: true,
|
|
||||||
view: true,
|
view: true,
|
||||||
post: true
|
post: true,
|
||||||
|
admin: member.name === msg.auth_user.name
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -132,10 +136,7 @@ async function list_threads(msg, respond) {
|
||||||
[msg.auth_user.id]
|
[msg.auth_user.id]
|
||||||
);
|
);
|
||||||
threads = await Promise.all(threads.rows.map(async thread => {
|
threads = await Promise.all(threads.rows.map(async thread => {
|
||||||
if (vybe.threads[thread.id])
|
thread.streams = vybe.threads[thread.id]?.streams || [];
|
||||||
Object.assign(thread, vybe.threads[thread.id]);
|
|
||||||
else
|
|
||||||
thread.streams = [];
|
|
||||||
thread.permissions = await check_permission(msg.auth_user.id, thread.id);
|
thread.permissions = await check_permission(msg.auth_user.id, thread.id);
|
||||||
return thread;
|
return thread;
|
||||||
}));
|
}));
|
||||||
|
@ -209,12 +210,9 @@ async function get_thread(msg, respond) {
|
||||||
members: Object.entries(members).map(member => ({
|
members: Object.entries(members).map(member => ({
|
||||||
id: member[0],
|
id: member[0],
|
||||||
...member[1]
|
...member[1]
|
||||||
}))
|
})),
|
||||||
|
streams: vybe.threads[thread.id]?.streams || []
|
||||||
};
|
};
|
||||||
if (vybe.threads[thread.id])
|
|
||||||
Object.assign(thread, vybe.threads[thread.id]);
|
|
||||||
else
|
|
||||||
thread.streams = [];
|
|
||||||
return respond({
|
return respond({
|
||||||
success: true,
|
success: true,
|
||||||
thread
|
thread
|
||||||
|
@ -332,35 +330,35 @@ async function edit_thread(msg, respond) {
|
||||||
where thread.id = ?`,
|
where thread.id = ?`,
|
||||||
[msg.id]
|
[msg.id]
|
||||||
)).rows.map(row => [row.name, row.id]));
|
)).rows.map(row => [row.name, row.id]));
|
||||||
if (!msg.permissions || !msg.permissions.view_limited) {
|
let thread = {
|
||||||
|
id: msg.id,
|
||||||
|
name: msg.name,
|
||||||
|
streams: vybe.threads[msg.id]?.streams || []
|
||||||
|
};
|
||||||
|
if (!msg.permissions?.view_limited) {
|
||||||
for (let username in vybe.users)
|
for (let username in vybe.users)
|
||||||
for (let socket of vybe.users[username].sockets)
|
for (let socket of vybe.users[username].sockets)
|
||||||
socket.emit('thread', {
|
socket.emit('thread', {
|
||||||
name: msg.name,
|
...thread,
|
||||||
id: msg.id,
|
|
||||||
permissions: {
|
permissions: {
|
||||||
is_member: username in members,
|
|
||||||
view: true,
|
view: true,
|
||||||
post: !msg.permissions || !msg.permissions.post_limited
|
post: !msg.permissions?.post_limited || username in members,
|
||||||
|| username in members,
|
|
||||||
admin: username === msg.auth_user.name && perms.admin,
|
admin: username === msg.auth_user.name && perms.admin,
|
||||||
...permissions
|
...permissions
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (let member in members) {
|
for (let membername in members) {
|
||||||
if (!vybe.users[member])
|
if (!vybe.users[membername])
|
||||||
continue;
|
continue;
|
||||||
for (let socket of vybe.users[member].sockets)
|
for (let socket of vybe.users[membername].sockets)
|
||||||
socket.emit('thread', {
|
socket.emit('thread', {
|
||||||
name: msg.name,
|
...thread,
|
||||||
id: msg.id,
|
|
||||||
permissions: {
|
permissions: {
|
||||||
is_member: true,
|
|
||||||
view: true,
|
view: true,
|
||||||
post: true,
|
post: true,
|
||||||
admin: member.id === msg.auth_user.id && perms.admin,
|
admin: membername === msg.auth_user.name && perms.admin,
|
||||||
...permissions
|
...permissions
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue