Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions client/src/components/chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@

<script lang="ts">
import { Component, Ref, Watch, Vue } from 'vue-property-decorator'
import { formatRelative } from 'date-fns'
import { formatRelative, parseISO } from 'date-fns'

import { Member } from '~/neko/types'

Expand Down Expand Up @@ -404,11 +404,13 @@
}

member(id: string) {
return this.$accessor.user.members[id] || { id, displayname: this.$t('somebody') }
return this.$accessor.user.members[id] || { id, displayname: id }
}

timestamp(time: Date) {
const str = formatRelative(time, new Date())
timestamp(time: Date | string) {
const dateObj = typeof time === 'string' ? parseISO(time) : time

const str = formatRelative(dateObj, new Date())
return `${str.charAt(0).toUpperCase()}${str.slice(1)}`
}

Expand Down
3 changes: 2 additions & 1 deletion client/src/neko/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const EVENT = {
KEYBOARD: 'control/keyboard',
},
CHAT: {
INIT: 'chat/init',
MESSAGE: 'chat/message',
EMOTE: 'chat/emote',
},
Expand Down Expand Up @@ -100,7 +101,7 @@ export type SignalEvents =
| typeof EVENT.SIGNAL.PROVIDE
| typeof EVENT.SIGNAL.CANDIDATE

export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE
export type ChatEvents = typeof EVENT.CHAT.INIT | typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE

export type FileTransferEvents = typeof EVENT.FILETRANSFER.LIST | typeof EVENT.FILETRANSFER.REFRESH

Expand Down
20 changes: 18 additions & 2 deletions client/src/neko/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SystemInitPayload,
AdminLockResource,
FileTransferListPayload,
ChatInitPayload,
} from './messages'

interface NekoEvents extends BaseEvents {}
Expand Down Expand Up @@ -335,7 +336,22 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
/////////////////////////////
// Chat Events
/////////////////////////////
protected [EVENT.CHAT.MESSAGE]({ id, content }: ChatPayload) {
protected [EVENT.CHAT.INIT]({ enabled, history }: ChatInitPayload) {
// Process message history if available
if (history && history.length > 0) {
// Process messages in chronological order
for (const message of history) {
this.$accessor.chat.newMessage({
id: message.id,
content: message.content,
type: 'text',
created: message.created,
})
}
}
}

protected [EVENT.CHAT.MESSAGE]({ id, content, created }: ChatPayload) {
const member = this.member(id)
if (!member || member.ignored) {
return
Expand All @@ -345,7 +361,7 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
id,
content,
type: 'text',
created: new Date(),
created: created,
})
}

Expand Down
13 changes: 13 additions & 0 deletions client/src/neko/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type WebSocketMessages =
| ControlMessage
| ScreenResolutionMessage
| ScreenConfigurationsMessage
| ChatInitMessage
| ChatMessage

export type WebSocketPayloads =
Expand All @@ -36,6 +37,7 @@ export type WebSocketPayloads =
| ControlPayload
| ControlClipboardPayload
| ControlKeyboardPayload
| ChatInitPayload
| ChatPayload
| ChatSendPayload
| EmojiSendPayload
Expand Down Expand Up @@ -168,6 +170,16 @@ export interface ControlKeyboardPayload {
/*
CHAT PAYLOADS
*/
// chat/init
export interface ChatInitMessage extends WebSocketMessage, ChatInitPayload {
event: typeof EVENT.CHAT.INIT
}

export interface ChatInitPayload {
enabled: boolean
history: ChatPayload[]
}

// chat/message
export interface ChatMessage extends WebSocketMessage, ChatPayload {
event: typeof EVENT.CHAT.MESSAGE
Expand All @@ -179,6 +191,7 @@ export interface ChatSendPayload {
export interface ChatPayload {
id: string
content: string
created: string | Date
}

// chat/emoji
Expand Down
2 changes: 1 addition & 1 deletion client/src/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Emotes {
interface Message {
id: string
content: string
created: Date
created: Date | string
type: 'text' | 'event'
}

Expand Down
75 changes: 38 additions & 37 deletions server/internal/http/legacy/event/events.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
package event

const (
SYSTEM_INIT = "system/init"
SYSTEM_DISCONNECT = "system/disconnect"
SYSTEM_ERROR = "system/error"
SYSTEM_INIT = "system/init"
SYSTEM_DISCONNECT = "system/disconnect"
SYSTEM_ERROR = "system/error"
)

const (
CLIENT_HEARTBEAT = "client/heartbeat"
CLIENT_HEARTBEAT = "client/heartbeat"
)

const (
SIGNAL_OFFER = "signal/offer"
SIGNAL_ANSWER = "signal/answer"
SIGNAL_PROVIDE = "signal/provide"
SIGNAL_CANDIDATE = "signal/candidate"
SIGNAL_OFFER = "signal/offer"
SIGNAL_ANSWER = "signal/answer"
SIGNAL_PROVIDE = "signal/provide"
SIGNAL_CANDIDATE = "signal/candidate"
)

const (
MEMBER_LIST = "member/list"
MEMBER_CONNECTED = "member/connected"
MEMBER_DISCONNECTED = "member/disconnected"
MEMBER_LIST = "member/list"
MEMBER_CONNECTED = "member/connected"
MEMBER_DISCONNECTED = "member/disconnected"
)

const (
CONTROL_LOCKED = "control/locked"
CONTROL_RELEASE = "control/release"
CONTROL_REQUEST = "control/request"
CONTROL_REQUESTING = "control/requesting"
CONTROL_GIVE = "control/give"
CONTROL_CLIPBOARD = "control/clipboard"
CONTROL_KEYBOARD = "control/keyboard"
CONTROL_LOCKED = "control/locked"
CONTROL_RELEASE = "control/release"
CONTROL_REQUEST = "control/request"
CONTROL_REQUESTING = "control/requesting"
CONTROL_GIVE = "control/give"
CONTROL_CLIPBOARD = "control/clipboard"
CONTROL_KEYBOARD = "control/keyboard"
)

const (
CHAT_MESSAGE = "chat/message"
CHAT_EMOTE = "chat/emote"
CHAT_INIT = "chat/init"
CHAT_MESSAGE = "chat/message"
CHAT_EMOTE = "chat/emote"
)

const (
FILETRANSFER_LIST = "filetransfer/list"
FILETRANSFER_REFRESH = "filetransfer/refresh"
FILETRANSFER_LIST = "filetransfer/list"
FILETRANSFER_REFRESH = "filetransfer/refresh"
)

const (
SCREEN_CONFIGURATIONS = "screen/configurations"
SCREEN_RESOLUTION = "screen/resolution"
SCREEN_SET = "screen/set"
SCREEN_CONFIGURATIONS = "screen/configurations"
SCREEN_RESOLUTION = "screen/resolution"
SCREEN_SET = "screen/set"
)

const (
BROADCAST_STATUS = "broadcast/status"
BROADCAST_CREATE = "broadcast/create"
BROADCAST_DESTROY = "broadcast/destroy"
BROADCAST_STATUS = "broadcast/status"
BROADCAST_CREATE = "broadcast/create"
BROADCAST_DESTROY = "broadcast/destroy"
)

const (
ADMIN_BAN = "admin/ban"
ADMIN_KICK = "admin/kick"
ADMIN_LOCK = "admin/lock"
ADMIN_MUTE = "admin/mute"
ADMIN_UNLOCK = "admin/unlock"
ADMIN_UNMUTE = "admin/unmute"
ADMIN_CONTROL = "admin/control"
ADMIN_RELEASE = "admin/release"
ADMIN_GIVE = "admin/give"
ADMIN_BAN = "admin/ban"
ADMIN_KICK = "admin/kick"
ADMIN_LOCK = "admin/lock"
ADMIN_MUTE = "admin/mute"
ADMIN_UNLOCK = "admin/unlock"
ADMIN_UNMUTE = "admin/unmute"
ADMIN_CONTROL = "admin/control"
ADMIN_RELEASE = "admin/release"
ADMIN_GIVE = "admin/give"
)
13 changes: 10 additions & 3 deletions server/internal/http/legacy/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message

import (
"github.com/m1k1o/neko/server/internal/http/legacy/types"
"time"

"github.com/pion/webrtc/v3"
)
Expand Down Expand Up @@ -91,10 +92,16 @@ type ChatReceive struct {
Content string `json:"content"`
}

type ChatInit struct {
Event string `json:"event"`
Enabled bool `json:"enabled"`
History []ChatSend `json:"history"`
}
type ChatSend struct {
Event string `json:"event"`
ID string `json:"id"`
Content string `json:"content"`
Event string `json:"event"`
ID string `json:"id"`
Content string `json:"content"`
Created time.Time `json:"created"`
}

type EmoteReceive struct {
Expand Down
24 changes: 22 additions & 2 deletions server/internal/http/legacy/wstoclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,27 @@ func (s *session) wsToClient(msg []byte) error {

// Chat Events
case chat.CHAT_INIT:
// ignore chat init, because it is not part of the legacy protocol
return nil
request := &chat.Init{}
err := json.Unmarshal(data.Payload, request)
if err != nil {
return err
}

history := []oldMessage.ChatSend{}
for _, msg := range request.History {
history = append(history, oldMessage.ChatSend{
Event: oldEvent.CHAT_MESSAGE,
ID: msg.ID,
Content: msg.Content.Text,
Created: msg.Created,
})
}

return s.toClient(&oldMessage.ChatInit{
Event: oldEvent.CHAT_INIT,
Enabled: request.Enabled,
History: history,
})

case chat.CHAT_MESSAGE:
request := &chat.Message{}
Expand All @@ -563,6 +582,7 @@ func (s *session) wsToClient(msg []byte) error {
Event: oldEvent.CHAT_MESSAGE,
ID: request.ID,
Content: request.Content.Text,
Created: request.Created,
})

case event.SEND_BROADCAST:
Expand Down
47 changes: 35 additions & 12 deletions server/internal/plugins/chat/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ func NewManager(
logger := log.With().Str("module", "chat").Logger()

return &Manager{
logger: logger,
config: config,
sessions: sessions,
logger: logger,
config: config,
sessions: sessions,
messageHistory: make([]Message, 0),
}
}

type Manager struct {
logger zerolog.Logger
config *Config
sessions types.SessionManager
logger zerolog.Logger
config *Config
sessions types.SessionManager
messageHistory []Message
}

type Settings struct {
Expand Down Expand Up @@ -66,7 +68,13 @@ func (m *Manager) settingsForSession(session types.Session) (Settings, error) {
}

func (m *Manager) sendMessage(session types.Session, content Content) {
now := time.Now()
// Create the message
message := Message{
ID: session.ID(),
Created: time.Now(),
Content: content,
}
m.addMessageToHistory(message)

// get all sessions that have chat enabled
var sessions []types.Session
Expand All @@ -80,19 +88,34 @@ func (m *Manager) sendMessage(session types.Session, content Content) {

// send content to all sessions
for _, s := range sessions {
s.Send(CHAT_MESSAGE, Message{
ID: session.ID(),
Created: now,
Content: content,
})
s.Send(CHAT_MESSAGE, message)
}
}

func (m *Manager) addMessageToHistory(message Message) {
// Add message to history
m.messageHistory = append(m.messageHistory, message)

// Keep only the last 100 messages
if len(m.messageHistory) > 100 {
m.messageHistory = m.messageHistory[len(m.messageHistory)-100:]
}
}

func (m *Manager) Start() error {
// send init message once a user connects
m.sessions.OnConnected(func(session types.Session) {
messageHistory := m.messageHistory
// Check if user can receive messages and reset the message history if he can't
settings, err := m.settingsForSession(session)
if err != nil || !settings.CanReceive {
messageHistory = make([]Message, 0)
}

// Send init message with message history
session.Send(CHAT_INIT, Init{
Enabled: m.config.Enabled,
History: messageHistory,
})
})

Expand Down
3 changes: 2 additions & 1 deletion server/internal/plugins/chat/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const (
)

type Init struct {
Enabled bool `json:"enabled"`
Enabled bool `json:"enabled"`
History []Message `json:"history"`
}

type Content struct {
Expand Down