This commit is contained in:
@@ -52,113 +52,6 @@ func convertToSimplifiedAddonWithoutFiles(addon models.Addon) models.SimplifiedA
|
||||
|
||||
func GetDuplicates(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
// First check if addon exists (lightweight query)
|
||||
var exists bool
|
||||
if err := initializers.DB.Model(&models.Addon{}).Select("1").Where("id = ?", id).Limit(1).Find(&exists).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Addon not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Get only the hashes from source addon files (don't load full addon yet)
|
||||
var sourceHashes []string
|
||||
if err := initializers.DB.Model(&models.AddonFile{}).
|
||||
Where("addon_id = ? AND hash != ''", id).
|
||||
Pluck("hash", &sourceHashes).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch source addon files"})
|
||||
return
|
||||
}
|
||||
|
||||
// Now load the full source addon for the response
|
||||
var sourceAddon models.Addon
|
||||
if err := initializers.DB.Preload("AddonFiles").First(&sourceAddon, "id = ?", id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Addon not found"})
|
||||
return
|
||||
}
|
||||
|
||||
simplifiedSourceAddon := convertToSimplifiedAddon(sourceAddon)
|
||||
|
||||
if len(sourceHashes) == 0 {
|
||||
c.JSON(http.StatusOK, models.DuplicatesResponse{
|
||||
SourceAddon: simplifiedSourceAddon,
|
||||
DuplicateAddons: make(map[string]models.AddonWithDuplicates),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Single query with JOIN to get all data
|
||||
type DuplicateRow struct {
|
||||
Hash string
|
||||
Path string
|
||||
Version string
|
||||
AddonID string
|
||||
AddonName string
|
||||
AddonType string
|
||||
Summary string
|
||||
Preview string
|
||||
Author string
|
||||
SubscriberCount int
|
||||
CurrentVersionNumber string
|
||||
}
|
||||
|
||||
var duplicates []DuplicateRow
|
||||
err := initializers.DB.Table("addon_files af").
|
||||
Select(`af.hash, af.path, af.version, af.addon_id,
|
||||
a.name as addon_name, a.type as addon_type, a.summary,
|
||||
a.preview, a.author, a.subscriber_count, a.current_version_number`).
|
||||
Joins("INNER JOIN addons a ON a.id = af.addon_id").
|
||||
Joins("LEFT JOIN whitelisted_hashes wh ON wh.hash = af.hash").
|
||||
Where(`af.hash IN ?
|
||||
AND af.addon_id != ?
|
||||
AND af.deleted_at IS NULL
|
||||
AND wh.hash IS NULL`,
|
||||
sourceHashes, id).
|
||||
Scan(&duplicates).Error
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch duplicates"})
|
||||
return
|
||||
}
|
||||
|
||||
// Build response from single result set
|
||||
response := models.DuplicatesResponse{
|
||||
SourceAddon: simplifiedSourceAddon,
|
||||
DuplicateAddons: make(map[string]models.AddonWithDuplicates),
|
||||
}
|
||||
|
||||
for _, dup := range duplicates {
|
||||
if _, exists := response.DuplicateAddons[dup.AddonID]; !exists {
|
||||
response.DuplicateAddons[dup.AddonID] = models.AddonWithDuplicates{
|
||||
Addon: models.SimplifiedAddon{
|
||||
ID: dup.AddonID,
|
||||
Name: dup.AddonName,
|
||||
Type: dup.AddonType,
|
||||
Summary: dup.Summary,
|
||||
Preview: dup.Preview,
|
||||
Author: dup.Author,
|
||||
SubscriberCount: dup.SubscriberCount,
|
||||
CurrentVersionNumber: dup.CurrentVersionNumber,
|
||||
AddonFiles: []models.SimplifiedAddonFile{},
|
||||
},
|
||||
Duplicates: []models.DuplicateFileInfo{},
|
||||
}
|
||||
}
|
||||
|
||||
entry := response.DuplicateAddons[dup.AddonID]
|
||||
entry.Duplicates = append(entry.Duplicates, models.DuplicateFileInfo{
|
||||
Path: dup.Path,
|
||||
Hash: dup.Hash,
|
||||
Version: dup.Version,
|
||||
AddonID: dup.AddonID,
|
||||
})
|
||||
response.DuplicateAddons[dup.AddonID] = entry
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
/*func GetDuplicates(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
fmt.Println("Fetching addon with ID:", id)
|
||||
|
||||
// Fetch the source addon with its files
|
||||
@@ -236,6 +129,19 @@ func GetDuplicates(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set Recheck flag for source addon and all duplicate addons
|
||||
if err := initializers.DB.Model(&models.Addon{}).Where("id = ?", id).Update("recheck", true).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update source addon"})
|
||||
return
|
||||
}
|
||||
|
||||
for _, addon := range duplicateAddons {
|
||||
if err := initializers.DB.Model(&models.Addon{}).Where("id = ?", addon.ID).Update("recheck", true).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update duplicate addon"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Build the response
|
||||
response := models.DuplicatesResponse{
|
||||
SourceAddon: simplifiedSourceAddon,
|
||||
@@ -269,7 +175,7 @@ func GetDuplicates(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}*/
|
||||
}
|
||||
|
||||
func GetPossibleAddons(c *gin.Context) {
|
||||
query := c.Query("q")
|
||||
|
||||
Reference in New Issue
Block a user