Skip to content

Commit 30a25b4

Browse files
committed
fix: replace useless code by existing package
1 parent cb20391 commit 30a25b4

File tree

6 files changed

+38
-68
lines changed

6 files changed

+38
-68
lines changed
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package trustfactor
22

3-
import "strings"
3+
import (
4+
"strings"
5+
6+
"gno.land/p/nt/ownable"
7+
)
48

59
// =============================================================================
610
// ADMIN FUNCTIONS (OWNER ONLY)
711
// =============================================================================
812

913
// RemoveUser removes a malicious user from the system (emergency function)
10-
func RemoveUser(target address) {
14+
func RemoveUser(cur realm, target address) {
1115
owner.AssertOwnedByCurrent()
1216

1317
trustScores.Remove(string(target))
@@ -29,17 +33,7 @@ func RemoveUser(target address) {
2933
invalidateCache()
3034
}
3135

32-
// TransferOwnership transfers contract ownership to a new address
33-
func TransferOwnership(newOwner address) error {
34-
return owner.TransferOwnership(newOwner)
35-
}
36-
37-
// DropOwnership removes the owner, effectively disabling all admin functions
38-
func DropOwnership() error {
39-
return owner.DropOwnershipByCurrent()
40-
}
41-
42-
// Owner returns the current owner address
43-
func Owner() address {
44-
return owner.Owner()
36+
// GetOwnable returns the ownable object for ownership management
37+
func GetOwnable() *ownable.Ownable {
38+
return owner
4539
}

packages/r/greg007/TrustFactor/cache.gno

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package trustfactor
22

3-
import "time"
3+
import (
4+
"strings"
5+
"time"
6+
)
47

58
// =============================================================================
69
// PERFORMANCE OPTIMIZATION - CACHING & SORTING
@@ -116,26 +119,7 @@ func FilterUsers(searchTerm string, limit int, offset int) ([]address, int) {
116119

117120
// containsSubstring checks if a string contains a substring (case-sensitive)
118121
func containsSubstring(str, substr string) bool {
119-
if len(substr) == 0 {
120-
return true
121-
}
122-
if len(substr) > len(str) {
123-
return false
124-
}
125-
126-
for i := 0; i <= len(str)-len(substr); i++ {
127-
match := true
128-
for j := 0; j < len(substr); j++ {
129-
if str[i+j] != substr[j] {
130-
match = false
131-
break
132-
}
133-
}
134-
if match {
135-
return true
136-
}
137-
}
138-
return false
122+
return strings.Index(str, substr) >= 0
139123
}
140124

141125
// GetPerformanceStats returns cache and performance statistics

packages/r/greg007/TrustFactor/comments.gno

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package trustfactor
22

33
import (
44
"time"
5-
65
"chain/runtime"
76

87
"gno.land/p/nt/avl"
@@ -64,7 +63,7 @@ func GetComments(target address) []Comment {
6463
tree := treeValue.(*avl.Tree)
6564

6665
comments := make([]Comment, 0)
67-
tree.ReverseIterate("", "", func(key string, value interface{}) bool {
66+
tree.ReverseIterate("", "", func(key string, value any) bool {
6867
if comment, ok := value.(Comment); ok {
6968
comments = append(comments, comment)
7069
}

packages/r/greg007/TrustFactor/getters.gno

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ func GetTrustDetails(addr address) TrustScore {
3737
// DATA ACCESS FUNCTIONS
3838
// =============================================================================
3939

40-
// GetAllUsers returns the complete trust scores map (used internally)
41-
func GetAllUsers() map[address]TrustScore {
42-
result := make(map[address]TrustScore)
43-
trustScores.Iterate("", "", func(key string, value interface{}) bool {
44-
result[address(key)] = *value.(*TrustScore)
45-
return false
46-
})
47-
return result
40+
// GetTotalUsers returns the total number of registered users
41+
func GetTotalUsers() int {
42+
return totalUsers
4843
}
4944

5045
// GetVoteHistory returns the voting history for a specific user

packages/r/greg007/TrustFactor/render.gno

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package trustfactor
22

33
import (
44
"strconv"
5-
"strings"
65

76
"gno.land/p/leon/svgbtn"
87
"gno.land/p/moul/md"
@@ -44,8 +43,9 @@ func renderMainPageWithFilter(filter string) string {
4443
}) + "\n"
4544

4645
var topUsers []address
47-
allUsers := GetAllUsers()
48-
for addr, trust := range allUsers {
46+
sortedUsers := GetSortedUsers()
47+
for _, addr := range sortedUsers {
48+
trust := GetTrustDetails(addr)
4949
if trust.TotalVotes >= 3 {
5050
topUsers = append(topUsers, addr)
5151
if len(topUsers) >= 5 {
@@ -238,19 +238,14 @@ func renderSearchResults(query string) string {
238238
}
239239

240240
addr := address(query)
241-
if _, exists := GetAllUsers()[addr]; exists {
241+
if _, exists := trustScores.Get(string(addr)); exists {
242242
return renderUserProfile(string(addr))
243243
}
244244

245245
result := md.H1("🔍 Search results for: " + md.InlineCode(query))
246246

247-
var matches []address
248-
for addr := range GetAllUsers() {
249-
addrStr := string(addr)
250-
if len(query) >= 4 && strings.Contains(addrStr, query) {
251-
matches = append(matches, addr)
252-
}
253-
}
247+
// Use FilterUsers to get matches (already sorted by score)
248+
matches, _ := FilterUsers(query, 100, 0) // Get up to 100 matches
254249

255250
matchCount := len(matches)
256251

packages/r/greg007/TrustFactor/trustfactor_test.gno

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,10 @@ func TestScoreBounds(t *testing.T) {
470470
}
471471
}
472472

473-
// TestGetAllUsers tests retrieving all registered users
474-
func TestGetAllUsers(t *testing.T) {
473+
// TestGetTotalUsers tests retrieving total user count
474+
func TestGetTotalUsers(t *testing.T) {
475475
trustScores = avl.NewTree()
476+
totalUsers = 0
476477

477478
addr1 := address("g1user1234567890123456789012345678")
478479
addr2 := address("g1user2345678901234567890123456789")
@@ -481,23 +482,25 @@ func TestGetAllUsers(t *testing.T) {
481482
trustScores.Set(string(addr1), &TrustScore{Score: 5.0, LastUpdate: time.Now().Unix()})
482483
trustScores.Set(string(addr2), &TrustScore{Score: 6.0, LastUpdate: time.Now().Unix()})
483484
trustScores.Set(string(addr3), &TrustScore{Score: 7.0, LastUpdate: time.Now().Unix()})
485+
totalUsers = 3
484486

485-
allUsers := GetAllUsers()
487+
count := GetTotalUsers()
486488

487-
if len(allUsers) != 3 {
488-
t.Errorf("Expected 3 users, got %d", len(allUsers))
489+
if count != 3 {
490+
t.Errorf("Expected 3 users, got %d", count)
489491
}
490492

491-
if _, exists := allUsers[addr1]; !exists {
492-
t.Error("addr1 should exist in all users")
493+
// Verify users exist using GetTrustDetails
494+
if trust := GetTrustDetails(addr1); trust.Score == 0 {
495+
t.Error("addr1 should exist")
493496
}
494497

495-
if _, exists := allUsers[addr2]; !exists {
496-
t.Error("addr2 should exist in all users")
498+
if trust := GetTrustDetails(addr2); trust.Score == 0 {
499+
t.Error("addr2 should exist")
497500
}
498501

499-
if _, exists := allUsers[addr3]; !exists {
500-
t.Error("addr3 should exist in all users")
502+
if trust := GetTrustDetails(addr3); trust.Score == 0 {
503+
t.Error("addr3 should exist")
501504
}
502505
}
503506

0 commit comments

Comments
 (0)