Skip to content

Commit

Permalink
feat: ability track by subforum
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 22, 2023
1 parent feee6dc commit 7c2e267
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 20 deletions.
22 changes: 19 additions & 3 deletions app/configurator/configurators.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ type Alerts struct {
PingMessage CfgAlertPingMessage
}

type Forum struct {
type ForumThread struct {
Watch ConfiguratorForumWatch
Ignore ConfiguratorForumIgnore
}

type ForumSubforum struct {
Watch ConfiguratorSubForumWatch
Ignore ConfiguratorSubForumIgnore
}

type Forum struct {
Thread ForumThread
Subforum ForumSubforum
}
type Configurators struct {
Bases ConfiguratorBase
Players Players
Expand Down Expand Up @@ -57,8 +67,14 @@ func NewConfigugurators(dbpath types.Dbpath) *Configurators {
PingMessage: NewCfgAlertPingMessage(configur),
},
Forum: Forum{
Watch: NewConfiguratorForumWatch(configur),
Ignore: NewConfiguratorForumIgnore(configur),
Thread: ForumThread{
Watch: NewConfiguratorForumWatch(configur),
Ignore: NewConfiguratorForumIgnore(configur),
},
Subforum: ForumSubforum{
Watch: NewConfiguratorSubForumWatch(configur),
Ignore: NewConfiguratorSubForumIgnore(configur),
},
},
}
}
2 changes: 2 additions & 0 deletions app/configurator/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (cg *Configurator) AutoMigrateSchema() *Configurator {
&models.TagRegion{},
&models.TagForumPostTrack{},
&models.TagForumPostIgnore{},
&models.TagForumSubforumTrack{},
&models.TagForumSubforumIgnore{},
&models.TagPlayerEvent{},
&models.AlertNeutralPlayersEqualOrGreater{},
&models.AlertEnemiesEqualOrGreater{},
Expand Down
8 changes: 8 additions & 0 deletions app/configurator/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ type TagForumPostIgnore struct {
TagTemplate
}

type TagForumSubforumTrack struct {
TagTemplate
}

type TagForumSubforumIgnore struct {
TagTemplate
}

// =========== Alerts ===============

type AlertTresholdShared struct {
Expand Down
12 changes: 11 additions & 1 deletion app/configurator/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type taggable interface {
models.TagPlayerEnemy |
models.TagPlayerEvent |
models.TagForumPostTrack |
models.TagForumPostIgnore
models.TagForumPostIgnore |
models.TagForumSubforumTrack |
models.TagForumSubforumIgnore
GetTag() types.Tag
}

Expand Down Expand Up @@ -67,6 +69,14 @@ type ConfiguratorForumIgnore = ConfiguratorTags[models.TagForumPostIgnore]

var NewConfiguratorForumIgnore = NewConfiguratorTags[models.TagForumPostIgnore]

type ConfiguratorSubForumWatch = ConfiguratorTags[models.TagForumSubforumTrack]

var NewConfiguratorSubForumWatch = NewConfiguratorTags[models.TagForumSubforumTrack]

type ConfiguratorSubForumIgnore = ConfiguratorTags[models.TagForumSubforumIgnore]

var NewConfiguratorSubForumIgnore = NewConfiguratorTags[models.TagForumSubforumIgnore]

func (c ConfiguratorTags[T]) TagsAdd(channelID types.DiscordChannelID, tags ...types.Tag) error {
objs := []T{}
for _, tag := range tags {
Expand Down
39 changes: 34 additions & 5 deletions app/consoler/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,51 @@ func CreateConsoler(
cmdgroup.Command("forum"),
cmdgroup.ShortDesc("forum commands"),
)
forumThreadGroup := forumGroup.GetChild(
forumGroup.CurrentCmd,
cmdgroup.Command("thread"),
cmdgroup.ShortDesc("track by thread name"),
)
NewTagCommands(
forumGroup.GetChild(
forumGroup.CurrentCmd,
forumThreadGroup.GetChild(
forumThreadGroup.CurrentCmd,
cmdgroup.Command("watch"),
cmdgroup.ShortDesc("Watch commands"),
),
configurator.NewConfiguratorForumWatch(configur),
configurator.NewConfiguratorChannel(configur),
)
NewTagCommands(
forumGroup.GetChild(
forumGroup.CurrentCmd,
forumThreadGroup.GetChild(
forumThreadGroup.CurrentCmd,
cmdgroup.Command("ignore"),
cmdgroup.ShortDesc("Ignore commands"),
),
configurator.NewConfiguratorForumWatch(configur),
configurator.NewConfiguratorChannel(configur),
)

forumSubforumGroup := forumGroup.GetChild(
forumGroup.CurrentCmd,
cmdgroup.Command("subforum"),
cmdgroup.ShortDesc("track by subforum name"),
)
NewTagCommands(
forumSubforumGroup.GetChild(
forumSubforumGroup.CurrentCmd,
cmdgroup.Command("watch"),
cmdgroup.ShortDesc("Watch commands"),
),
configurator.NewConfiguratorForumIgnore(configur),
configurator.NewConfiguratorSubForumWatch(configur),
configurator.NewConfiguratorChannel(configur),
)
NewTagCommands(
forumSubforumGroup.GetChild(
forumSubforumGroup.CurrentCmd,
cmdgroup.Command("ignore"),
cmdgroup.ShortDesc("Ignore commands"),
),
configurator.NewConfiguratorSubForumIgnore(configur),
configurator.NewConfiguratorChannel(configur),
)

Expand Down
13 changes: 13 additions & 0 deletions app/forumer/forum_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,25 @@ func (p *PostRequester) GetDetailedPost(thread *forum_types.LatestThread) (*foru
post_content = strings.ReplaceAll(post_content, "\n\n", "\n")
}

navigation := doc.Find("div", "class", "navigation")
navigation_childs := navigation.Children()

subforums := []forum_types.Subforum{}
for _, may_be_subforum := range navigation_childs {
if may_be_subforum.NodeValue != "a" {
continue
}

subforums = append(subforums, forum_types.Subforum(may_be_subforum.Text()))
}

return &forum_types.Post{
LatestThread: thread,
PostID: post_id,
PostContent: forum_types.PostContent(post_content),
PostPermamentLink: forum_types.PostPermamentLink(query.ResponseFullUrl),
ThreadFullName: forum_types.ThreadFullName(thread_name),
PostAuthorAvatarLink: forum_types.Url(author_avatar_url),
Subforums: subforums[1:], // first subforum is always root
}, nil
}
1 change: 1 addition & 0 deletions app/forumer/forum_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ func TestGetDetailedPost(t *testing.T) {
_ = detailed_post
fmt.Println("err=", err)
assert.Nil(t, err, "expected error to be nil")
assert.Greater(t, len(detailed_post.Subforums), 0)
}
2 changes: 2 additions & 0 deletions app/forumer/forum_types/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ type PostID string
type PostContent string
type PostPermamentLink Url
type ThreadFullName string
type Subforum string
type Post struct {
*LatestThread
PostID PostID
PostContent PostContent
PostPermamentLink PostPermamentLink
ThreadFullName ThreadFullName
PostAuthorAvatarLink Url
Subforums []Subforum
}
50 changes: 39 additions & 11 deletions app/forumer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,57 @@ func (v *Forumer) GetPost(thread *forum_types.LatestThread, new_post_callback fu
}

func (v *Forumer) isPostMatchTags(channel types.DiscordChannelID, new_post *forum_types.Post) (bool, []string) {
do_we_show_this_post := false
var matched_tags []string

watch_tags, err := v.Forum.Watch.TagsList(channel)
if logus.CheckDebug(err, "failed to get watch tags") {
return false, matched_tags
}
thread_watch_tags, err := v.Forum.Thread.Watch.TagsList(channel)
logus.CheckDebug(err, "failed to get watch tags")

ignore_tags, err := v.Forum.Ignore.TagsList(channel)
thread_ignore_tags, err := v.Forum.Thread.Ignore.TagsList(channel)
logus.CheckDebug(err, "failed to get ignore tags")

do_we_show_this_post := false
for _, watch_tag := range watch_tags {
subforum_watch_tags, err := v.Forum.Subforum.Watch.TagsList(channel)
logus.CheckDebug(err, "failed to get watch tags")

subforum_ignore_tags, err := v.Forum.Subforum.Ignore.TagsList(channel)
logus.CheckDebug(err, "failed to get ignore tags")

for _, watch_tag := range thread_watch_tags {
if strings.Contains(string(new_post.ThreadFullName), string(watch_tag)) ||
strings.Contains(strings.ToLower(string(new_post.ThreadFullName)), strings.ToLower(string(watch_tag))) {
do_we_show_this_post = true
matched_tags = append(matched_tags, string(fmt.Sprintf(`"%s"`, watch_tag)))
}
}

for _, ignore_tag := range ignore_tags {
for _, watch_tag := range subforum_watch_tags {
for _, subforum := range new_post.Subforums {
if strings.Contains(string(subforum), string(watch_tag)) ||
strings.Contains(strings.ToLower(string(new_post.ThreadFullName)), strings.ToLower(string(watch_tag))) {
do_we_show_this_post = true
matched_tags = append(matched_tags, string(fmt.Sprintf(`"%s"`, watch_tag)))
}
}

}

for _, ignore_tag := range thread_ignore_tags {
if strings.Contains(string(new_post.ThreadFullName), string(ignore_tag)) {
do_we_show_this_post = false
break
}
}

if !do_we_show_this_post {
return false, matched_tags
for _, ignore_tag := range subforum_ignore_tags {
for _, subforum := range new_post.Subforums {
if strings.Contains(string(subforum), string(ignore_tag)) {
do_we_show_this_post = false
break
}
}
}
return true, matched_tags

return do_we_show_this_post, matched_tags
}

func CreateDeDuplicator(new_post *forum_types.Post, msgs []*discorder.DiscordMessage) *discorder.Deduplicator {
Expand Down Expand Up @@ -183,6 +204,13 @@ func (v *Forumer) TrySendMsg(channel types.DiscordChannelID, new_post *forum_typ
Inline: true,
})

subforums := utils.CompL(new_post.Subforums, func(x forum_types.Subforum) string { return string(x) })
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "Subforums",
Value: strings.Join(subforums, " / "),
Inline: false,
})

var post_content string = string(new_post.PostContent)
if len(post_content) >= 600 {
post_content = post_content[:600]
Expand Down
23 changes: 23 additions & 0 deletions app/forumer/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,28 @@ func TestForumerSending(t *testing.T) {

forum.update()
})
}

func TestSubForumSending(t *testing.T) {

mocked_post_requester := FixtureDetailedRequester()
configurator.FixtureMigrator(func(dbpath types.Dbpath) {
dev_env_channel := types.DiscordChannelID("1079189823098724433")
cg := configurator.NewConfiguratorSubForumWatch(configurator.NewConfigurator(dbpath))
cg.TagsAdd(dev_env_channel, []types.Tag{"Communication Channel"}...)

cg_channel := configurator.NewConfiguratorChannel(configurator.NewConfigurator(dbpath))

if FixtureDevEnv() {
cg_channel.Add(dev_env_channel)
}

forum := NewForumer(
dbpath,
WithThreadsRequester(newMockedThreadsQuery()),
WithDetailedPostRequest(NewDetailedPostRequester(WithMockedRequester(mocked_post_requester))),
)

forum.update()
})
}

0 comments on commit 7c2e267

Please sign in to comment.