Skip to content

Commit 7c2e267

Browse files
committed
feat: ability track by subforum
1 parent feee6dc commit 7c2e267

File tree

10 files changed

+152
-20
lines changed

10 files changed

+152
-20
lines changed

app/configurator/configurators.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ type Alerts struct {
1919
PingMessage CfgAlertPingMessage
2020
}
2121

22-
type Forum struct {
22+
type ForumThread struct {
2323
Watch ConfiguratorForumWatch
2424
Ignore ConfiguratorForumIgnore
2525
}
26+
27+
type ForumSubforum struct {
28+
Watch ConfiguratorSubForumWatch
29+
Ignore ConfiguratorSubForumIgnore
30+
}
31+
32+
type Forum struct {
33+
Thread ForumThread
34+
Subforum ForumSubforum
35+
}
2636
type Configurators struct {
2737
Bases ConfiguratorBase
2838
Players Players
@@ -57,8 +67,14 @@ func NewConfigugurators(dbpath types.Dbpath) *Configurators {
5767
PingMessage: NewCfgAlertPingMessage(configur),
5868
},
5969
Forum: Forum{
60-
Watch: NewConfiguratorForumWatch(configur),
61-
Ignore: NewConfiguratorForumIgnore(configur),
70+
Thread: ForumThread{
71+
Watch: NewConfiguratorForumWatch(configur),
72+
Ignore: NewConfiguratorForumIgnore(configur),
73+
},
74+
Subforum: ForumSubforum{
75+
Watch: NewConfiguratorSubForumWatch(configur),
76+
Ignore: NewConfiguratorSubForumIgnore(configur),
77+
},
6278
},
6379
}
6480
}

app/configurator/connection.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (cg *Configurator) AutoMigrateSchema() *Configurator {
4545
&models.TagRegion{},
4646
&models.TagForumPostTrack{},
4747
&models.TagForumPostIgnore{},
48+
&models.TagForumSubforumTrack{},
49+
&models.TagForumSubforumIgnore{},
4850
&models.TagPlayerEvent{},
4951
&models.AlertNeutralPlayersEqualOrGreater{},
5052
&models.AlertEnemiesEqualOrGreater{},

app/configurator/models/models.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ type TagForumPostIgnore struct {
4545
TagTemplate
4646
}
4747

48+
type TagForumSubforumTrack struct {
49+
TagTemplate
50+
}
51+
52+
type TagForumSubforumIgnore struct {
53+
TagTemplate
54+
}
55+
4856
// =========== Alerts ===============
4957

5058
type AlertTresholdShared struct {

app/configurator/tags.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ type taggable interface {
2222
models.TagPlayerEnemy |
2323
models.TagPlayerEvent |
2424
models.TagForumPostTrack |
25-
models.TagForumPostIgnore
25+
models.TagForumPostIgnore |
26+
models.TagForumSubforumTrack |
27+
models.TagForumSubforumIgnore
2628
GetTag() types.Tag
2729
}
2830

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

6870
var NewConfiguratorForumIgnore = NewConfiguratorTags[models.TagForumPostIgnore]
6971

72+
type ConfiguratorSubForumWatch = ConfiguratorTags[models.TagForumSubforumTrack]
73+
74+
var NewConfiguratorSubForumWatch = NewConfiguratorTags[models.TagForumSubforumTrack]
75+
76+
type ConfiguratorSubForumIgnore = ConfiguratorTags[models.TagForumSubforumIgnore]
77+
78+
var NewConfiguratorSubForumIgnore = NewConfiguratorTags[models.TagForumSubforumIgnore]
79+
7080
func (c ConfiguratorTags[T]) TagsAdd(channelID types.DiscordChannelID, tags ...types.Tag) error {
7181
objs := []T{}
7282
for _, tag := range tags {

app/consoler/commands/root.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,51 @@ func CreateConsoler(
6060
cmdgroup.Command("forum"),
6161
cmdgroup.ShortDesc("forum commands"),
6262
)
63+
forumThreadGroup := forumGroup.GetChild(
64+
forumGroup.CurrentCmd,
65+
cmdgroup.Command("thread"),
66+
cmdgroup.ShortDesc("track by thread name"),
67+
)
6368
NewTagCommands(
64-
forumGroup.GetChild(
65-
forumGroup.CurrentCmd,
69+
forumThreadGroup.GetChild(
70+
forumThreadGroup.CurrentCmd,
6671
cmdgroup.Command("watch"),
6772
cmdgroup.ShortDesc("Watch commands"),
6873
),
6974
configurator.NewConfiguratorForumWatch(configur),
7075
configurator.NewConfiguratorChannel(configur),
7176
)
7277
NewTagCommands(
73-
forumGroup.GetChild(
74-
forumGroup.CurrentCmd,
78+
forumThreadGroup.GetChild(
79+
forumThreadGroup.CurrentCmd,
7580
cmdgroup.Command("ignore"),
81+
cmdgroup.ShortDesc("Ignore commands"),
82+
),
83+
configurator.NewConfiguratorForumWatch(configur),
84+
configurator.NewConfiguratorChannel(configur),
85+
)
86+
87+
forumSubforumGroup := forumGroup.GetChild(
88+
forumGroup.CurrentCmd,
89+
cmdgroup.Command("subforum"),
90+
cmdgroup.ShortDesc("track by subforum name"),
91+
)
92+
NewTagCommands(
93+
forumSubforumGroup.GetChild(
94+
forumSubforumGroup.CurrentCmd,
95+
cmdgroup.Command("watch"),
7696
cmdgroup.ShortDesc("Watch commands"),
7797
),
78-
configurator.NewConfiguratorForumIgnore(configur),
98+
configurator.NewConfiguratorSubForumWatch(configur),
99+
configurator.NewConfiguratorChannel(configur),
100+
)
101+
NewTagCommands(
102+
forumSubforumGroup.GetChild(
103+
forumSubforumGroup.CurrentCmd,
104+
cmdgroup.Command("ignore"),
105+
cmdgroup.ShortDesc("Ignore commands"),
106+
),
107+
configurator.NewConfiguratorSubForumIgnore(configur),
79108
configurator.NewConfiguratorChannel(configur),
80109
)
81110

app/forumer/forum_post.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,25 @@ func (p *PostRequester) GetDetailedPost(thread *forum_types.LatestThread) (*foru
8989
post_content = strings.ReplaceAll(post_content, "\n\n", "\n")
9090
}
9191

92+
navigation := doc.Find("div", "class", "navigation")
93+
navigation_childs := navigation.Children()
94+
95+
subforums := []forum_types.Subforum{}
96+
for _, may_be_subforum := range navigation_childs {
97+
if may_be_subforum.NodeValue != "a" {
98+
continue
99+
}
100+
101+
subforums = append(subforums, forum_types.Subforum(may_be_subforum.Text()))
102+
}
103+
92104
return &forum_types.Post{
93105
LatestThread: thread,
94106
PostID: post_id,
95107
PostContent: forum_types.PostContent(post_content),
96108
PostPermamentLink: forum_types.PostPermamentLink(query.ResponseFullUrl),
97109
ThreadFullName: forum_types.ThreadFullName(thread_name),
98110
PostAuthorAvatarLink: forum_types.Url(author_avatar_url),
111+
Subforums: subforums[1:], // first subforum is always root
99112
}, nil
100113
}

app/forumer/forum_post_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ func TestGetDetailedPost(t *testing.T) {
4848
_ = detailed_post
4949
fmt.Println("err=", err)
5050
assert.Nil(t, err, "expected error to be nil")
51+
assert.Greater(t, len(detailed_post.Subforums), 0)
5152
}

app/forumer/forum_types/post.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ type PostID string
44
type PostContent string
55
type PostPermamentLink Url
66
type ThreadFullName string
7+
type Subforum string
78
type Post struct {
89
*LatestThread
910
PostID PostID
1011
PostContent PostContent
1112
PostPermamentLink PostPermamentLink
1213
ThreadFullName ThreadFullName
1314
PostAuthorAvatarLink Url
15+
Subforums []Subforum
1416
}

app/forumer/run.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,57 @@ func (v *Forumer) GetPost(thread *forum_types.LatestThread, new_post_callback fu
101101
}
102102

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

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

111-
ignore_tags, err := v.Forum.Ignore.TagsList(channel)
110+
thread_ignore_tags, err := v.Forum.Thread.Ignore.TagsList(channel)
112111
logus.CheckDebug(err, "failed to get ignore tags")
113112

114-
do_we_show_this_post := false
115-
for _, watch_tag := range watch_tags {
113+
subforum_watch_tags, err := v.Forum.Subforum.Watch.TagsList(channel)
114+
logus.CheckDebug(err, "failed to get watch tags")
115+
116+
subforum_ignore_tags, err := v.Forum.Subforum.Ignore.TagsList(channel)
117+
logus.CheckDebug(err, "failed to get ignore tags")
118+
119+
for _, watch_tag := range thread_watch_tags {
116120
if strings.Contains(string(new_post.ThreadFullName), string(watch_tag)) ||
117121
strings.Contains(strings.ToLower(string(new_post.ThreadFullName)), strings.ToLower(string(watch_tag))) {
118122
do_we_show_this_post = true
119123
matched_tags = append(matched_tags, string(fmt.Sprintf(`"%s"`, watch_tag)))
120124
}
121125
}
122126

123-
for _, ignore_tag := range ignore_tags {
127+
for _, watch_tag := range subforum_watch_tags {
128+
for _, subforum := range new_post.Subforums {
129+
if strings.Contains(string(subforum), string(watch_tag)) ||
130+
strings.Contains(strings.ToLower(string(new_post.ThreadFullName)), strings.ToLower(string(watch_tag))) {
131+
do_we_show_this_post = true
132+
matched_tags = append(matched_tags, string(fmt.Sprintf(`"%s"`, watch_tag)))
133+
}
134+
}
135+
136+
}
137+
138+
for _, ignore_tag := range thread_ignore_tags {
124139
if strings.Contains(string(new_post.ThreadFullName), string(ignore_tag)) {
125140
do_we_show_this_post = false
126141
break
127142
}
128143
}
129144

130-
if !do_we_show_this_post {
131-
return false, matched_tags
145+
for _, ignore_tag := range subforum_ignore_tags {
146+
for _, subforum := range new_post.Subforums {
147+
if strings.Contains(string(subforum), string(ignore_tag)) {
148+
do_we_show_this_post = false
149+
break
150+
}
151+
}
132152
}
133-
return true, matched_tags
153+
154+
return do_we_show_this_post, matched_tags
134155
}
135156

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

207+
subforums := utils.CompL(new_post.Subforums, func(x forum_types.Subforum) string { return string(x) })
208+
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
209+
Name: "Subforums",
210+
Value: strings.Join(subforums, " / "),
211+
Inline: false,
212+
})
213+
186214
var post_content string = string(new_post.PostContent)
187215
if len(post_content) >= 600 {
188216
post_content = post_content[:600]

app/forumer/run_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,28 @@ func TestForumerSending(t *testing.T) {
5252

5353
forum.update()
5454
})
55+
}
56+
57+
func TestSubForumSending(t *testing.T) {
58+
59+
mocked_post_requester := FixtureDetailedRequester()
60+
configurator.FixtureMigrator(func(dbpath types.Dbpath) {
61+
dev_env_channel := types.DiscordChannelID("1079189823098724433")
62+
cg := configurator.NewConfiguratorSubForumWatch(configurator.NewConfigurator(dbpath))
63+
cg.TagsAdd(dev_env_channel, []types.Tag{"Communication Channel"}...)
64+
65+
cg_channel := configurator.NewConfiguratorChannel(configurator.NewConfigurator(dbpath))
66+
67+
if FixtureDevEnv() {
68+
cg_channel.Add(dev_env_channel)
69+
}
70+
71+
forum := NewForumer(
72+
dbpath,
73+
WithThreadsRequester(newMockedThreadsQuery()),
74+
WithDetailedPostRequest(NewDetailedPostRequester(WithMockedRequester(mocked_post_requester))),
75+
)
5576

77+
forum.update()
78+
})
5679
}

0 commit comments

Comments
 (0)