Compare commits
1 Commits
master
...
stupid-bro
Author | SHA1 | Date |
---|---|---|
Ben Visness | b0b863d134 |
|
@ -9,6 +9,15 @@
|
||||||
respond "{\"m.server\": \"matrix.handmade.network:443\"}"
|
respond "{\"m.server\": \"matrix.handmade.network:443\"}"
|
||||||
header Content-Type application/json
|
header Content-Type application/json
|
||||||
}
|
}
|
||||||
|
# Uncomment this ONLY FOR BETA!
|
||||||
|
# It disables all search engine indexing!
|
||||||
|
# If you do this on the real site you will destroy all the site's SEO!
|
||||||
|
# ONLY UNCOMMENT THIS IN BETA!
|
||||||
|
# handle /robots.txt {
|
||||||
|
# respond "User-agent: *
|
||||||
|
# Disallow: /
|
||||||
|
# "
|
||||||
|
# }
|
||||||
handle /public/* {
|
handle /public/* {
|
||||||
file_server {
|
file_server {
|
||||||
root /home/hmn/hmn
|
root /home/hmn/hmn
|
||||||
|
|
|
@ -76,7 +76,6 @@ type DiscordConfig struct {
|
||||||
MemberRoleID string
|
MemberRoleID string
|
||||||
ShowcaseChannelID string
|
ShowcaseChannelID string
|
||||||
LibraryChannelID string
|
LibraryChannelID string
|
||||||
JamShowcaseChannelID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EpisodeGuide struct {
|
type EpisodeGuide struct {
|
||||||
|
|
|
@ -598,7 +598,6 @@ func (bot *botInstance) messageCreateOrUpdate(ctx context.Context, msg *Message)
|
||||||
logging.ExtractLogger(ctx).Error().Err(err).Msg("failed to process showcase message")
|
logging.ExtractLogger(ctx).Error().Err(err).Msg("failed to process showcase message")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.ChannelID == config.Config.Discord.LibraryChannelID {
|
if msg.ChannelID == config.Config.Discord.LibraryChannelID {
|
||||||
|
@ -607,7 +606,6 @@ func (bot *botInstance) messageCreateOrUpdate(ctx context.Context, msg *Message)
|
||||||
logging.ExtractLogger(ctx).Error().Err(err).Msg("failed to process library message")
|
logging.ExtractLogger(ctx).Error().Err(err).Msg("failed to process library message")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpdateSnippetTagsIfAny(ctx, bot.dbConn, msg)
|
err := UpdateSnippetTagsIfAny(ctx, bot.dbConn, msg)
|
||||||
|
|
|
@ -193,6 +193,7 @@ func handleHistoryMessage(ctx context.Context, dbConn *pgxpool.Pool, msg *Messag
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
newMsg, err := SaveMessageAndContents(ctx, tx, msg)
|
newMsg, err := SaveMessageAndContents(ctx, tx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -61,17 +61,15 @@ func (bot *botInstance) processShowcaseMsg(ctx context.Context, msg *Message) er
|
||||||
// ...and maybe make a snippet too, if the user wants us to
|
// ...and maybe make a snippet too, if the user wants us to
|
||||||
duser, err := FetchDiscordUser(ctx, tx, newMsg.UserID)
|
duser, err := FetchDiscordUser(ctx, tx, newMsg.UserID)
|
||||||
if err == nil && duser.HMNUser.DiscordSaveShowcase {
|
if err == nil && duser.HMNUser.DiscordSaveShowcase {
|
||||||
err = CreateMessageSnippets(ctx, tx, newMsg.UserID, msg.ID)
|
err = CreateMessageSnippets(ctx, tx, msg.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oops.New(err, "failed to create snippet in gateway")
|
return oops.New(err, "failed to create snippet in gateway")
|
||||||
}
|
}
|
||||||
} else {
|
} else if err == db.NotFound {
|
||||||
if err == db.NotFound {
|
|
||||||
// this is fine, just don't create a snippet
|
// this is fine, just don't create a snippet
|
||||||
} else {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = tx.Commit(ctx)
|
err = tx.Commit(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -148,11 +146,9 @@ func SaveMessage(
|
||||||
|
|
||||||
guildID := msg.GuildID
|
guildID := msg.GuildID
|
||||||
if guildID == nil {
|
if guildID == nil {
|
||||||
/*
|
// This is weird, but it can happen when we fetch messages from
|
||||||
This is weird, but it can happen when we fetch messages from
|
// history instead of receiving it from the gateway. In this case
|
||||||
history instead of receiving it from the gateway. In this case
|
// we just assume it's from the HMN server.
|
||||||
we just assume it's from the HMN server.
|
|
||||||
*/
|
|
||||||
guildID = &config.Config.Discord.GuildID
|
guildID = &config.Config.Discord.GuildID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,7 +589,13 @@ func CreateMessageSnippets(ctx context.Context, dbConn db.ConnOrTx, msgIDs ...st
|
||||||
`,
|
`,
|
||||||
msgID,
|
msgID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err == db.NotFound {
|
||||||
|
logging.ExtractLogger(ctx).Warn().
|
||||||
|
Str("messageID", msgID).
|
||||||
|
Stack().
|
||||||
|
Msg("No record was found for this Discord message at all. Is it actually a message ID?")
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
return oops.New(err, "failed to check for existing snippet for message %s", msgID)
|
return oops.New(err, "failed to check for existing snippet for message %s", msgID)
|
||||||
}
|
}
|
||||||
existing := iexisting.(*existingSnippetResult)
|
existing := iexisting.(*existingSnippetResult)
|
||||||
|
@ -689,6 +691,8 @@ If no Discord user is linked, or no snippet exists, or whatever, this will do
|
||||||
nothing and return no error.
|
nothing and return no error.
|
||||||
*/
|
*/
|
||||||
func UpdateSnippetTagsIfAny(ctx context.Context, dbConn db.ConnOrTx, msg *Message) error {
|
func UpdateSnippetTagsIfAny(ctx context.Context, dbConn db.ConnOrTx, msg *Message) error {
|
||||||
|
logging.ExtractLogger(ctx).Debug().Str("msgID", msg.ID).Msg("updating snippets for message (if any)")
|
||||||
|
|
||||||
tx, err := dbConn.Begin(ctx)
|
tx, err := dbConn.Begin(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oops.New(err, "failed to start transaction")
|
return oops.New(err, "failed to start transaction")
|
||||||
|
@ -720,10 +724,50 @@ func UpdateSnippetTagsIfAny(ctx context.Context, dbConn db.ConnOrTx, msg *Messag
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return oops.New(err, "failed to look up user projects")
|
return oops.New(err, "failed to look up user projects")
|
||||||
}
|
}
|
||||||
projectIDs := make([]int, len(projects))
|
userTagIDs := make([]int, 0, len(projects))
|
||||||
for i, p := range projects {
|
for _, p := range projects {
|
||||||
projectIDs[i] = p.Project.ID
|
if p.Project.TagID != nil {
|
||||||
|
userTagIDs = append(userTagIDs, *p.Project.TagID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to associate tags in the message with project tags in HMN.
|
||||||
|
// Match only tags for projects in which the current user is a collaborator.
|
||||||
|
messageTags := getDiscordTags(s.Snippet.Description)
|
||||||
|
type tagsRow struct {
|
||||||
|
Tag models.Tag `db:"tags"`
|
||||||
|
}
|
||||||
|
itUserTags, err := db.Query(ctx, tx, tagsRow{},
|
||||||
|
`
|
||||||
|
SELECT $columns
|
||||||
|
FROM
|
||||||
|
tags
|
||||||
|
WHERE
|
||||||
|
id = ANY ($1)
|
||||||
|
`,
|
||||||
|
userTagIDs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return oops.New(err, "failed to fetch tags for user projects")
|
||||||
|
}
|
||||||
|
iUserTags := itUserTags.ToSlice()
|
||||||
|
|
||||||
|
var tagIDs []int
|
||||||
|
for _, itag := range iUserTags {
|
||||||
|
tag := itag.(*tagsRow).Tag
|
||||||
|
for _, messageTag := range messageTags {
|
||||||
|
if tag.Text == messageTag {
|
||||||
|
userTagIDs = append(userTagIDs, tag.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logging.ExtractLogger(ctx).Info().
|
||||||
|
Interface("messageTags", messageTags).
|
||||||
|
Interface("tagIDs", tagIDs).
|
||||||
|
Int("snippetID", s.Snippet.ID).
|
||||||
|
Str("discordMsgID", msg.ID).
|
||||||
|
Msg("adding tags to snippet based on Discord message")
|
||||||
|
|
||||||
// Delete any existing project tags for this snippet. We don't want to
|
// Delete any existing project tags for this snippet. We don't want to
|
||||||
// delete other tags in case in the future we have manual tagging on the
|
// delete other tags in case in the future we have manual tagging on the
|
||||||
|
@ -743,39 +787,6 @@ func UpdateSnippetTagsIfAny(ctx context.Context, dbConn db.ConnOrTx, msg *Messag
|
||||||
return oops.New(err, "failed to delete existing snippet tags")
|
return oops.New(err, "failed to delete existing snippet tags")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to associate tags in the message with project tags in HMN.
|
|
||||||
// Match only tags for projects in which the current user is a collaborator.
|
|
||||||
messageTags := getDiscordTags(s.Snippet.Description)
|
|
||||||
type tagsRow struct {
|
|
||||||
Tag models.Tag `db:"tags"`
|
|
||||||
}
|
|
||||||
itUserTags, err := db.Query(ctx, tx, tagsRow{},
|
|
||||||
`
|
|
||||||
SELECT $columns
|
|
||||||
FROM
|
|
||||||
tags
|
|
||||||
JOIN handmade_project AS project ON project.tag = tags.id
|
|
||||||
JOIN handmade_user_projects AS user_project ON user_project.project_id = project.id
|
|
||||||
WHERE
|
|
||||||
project.id = ANY ($1)
|
|
||||||
`,
|
|
||||||
projectIDs,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return oops.New(err, "failed to fetch tags for user projects")
|
|
||||||
}
|
|
||||||
iUserTags := itUserTags.ToSlice()
|
|
||||||
|
|
||||||
var tagIDs []int
|
|
||||||
for _, itag := range iUserTags {
|
|
||||||
tag := itag.(*tagsRow).Tag
|
|
||||||
for _, messageTag := range messageTags {
|
|
||||||
if tag.Text == messageTag {
|
|
||||||
tagIDs = append(tagIDs, tag.ID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tagID := range tagIDs {
|
for _, tagID := range tagIDs {
|
||||||
_, err = tx.Exec(ctx,
|
_, err = tx.Exec(ctx,
|
||||||
`
|
`
|
||||||
|
|
Loading…
Reference in New Issue