Compare commits

...

1 Commits

Author SHA1 Message Date
Ben Visness b0b863d134 A bunch of stupid junk that didn't work re: snippets
This was a pile of changes I made live on master that ultimately just
did not work at all. Don't program on 4 hours of sleep.

Handle a Not Found more gracefully in snippet code

I don't really know how this happens, but there's no reason to crash if
it happens.

Add example of disabling search engine indexing in beta

Fix incorrect varargs

I dunno change some snippet stuff

I'm so done with this

What if we didn't return for no reason

Actually parse the discord messages for tags

none of this is shippable. none of it

Final WIP
2021-12-14 20:52:52 -06:00
5 changed files with 74 additions and 56 deletions

View File

@ -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

View File

@ -72,11 +72,10 @@ type DiscordConfig struct {
OAuthClientID string OAuthClientID string
OAuthClientSecret string OAuthClientSecret string
GuildID string GuildID string
MemberRoleID string MemberRoleID string
ShowcaseChannelID string ShowcaseChannelID string
LibraryChannelID string LibraryChannelID string
JamShowcaseChannelID string
} }
type EpisodeGuide struct { type EpisodeGuide struct {

View File

@ -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)

View File

@ -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 {

View File

@ -61,16 +61,14 @@ 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 if err != nil {
} else { return err
return err
}
} }
err = tx.Commit(ctx) err = tx.Commit(ctx)
@ -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,11 +724,51 @@ 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
// website or whatever, and this would clear those out. // website or whatever, and this would clear those out.
@ -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,
` `