84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package ui
|
|
|
|
import (
|
|
"cwtch.im/cwtch/event"
|
|
"cwtch.im/cwtch/model/attr"
|
|
"cwtch.im/cwtch/model/constants"
|
|
"github.com/charmbracelet/bubbles/viewport"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type profile struct {
|
|
name string
|
|
onion string
|
|
statusViewport viewport.Model
|
|
queue event.Queue
|
|
eventBus event.Manager
|
|
}
|
|
|
|
type profiles []profile
|
|
|
|
func (ps profiles) names() []string {
|
|
var names []string
|
|
for _, profile := range ps {
|
|
names = append(names, profile.name)
|
|
}
|
|
return names
|
|
}
|
|
|
|
func getProfile(m model, onion string) profile {
|
|
var profile profile
|
|
for _, p := range m.profiles {
|
|
if p.onion == onion {
|
|
profile = p
|
|
}
|
|
}
|
|
return profile
|
|
}
|
|
|
|
func setProfile(m *model, profile profile) {
|
|
for idx, p := range m.profiles {
|
|
if p.onion == profile.onion {
|
|
m.profiles[idx] = profile
|
|
}
|
|
}
|
|
}
|
|
|
|
func unlockProfiles(m model, password string) profiles {
|
|
var unlocked []profile
|
|
|
|
m.app.LoadProfiles(password)
|
|
|
|
for _, onion := range m.app.ListProfiles() {
|
|
peer := m.app.GetPeer(onion)
|
|
name, _ := peer.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name)
|
|
unlocked = append(unlocked, profile{
|
|
name: name,
|
|
onion: onion,
|
|
statusViewport: newViewport(m.width, m.height-3),
|
|
})
|
|
}
|
|
|
|
return unlocked
|
|
}
|
|
|
|
func startProfileCmd(m *model, onion string) tea.Msg {
|
|
profile := getProfile(*m, onion)
|
|
|
|
profile.queue = event.NewQueue()
|
|
profile.eventBus = m.app.GetEventBus(onion)
|
|
|
|
profile.eventBus.Subscribe(event.NewMessageFromPeer, profile.queue)
|
|
profile.eventBus.Subscribe(event.PeerAcknowledgement, profile.queue)
|
|
profile.eventBus.Subscribe(event.SendMessageToPeerError, profile.queue)
|
|
profile.eventBus.Subscribe(event.PeerStateChange, profile.queue)
|
|
profile.eventBus.Subscribe(event.NewGetValMessageFromPeer, profile.queue)
|
|
profile.eventBus.Subscribe(event.ContactCreated, profile.queue)
|
|
|
|
setProfile(m, profile)
|
|
|
|
m.app.ActivatePeerEngine(profile.onion)
|
|
|
|
return startProfileQueuePollMsg{onion: onion}
|
|
}
|