chat

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxFileSize    = 200 * 1024 * 1024 // 200 MB
	ChunkSize      = 64 * 1024         // 64 KB chunks
	FileTransferV1 = "FILE_TRANSFER_V1"
)
View Source
const (
	MaxMessageSize  = 10 * 1024 * 1024 // 10 MB - maximum message size
	MaxContactName  = 256              // Maximum contact name length
	MaxContactCount = 10000            // Maximum number of contacts
)

Security limits

Variables

This section is empty.

Functions

func CalculateFileHash

func CalculateFileHash(filePath string) (string, error)

CalculateFileHash calculates SHA256 hash of file

func CheckFzfInstalled

func CheckFzfInstalled() error

CheckFzfInstalled checks if fzf and fd are installed

func CheckNativePickerAvailable

func CheckNativePickerAvailable() bool

CheckNativePickerAvailable checks if native picker is available

func CreateFzfCommand

func CreateFzfCommand(startDir string) tea.Cmd

CreateFzfCommand creates command to launch fzf+fd

func CreateNativeFilePickerCommand

func CreateNativeFilePickerCommand() tea.Cmd

CreateNativeFilePickerCommand creates a command to launch the OS native file picker

func EncodeFileMessage

func EncodeFileMessage(msg *FileTransferMessage) ([]byte, error)

EncodeFileMessage encodes file transfer message

func GenerateTransferID

func GenerateTransferID(peerID router.PeerID, fileName string) string

GenerateTransferID generates unique transfer ID

func NewTUI

func NewTUI(chat *Chat, myID router.PeerID) *model

NewTUI creates a new TUI model

func ReadFzfResult

func ReadFzfResult(startDir string) (string, error)

ReadFzfResult reads the result from a temporary file

func ReadNativePickerResult

func ReadNativePickerResult() (string, error)

ReadNativePickerResult reads the result from a temporary file

func RunTUI

func RunTUI(chat *Chat, myID router.PeerID) error

RunTUI starts the TUI application

func ValidateFileName

func ValidateFileName(fileName string) error

ValidateFileName checks file name for security

Types

type Chat

type Chat struct {
	// contains filtered or unexported fields
}

func NewChat

func NewChat(connector *p2p.Connector, storage *Storage, dataDir string) *Chat

NewChat creates a new chat instance

func (*Chat) AddContact

func (c *Chat) AddContact(hexID string, name string) error

AddContact adds new contact

func (*Chat) BlockContact

func (c *Chat) BlockContact(peerID router.PeerID) error

BlockContact blocks contact and terminates connection

func (*Chat) Close

func (c *Chat) Close() error

Close closes the chat

func (*Chat) Connect

func (c *Chat) Connect(hexID string) error

Connect establishes connection with contact

func (*Chat) DeleteContact

func (c *Chat) DeleteContact(peerID router.PeerID) error

DeleteContact deletes a contact and all conversation history

func (*Chat) Disconnect

func (c *Chat) Disconnect(peerID router.PeerID) error

Disconnect terminates connection with contact

func (*Chat) Events

func (c *Chat) Events() <-chan ChatEvent

Events returns chat events channel

func (*Chat) GetContacts

func (c *Chat) GetContacts() ([]*Contact, error)

GetContacts returns all contacts

func (*Chat) GetMessages

func (c *Chat) GetMessages(peerID router.PeerID, limit int) ([]*Message, error)

GetMessages returns messages with a contact

func (*Chat) GetUnreadCount

func (c *Chat) GetUnreadCount(peerID router.PeerID) (int, error)

GetUnreadCount returns the number of unread messages

func (*Chat) IsOnline

func (c *Chat) IsOnline(peerID router.PeerID) bool

IsOnline checks if a contact is online

func (*Chat) MarkAsRead

func (c *Chat) MarkAsRead(peerID router.PeerID) error

MarkAsRead marks messages as read

func (*Chat) RenameContact

func (c *Chat) RenameContact(peerID router.PeerID, newName string) error

RenameContact renames a contact

func (*Chat) SearchMessages

func (c *Chat) SearchMessages(query string, limit int) ([]*SearchResult, error)

SearchMessages searches for messages containing the query string across all contacts

func (*Chat) SendFile

func (c *Chat) SendFile(peerID router.PeerID, filePath string) error

SendFile starts file sending to contact

func (*Chat) SendMessage

func (c *Chat) SendMessage(peerID router.PeerID, content string) error

SendMessage sends message to contact

func (*Chat) UnblockContact

func (c *Chat) UnblockContact(peerID router.PeerID) error

UnblockContact unblocks a contact

type ChatEvent

type ChatEvent struct {
	Type         ChatEventType
	PeerID       router.PeerID
	Message      *Message
	Contact      *Contact
	FileTransfer *FileTransfer
	Error        error
}

ChatEvent represents a chat event

type ChatEventType

type ChatEventType uint8

ChatEventType defines chat event type

const (
	ChatEventMessageReceived ChatEventType = iota
	ChatEventMessageSent
	ChatEventContactAdded
	ChatEventContactOnline
	ChatEventContactOffline
	ChatEventConnectionFailed
	ChatEventError
	ChatEventFileTransferStarted
	ChatEventFileTransferProgress
	ChatEventFileTransferCompleted
	ChatEventFileTransferFailed
)

type Contact

type Contact struct {
	PeerID               router.PeerID
	Name                 string
	AddedAt              time.Time
	LastSeen             time.Time
	IsBlocked            bool
	NotificationsBlocked bool // Block notifications from this contact
}

Contact represents a contact in address book

type FilePickerModel

type FilePickerModel struct {
	// contains filtered or unexported fields
}

FilePickerModel represents a file browser

func NewFilePicker

func NewFilePicker(startDir string, onSelect func(string), onCancel func()) *FilePickerModel

NewFilePicker creates a new file browser

func (*FilePickerModel) Init

func (fp *FilePickerModel) Init() tea.Cmd

Init initializes the model

func (*FilePickerModel) Update

func (fp *FilePickerModel) Update(msg tea.Msg) (*FilePickerModel, tea.Cmd)

Update handles events

func (*FilePickerModel) View

func (fp *FilePickerModel) View() string

View renders the file browser

type FileTransfer

type FileTransfer struct {
	ID          string
	PeerID      router.PeerID
	FileName    string
	FileSize    int64
	FilePath    string // File path (for sending or saving)
	IsOutgoing  bool
	Status      FileTransferStatus
	Progress    int // Completion percentage
	ChunksRecv  map[int]bool
	TotalChunks int
	File        *os.File
	Hash        string
	StartedAt   time.Time
	// contains filtered or unexported fields
}

FileTransfer represents an active file transfer

func (*FileTransfer) Close

func (ft *FileTransfer) Close() error

Close closes transfer file

func (*FileTransfer) UpdateProgress

func (ft *FileTransfer) UpdateProgress(chunksCompleted int)

UpdateProgress updates transfer progress

type FileTransferManager

type FileTransferManager struct {
	// contains filtered or unexported fields
}

FileTransferManager manages file transfers

func NewFileTransferManager

func NewFileTransferManager(storage *Storage, dataDir string) *FileTransferManager

NewFileTransferManager creates a new transfer manager

func (*FileTransferManager) GetTransfer

func (ftm *FileTransferManager) GetTransfer(transferID string) (*FileTransfer, bool)

GetTransfer returns transfer by ID

func (*FileTransferManager) StartReceiving

func (ftm *FileTransferManager) StartReceiving(peerID router.PeerID, msg *FileTransferMessage) (*FileTransfer, error)

StartReceiving starts file receiving

func (*FileTransferManager) StartSending

func (ftm *FileTransferManager) StartSending(peerID router.PeerID, filePath string) (*FileTransfer, error)

StartSending starts file sending

type FileTransferMessage

type FileTransferMessage struct {
	Type        FileTransferType `json:"type"`
	TransferID  string           `json:"transfer_id"`  // Unique transfer ID
	FileName    string           `json:"file_name"`    // File name
	FileSize    int64            `json:"file_size"`    // File size
	MimeType    string           `json:"mime_type"`    // MIME type
	ChunkIndex  int              `json:"chunk_index"`  // Chunk index
	TotalChunks int              `json:"total_chunks"` // Total chunks
	Data        []byte           `json:"data"`         // Chunk data
	SHA256Hash  string           `json:"sha256_hash"`  // SHA256 file hash
}

FileTransferMessage represents a file transfer message

func DecodeFileMessage

func DecodeFileMessage(data []byte) (*FileTransferMessage, error)

DecodeFileMessage decodes file transfer message

type FileTransferStatus

type FileTransferStatus string

FileTransferStatus defines transfer status

const (
	FileTransferPending      FileTransferStatus = "pending"
	FileTransferTransferring FileTransferStatus = "transferring"
	FileTransferCompleted    FileTransferStatus = "completed"
	FileTransferFailed       FileTransferStatus = "failed"
	FileTransferCancelled    FileTransferStatus = "cancelled"
)

type FileTransferType

type FileTransferType uint8

FileTransferType defines file transfer message type

const (
	FileTransferStart  FileTransferType = iota // Start of transfer (metadata)
	FileTransferChunk                          // Data chunk
	FileTransferEnd                            // End of transfer (with hash)
	FileTransferAck                            // Acknowledgment of chunk receipt
	FileTransferCancel                         // Transfer cancellation
)

type Message

type Message struct {
	ID         int64
	PeerID     router.PeerID
	Content    string
	Timestamp  time.Time
	IsOutgoing bool // true if we sent, false if received
	IsRead     bool
}

Message represents a message in chat

type SearchResult

type SearchResult struct {
	Message
	ContactName string
}

SearchResult represents a search result with contact info

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage manages message and contact storage

func NewStorage

func NewStorage(dbPath string) (*Storage, error)

NewStorage creates a new storage

func (*Storage) AddContact

func (s *Storage) AddContact(peerID router.PeerID, name string) error

AddContact adds a new contact

func (*Storage) Close

func (s *Storage) Close() error

Close closes database connection

func (*Storage) DeleteContact

func (s *Storage) DeleteContact(peerID router.PeerID) error

DeleteContact deletes contact and all conversation history

func (*Storage) GetAllContacts

func (s *Storage) GetAllContacts() ([]*Contact, error)

GetAllContacts returns all contacts

func (*Storage) GetContact

func (s *Storage) GetContact(peerID router.PeerID) (*Contact, error)

GetContact returns contact by ID

func (*Storage) GetFileTransfer

func (s *Storage) GetFileTransfer(transferID string) (peerID router.PeerID, fileName string, fileSize int64, filePath string, isOutgoing bool, status string, progress int, err error)

GetFileTransfer returns transfer information by ID

func (*Storage) GetFileTransfers

func (s *Storage) GetFileTransfers(peerID router.PeerID, limit int) ([]struct {
	TransferID  string
	FileName    string
	FileSize    int64
	IsOutgoing  bool
	Status      string
	Progress    int
	StartedAt   time.Time
	CompletedAt *time.Time
}, error)

GetFileTransfers returns list of transfers for contact

func (*Storage) GetMessages

func (s *Storage) GetMessages(peerID router.PeerID, limit int) ([]*Message, error)

GetMessages returns messages with a contact

func (*Storage) GetUnreadCount

func (s *Storage) GetUnreadCount(peerID router.PeerID) (int, error)

GetUnreadCount returns the number of unread messages from contact

func (*Storage) MarkAsRead

func (s *Storage) MarkAsRead(peerID router.PeerID) error

MarkAsRead marks all messages from contact as read

func (*Storage) SaveFileTransfer

func (s *Storage) SaveFileTransfer(transferID string, peerID router.PeerID, fileName string, fileSize int64, filePath string, isOutgoing bool, status string) error

SaveFileTransfer saves file transfer information

func (*Storage) SaveMessage

func (s *Storage) SaveMessage(msg *Message) error

SaveMessage saves a message

func (*Storage) SearchMessages

func (s *Storage) SearchMessages(query string, limit int) ([]*SearchResult, error)

SearchMessages searches for messages containing the query string Returns results from all contacts, sorted by timestamp (newest first)

func (*Storage) SetBlocked

func (s *Storage) SetBlocked(peerID router.PeerID, blocked bool) error

SetBlocked sets contact blocked status

func (*Storage) SetNotificationsBlocked

func (s *Storage) SetNotificationsBlocked(peerID router.PeerID, blocked bool) error

SetNotificationsBlocked sets notification blocking for contact

func (*Storage) UpdateContactName

func (s *Storage) UpdateContactName(peerID router.PeerID, name string) error

UpdateContactName updates contact name

func (*Storage) UpdateFileTransferProgress

func (s *Storage) UpdateFileTransferProgress(transferID string, progress int) error

UpdateFileTransferProgress updates transfer progress

func (*Storage) UpdateFileTransferStatus

func (s *Storage) UpdateFileTransferStatus(transferID string, status string, hash string) error

UpdateFileTransferStatus updates transfer status

func (*Storage) UpdateLastSeen

func (s *Storage) UpdateLastSeen(peerID router.PeerID) error

UpdateLastSeen updates contact's last activity time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL