fix: allow empty category id when sorting chats and fix error messages (#2301)
* fix: allow empty category id when sorting chats and fix error messages * fix: unordered maps
This commit is contained in:
parent
20363e32ef
commit
fe086b2fdd
5 changed files with 107 additions and 47 deletions
|
@ -151,25 +151,61 @@ func (o *Community) ReorderCategories(categoryID string, newPosition int) (*Comm
|
||||||
return nil, ErrNotAdmin
|
return nil, ErrNotAdmin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
||||||
|
return nil, ErrCategoryNotFound
|
||||||
|
}
|
||||||
|
|
||||||
if newPosition > 0 && newPosition >= len(o.config.CommunityDescription.Categories) {
|
if newPosition > 0 && newPosition >= len(o.config.CommunityDescription.Categories) {
|
||||||
newPosition = len(o.config.CommunityDescription.Categories) - 1
|
newPosition = len(o.config.CommunityDescription.Categories) - 1
|
||||||
} else if newPosition < 0 {
|
} else if newPosition < 0 {
|
||||||
newPosition = 0
|
newPosition = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
o.config.CommunityDescription.Categories[categoryID].Position = int32(newPosition)
|
category := o.config.CommunityDescription.Categories[categoryID]
|
||||||
|
if category.Position == int32(newPosition) {
|
||||||
|
return nil, ErrNoChangeInPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
decrease := false
|
||||||
|
if category.Position > int32(newPosition) {
|
||||||
|
decrease = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sorting the categories because maps are not guaranteed to keep order
|
||||||
s := make(sortSlice, 0, len(o.config.CommunityDescription.Categories))
|
s := make(sortSlice, 0, len(o.config.CommunityDescription.Categories))
|
||||||
for catID, category := range o.config.CommunityDescription.Categories {
|
for k, v := range o.config.CommunityDescription.Categories {
|
||||||
|
|
||||||
position := category.Position
|
|
||||||
if category.CategoryId != categoryID && position >= int32(newPosition) {
|
|
||||||
position = position + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
s = append(s, sorterHelperIdx{
|
s = append(s, sorterHelperIdx{
|
||||||
pos: position,
|
pos: v.Position,
|
||||||
catID: catID,
|
catID: k,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sort.Sort(s)
|
||||||
|
var communityCategories []*protobuf.CommunityCategory
|
||||||
|
for _, currCat := range s {
|
||||||
|
communityCategories = append(communityCategories, o.config.CommunityDescription.Categories[currCat.catID])
|
||||||
|
}
|
||||||
|
|
||||||
|
var sortedCategoryIDs []string
|
||||||
|
for _, v := range communityCategories {
|
||||||
|
if v != category && ((decrease && v.Position < int32(newPosition)) || (!decrease && v.Position <= int32(newPosition))) {
|
||||||
|
sortedCategoryIDs = append(sortedCategoryIDs, v.CategoryId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sortedCategoryIDs = append(sortedCategoryIDs, categoryID)
|
||||||
|
|
||||||
|
for _, v := range communityCategories {
|
||||||
|
if v.CategoryId == categoryID || (decrease && v.Position < int32(newPosition)) || (!decrease && v.Position <= int32(newPosition)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sortedCategoryIDs = append(sortedCategoryIDs, v.CategoryId)
|
||||||
|
}
|
||||||
|
|
||||||
|
s = make(sortSlice, 0, len(o.config.CommunityDescription.Categories))
|
||||||
|
for i, k := range sortedCategoryIDs {
|
||||||
|
s = append(s, sorterHelperIdx{
|
||||||
|
pos: int32(i),
|
||||||
|
catID: k,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,8 +236,10 @@ func (o *Community) ReorderChat(categoryID string, chatID string, newPosition in
|
||||||
return nil, ErrNotAdmin
|
return nil, ErrNotAdmin
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
if categoryID != "" {
|
||||||
return nil, ErrCategoryNotFound
|
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
||||||
|
return nil, ErrCategoryNotFound
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var chat *protobuf.CommunityChat
|
var chat *protobuf.CommunityChat
|
||||||
|
@ -258,10 +296,19 @@ func (o *Community) SortCategoryChats(changes *CommunityChanges, categoryID stri
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Community) insertAndSort(changes *CommunityChanges, categoryID string, chat *protobuf.CommunityChat, newPosition int) {
|
func (o *Community) insertAndSort(changes *CommunityChanges, categoryID string, chat *protobuf.CommunityChat, newPosition int) {
|
||||||
|
// We sort the chats here because maps are not guaranteed to keep order
|
||||||
var catChats []string
|
var catChats []string
|
||||||
for k, c := range o.config.CommunityDescription.Chats {
|
sortedChats := make(sortSlice, 0, len(o.config.CommunityDescription.Chats))
|
||||||
if c.CategoryId == categoryID {
|
for k, v := range o.config.CommunityDescription.Chats {
|
||||||
catChats = append(catChats, k)
|
sortedChats = append(sortedChats, sorterHelperIdx{
|
||||||
|
pos: v.Position,
|
||||||
|
chatID: k,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sort.Sort(sortedChats)
|
||||||
|
for _, k := range sortedChats {
|
||||||
|
if o.config.CommunityDescription.Chats[k.chatID].CategoryId == categoryID {
|
||||||
|
catChats = append(catChats, k.chatID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,28 +318,45 @@ func (o *Community) insertAndSort(changes *CommunityChanges, categoryID string,
|
||||||
newPosition = 0
|
newPosition = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
sortedChats := make(sortSlice, 0, len(catChats))
|
if int32(newPosition) == chat.Position {
|
||||||
for _, k := range catChats {
|
return
|
||||||
position := chat.Position
|
|
||||||
if o.config.CommunityDescription.Chats[k] != chat && position >= int32(newPosition) {
|
|
||||||
position = position + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
sortedChats = append(sortedChats, sorterHelperIdx{
|
|
||||||
pos: position,
|
|
||||||
chatID: k,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(sortedChats)
|
decrease := false
|
||||||
|
if chat.Position > int32(newPosition) {
|
||||||
|
decrease = true
|
||||||
|
}
|
||||||
|
|
||||||
for i, chatSortHelper := range sortedChats {
|
idx := -1
|
||||||
if o.config.CommunityDescription.Chats[chatSortHelper.chatID].Position != int32(i) {
|
currChatID := ""
|
||||||
o.config.CommunityDescription.Chats[chatSortHelper.chatID].Position = int32(i)
|
var sortedChatIDs []string
|
||||||
if changes.ChatsModified[chatSortHelper.chatID] != nil {
|
for i, k := range catChats {
|
||||||
changes.ChatsModified[chatSortHelper.chatID].PositionModified = i
|
if o.config.CommunityDescription.Chats[k] != chat && ((decrease && o.config.CommunityDescription.Chats[k].Position < int32(newPosition)) || (!decrease && o.config.CommunityDescription.Chats[k].Position <= int32(newPosition))) {
|
||||||
|
sortedChatIDs = append(sortedChatIDs, k)
|
||||||
|
} else {
|
||||||
|
if o.config.CommunityDescription.Chats[k] == chat {
|
||||||
|
idx = i
|
||||||
|
currChatID = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sortedChatIDs = append(sortedChatIDs, currChatID)
|
||||||
|
|
||||||
|
for i, k := range catChats {
|
||||||
|
if i == idx || (decrease && o.config.CommunityDescription.Chats[k].Position < int32(newPosition)) || (!decrease && o.config.CommunityDescription.Chats[k].Position <= int32(newPosition)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sortedChatIDs = append(sortedChatIDs, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, sortedChatID := range sortedChatIDs {
|
||||||
|
if o.config.CommunityDescription.Chats[sortedChatID].Position != int32(i) {
|
||||||
|
o.config.CommunityDescription.Chats[sortedChatID].Position = int32(i)
|
||||||
|
if changes.ChatsModified[sortedChatID] != nil {
|
||||||
|
changes.ChatsModified[sortedChatID].PositionModified = i
|
||||||
} else {
|
} else {
|
||||||
changes.ChatsModified[chatSortHelper.chatID] = &CommunityChatChanges{
|
changes.ChatsModified[sortedChatID] = &CommunityChatChanges{
|
||||||
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
||||||
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
||||||
PositionModified: i,
|
PositionModified: i,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import "errors"
|
||||||
|
|
||||||
var ErrChatNotFound = errors.New("chat not found")
|
var ErrChatNotFound = errors.New("chat not found")
|
||||||
var ErrCategoryNotFound = errors.New("category not found")
|
var ErrCategoryNotFound = errors.New("category not found")
|
||||||
|
var ErrNoChangeInPosition = errors.New("no change in category position")
|
||||||
var ErrChatAlreadyAssigned = errors.New("chat already assigned to a category")
|
var ErrChatAlreadyAssigned = errors.New("chat already assigned to a category")
|
||||||
var ErrOrgNotFound = errors.New("community not found")
|
var ErrOrgNotFound = errors.New("community not found")
|
||||||
var ErrChatAlreadyExists = errors.New("chat already exists")
|
var ErrChatAlreadyExists = errors.New("chat already exists")
|
||||||
|
|
|
@ -6,8 +6,8 @@ import (
|
||||||
"github.com/status-im/status-go/eth-node/types"
|
"github.com/status-im/status-go/eth-node/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrDeleteCommunityCategoryInvalidCommunityID = errors.New("set-community-chat-category: invalid community id")
|
var ErrDeleteCommunityCategoryInvalidCommunityID = errors.New("delete-community-category: invalid community id")
|
||||||
var ErrDeleteCommunityCategoryInvalidCategoryID = errors.New("set-community-chat-category: invalid category id")
|
var ErrDeleteCommunityCategoryInvalidCategoryID = errors.New("delete-community-category: invalid category id")
|
||||||
|
|
||||||
type DeleteCommunityCategory struct {
|
type DeleteCommunityCategory struct {
|
||||||
CommunityID types.HexBytes `json:"communityId"`
|
CommunityID types.HexBytes `json:"communityId"`
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"github.com/status-im/status-go/eth-node/types"
|
"github.com/status-im/status-go/eth-node/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrReorderCommunityCategoryInvalidCommunityID = errors.New("edit-community-category: invalid community id")
|
var ErrReorderCommunityCategoryInvalidCommunityID = errors.New("reorder-community-category: invalid community id")
|
||||||
var ErrReorderCommunityCategoryInvalidCategoryID = errors.New("edit-community-category: invalid category id")
|
var ErrReorderCommunityCategoryInvalidCategoryID = errors.New("reorder-community-category: invalid category id")
|
||||||
var ErrReorderCommunityCategoryInvalidPosition = errors.New("edit-community-category: invalid position")
|
var ErrReorderCommunityCategoryInvalidPosition = errors.New("reorder-community-category: invalid position")
|
||||||
|
|
||||||
type ReorderCommunityCategories struct {
|
type ReorderCommunityCategories struct {
|
||||||
CommunityID types.HexBytes `json:"communityId"`
|
CommunityID types.HexBytes `json:"communityId"`
|
||||||
|
@ -22,7 +22,7 @@ func (j *ReorderCommunityCategories) Validate() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(j.CategoryID) == 0 {
|
if len(j.CategoryID) == 0 {
|
||||||
return ErrEditCommunityCategoryInvalidCategoryID
|
return ErrReorderCommunityCategoryInvalidCategoryID
|
||||||
}
|
}
|
||||||
|
|
||||||
if j.Position < 0 {
|
if j.Position < 0 {
|
||||||
|
|
|
@ -6,10 +6,9 @@ import (
|
||||||
"github.com/status-im/status-go/eth-node/types"
|
"github.com/status-im/status-go/eth-node/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrReorderCommunityChatInvalidCommunityID = errors.New("edit-community-category: invalid community id")
|
var ErrReorderCommunityChatInvalidCommunityID = errors.New("reorder-community-chat: invalid community id")
|
||||||
var ErrReorderCommunityChatInvalidCategoryID = errors.New("edit-community-category: invalid category id")
|
var ErrReorderCommunityChatInvalidChatID = errors.New("reorder-community-chat: invalid chat id")
|
||||||
var ErrReorderCommunityChatInvalidChatID = errors.New("edit-community-category: invalid chat id")
|
var ErrReorderCommunityChatInvalidPosition = errors.New("reorder-community-chat: invalid position")
|
||||||
var ErrReorderCommunityChatInvalidPosition = errors.New("edit-community-category: invalid position")
|
|
||||||
|
|
||||||
type ReorderCommunityChat struct {
|
type ReorderCommunityChat struct {
|
||||||
CommunityID types.HexBytes `json:"communityId"`
|
CommunityID types.HexBytes `json:"communityId"`
|
||||||
|
@ -23,10 +22,6 @@ func (j *ReorderCommunityChat) Validate() error {
|
||||||
return ErrReorderCommunityChatInvalidCommunityID
|
return ErrReorderCommunityChatInvalidCommunityID
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(j.CategoryID) == 0 {
|
|
||||||
return ErrReorderCommunityChatInvalidCategoryID
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(j.ChatID) == 0 {
|
if len(j.ChatID) == 0 {
|
||||||
return ErrReorderCommunityChatInvalidChatID
|
return ErrReorderCommunityChatInvalidChatID
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue