socket update

This commit is contained in:
Troplo 2022-08-01 00:15:40 +10:00
parent 970c0021c3
commit 250fce8f30
5 changed files with 138 additions and 8 deletions

View file

@ -1,5 +1,5 @@
const auth = require("../lib/authorize_socket.js") const auth = require("../lib/authorize_socket.js")
const { User, Friend } = require("../models") const { User, Friend, Session, Theme } = require("../models")
module.exports = { module.exports = {
init(app, server) { init(app, server) {
const io = require("socket.io")(server, { const io = require("socket.io")(server, {
@ -122,6 +122,127 @@ module.exports = {
socket.emit("unauthorized", { socket.emit("unauthorized", {
message: "Please reauth." message: "Please reauth."
}) })
socket.on("token", async (token) => {
const session = await Session.findOne({ where: { session: token } })
if (session) {
const user = await User.findOne({
where: { id: session.userId },
attributes: {
exclude: ["totp", "password", "emailToken"]
},
include: [
{
model: Theme,
as: "themeObject"
}
]
})
if (user) {
socket.user = user
socket.join(user.id)
socket.emit("authorized")
socket.join(user.id)
socket.emit("siteState", {
release: process.env.RELEASE,
notification: process.env.NOTIFICATION,
notificationType: process.env.NOTIFICATION_TYPE,
latestVersion: require("../../frontend/package.json").version
})
const friends = await Friend.findAll({
where: {
userId: user.id,
status: "accepted"
}
})
await user.update({
status:
user.storedStatus === "invisible"
? "offline"
: user.storedStatus
})
friends.forEach((friend) => {
io.to(friend.friendId).emit("userStatus", {
userId: user.id,
status:
user.storedStatus === "invisible"
? "offline"
: user.storedStatus
})
})
socket.on("ping", () => {
socket.emit("pong")
})
socket.on("bcBots/deleteMessage", (e) => {
if (socket.user.bot) {
socket.to(e.userId).emit("deleteMessage", e)
} else {
socket.emit("bcBots/deleteMessage", {
error: "You cannot perform this action."
})
}
})
socket.on("idle", async () => {
const user = await User.findOne({
where: {
id: socket.user.id
}
})
if (user.storedStatus === "online") {
friends.forEach((friend) => {
io.to(friend.friendId).emit("userStatus", {
userId: user.id,
status: "away"
})
})
io.to(user.id).emit("userStatus", {
userId: user.id,
status: "away"
})
await user.update({
status: "away"
})
}
})
socket.on("online", async () => {
const user = await User.findOne({
where: {
id: socket.user.id
}
})
if (user.storedStatus === "online") {
friends.forEach((friend) => {
io.to(friend.friendId).emit("userStatus", {
userId: user.id,
status: "online"
})
})
io.to(user.id).emit("userStatus", {
userId: user.id,
status: "online"
})
await user.update({
status: "online"
})
}
})
socket.on("disconnect", async function () {
const clients =
io.sockets.adapter.rooms.get(user.id) || new Set()
if (!clients.size || clients.size === 0) {
friends.forEach((friend) => {
io.to(friend.friendId).emit("userStatus", {
userId: user.id,
status: "offline"
})
})
await user.update({
status: "offline"
})
}
})
}
}
})
console.log("Unauthenticated user") console.log("Unauthenticated user")
socket.on("reAuth", async () => { socket.on("reAuth", async () => {
socket.disconnect() socket.disconnect()

View file

@ -201,7 +201,7 @@ router.post("/login", async (req, res, next) => {
res.cookie("session", session.session, { res.cookie("session", session.session, {
maxAge: 1000 * 60 * 60 * 24 * 365, maxAge: 1000 * 60 * 60 * 24 * 365,
httpOnly: true, httpOnly: true,
secure: true, secure: false,
sameSite: "strict" sameSite: "strict"
}) })
res.json({ res.json({
@ -290,7 +290,7 @@ router.post("/register", limiter, async (req, res, next) => {
res.cookie("session", session.session, { res.cookie("session", session.session, {
maxAge: 1000 * 60 * 60 * 24 * 365, maxAge: 1000 * 60 * 60 * 24 * 365,
httpOnly: true, httpOnly: true,
secure: true, secure: false,
sameSite: "strict" sameSite: "strict"
}) })
res.json({ res.json({

View file

@ -771,6 +771,9 @@ export default {
this.$store.dispatch("getUserInfo") this.$store.dispatch("getUserInfo")
}) })
this.$socket.connect() this.$socket.connect()
this.$socket.on("unauthorized", () => {
this.$socket.emit("token", localStorage.getItem("session"))
})
document.title = this.$route.name document.title = this.$route.name
? this.$route.name + " - " + this.$store.state.site.name ? this.$route.name + " - " + this.$store.state.site.name
: this.$store.state.site.name || "Colubrina" : this.$store.state.site.name || "Colubrina"

View file

@ -77,12 +77,15 @@ Vue.use(VueNativeNotification, {
Vue.use({ Vue.use({
install(Vue) { install(Vue) {
Vue.prototype.$socket = SocketIO(process.env.VUE_APP_SOCKET_URL, { Vue.prototype.$socket = SocketIO(
localStorage.getItem("instance") || process.env.VUE_APP_SOCKET_URL,
{
transports: ["websocket", "polling"], transports: ["websocket", "polling"],
headers: { headers: {
Authorization: localStorage.getItem("session") Authorization: localStorage.getItem("session")
} }
}) }
)
} }
}) })

View file

@ -167,6 +167,9 @@ export default {
} else { } else {
this.$router.push("/") this.$router.push("/")
} }
if (this.isElectron()) {
window.location.reload()
}
}) })
.catch((e) => { .catch((e) => {
if ( if (