🐛 Fix flush reading anchor
This commit is contained in:
		| @@ -4,7 +4,6 @@ import ( | ||||
| 	"git.solsynth.dev/hypernet/messaging/pkg/internal/database" | ||||
| 	"git.solsynth.dev/hypernet/messaging/pkg/internal/models" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/samber/lo" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| @@ -22,19 +21,15 @@ func FlushReadingAnchor() { | ||||
| 	if len(readingAnchorQueue) == 0 { | ||||
| 		return | ||||
| 	} | ||||
| 	idSet := lo.Uniq(lo.Map(lo.Keys(readingAnchorQueue), func(item uint, _ int) uint { | ||||
| 		return item | ||||
| 	})) | ||||
| 	var pairs []map[string]any | ||||
| 	for k, v := range readingAnchorQueue { | ||||
| 		pairs = append(pairs, map[string]any{ | ||||
| 			"id":             k, | ||||
| 			"reading_anchor": gorm.Expr("GREATEST(reading_anchor, ?)", v), | ||||
| 		}) | ||||
| 	} | ||||
| 	if err := database.C.Model(&models.ChannelMember{}). | ||||
| 		Where("id IN ?", idSet). | ||||
| 		Updates(pairs).Error; err != nil { | ||||
| 		log.Error().Err(err).Msg("An error occurred when flushing reading anchor...") | ||||
| 		if err := database.C.Model(&models.ChannelMember{}). | ||||
| 			Where("id = ?", k). | ||||
| 			Updates(map[string]any{ | ||||
| 				"reading_anchor": gorm.Expr("GREATEST(reading_anchor, ?)", v), | ||||
| 			}).Error; err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when flushing reading anchor...") | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| 	clear(readingAnchorQueue) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user