|
| 1 | +package formatter |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/traPtitech/BOT_GPT/internal/bot" |
| 8 | + "github.com/traPtitech/go-traq" |
| 9 | +) |
| 10 | + |
| 11 | +const quoteRegexStr = `\bhttps://q\\.trap\\.jp/messages/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b` |
| 12 | + |
| 13 | +var quoteRegex = regexp.MustCompile(quoteRegexStr) |
| 14 | + |
| 15 | +var allowingPrefixes = []string{"/event", "/general", "/random", "/services", "/team/SysAd"} |
| 16 | + |
| 17 | +func isChannelAllowingQuotes(channelID string) (bool, error) { |
| 18 | + channelPath, err := bot.GetChannelPath(channelID) |
| 19 | + if err != nil { |
| 20 | + return false, err |
| 21 | + } |
| 22 | + |
| 23 | + for _, prefix := range allowingPrefixes { |
| 24 | + if strings.HasPrefix(channelPath, prefix) { |
| 25 | + return true, nil |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return false, nil |
| 30 | +} |
| 31 | + |
| 32 | +func isUserAllowingQuotes(userID string, messageUserID string) (bool, error) { |
| 33 | + if userID == messageUserID { |
| 34 | + return true, nil |
| 35 | + } |
| 36 | + |
| 37 | + messageUser, err := bot.GetUser(userID) |
| 38 | + if err != nil { |
| 39 | + return false, err |
| 40 | + } |
| 41 | + |
| 42 | + if messageUser.Bot { |
| 43 | + return true, nil |
| 44 | + } |
| 45 | + |
| 46 | + return false, nil |
| 47 | +} |
| 48 | + |
| 49 | +func getQuoteMarkdown(message *traq.Message) (string, error) { |
| 50 | + user, err := bot.GetUser(message.UserId) |
| 51 | + if err != nil { |
| 52 | + return "", err |
| 53 | + } |
| 54 | + |
| 55 | + return "> " + user.Name + ":\n> " + strings.ReplaceAll(message.Content, "\n", "\n> "), nil |
| 56 | +} |
| 57 | + |
| 58 | +const maxQuoteLength = 10000 |
| 59 | + |
| 60 | +func FormatQuotedMessage(userID string, content string) (string, error) { |
| 61 | + matches := quoteRegex.FindAllSubmatch([]byte(content), len(content)) |
| 62 | + messageIDs := make([]string, 0, len(matches)) |
| 63 | + for _, match := range matches { |
| 64 | + if len(match) < 2 { |
| 65 | + continue |
| 66 | + } |
| 67 | + messageID := string(match[1]) |
| 68 | + messageIDs = append(messageIDs, messageID) |
| 69 | + } |
| 70 | + |
| 71 | + var formattedContent strings.Builder |
| 72 | + formattedContent.WriteString(quoteRegex.ReplaceAllString(content, "")) |
| 73 | + |
| 74 | + for _, messageID := range messageIDs { |
| 75 | + message := bot.GetMessage(messageID) |
| 76 | + if message == nil { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + if len(message.Content) > maxQuoteLength { |
| 81 | + message.Content = message.Content[:maxQuoteLength] + "(以下略)" |
| 82 | + } |
| 83 | + |
| 84 | + allowed, err := isChannelAllowingQuotes(message.ChannelId) |
| 85 | + if err != nil { |
| 86 | + return "", err |
| 87 | + } |
| 88 | + if !allowed { |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + allowed, err = isUserAllowingQuotes(userID, message.UserId) |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + if !allowed { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + quote, err := getQuoteMarkdown(message) |
| 101 | + if err != nil { |
| 102 | + return "", err |
| 103 | + } |
| 104 | + formattedContent.WriteString("\n\n" + quote) |
| 105 | + } |
| 106 | + return formattedContent.String(), nil |
| 107 | +} |
0 commit comments