1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-27 14:27:43 +00:00
cchat-discord/internal/discord/session/guild/guild.go

102 lines
2.5 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package guild
import (
"context"
"sort"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/discord"
2020-09-08 04:44:09 +00:00
"github.com/diamondburned/cchat"
2021-03-15 04:30:05 +00:00
"github.com/diamondburned/cchat-discord/internal/discord/session/channel"
"github.com/diamondburned/cchat-discord/internal/discord/session/channel/category"
2020-09-08 04:44:09 +00:00
"github.com/diamondburned/cchat-discord/internal/discord/state"
2021-03-15 04:30:05 +00:00
"github.com/diamondburned/cchat-discord/internal/funcutil"
2020-10-07 01:53:15 +00:00
"github.com/diamondburned/cchat/utils/empty"
2020-09-08 04:44:09 +00:00
"github.com/pkg/errors"
)
type Guild struct {
2020-10-07 01:53:15 +00:00
empty.Server
2020-09-08 04:44:09 +00:00
id discord.GuildID
state *state.Instance
}
2020-10-07 01:53:15 +00:00
func New(s *state.Instance, g *discord.Guild) cchat.Server {
2020-09-08 04:44:09 +00:00
return &Guild{
id: g.ID,
state: s,
}
}
2020-10-07 01:53:15 +00:00
func NewFromID(s *state.Instance, gID discord.GuildID) (cchat.Server, error) {
2021-01-06 05:02:54 +00:00
g, err := s.Cabinet.Guild(gID)
2020-09-08 04:44:09 +00:00
if err != nil {
return nil, err
}
return New(s, g), nil
}
2021-01-06 05:02:54 +00:00
func (g *Guild) self() (*discord.Guild, error) {
2020-12-20 05:44:26 +00:00
return g.state.Cabinet.Guild(g.id)
2020-09-08 04:44:09 +00:00
}
func (g *Guild) ID() cchat.ID {
return g.id.String()
}
2021-03-13 11:49:23 +00:00
func (g *Guild) Name(ctx context.Context, l cchat.LabelContainer) (func(), error) {
2021-03-15 04:30:05 +00:00
return g.state.Labels.AddGuildLabel(g.id, l), nil
2020-09-08 04:44:09 +00:00
}
2021-03-18 21:27:24 +00:00
func (g *Guild) Columnate() bool { return true }
2021-03-18 17:12:31 +00:00
2020-10-07 01:53:15 +00:00
func (g *Guild) AsLister() cchat.Lister { return g }
2020-09-08 04:44:09 +00:00
2021-03-13 11:49:23 +00:00
func (g *Guild) Servers(container cchat.ServersContainer) (func(), error) {
2020-09-08 04:44:09 +00:00
c, err := g.state.Channels(g.id)
if err != nil {
2021-03-15 04:30:05 +00:00
return nil, errors.Wrap(err, "Failed to get channels")
2020-09-08 04:44:09 +00:00
}
// Only get top-level channels (those with category ID being null).
var toplevels = category.FilterAccessible(g.state, category.FilterCategory(c, 0))
// Sort so that positions are correct.
sort.SliceStable(toplevels, func(i, j int) bool {
return toplevels[i].Position < toplevels[j].Position
})
// Sort so that channels are before categories.
sort.SliceStable(toplevels, func(i, _ int) bool {
return toplevels[i].Type != discord.GuildCategory
})
2021-03-13 08:21:12 +00:00
chs := make([]cchat.Server, 0, len(toplevels))
2021-03-13 11:49:23 +00:00
// ids := make(map[discord.ChannelID]struct{}, len(toplevels))
2020-09-08 04:44:09 +00:00
for _, ch := range toplevels {
switch ch.Type {
case discord.GuildCategory:
chs = append(chs, category.New(g.state, ch))
case discord.GuildText:
c, err := channel.New(g.state, ch)
if err != nil {
2021-03-13 11:49:23 +00:00
return nil, errors.Wrapf(err, "Failed to make channel %q: %v", ch.Name, err)
2020-09-08 04:44:09 +00:00
}
chs = append(chs, c)
2021-03-13 08:21:12 +00:00
default:
continue
2020-09-08 04:44:09 +00:00
}
}
container.SetServers(chs)
2021-03-13 08:21:12 +00:00
2021-03-15 04:30:05 +00:00
// TODO: account for insertion/deletion.
2021-03-13 11:49:23 +00:00
// TODO: RACEEEEEEEEEEEEEEEEEEEEEEE CONDITION!!!!!!!!!!!!
// TODO: Add channel stuff.
stop := funcutil.JoinCancels()
return stop, nil
2020-09-08 04:44:09 +00:00
}