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,37 +450,31 @@ func (o *Community) CommunityTokensMetadata() []*protobuf.CommunityTokenMetadata
}
func (o *Community) Tags() []CommunityTag {
if o != nil &&
o.config != nil &&
o.config.CommunityDescription != nil {
var result []CommunityTag
if o == nil ||
o.config == nil ||
o.config.CommunityDescription == nil {
return nil
}
result := make([]CommunityTag, 0, len(o.config.CommunityDescription.Tags))
for _, t := range o.config.CommunityDescription.Tags {
result = append(result, CommunityTag{
Name: t,
Emoji: requests.TagsEmojies[t],
Emoji: requests.TagEmoji(t),
})
}
return result
}
return nil
}
func (o *Community) TagsRaw() []string {
return o.config.CommunityDescription.Tags
}
func (o *Community) TagsIndices() []uint32 {
indices := []uint32{}
var indices []uint32
for _, t := range o.config.CommunityDescription.Tags {
i := uint32(0)
for k := range requests.TagsEmojies {
if k == t {
indices = append(indices, i)
indices = append(indices, requests.TagIndex(t))
}
i++
}
}
return indices
}

View File

@ -41,9 +41,10 @@ func (s *MessengerShareUrlsSuite) createCommunity() *communities.Community {
Name: "status",
Color: "#ffffff",
Description: "status community description",
Tags: RandomCommunityTags(3),
}
// Create an community chat
// Create a community chat
response, err := s.m.CreateCommunity(description, false)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -245,6 +246,13 @@ func (s *MessengerShareUrlsSuite) TestShareCommunityURLWithData() {
expectedURL := fmt.Sprintf("%s/c/%s#%s", baseShareURL, communityData, chatKey)
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() {

View File

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

View File

@ -1,66 +1,24 @@
package requests
var TagsEmojies map[string]string
var tags []string
var tagsEmojis map[string]string
var tagsIndices map[string]uint32
func init() {
TagsEmojies = make(map[string]string)
TagsEmojies["Activism"] = "✊"
TagsEmojies["Art"] = "🎨"
TagsEmojies["Blockchain"] = "🔗"
TagsEmojies["Books & blogs"] = "📚"
TagsEmojies["Career"] = "💼"
TagsEmojies["Collaboration"] = "🤝"
TagsEmojies["Commerce"] = "🛒"
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"] = "🌐"
tags = make([]string, 0, len(allTags))
tagsEmojis = make(map[string]string, len(allTags))
tagsIndices = make(map[string]uint32, len(allTags))
for i, pair := range allTags {
tags = append(tags, pair[0])
tagsEmojis[pair[0]] = pair[1]
tagsIndices[pair[0]] = uint32(i)
}
}
func ValidateTags(input []string) bool {
for _, t := range input {
_, ok := TagsEmojies[t]
_, ok := tagsEmojis[t]
if !ok {
return false
}
@ -73,7 +31,7 @@ func ValidateTags(input []string) bool {
func RemoveUnknownAndDeduplicateTags(input []string) []string {
var result []string
for _, t := range input {
_, ok := TagsEmojies[t]
_, ok := tagsEmojis[t]
if ok {
result = append(result, t)
}
@ -92,3 +50,83 @@ func unique(slice []string) []string {
}
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
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