Compentize Workload (#4)

This commit is contained in:
2023-03-30 23:51:05 -04:00
committed by GitHub
parent 228c293b70
commit 3d15d09dd7
21 changed files with 528 additions and 319 deletions

View File

@ -1,7 +1,43 @@
package core
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/yeslayla/birdbot/common"
)
type Channel struct {
Name string
ID string
Verified bool
}
func GenerateEventChannelName(eventName string, location string, dateTime time.Time) string {
month := GetMonthPrefix(dateTime)
day := dateTime.Day()
city := GetCityFromLocation(location)
year := dateTime.Year()
channel := fmt.Sprint(month, "-", day, city, "-", eventName, "-", year)
channel = strings.ReplaceAll(channel, " ", "-")
channel = strings.ToLower(channel)
re, _ := regexp.Compile(`[^\w\-]`)
channel = re.ReplaceAllString(channel, "")
return channel
}
// GenerateChannel returns a channel object associated with an event
func GenerateChannel(event common.Event) *Channel {
channelName := GenerateEventChannelName(event.Name, event.Location, event.DateTime)
return &Channel{
Name: channelName,
Verified: false,
}
}

View File

@ -3,6 +3,7 @@ package core
type Config struct {
Discord DiscordConfig `yaml:"discord"`
Mastodon MastodonConfig `yaml:"mastodon"`
Features Features `yaml:"features"`
}
type DiscordConfig struct {
@ -21,3 +22,10 @@ type MastodonConfig struct {
ClientID string `yaml:"client_id" env:"MASTODON_CLIENT_ID"`
ClientSecret string `yaml:"client_secret" env:"MASTODON_CLIENT_SECRET"`
}
type Features struct {
ManageEventChannels bool `yaml:"manage_event_channels" env:"BIRD_EVENT_CHANNELS" env-default:"true"`
AnnounceEvents bool `yaml:"announce_events" env:"BIRD_ANNOUNCE_EVENTS" env-default:"true"`
ReccurringEvents bool `yaml:"recurring_events" env:"BIRD_RECURRING_EVENTS" env-default:"true"`
LoadGamePlugins bool `yaml:"load_game_plugins" env:"BIRD_LOAD_GAME_PLUGINS" env-default:"true"`
}

View File

@ -1,99 +0,0 @@
package core
import (
"fmt"
"regexp"
"strings"
"time"
)
const REMOTE_LOCATION string = "online"
type Event struct {
Name string
ID string
Location string
Completed bool
DateTime time.Time
CompleteTime time.Time
Description string
Image string
Organizer *User
}
// Channel returns a channel object associated with an event
func (event *Event) Channel() *Channel {
month := event.GetMonthPrefix()
day := event.DateTime.Day()
city := event.GetCityFromLocation()
year := event.DateTime.Year()
channel := fmt.Sprint(month, "-", day, city, "-", event.Name, "-", year)
channel = strings.ReplaceAll(channel, " ", "-")
channel = strings.ToLower(channel)
re, _ := regexp.Compile(`[^\w\-]`)
channel = re.ReplaceAllString(channel, "")
return &Channel{
Name: channel,
Verified: false,
}
}
// GetCityFromLocation returns the city name of an event's location
func (event *Event) GetCityFromLocation() string {
if event.Location == REMOTE_LOCATION {
return fmt.Sprint("-", REMOTE_LOCATION)
}
parts := strings.Split(event.Location, " ")
index := -1
loc := event.Location
for i, v := range parts {
part := strings.ToLower(v)
if part == "mi" || part == "michigan" {
index = i - 1
if index < 0 {
return ""
}
if index > 0 && parts[index] == "," {
index -= 1
}
if index > 1 && strings.Contains(parts[index-2], ",") {
loc = fmt.Sprintf("%s-%s", parts[index-1], parts[index])
break
}
loc = parts[index]
break
}
}
return fmt.Sprint("-", loc)
}
// GetMonthPrefix returns a month in short form
func (event *Event) GetMonthPrefix() string {
month := event.DateTime.Month()
data := map[time.Month]string{
time.January: "jan",
time.February: "feb",
time.March: "march",
time.April: "april",
time.May: "may",
time.June: "june",
time.July: "july",
time.August: "aug",
time.September: "sept",
time.October: "oct",
time.November: "nov",
time.December: "dec",
}
return data[month]
}

View File

@ -1,62 +0,0 @@
package core
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetChannelName(t *testing.T) {
assert := assert.New(t)
// Test Valid Address
event := Event{
Name: "Hello World",
Location: "1234 Place Rd, Ann Arbor, MI 00000",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-ann-arbor-hello-world-2022", event.Channel().Name)
// Test Unparsable Location
// lmanley: Note it'd be nice to expand support for this
event = Event{
Name: "Hello World",
Location: "Michigan Theater, Ann Arbor",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-hello-world-2022", event.Channel().Name)
// Test Short Location
event = Event{
Name: "Hello World",
Location: "Monroe, MI",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-monroe-hello-world-2022", event.Channel().Name)
// Test Short Location
event = Event{
Name: "Hello World",
Location: "Monroe St, Monroe , MI",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-monroe-hello-world-2022", event.Channel().Name)
// Test Remote Event
event = Event{
Name: "Hello World",
Location: REMOTE_LOCATION,
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-online-hello-world-2022", event.Channel().Name)
}
func TestMonthPrefix(t *testing.T) {
assert := assert.New(t)
event := Event{
DateTime: time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan", event.GetMonthPrefix())
}

42
core/location.go Normal file
View File

@ -0,0 +1,42 @@
package core
import (
"fmt"
"strings"
)
const REMOTE_LOCATION string = "online"
// GetCityFromLocation returns the city name of an event's location
func GetCityFromLocation(location string) string {
if location == REMOTE_LOCATION {
return fmt.Sprint("-", REMOTE_LOCATION)
}
parts := strings.Split(location, " ")
index := -1
loc := location
for i, v := range parts {
part := strings.ToLower(v)
if part == "mi" || part == "michigan" {
index = i - 1
if index < 0 {
return ""
}
if index > 0 && parts[index] == "," {
index -= 1
}
if index > 1 && strings.Contains(parts[index-2], ",") {
loc = fmt.Sprintf("%s-%s", parts[index-1], parts[index])
break
}
loc = parts[index]
break
}
}
return fmt.Sprint("-", loc)
}

24
core/time.go Normal file
View File

@ -0,0 +1,24 @@
package core
import "time"
// GetMonthPrefix returns a month in short form
func GetMonthPrefix(dateTime time.Time) string {
month := dateTime.Month()
data := map[time.Month]string{
time.January: "jan",
time.February: "feb",
time.March: "march",
time.April: "april",
time.May: "may",
time.June: "june",
time.July: "july",
time.August: "aug",
time.September: "sept",
time.October: "oct",
time.November: "nov",
time.December: "dec",
}
return data[month]
}

View File

@ -1,16 +0,0 @@
package core
import "fmt"
type User struct {
ID string
}
// Mention generated a Discord mention string for the user
func (user *User) Mention() string {
if user == nil {
return "<NULL>"
}
return fmt.Sprintf("<@%s>", user.ID)
}

View File

@ -1,23 +0,0 @@
package core
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUserMention(t *testing.T) {
assert := assert.New(t)
// Create user object
user := &User{
ID: "sample_id",
}
assert.Equal("<@sample_id>", user.Mention())
// Test null user
var nullUser *User = nil
assert.NotEmpty(nullUser.Mention())
}