thread fix

main
jerl 2024-05-28 21:32:00 -05:00
parent 5798d9030c
commit 84563d1829
1 changed files with 60 additions and 62 deletions

View File

@ -4,32 +4,34 @@ const check_permission = require('../check_permission');
async function create_thread(msg, respond) {
// validate inputs
if (typeof msg.name !== 'string') {
if (typeof msg.name !== 'string')
return respond({
success: false,
message: 'thread name required'
});
}
if (msg.name.length > 200) {
if (msg.name.length > 200)
return respond({
success: false,
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
const insert = await db.query(
const thread_id = (await db.query(
'insert into thread (name, creator) values (?, ?) returning id',
[msg.name, msg.auth_user.id]
);
const thread_id = insert.rows[0].id;
)).rows[0].id;
// set up permissions
if (!msg.permissions || !msg.permissions.view_limited) {
if (!msg.permissions?.view_limited) {
await db.query(
`insert into permission (thread, type, mutable, permission, value)
values (?, ?, ?, ?, ?)`,
[thread_id, 'everyone', true, 'view', 'true']
);
if (!msg.permissions || !msg.permissions.post_limited) {
if (!msg.permissions?.post_limited) {
await db.query(
`insert into permission (thread, type, mutable, permission, value)
values (?, ?, ?, ?, ?)`,
@ -55,56 +57,58 @@ async function create_thread(msg, respond) {
);
}
// add members
if (Array.isArray(msg.members)) {
for (let member of msg.members) {
if (!member) continue;
// get user id
const id = await db.query('select id from user where name = ?', [
member.name,
]);
if (id.rows.length === 0) {
console.log('user not found: ' + member.name);
continue;
}
await db.query(
'insert into member (thread, user) values (?, ?)',
[thread_id, id.rows[0].id]
);
if (typeof member.permissions === 'object')
for (let permission in member.permissions)
await db.query(`
insert into permission (thread, type, user, mutable, permission, value)
values (?, ?, ?, ?, ?, ?)`,
[thread_id, 'user', id.rows[0].id, true, permission, member.permissions[permission]]);
let members = {};
for (let member of msg.members) {
// get user id
const id = await db.query('select id from user where name = ?', [
member.name,
]);
if (id.rows.length === 0) {
console.log('user not found: ' + member.name);
continue;
}
await db.query(
'insert into member (thread, user) values (?, ?)',
[thread_id, id.rows[0].id]
);
members[member.name] = true;
if (typeof member.permissions === 'object')
for (let permission in member.permissions)
await db.query(`
insert into permission (thread, type, user, mutable, permission, value)
values (?, ?, ?, ?, ?, ?)`,
[thread_id, 'user', id.rows[0].id, true, permission, member.permissions[permission]]);
}
if (!msg.permissions || !msg.permissions.view_limited) {
let thread = {
id: thread_id,
name: msg.name,
streams: []
};
if (!msg.permissions?.view_limited) {
for (let username in vybe.users) {
for (let socket of vybe.users[username].sockets) {
socket.emit('thread', {
name: msg.name,
id: insert.rows[0].id,
...thread,
permissions: {
is_member: false,
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) {
if (!vybe.users[member.name])
continue;
for (let socket of vybe.users[member.name].sockets) {
socket.emit('thread', {
name: msg.name,
id: insert.rows[0].id,
...thread,
permissions: {
is_member: 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]
);
threads = await Promise.all(threads.rows.map(async thread => {
if (vybe.threads[thread.id])
Object.assign(thread, vybe.threads[thread.id]);
else
thread.streams = [];
thread.streams = vybe.threads[thread.id]?.streams || [];
thread.permissions = await check_permission(msg.auth_user.id, thread.id);
return thread;
}));
@ -209,12 +210,9 @@ async function get_thread(msg, respond) {
members: Object.entries(members).map(member => ({
id: member[0],
...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({
success: true,
thread
@ -332,35 +330,35 @@ async function edit_thread(msg, respond) {
where thread.id = ?`,
[msg.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 socket of vybe.users[username].sockets)
socket.emit('thread', {
name: msg.name,
id: msg.id,
...thread,
permissions: {
is_member: username in members,
view: true,
post: !msg.permissions || !msg.permissions.post_limited
|| username in members,
post: !msg.permissions?.post_limited || username in members,
admin: username === msg.auth_user.name && perms.admin,
...permissions
}
});
}
else {
for (let member in members) {
if (!vybe.users[member])
for (let membername in members) {
if (!vybe.users[membername])
continue;
for (let socket of vybe.users[member].sockets)
for (let socket of vybe.users[membername].sockets)
socket.emit('thread', {
name: msg.name,
id: msg.id,
...thread,
permissions: {
is_member: true,
view: true,
post: true,
admin: member.id === msg.auth_user.id && perms.admin,
admin: membername === msg.auth_user.name && perms.admin,
...permissions
}
});