Fix/community tags indices (#4992)

* fix: strict order of community tags

* make tags containers private

* fix RandomCommunityTags implementation
This commit is contained in:
Igor Sirotin 2024-03-26 20:02:12 +00:00 committed by GitHub
parent 30fe620ff0
commit 1a2880b365
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 129 additions and 90 deletions

View File

@ -450,19 +450,20 @@ func (o *Community) CommunityTokensMetadata() []*protobuf.CommunityTokenMetadata
} }
func (o *Community) Tags() []CommunityTag { func (o *Community) Tags() []CommunityTag {
if o != nil && if o == nil ||
o.config != nil && o.config == nil ||
o.config.CommunityDescription != nil { o.config.CommunityDescription == nil {
var result []CommunityTag return nil
}
result := make([]CommunityTag, 0, len(o.config.CommunityDescription.Tags))
for _, t := range o.config.CommunityDescription.Tags { for _, t := range o.config.CommunityDescription.Tags {
result = append(result, CommunityTag{ result = append(result, CommunityTag{
Name: t, Name: t,
Emoji: requests.TagsEmojies[t], Emoji: requests.TagEmoji(t),
}) })
} }
return result return result
}
return nil
} }
func (o *Community) TagsRaw() []string { func (o *Community) TagsRaw() []string {
@ -470,17 +471,10 @@ func (o *Community) TagsRaw() []string {
} }
func (o *Community) TagsIndices() []uint32 { func (o *Community) TagsIndices() []uint32 {
indices := []uint32{} var indices []uint32
for _, t := range o.config.CommunityDescription.Tags { for _, t := range o.config.CommunityDescription.Tags {
i := uint32(0) indices = append(indices, requests.TagIndex(t))
for k := range requests.TagsEmojies {
if k == t {
indices = append(indices, i)
} }
i++
}
}
return indices return indices
} }

View File

@ -41,9 +41,10 @@ func (s *MessengerShareUrlsSuite) createCommunity() *communities.Community {
Name: "status", Name: "status",
Color: "#ffffff", Color: "#ffffff",
Description: "status community description", Description: "status community description",
Tags: RandomCommunityTags(3),
} }
// Create an community chat // Create a community chat
response, err := s.m.CreateCommunity(description, false) response, err := s.m.CreateCommunity(description, false)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
@ -245,6 +246,13 @@ func (s *MessengerShareUrlsSuite) TestShareCommunityURLWithData() {
expectedURL := fmt.Sprintf("%s/c/%s#%s", baseShareURL, communityData, chatKey) expectedURL := fmt.Sprintf("%s/c/%s#%s", baseShareURL, communityData, chatKey)
s.Require().Equal(expectedURL, url) s.Require().Equal(expectedURL, url)
response, err := ParseSharedURL(url)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().NotNil(response.Community)
s.Require().Equal(community.IDString(), response.Community.CommunityID)
s.Require().Equal(community.TagsIndices(), response.Community.TagIndices)
} }
func (s *MessengerShareUrlsSuite) TestParseCommunityURLWithData() { func (s *MessengerShareUrlsSuite) TestParseCommunityURLWithData() {

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
mathRand "math/rand"
"sync" "sync"
"time" "time"
@ -16,8 +17,6 @@ import (
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
waku2 "github.com/status-im/status-go/wakuv2" waku2 "github.com/status-im/status-go/wakuv2"
"golang.org/x/exp/maps"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
@ -391,20 +390,20 @@ func RandomColor() string {
} }
func RandomCommunityTags(count int) []string { func RandomCommunityTags(count int) []string {
all := maps.Keys(requests.TagsEmojies) availableTagsCount := requests.AvailableTagsCount()
tags := make([]string, 0, count)
indexes := map[int]struct{}{}
for len(indexes) != count { if count > availableTagsCount {
index := randomInt(len(all)) count = availableTagsCount
indexes[index] = struct{}{}
} }
for index := range indexes { //source := mathRand.New(mathRand.NewSource(time.Now().UnixNano()))
tags = append(tags, all[index]) indices := mathRand.Perm(availableTagsCount)
shuffled := make([]string, count)
for i := 0; i < count; i++ {
shuffled[i] = requests.TagByIndex(uint32(indices[i]))
} }
return tags return shuffled
} }
func RandomBytes(length int) []byte { func RandomBytes(length int) []byte {

View File

@ -1,66 +1,24 @@
package requests package requests
var TagsEmojies map[string]string var tags []string
var tagsEmojis map[string]string
var tagsIndices map[string]uint32
func init() { func init() {
TagsEmojies = make(map[string]string) tags = make([]string, 0, len(allTags))
TagsEmojies["Activism"] = "✊" tagsEmojis = make(map[string]string, len(allTags))
TagsEmojies["Art"] = "🎨" tagsIndices = make(map[string]uint32, len(allTags))
TagsEmojies["Blockchain"] = "🔗"
TagsEmojies["Books & blogs"] = "📚" for i, pair := range allTags {
TagsEmojies["Career"] = "💼" tags = append(tags, pair[0])
TagsEmojies["Collaboration"] = "🤝" tagsEmojis[pair[0]] = pair[1]
TagsEmojies["Commerce"] = "🛒" tagsIndices[pair[0]] = uint32(i)
TagsEmojies["Culture"] = "🎎" }
TagsEmojies["DAO"] = "🚀"
TagsEmojies["DeFi"] = "📈"
TagsEmojies["Design"] = "🧩"
TagsEmojies["DIY"] = "🔨"
TagsEmojies["Environment"] = "🌿"
TagsEmojies["Education"] = "🎒"
TagsEmojies["Entertainment"] = "🍿"
TagsEmojies["Ethereum"] = "Ξ"
TagsEmojies["Event"] = "🗓"
TagsEmojies["Fantasy"] = "🧙‍♂️"
TagsEmojies["Fashion"] = "🧦"
TagsEmojies["Food"] = "🌶"
TagsEmojies["Gaming"] = "🎮"
TagsEmojies["Global"] = "🌍"
TagsEmojies["Health"] = "🧠"
TagsEmojies["Hobby"] = "📐"
TagsEmojies["Innovation"] = "🧪"
TagsEmojies["Language"] = "📜"
TagsEmojies["Lifestyle"] = "✨"
TagsEmojies["Local"] = "📍"
TagsEmojies["Love"] = "❤️"
TagsEmojies["Markets"] = "💎"
TagsEmojies["Movies & TV"] = "🎞"
TagsEmojies["Music"] = "🎶"
TagsEmojies["News"] = "🗞"
TagsEmojies["NFT"] = "🖼"
TagsEmojies["Non-profit"] = "🙏"
TagsEmojies["NSFW"] = "🍆"
TagsEmojies["Org"] = "🏢"
TagsEmojies["Pets"] = "🐶"
TagsEmojies["Play"] = "🎲"
TagsEmojies["Podcast"] = "🎙️"
TagsEmojies["Politics"] = "🗳️"
TagsEmojies["Product"] = "🍱"
TagsEmojies["Psyche"] = "🍁"
TagsEmojies["Privacy"] = "👻"
TagsEmojies["Security"] = "🔒"
TagsEmojies["Social"] = "☕"
TagsEmojies["Software dev"] = "👩‍💻"
TagsEmojies["Sports"] = "⚽️"
TagsEmojies["Tech"] = "📱"
TagsEmojies["Travel"] = "🗺"
TagsEmojies["Vehicles"] = "🚕"
TagsEmojies["Web3"] = "🌐"
} }
func ValidateTags(input []string) bool { func ValidateTags(input []string) bool {
for _, t := range input { for _, t := range input {
_, ok := TagsEmojies[t] _, ok := tagsEmojis[t]
if !ok { if !ok {
return false return false
} }
@ -73,7 +31,7 @@ func ValidateTags(input []string) bool {
func RemoveUnknownAndDeduplicateTags(input []string) []string { func RemoveUnknownAndDeduplicateTags(input []string) []string {
var result []string var result []string
for _, t := range input { for _, t := range input {
_, ok := TagsEmojies[t] _, ok := tagsEmojis[t]
if ok { if ok {
result = append(result, t) result = append(result, t)
} }
@ -92,3 +50,83 @@ func unique(slice []string) []string {
} }
return uniqSlice return uniqSlice
} }
func TagByIndex(i uint32) string {
return tags[i]
}
func TagEmoji(tag string) string {
return tagsEmojis[tag]
}
func TagIndex(tag string) uint32 {
return tagsIndices[tag]
}
func AvailableTagsCount() int {
return len(tags)
}
func AvailableTagsEmojis() map[string]string {
// Make a deep copy of the map to keep it immutable
emojis := make(map[string]string, len(tagsEmojis))
for t, e := range tagsEmojis {
emojis[t] = e
}
return emojis
}
var allTags = [][]string{
{"Activism", "✊"},
{"Art", "🎨"},
{"Blockchain", "🔗"},
{"Books & blogs", "📚"},
{"Career", "💼"},
{"Collaboration", "🤝"},
{"Commerce", "🛒"},
{"Culture", "🎎"},
{"DAO", "🚀"},
{"DeFi", "📈"},
{"Design", "🧩"},
{"DIY", "🔨"},
{"Environment", "🌿"},
{"Education", "🎒"},
{"Entertainment", "🍿"},
{"Ethereum", "Ξ"},
{"Event", "🗓"},
{"Fantasy", "🧙‍♂️"},
{"Fashion", "🧦"},
{"Food", "🌶"},
{"Gaming", "🎮"},
{"Global", "🌍"},
{"Health", "🧠"},
{"Hobby", "📐"},
{"Innovation", "🧪"},
{"Language", "📜"},
{"Lifestyle", "✨"},
{"Local", "📍"},
{"Love", "❤️"},
{"Markets", "💎"},
{"Movies & TV", "🎞"},
{"Music", "🎶"},
{"News", "🗞"},
{"NFT", "🖼"},
{"Non-profit", "🙏"},
{"NSFW", "🍆"},
{"Org", "🏢"},
{"Pets", "🐶"},
{"Play", "🎲"},
{"Podcast", "🎙️"},
{"Politics", "🗳️"},
{"Product", "🍱"},
{"Psyche", "🍁"},
{"Privacy", "👻"},
{"Security", "🔒"},
{"Social", "☕"},
{"Software dev", "👩‍💻"},
{"Sports", "⚽️"},
{"Tech", "📱"},
{"Travel", "🗺"},
{"Vehicles", "🚕"},
{"Web3", "🌐"},
}

View File

@ -408,7 +408,7 @@ func (api *PublicAPI) IsDisplayNameDupeOfCommunityMember(name string) (bool, err
// CommunityTags return the list of possible community tags // CommunityTags return the list of possible community tags
func (api *PublicAPI) CommunityTags(parent context.Context) map[string]string { func (api *PublicAPI) CommunityTags(parent context.Context) map[string]string {
return requests.TagsEmojies return requests.AvailableTagsEmojis()
} }
// CuratedCommunities returns the list of curated communities stored in the smart contract. If a community is // CuratedCommunities returns the list of curated communities stored in the smart contract. If a community is