Skip to content

Commit

Permalink
fix: Deleting the default channels no longer bricks the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Chedski committed Dec 10, 2024
1 parent 0f9b367 commit 1ce5e55
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
config.yml
.replit*
replit*
data*.db
data*.db
log/
29 changes: 27 additions & 2 deletions client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var user = {
}
var rooms = {}

const january_proxy_url = "https://chedski.net/january/proxy?url="
const january_embed_url = "https://chedski.net/january/embed?url="
const january_proxy_url = "https://chedski.net/janu/proxy?url="
const january_embed_url = "https://chedski.net/janu/embed?url="

var session_data = {
auth_key: null,
Expand Down Expand Up @@ -781,6 +781,12 @@ function command_list_public_rooms(args) {
document.getElementById("message_content").value = ""
}

/** @param {String} args */
function command_list_all_rooms(args) {
socket.send(JSON.stringify({ n: "get_full_room_list", d: args }))
document.getElementById("message_content").value = ""
}

/** @param {String} args */
function command_list_rooms(args) {
var memrooms = Object.values(rooms)
Expand Down Expand Up @@ -944,6 +950,11 @@ function send(content) {
command_list_public_rooms(args)
return

case "lsa":
case "allrooms":
command_list_all_rooms(args)
return

case "deleteroom":
command_delete_room(args)
return
Expand Down Expand Up @@ -1164,6 +1175,20 @@ socket.onmessage = (real_event) => {
}
break


case "full_room_list":
var pubrooms = data
var si = pubrooms.length == 1
var items = [qspan(`There ${si ? 'is' : 'are'} ${pubrooms.length} room${si ? '' : 's'}: `)]
for (let i = 0; i < pubrooms.length; i++) {
const room = pubrooms[i]
if (i != 0) { items.push(qspan(", ")) }
items.push(room_span(room))
}
system_message(...items)

break

case "public_room_list":
if (on_pubroom_update != null) {
on_pubroom_update(data)
Expand Down
5 changes: 2 additions & 3 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<script src="twemoji.js" crossorigin="anonymous"></script>
<script src="client.js"></script>
<meta content="Vessel" property="og:title">
<meta content="Cook That Shit Up Quay!" property="og:description">
<meta content="https://replit.com/@mhyl/Vessel#client/favicon.png" property="og:image">
<meta content="its a thing" property="og:description">
<meta content="#ffffff" data-react-helmet="true" name="theme-color">
<link rel="shortcut icon" type="image/x-icon" href="favicon3.ico">
</head>
Expand Down Expand Up @@ -131,4 +130,4 @@
<input id="message_content" type="text" placeholder="Type a message or type /help for a list of commands" autocomplete="off">
</div>
</body>
</html>
</html>
69 changes: 45 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ const crypto = require('crypto')
const wss = require('lup-express-ws').default(express())
const app = wss.app
const namegen = require('./namegen')
const fs = require('fs');
const util = require('util');
/** @type {http.Server} */
var httpserver

var logFile = fs.createWriteStream('log/log.txt', { flags: 'a' });
// Or 'w' to truncate the file every time the process starts.
var logStdout = process.stdout;
console.log = function () {
logFile.write(util.format.apply(null, arguments) + '\n');
logStdout.write(util.format.apply(null, arguments) + '\n');
}

/** @type {string} */
const ver = require("./package.json").version
Expand All @@ -35,6 +44,7 @@ ${' '.repeat(Math.floor((63 - sq.length) / 2))}${sq}
* @param {string} name The specific part of Vessel that logged this message
* @param {...any} data
*/

function log2(name, ...data) {
console.log(`[${new Date().toUTCString()}] [${name}] `, ...data)
}
Expand All @@ -45,7 +55,8 @@ const config = require("./config_handler").config
const { Sequelize, Model, DataTypes } = require('sequelize')
var silence_db = false
const sequelize = new Sequelize(config.database, {
logging: (msg) => { if (!silence_db) { log2("Database", msg) } }
// logging: (msg) => { if (!silence_db) { log2("Database", msg) } }
logging: () => {}
})


Expand Down Expand Up @@ -250,7 +261,7 @@ class User {
if (typeof (data.name) == "string") { new Room(data.name, data.type, self) }
else { throw new VesselDataError("Name must be a string") }
break

_
case "change_name":
if (typeof data === 'string') { self.set_nickname(data) }
else { throw new VesselDataError("Data must be a string") }
Expand Down Expand Up @@ -287,6 +298,13 @@ class User {
self.send("public_room_list", pubrooms)
break

case "get_full_room_list":
if (!self.isAdmin) { self.socket.close(); return }
let allrooms = []
Object.values(rooms).forEach((room) => { allrooms.push(room.get_client_object(self)) })
self.send("full_room_list", allrooms)
break

case "join_room":
if (rooms[data.id]) { rooms[data.id].add(self) }
else { throw new VesselResourceError("Room does not exist") }
Expand Down Expand Up @@ -576,7 +594,7 @@ class Room {
this.userdata = JSON.parse(data.userdata)
} catch (err) {
log2("Rooms", `The room ${this.id}'s userdata JSON failed to load':`)
log2("Ro oms", data.userdata)
log2("Rooms", data.userdata)
}

} else {
Expand Down Expand Up @@ -804,10 +822,10 @@ class Room {
}

async function create_initial_rooms() {
log2("Onboarding", `No rooms found, creating default rooms.`)
log2("Onboarding", `No public rooms found, creating default rooms.`)

log2("Onboarding", `Creating ${config.main_channel_name}.`)
var main_room = new Room(config.main_channel_name, "public", undefined, "27b9bef4-ffb7-451e-b010-29870760e2b1")
var main_room = new Room(config.main_channel_name, "public", undefined)//"27b9bef4-ffb7-451e-b010-29870760e2b1")
log2("Onboarding", `Configuring ${config.main_channel_name}.`)
main_room.isMain = true
main_room.autoJoin = true
Expand All @@ -817,7 +835,7 @@ async function create_initial_rooms() {
await main_room.save()

log2("Onboarding", `Creating ${config.shout_channel_name}.`)
var shout_room = new Room(config.shout_channel_name, "public", undefined, "823d68d9-a20c-409e-b6db-12e313ed9a16")
var shout_room = new Room(config.shout_channel_name, "public", undefined)//, "823d68d9-a20c-409e-b6db-12e313ed9a16")
log2("Onboarding", `Configuring ${config.shout_channel_name}.`)
shout_room.isShout = true
shout_room.adminOnly = true
Expand All @@ -835,11 +853,11 @@ async function run() {
var got = await import('got')

log2("Resources", `Saving copies of client JS scripts to memory...`)
var js_twemoji = await got.got("https://twemoji.maxcdn.com/v/latest/twemoji.min.js")
//var js_twemoji = await got.got("https://twemoji.maxcdn.com/v/latest/twemoji.min.js")
var js_marked = await got.got("https://cdn.jsdelivr.net/npm/marked/marked.min.js")
log2("Resources", `Done!`)

app.get("/twemoji.js", (req, res) => { res.setHeader("Content-Type", "text/javascript").send(js_twemoji.body) })
//app.get("/twemoji.js", (req, res) => { res.setHeader("Content-Type", "text/javascript").send(js_twemoji.body) })
app.get("/marked.js", (req, res) => { res.setHeader("Content-Type", "text/javascript").send(js_marked.body) })

log2("Persist", `Making sure the room table exists.`)
Expand All @@ -851,25 +869,28 @@ async function run() {
log2("Persist", `Getting rooms from database.`)
var roomList = await PersistentRoom.findAll()
log2("Persist", `Done!`)
if (roomList.length == 0) {
var main_counter = 0
var shout_counter = 0
Object.values(rooms).forEach((room) => {
if (room.isPublic && room.isMain && room.autoJoin) { main_counter++ }
if (room.isPublic && room.isShout && room.autoJoin) { shout_counter++ }
})
if (main_counter == 0 && shout_counter == 0) {
await create_initial_rooms()
} else {
log2("Persist", `Loading rooms...`)
silence_db = true
roomList.forEach((data) => {

if (data["id"]) {
new Room("", "loaded", undefined, data["id"], data)
} else {
log2("Persist", `A room was found with a missing ID. Destroying.`)
data.destroy({ force: true })
}
})
silence_db = false
log2("Persist", `Done!`)
}
log2("Persist", `Loading rooms...`)
silence_db = true
roomList.forEach((data) => {


if (data["id"]) {
new Room("", "loaded", undefined, data["id"], data)
} else {
log2("Persist", `A room was found with a missing ID. Destroying.`)
data.destroy({ force: true })
}
})
silence_db = false
log2("Persist", `Done!`)

app.ws("/ev", (s) => {
if (shutting_down) { s.close() }
Expand Down

0 comments on commit 1ce5e55

Please sign in to comment.