2020-09-08 04:44:09 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v2/session"
|
2020-09-08 04:44:09 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/folder"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/guild"
|
2020-12-17 08:01:58 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/private"
|
2020-09-08 04:44:09 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/urlutils"
|
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-10-07 01:53:15 +00:00
|
|
|
"github.com/diamondburned/cchat/utils/empty"
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/ningen/v2"
|
2020-09-08 04:44:09 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
var ErrMFA = session.ErrMFA
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
type Session struct {
|
2020-10-14 20:22:11 +00:00
|
|
|
empty.Session
|
2020-12-17 08:01:58 +00:00
|
|
|
private cchat.Server
|
|
|
|
state *state.Instance
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
func NewFromInstance(i *state.Instance) (cchat.Session, error) {
|
2020-12-17 08:01:58 +00:00
|
|
|
priv, err := private.New(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to make main private server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Session{
|
|
|
|
private: priv,
|
|
|
|
state: i,
|
|
|
|
}, nil
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) ID() cchat.ID {
|
2020-12-17 08:01:58 +00:00
|
|
|
return s.state.UserID.String()
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Name() text.Rich {
|
2020-12-20 05:44:26 +00:00
|
|
|
u, err := s.state.Cabinet.Me()
|
2020-09-08 04:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen, ever.
|
2020-12-17 08:01:58 +00:00
|
|
|
return text.Rich{Content: "<@" + s.state.UserID.String() + ">"}
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text.Rich{Content: u.Username + "#" + u.Discriminator}
|
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
func (s *Session) AsIconer() cchat.Iconer { return s }
|
2020-09-08 04:44:09 +00:00
|
|
|
|
|
|
|
func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), error) {
|
2020-12-17 08:01:58 +00:00
|
|
|
u, err := s.state.Me()
|
2020-09-08 04:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to get the current user")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thanks to arikawa, AvatarURL is never empty.
|
|
|
|
iconer.SetIcon(urlutils.AvatarURL(u.AvatarURL()))
|
|
|
|
|
2020-12-17 08:01:58 +00:00
|
|
|
return s.state.AddHandler(func(*gateway.UserUpdateEvent) {
|
2020-09-08 04:44:09 +00:00
|
|
|
// Bypass the event and use the state cache.
|
2020-12-20 05:44:26 +00:00
|
|
|
if u, err := s.state.Cabinet.Me(); err == nil {
|
2020-09-08 04:44:09 +00:00
|
|
|
iconer.SetIcon(urlutils.AvatarURL(u.AvatarURL()))
|
|
|
|
}
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Disconnect() error {
|
2020-12-17 08:01:58 +00:00
|
|
|
return s.state.Close()
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 08:01:58 +00:00
|
|
|
func (s *Session) AsSessionSaver() cchat.SessionSaver { return s.state }
|
2020-09-08 04:44:09 +00:00
|
|
|
|
|
|
|
func (s *Session) Servers(container cchat.ServersContainer) error {
|
|
|
|
// Reset the entire container when the session is closed.
|
2020-12-17 08:01:58 +00:00
|
|
|
s.state.AddHandler(func(*session.Closed) {
|
2020-09-08 04:44:09 +00:00
|
|
|
container.SetServers(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set the entire container again once reconnected.
|
2020-12-17 08:01:58 +00:00
|
|
|
s.state.AddHandler(func(*ningen.Connected) {
|
2020-09-08 04:44:09 +00:00
|
|
|
s.servers(container)
|
|
|
|
})
|
|
|
|
|
|
|
|
return s.servers(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) servers(container cchat.ServersContainer) error {
|
2020-12-20 05:44:26 +00:00
|
|
|
ready := s.state.Ready()
|
2020-12-17 08:01:58 +00:00
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
switch {
|
|
|
|
// If the user has guild folders:
|
2020-12-20 05:44:26 +00:00
|
|
|
case len(ready.UserSettings.GuildFolders) > 0:
|
2020-09-08 04:44:09 +00:00
|
|
|
// TODO: account for missing guilds.
|
2020-12-20 05:44:26 +00:00
|
|
|
toplevels := make([]cchat.Server, 1, len(ready.UserSettings.GuildFolders)+1)
|
2020-12-17 08:01:58 +00:00
|
|
|
toplevels[0] = s.private
|
2020-09-08 04:44:09 +00:00
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
for _, guildFolder := range ready.UserSettings.GuildFolders {
|
2020-09-08 04:44:09 +00:00
|
|
|
// TODO: correct.
|
|
|
|
switch {
|
2020-10-14 20:22:11 +00:00
|
|
|
case guildFolder.ID != 0:
|
2020-09-08 04:44:09 +00:00
|
|
|
fallthrough
|
|
|
|
case len(guildFolder.GuildIDs) > 1:
|
2020-12-17 08:01:58 +00:00
|
|
|
toplevels = append(toplevels, folder.New(s.state, guildFolder))
|
2020-09-08 04:44:09 +00:00
|
|
|
|
|
|
|
case len(guildFolder.GuildIDs) == 1:
|
2020-12-17 08:01:58 +00:00
|
|
|
g, err := guild.NewFromID(s.state, guildFolder.GuildIDs[0])
|
2020-09-08 04:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toplevels = append(toplevels, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.SetServers(toplevels)
|
|
|
|
|
|
|
|
// If the user doesn't have guild folders but has sorted their guilds
|
|
|
|
// before:
|
2020-12-20 05:44:26 +00:00
|
|
|
case len(ready.UserSettings.GuildPositions) > 0:
|
|
|
|
guilds := make([]cchat.Server, 1, len(ready.UserSettings.GuildPositions)+1)
|
2020-12-17 08:01:58 +00:00
|
|
|
guilds[0] = s.private
|
2020-09-08 04:44:09 +00:00
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
for _, id := range ready.UserSettings.GuildPositions {
|
2020-12-17 08:01:58 +00:00
|
|
|
g, err := guild.NewFromID(s.state, id)
|
2020-09-08 04:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
guilds = append(guilds, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
container.SetServers(guilds)
|
|
|
|
|
|
|
|
// None of the above:
|
|
|
|
default:
|
2020-12-17 08:01:58 +00:00
|
|
|
g, err := s.state.Guilds()
|
2020-09-08 04:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-17 08:01:58 +00:00
|
|
|
servers := make([]cchat.Server, len(g)+1)
|
|
|
|
servers[0] = s.private
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
for i := range g {
|
2020-12-17 08:01:58 +00:00
|
|
|
servers[i+1] = guild.New(s.state, &g[i])
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
container.SetServers(servers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|