Initial working

This commit is contained in:
Jack Hadrill 2022-03-19 21:47:54 +00:00
commit b1977447a2
28 changed files with 2217 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["johnsoncodehk.volar"]
}

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# Vue 3 + Vite
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
## Recommended IDE Setup
- [VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar)

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

1516
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "mercury",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"animate.css": "^4.1.1",
"bennc-js": "git+https://git.jacknet.io/TerribleCodeClub/bennc-js.git",
"bootstrap": "^5.1.3",
"bootstrap-icons": "^1.8.1",
"color": "^4.2.1",
"pinia": "^2.0.12",
"reconnecting-websocket": "^4.4.0",
"romulus-js": "git+https://git.jacknet.io/TerribleCodeClub/romulus-js.git",
"uuid": "^8.3.2",
"vue": "^3.2.25",
"vue-router": "^4.0.14"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.2.0",
"vite": "^2.8.0"
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

53
src/App.vue Normal file
View File

@ -0,0 +1,53 @@
<template>
<div class="container-fluid vh-100">
<div class="row vh-100">
<div class="col-2 bg-dark">
<div class="d-flex flex-column vh-100">
<div class="d-flex flex-row logo align-items-center py-2">
<img src="./assets/logo-dark.png" class="me-2" />
<span class="fs-3 text-light">Mercury</span>
</div>
<div>
<h5 class="text-muted">Channels</h5>
<Channels />
</div>
<div>
<h5 class="text-muted mt-3">Users</h5>
<Users />
</div>
<div class="d-flex flex-column mt-auto mb-3">
<span v-if="connected" class="text-light"><i class="bi bi-circle-fill text-success"></i> Connected</span>
<span v-else class="text-light"><i class="bi bi-circle-fill text-warning"></i> Connecting...</span>
<router-link :to="{ name: 'Settings' }" class="text-muted"><i class="bi bi-gear-fill"></i> Settings</router-link>
</div>
</div>
</div>
<div class="col g-0">
<router-view />
</div>
</div>
</div>
</template>
<script setup>
import { computed } from 'vue'
import { useMercuryStore } from './stores/mercuryStore'
import Channels from './components/sidebar/Channels.vue'
import Users from './components/sidebar/Users.vue'
const mercuryStore = useMercuryStore()
const connected = computed(() => { return mercuryStore.connected })
</script>
<style>
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.logo img {
width: 2em;
height: 2em;
}
</style>

BIN
src/assets/logo-dark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
src/assets/logo-light.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
src/assets/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

13
src/common/animate.js Normal file
View File

@ -0,0 +1,13 @@
export const animateCSS = (element, animation, prefix = 'animate__') => new Promise((resolve, reject) => {
const animationName = `${prefix}${animation}`;
element.classList.add(`${prefix}animated`, animationName);
function handleAnimationEnd(event) {
event.stopPropagation()
element.classList.remove(`${prefix}animated`, animationName)
resolve('Animation ended')
}
element.addEventListener('animationend', handleAnimationEnd, {once: true})
})

40
src/components/Input.vue Normal file
View File

@ -0,0 +1,40 @@
<template>
<div class="input-group mb-3">
<input ref="input" @keyup.enter="sendMessage" v-model="message" type="text" class="form-control" placeholder="Message" aria-label="Message" autofocus autocomplete="off">
<button @click="sendMessage" class="btn btn-primary" type="button"><i class="bi bi-send-fill"></i></button>
</div>
</template>
<script setup>
import { computed, inject, ref } from 'vue'
import { MessageTypes, packers } from 'bennc-js'
import { useMessageStore } from '../stores/messageStore'
import { animateCSS } from '../common/animate'
import { onBeforeRouteUpdate } from 'vue-router';
const encoder = new TextEncoder()
const ws = inject('websocket')
const props = defineProps(['channel'])
const messageStore = useMessageStore()
const channel = computed(() => { return props.channel })
const message = ref("")
const input = ref(null)
const sendMessage = () => {
if (message.value.trim() === '') {
animateCSS(input.value, 'headShake')
} else {
ws.send(packers[MessageTypes.Basic](encoder.encode(message.value), channel.value.key.raw))
messageStore.addMessage(channel.value.id, -1, message.value)
message.value = ''
}
}
onBeforeRouteUpdate(() => {
message.value = ''
input.value.focus()
})
</script>

19
src/components/Key.vue Normal file
View File

@ -0,0 +1,19 @@
<template>
<span class="text-muted" v-show="!locked"><i @click="locked = !locked" class="bi bi-unlock-fill me-2"></i><code>{{ key }}</code></span>
<span class="text-muted" @click="locked = !locked"><i v-show="locked" class="bi bi-lock-fill"></i></span>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps(['channel'])
const key = props.channel.key.base64
const locked = ref(true)
</script>
<style scoped>
i {
cursor: pointer;
}
</style>

View File

@ -0,0 +1,18 @@
<template>
<div>
<span class="text-muted">{{ message.received.toLocaleTimeString() }}&nbsp;</span>
<span class="fw-bold" :style="{ color: user.color.hex() }">{{ user.name }}&nbsp;</span>
<span>{{ message.content }}</span>
</div>
</template>
<script setup>
import { computed } from 'vue'
import { useUserStore } from '../stores/userStore'
const props = defineProps(['message'])
const message = props.message
const userStore = useUserStore()
const user = computed(() => { return userStore.getUserById(message.userId)})
</script>

View File

@ -0,0 +1,13 @@
<template>
<nav class="d-flex flex-column">
<router-link v-for="channel in channelStore.channels" :key="channel.id" :to="{ name: 'Channel', params: { channelId: channel.id }}" class="text-light">
{{ channel.name }}
</router-link>
</nav>
</template>
<script setup>
import { useChannelStore } from '../../stores/channelStore'
const channelStore = useChannelStore()
</script>

View File

@ -0,0 +1,12 @@
<template>
<span :style="{ color: color }">{{ user.name }}</span>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps(['user'])
const user = props.user
const color = computed(() => { return user.color?.hex() ?? '#000000' })
</script>

View File

@ -0,0 +1,23 @@
<template>
<div class="d-flex flex-column">
<User :user="self" />
<div v-for="user in users" :key="user.id" class="d-flex flex-row align-items-center">
<User :user="user" />
</div>
</div>
</template>
<script setup>
import { computed, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '../../stores/userStore'
import User from './User.vue'
const route = useRoute()
const userStore = useUserStore()
const channelId = computed(() => { return route.params.channelId })
const users = computed(() => { return userStore.getUsersByChannelId(channelId.value) })
const self = userStore.getSelf()
</script>

23
src/main.js Normal file
View File

@ -0,0 +1,23 @@
import { createApp } from 'vue'
import "bootstrap/dist/css/bootstrap.min.css"
import 'bootstrap-icons/font/bootstrap-icons.css'
import "bootstrap"
import 'animate.css'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import websocket from './plugins/websocket'
const app = createApp(App)
const pinia = createPinia()
app.use(router)
app.use(pinia)
app.use(websocket)
app.mount('#app')

164
src/plugins/websocket.js Normal file
View File

@ -0,0 +1,164 @@
import ReconnectingWebSocket from 'reconnecting-websocket'
import { MessageTypes, numberToUint16BE, packers, unpackers, unpackIncomingPacket } from 'bennc-js'
import { useMercuryStore } from '../stores/mercuryStore'
import { useUserStore } from '../stores/userStore'
import { useMessageStore } from '../stores/messageStore'
import { useChannelStore } from '../stores/channelStore'
import { decrypt } from 'romulus-js'
const CLIENT_ID = 'Mercury'
const SUBSCRIBED_MESSAGE_TYPES = [MessageTypes.Basic, MessageTypes.UserDataRequest, MessageTypes.UserDataResponse]
const encoder = new TextEncoder()
const decoder = new TextDecoder()
export default {
install: (app, options) => {
// Create websocket.
const ws = new ReconnectingWebSocket('wss://chat.3t.network/bennc')
ws.binaryType = 'arraybuffer'
app.provide('websocket', ws)
// Create store accesses.
const mercuryStore = useMercuryStore()
const channelStore = useChannelStore()
const messageStore = useMessageStore()
const userStore = useUserStore()
function handleBasicMessage(incomingPacket) {
// Try decryption with each encryption key.
for (let channel of channelStore.channels) {
// Attempt decryption of incoming message.
const result = decrypt(incomingPacket.data, numberToUint16BE(MessageTypes.Basic), channel.key.raw)
// If encryption key is found...
if (result.success) {
const unpackedData = unpackers[MessageTypes.Basic](result.plaintext)
// Add channel to user's channel list.
userStore.addChannel(incomingPacket.senderId, channel.id)
// Add message to message store.
messageStore.addMessage(channel.id, incomingPacket.senderId, decoder.decode(unpackedData))
// Don't process other keys.
break
}
}
}
function handleUserDataRequest(incomingPacket) {
// Try decryption with each encryption key.
for (let channel of channelStore.channels) {
// Attempt decryption of incoming message.
const result = decrypt(incomingPacket.data, numberToUint16BE(MessageTypes.UserDataRequest), channel.key.raw)
// If encryption key is found...
if (result.success) {
// Unpack user data request.
const userDataRequest = unpackers[MessageTypes.UserDataRequest](result.plaintext)
// Update user in user store.
userStore.setName(incomingPacket.senderId, userDataRequest.username)
userStore.setColor(incomingPacket.senderId, userDataRequest.colour)
userStore.addChannel(incomingPacket.senderId, channel.id)
// Respond to user data request with a user data response.
const self = userStore.getSelf()
const userDataResponse = packers[MessageTypes.UserDataResponse]({ username: self.name, colour: self.color, clientId: CLIENT_ID }, channel.key.raw)
ws.send(userDataResponse)
// Don't process other keys.
break
}
}
}
function handleUserDataResponse(incomingPacket) {
// Try decryption with each encryption key.
for (let channel of channelStore.channels) {
// Attempt decryption of incoming message.
const result = decrypt(incomingPacket.data, numberToUint16BE(MessageTypes.UserDataResponse), channel.key.raw)
// If encryption key is found...
if (result.success) {
// Unpack user data response.
const userDataResponse = unpackers[MessageTypes.UserDataResponse](result.plaintext)
// Update user in user store.
userStore.setName(incomingPacket.senderId, userDataResponse.username)
userStore.setColor(incomingPacket.senderId, userDataResponse.colour)
userStore.addChannel(incomingPacket.senderId, channel.id)
// Don't process other keys.
break
}
}
}
function requestUserData() {
const self = userStore.getSelf()
channelStore.channels.forEach(channel => {
const userDataRequest = packers[MessageTypes.UserDataRequest]({ username: self.name, colour: self.color, clientId: CLIENT_ID }, channel.key.raw)
ws.send(userDataRequest)
})
}
// Keepalive timeout.
var timeout = setInterval(() => {
ws.send(packers[MessageTypes.Keepalive]())
}, 30000)
setInterval(() => {
requestUserData()
}, 30000)
ws.addEventListener('open', () => {
// Set global connection state for UI.
mercuryStore.setConnectionState(true)
// Subscribe to all messages.
SUBSCRIBED_MESSAGE_TYPES.forEach((messageType) => {
ws.send(packers[MessageTypes.Subscribe]({ messageType: messageType }))
})
// Make a user data request for each channel.
requestUserData()
})
ws.addEventListener('close', () => {
mercuryStore.setConnectionState(false)
})
ws.addEventListener('message', (event) => {
const incomingPacket = unpackIncomingPacket(new Uint8Array(event.data))
// Update user store.
userStore.updateUser({id: incomingPacket.senderId})
// Parse packet using message type.
switch (incomingPacket.messageType) {
case MessageTypes.Basic:
handleBasicMessage(incomingPacket)
break;
case MessageTypes.UserDataRequest:
handleUserDataRequest(incomingPacket)
break;
case MessageTypes.UserDataResponse:
handleUserDataResponse(incomingPacket)
break;
default:
console.error(`Received unknown message type ${incomingPacket.messageType}`);
}
// Restart keepalive.
clearInterval(timeout)
timeout = setInterval(() => {
console.log("Keepalive message sent!")
ws.send(packers[MessageTypes.Keepalive]())
}, 30000)
})
}
}

20
src/router/index.js Normal file
View File

@ -0,0 +1,20 @@
import { createRouter, createWebHistory } from 'vue-router'
import ConversationView from '../views/ConversationView.vue'
import SettingsView from '../views/SettingsView.vue'
export default createRouter({
history: createWebHistory(),
routes: [
{
name: 'Channel',
path: '/channel/:channelId',
component: ConversationView
},
{
name: 'Settings',
path: '/settings',
component: SettingsView
}
]
})

View File

@ -0,0 +1,36 @@
import { defineStore } from 'pinia'
class Key {
constructor(base64) {
this.base64 = base64
this.raw = Uint8Array.from(window.atob(base64), c => c.charCodeAt(0))
}
}
export const useChannelStore = defineStore({
id: 'channelStore',
state: () => ({
channels: [
{
id: 'a798423c-3fbf-434d-9a00-57d7642bff65',
name: 'Default',
key: new Key('')
},
{
id: '47198ee7-8bf4-41fe-8b42-bb001bd5f140',
name: 'HackerNews',
key: new Key('HACKERNEWQAAAAAAAAAAAA==')
}
]
}),
getters: {
getChannelById: (state) => {
return (channelId) => state.channels.find((channel) => channel.id === channelId)
}
},
actions: {
addChannel(channel) {
this.channels.push(channel)
}
}
})

View File

@ -0,0 +1,14 @@
import { defineStore } from 'pinia'
export const useMercuryStore = defineStore({
id: 'mercuryStore',
state: () => ({
connected: false
}),
getters: {},
actions: {
setConnectionState(state) {
this.connected = state
}
}
})

View File

@ -0,0 +1,25 @@
import { v4 as uuidv4 } from 'uuid'
import { defineStore } from 'pinia'
export const useMessageStore = defineStore({
id: 'messageStore',
state: () => ({
messages: []
}),
getters: {
getMessagesByChannelId: (state) => {
return (channelId) => state.messages.filter((message) => message.channelId === channelId)
}
},
actions: {
addMessage(channelId, senderId, content) {
this.messages.push({
id: uuidv4(),
channelId: channelId,
userId: senderId,
content: content,
received: new Date()
})
}
}
})

57
src/stores/userStore.js Normal file
View File

@ -0,0 +1,57 @@
import Color from 'color'
import { defineStore } from 'pinia'
export const useUserStore = defineStore({
id: 'userStore',
state: () => ({
users: [
{
id: -1,
name: 'Mercury',
color: Color('#ff4000'),
channels: [],
self: true
}
]
}),
getters: {
getSelf: (state) => {
return (self) => state.users.find(user => user.self === true)
},
getUserById: (state) => {
return (id) => state.users.find(user => user.id === id)
},
getUsersByChannelId: (state) => {
return (channelId) => state.users.filter(user => { return user.channels.includes(channelId) })
}
},
actions: {
updateUser(newUser) {
var user = this.users.find(user => user.id == newUser.id)
if (!user) {
user = {
id: newUser.id,
name: String(newUser.id),
color: Color('#000000'),
channels: []
}
this.users.push(user)
}
user.lastSeen = new Date()
},
setName(id, name) {
var user = this.users.find(user => user.id == id)
user.name = name
},
setColor(id, color) {
var user = this.users.find(user => user.id == id)
user.color = color
},
addChannel(id, channelId) {
var user = this.users.find(user => user.id == id)
if (!user.channels.includes(channelId)) {
user.channels.push(channelId)
}
}
}
})

View File

@ -0,0 +1,43 @@
<template>
<div class="d-flex flex-column vh-100">
<header class="bg-light px-3 py-2 d-flex flex-row align-items-center">
<span class="mb-0 me-3 fs-3">{{ channel.name }}</span>
<Key :channel="channel" />
</header>
<div class="messages overflow-auto p-3">
<Message v-for="message in messages" :key="message.id" :message="message" />
<div ref="bottom"></div>
</div>
<Input :channel="channel" class="mt-auto px-3" />
</div>
</template>
<script setup>
import { computed, onMounted, onUpdated, ref } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { useChannelStore } from '../stores/channelStore'
import { useMessageStore } from '../stores/messageStore'
import Input from '../components/Input.vue'
import Message from '../components/Message.vue'
import Key from '../components/Key.vue'
const route = useRoute()
const channelStore = useChannelStore()
const messageStore = useMessageStore()
const channel = computed(() => { return channelStore.getChannelById(route.params.channelId) })
const messages = computed(() => { return messageStore.getMessagesByChannelId(route.params.channelId) })
// Show the key.
const locked = ref(false)
onBeforeRouteUpdate(async (to, from) => { locked.value = false })
// Scroll to bottom.
const bottom = ref(null)
onUpdated(() => {
bottom.value.scrollIntoView({ behavior: "smooth", block: "center" })
})
onMounted(() => {
bottom.value.scrollIntoView({ block: "center" })
})
</script>

View File

@ -0,0 +1,47 @@
<template>
<div class="d-flex flex-column vh-100">
<header class="bg-light px-3 py-2 d-flex flex-row align-items-center">
<h2 class="mb-0 me-3">Settings</h2>
</header>
<div class="px-3 py-2">
<h3>Channels</h3>
<table class="table">
<thead>
<th scope="col">Name</th>
<th scope="col">Key</th>
<th scope="col" class="text-end">Delete</th>
</thead>
<tbody>
<tr v-for="channel in channelStore.channels" :key="channel.id">
<td class="w-25">{{ channel.name }}</td>
<td class="w-25"><Key :channel="channel" /></td>
<td class="w-25 text-end"><i class="deleteChannel bi bi-trash-fill text-danger text-end"></i></td>
</tr>
</tbody>
</table>
<h3>Add channel</h3>
<div class="row mb-3">
<div class="col">
<input type="text" class="form-control" placeholder="Channel name" aria-label="Channel name">
</div>
<div class="col">
<input type="password" class="form-control" placeholder="Channel key" aria-label="Channel key">
</div>
</div>
<button type="submit" class="btn btn-primary float-end">Add channel</button>
</div>
</div>
</template>
<script setup>
import { useChannelStore } from '../stores/channelStore'
import Key from '../components/Key.vue'
const channelStore = useChannelStore()
</script>
<style scope>
.deleteChannel {
cursor: pointer;
}
</style>

7
vite.config.js Normal file
View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()]
})