|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "html" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + neturl "net/url" |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "report-analyze-pipeline/database" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + htmlTagPattern = regexp.MustCompile(`(?s)<[^>]*>`) |
| 20 | + whitespacePattern = regexp.MustCompile(`\s+`) |
| 21 | + lineBreakTagPattern = regexp.MustCompile(`(?i)<br\s*/?>|</p>|</div>|</blockquote>`) |
| 22 | +) |
| 23 | + |
| 24 | +type xOEmbedResponse struct { |
| 25 | + AuthorName string `json:"author_name"` |
| 26 | + AuthorURL string `json:"author_url"` |
| 27 | + HTML string `json:"html"` |
| 28 | +} |
| 29 | + |
| 30 | +func (s *Service) enrichAnalysisInput(report *database.Report, baseInput string) string { |
| 31 | + shareContext, err := fetchShareContext(report) |
| 32 | + if err != nil { |
| 33 | + log.Printf("Report %d: failed to fetch share context for %q: %v", report.Seq, report.SourceURL, err) |
| 34 | + } |
| 35 | + |
| 36 | + parts := make([]string, 0, 3) |
| 37 | + if trimmed := strings.TrimSpace(baseInput); trimmed != "" { |
| 38 | + parts = append(parts, trimmed) |
| 39 | + } |
| 40 | + if shareContext != "" { |
| 41 | + parts = append(parts, shareContext) |
| 42 | + } |
| 43 | + if shouldWarnAboutThinEvidence(report, shareContext) { |
| 44 | + parts = append(parts, "Evidence quality note: there is no attached image and no retrievable remote post/page content. Do not invent specific bug details, metrics, IDs, or impacted products. If evidence is insufficient, say so clearly and keep the analysis generic.") |
| 45 | + } |
| 46 | + return strings.Join(parts, "\n\n") |
| 47 | +} |
| 48 | + |
| 49 | +func shouldWarnAboutThinEvidence(report *database.Report, shareContext string) bool { |
| 50 | + if len(strings.TrimSpace(shareContext)) > 0 { |
| 51 | + return false |
| 52 | + } |
| 53 | + if strings.TrimSpace(report.SharedText) != "" { |
| 54 | + return false |
| 55 | + } |
| 56 | + description := strings.TrimSpace(report.Description) |
| 57 | + if description == "" { |
| 58 | + return true |
| 59 | + } |
| 60 | + return strings.EqualFold(description, "Human report submission") |
| 61 | +} |
| 62 | + |
| 63 | +func fetchShareContext(report *database.Report) (string, error) { |
| 64 | + sourceURL := strings.TrimSpace(report.SourceURL) |
| 65 | + if sourceURL == "" { |
| 66 | + return "", nil |
| 67 | + } |
| 68 | + |
| 69 | + parsed, err := neturl.Parse(sourceURL) |
| 70 | + if err != nil { |
| 71 | + return "", fmt.Errorf("invalid source url: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + host := strings.ToLower(strings.TrimPrefix(parsed.Hostname(), "www.")) |
| 75 | + switch host { |
| 76 | + case "x.com", "twitter.com": |
| 77 | + return fetchXOEmbedContext(sourceURL) |
| 78 | + default: |
| 79 | + return "", nil |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func fetchXOEmbedContext(sourceURL string) (string, error) { |
| 84 | + query := neturl.Values{} |
| 85 | + query.Set("url", sourceURL) |
| 86 | + query.Set("omit_script", "true") |
| 87 | + query.Set("dnt", "true") |
| 88 | + |
| 89 | + endpoint := "https://publish.twitter.com/oembed?" + query.Encode() |
| 90 | + client := &http.Client{Timeout: 5 * time.Second} |
| 91 | + req, err := http.NewRequest(http.MethodGet, endpoint, nil) |
| 92 | + if err != nil { |
| 93 | + return "", err |
| 94 | + } |
| 95 | + req.Header.Set("User-Agent", "CleanAppAnalyzer/1.0") |
| 96 | + |
| 97 | + resp, err := client.Do(req) |
| 98 | + if err != nil { |
| 99 | + return "", err |
| 100 | + } |
| 101 | + defer resp.Body.Close() |
| 102 | + |
| 103 | + if resp.StatusCode != http.StatusOK { |
| 104 | + body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) |
| 105 | + return "", fmt.Errorf("oembed status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) |
| 106 | + } |
| 107 | + |
| 108 | + var payload xOEmbedResponse |
| 109 | + if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + |
| 113 | + postText := extractTextFromOEmbedHTML(payload.HTML) |
| 114 | + if postText == "" { |
| 115 | + return "", nil |
| 116 | + } |
| 117 | + |
| 118 | + parts := []string{"Fetched X post context:"} |
| 119 | + if author := strings.TrimSpace(payload.AuthorName); author != "" { |
| 120 | + parts = append(parts, "Author: "+author) |
| 121 | + } |
| 122 | + if authorURL := strings.TrimSpace(payload.AuthorURL); authorURL != "" { |
| 123 | + parts = append(parts, "Author profile: "+authorURL) |
| 124 | + } |
| 125 | + parts = append(parts, "Post text: "+postText) |
| 126 | + return strings.Join(parts, "\n"), nil |
| 127 | +} |
| 128 | + |
| 129 | +func extractTextFromOEmbedHTML(fragment string) string { |
| 130 | + fragment = strings.TrimSpace(fragment) |
| 131 | + if fragment == "" { |
| 132 | + return "" |
| 133 | + } |
| 134 | + |
| 135 | + normalized := lineBreakTagPattern.ReplaceAllString(fragment, "\n") |
| 136 | + normalized = htmlTagPattern.ReplaceAllString(normalized, " ") |
| 137 | + normalized = html.UnescapeString(normalized) |
| 138 | + |
| 139 | + lines := strings.Split(normalized, "\n") |
| 140 | + cleaned := make([]string, 0, len(lines)) |
| 141 | + for _, line := range lines { |
| 142 | + line = whitespacePattern.ReplaceAllString(strings.TrimSpace(line), " ") |
| 143 | + if line == "" { |
| 144 | + continue |
| 145 | + } |
| 146 | + if strings.HasPrefix(line, "— ") || strings.HasPrefix(line, "— ") { |
| 147 | + continue |
| 148 | + } |
| 149 | + cleaned = append(cleaned, line) |
| 150 | + } |
| 151 | + |
| 152 | + return strings.Join(cleaned, "\n") |
| 153 | +} |
0 commit comments