2021-05-23 13:34:17 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (o *Community) CreateCategory(categoryID string, categoryName string, chatIDs []string) (*CommunityChanges, error) {
|
|
|
|
o.mutex.Lock()
|
|
|
|
defer o.mutex.Unlock()
|
|
|
|
|
|
|
|
if o.config.PrivateKey == nil {
|
|
|
|
return nil, ErrNotAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.config.CommunityDescription.Categories == nil {
|
|
|
|
o.config.CommunityDescription.Categories = make(map[string]*protobuf.CommunityCategory)
|
|
|
|
}
|
|
|
|
if _, ok := o.config.CommunityDescription.Categories[categoryID]; ok {
|
|
|
|
return nil, ErrCategoryAlreadyExists
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cid := range chatIDs {
|
|
|
|
c, exists := o.config.CommunityDescription.Chats[cid]
|
|
|
|
if !exists {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists && c.CategoryId != categoryID && c.CategoryId != "" {
|
|
|
|
return nil, ErrChatAlreadyAssigned
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := o.emptyCommunityChanges()
|
|
|
|
|
|
|
|
o.config.CommunityDescription.Categories[categoryID] = &protobuf.CommunityCategory{
|
|
|
|
CategoryId: categoryID,
|
|
|
|
Name: categoryName,
|
|
|
|
Position: int32(len(o.config.CommunityDescription.Categories)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, cid := range chatIDs {
|
|
|
|
o.config.CommunityDescription.Chats[cid].CategoryId = categoryID
|
|
|
|
o.config.CommunityDescription.Chats[cid].Position = int32(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
o.SortCategoryChats(changes, "")
|
|
|
|
|
|
|
|
o.increaseClock()
|
|
|
|
|
|
|
|
changes.CategoriesAdded[categoryID] = o.config.CommunityDescription.Categories[categoryID]
|
|
|
|
for i, cid := range chatIDs {
|
|
|
|
changes.ChatsModified[cid] = &CommunityChatChanges{
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
CategoryModified: categoryID,
|
|
|
|
PositionModified: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) EditCategory(categoryID string, categoryName string, chatIDs []string) (*CommunityChanges, error) {
|
|
|
|
o.mutex.Lock()
|
|
|
|
defer o.mutex.Unlock()
|
|
|
|
|
|
|
|
if o.config.PrivateKey == nil {
|
|
|
|
return nil, ErrNotAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.config.CommunityDescription.Categories == nil {
|
|
|
|
o.config.CommunityDescription.Categories = make(map[string]*protobuf.CommunityCategory)
|
|
|
|
}
|
|
|
|
if _, ok := o.config.CommunityDescription.Categories[categoryID]; !ok {
|
|
|
|
return nil, ErrCategoryNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cid := range chatIDs {
|
|
|
|
c, exists := o.config.CommunityDescription.Chats[cid]
|
|
|
|
if !exists {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists && c.CategoryId != categoryID && c.CategoryId != "" {
|
|
|
|
return nil, ErrChatAlreadyAssigned
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := o.emptyCommunityChanges()
|
|
|
|
|
|
|
|
emptyCatLen := o.getCategoryChatCount("")
|
|
|
|
|
|
|
|
// remove any chat that might have been assigned before and now it's not part of the category
|
|
|
|
var chatsToRemove []string
|
|
|
|
for k, chat := range o.config.CommunityDescription.Chats {
|
|
|
|
if chat.CategoryId == categoryID {
|
|
|
|
found := false
|
|
|
|
for _, c := range chatIDs {
|
|
|
|
if k == c {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
chat.CategoryId = ""
|
|
|
|
chatsToRemove = append(chatsToRemove, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o.config.CommunityDescription.Categories[categoryID].Name = categoryName
|
|
|
|
|
|
|
|
for i, cid := range chatIDs {
|
|
|
|
o.config.CommunityDescription.Chats[cid].CategoryId = categoryID
|
|
|
|
o.config.CommunityDescription.Chats[cid].Position = int32(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, cid := range chatsToRemove {
|
|
|
|
o.config.CommunityDescription.Chats[cid].Position = int32(emptyCatLen + i)
|
|
|
|
changes.ChatsModified[cid] = &CommunityChatChanges{
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
CategoryModified: "",
|
|
|
|
PositionModified: int(o.config.CommunityDescription.Chats[cid].Position),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o.SortCategoryChats(changes, "")
|
|
|
|
|
|
|
|
o.increaseClock()
|
|
|
|
|
|
|
|
changes.CategoriesModified[categoryID] = o.config.CommunityDescription.Categories[categoryID]
|
|
|
|
for i, cid := range chatIDs {
|
|
|
|
changes.ChatsModified[cid] = &CommunityChatChanges{
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
CategoryModified: categoryID,
|
|
|
|
PositionModified: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) ReorderCategories(categoryID string, newPosition int) (*CommunityChanges, error) {
|
|
|
|
o.mutex.Lock()
|
|
|
|
defer o.mutex.Unlock()
|
|
|
|
|
|
|
|
if o.config.PrivateKey == nil {
|
|
|
|
return nil, ErrNotAdmin
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
|
|
|
return nil, ErrCategoryNotFound
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
if newPosition > 0 && newPosition >= len(o.config.CommunityDescription.Categories) {
|
|
|
|
newPosition = len(o.config.CommunityDescription.Categories) - 1
|
|
|
|
} else if newPosition < 0 {
|
|
|
|
newPosition = 0
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
category := o.config.CommunityDescription.Categories[categoryID]
|
|
|
|
if category.Position == int32(newPosition) {
|
|
|
|
return nil, ErrNoChangeInPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
decrease := false
|
|
|
|
if category.Position > int32(newPosition) {
|
|
|
|
decrease = true
|
|
|
|
}
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
// Sorting the categories because maps are not guaranteed to keep order
|
2021-05-23 13:34:17 +00:00
|
|
|
s := make(sortSlice, 0, len(o.config.CommunityDescription.Categories))
|
2021-08-25 16:52:28 +00:00
|
|
|
for k, v := range o.config.CommunityDescription.Categories {
|
|
|
|
s = append(s, sorterHelperIdx{
|
|
|
|
pos: v.Position,
|
|
|
|
catID: k,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sort.Sort(s)
|
|
|
|
var communityCategories []*protobuf.CommunityCategory
|
|
|
|
for _, currCat := range s {
|
|
|
|
communityCategories = append(communityCategories, o.config.CommunityDescription.Categories[currCat.catID])
|
|
|
|
}
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
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)
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
2021-08-25 16:52:28 +00:00
|
|
|
}
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
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 {
|
2021-05-23 13:34:17 +00:00
|
|
|
s = append(s, sorterHelperIdx{
|
2021-08-25 16:52:28 +00:00
|
|
|
pos: int32(i),
|
|
|
|
catID: k,
|
2021-05-23 13:34:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := o.emptyCommunityChanges()
|
|
|
|
|
|
|
|
o.setModifiedCategories(changes, s)
|
|
|
|
|
|
|
|
o.increaseClock()
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) setModifiedCategories(changes *CommunityChanges, s sortSlice) {
|
|
|
|
sort.Sort(s)
|
|
|
|
for i, catSortHelper := range s {
|
|
|
|
if o.config.CommunityDescription.Categories[catSortHelper.catID].Position != int32(i) {
|
|
|
|
o.config.CommunityDescription.Categories[catSortHelper.catID].Position = int32(i)
|
|
|
|
changes.CategoriesModified[catSortHelper.catID] = o.config.CommunityDescription.Categories[catSortHelper.catID]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) ReorderChat(categoryID string, chatID string, newPosition int) (*CommunityChanges, error) {
|
|
|
|
o.mutex.Lock()
|
|
|
|
defer o.mutex.Unlock()
|
|
|
|
|
|
|
|
if o.config.PrivateKey == nil {
|
|
|
|
return nil, ErrNotAdmin
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
if categoryID != "" {
|
|
|
|
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
|
|
|
return nil, ErrCategoryNotFound
|
|
|
|
}
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var chat *protobuf.CommunityChat
|
|
|
|
var exists bool
|
|
|
|
if chat, exists = o.config.CommunityDescription.Chats[chatID]; !exists {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCategoryID := chat.CategoryId
|
|
|
|
chat.CategoryId = categoryID
|
|
|
|
|
|
|
|
changes := o.emptyCommunityChanges()
|
|
|
|
|
|
|
|
o.SortCategoryChats(changes, oldCategoryID)
|
2021-11-30 14:26:17 +00:00
|
|
|
o.insertAndSort(changes, oldCategoryID, categoryID, chatID, chat, newPosition)
|
2021-05-23 13:34:17 +00:00
|
|
|
|
|
|
|
o.increaseClock()
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) SortCategoryChats(changes *CommunityChanges, categoryID string) {
|
|
|
|
var catChats []string
|
|
|
|
for k, c := range o.config.CommunityDescription.Chats {
|
|
|
|
if c.CategoryId == categoryID {
|
|
|
|
catChats = append(catChats, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedChats := make(sortSlice, 0, len(catChats))
|
|
|
|
for _, k := range catChats {
|
|
|
|
sortedChats = append(sortedChats, sorterHelperIdx{
|
|
|
|
pos: o.config.CommunityDescription.Chats[k].Position,
|
|
|
|
chatID: k,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sortedChats)
|
|
|
|
|
|
|
|
for i, chatSortHelper := range sortedChats {
|
|
|
|
if o.config.CommunityDescription.Chats[chatSortHelper.chatID].Position != int32(i) {
|
|
|
|
o.config.CommunityDescription.Chats[chatSortHelper.chatID].Position = int32(i)
|
|
|
|
if changes.ChatsModified[chatSortHelper.chatID] != nil {
|
|
|
|
changes.ChatsModified[chatSortHelper.chatID].PositionModified = i
|
|
|
|
} else {
|
|
|
|
changes.ChatsModified[chatSortHelper.chatID] = &CommunityChatChanges{
|
|
|
|
PositionModified: i,
|
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 14:26:17 +00:00
|
|
|
func (o *Community) insertAndSort(changes *CommunityChanges, oldCategoryID string, categoryID string, chatID string, chat *protobuf.CommunityChat, newPosition int) {
|
2021-08-25 16:52:28 +00:00
|
|
|
// We sort the chats here because maps are not guaranteed to keep order
|
2021-05-23 13:34:17 +00:00
|
|
|
var catChats []string
|
2021-08-25 16:52:28 +00:00
|
|
|
sortedChats := make(sortSlice, 0, len(o.config.CommunityDescription.Chats))
|
|
|
|
for k, v := range o.config.CommunityDescription.Chats {
|
|
|
|
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)
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if newPosition > 0 && newPosition >= len(catChats) {
|
|
|
|
newPosition = len(catChats) - 1
|
|
|
|
} else if newPosition < 0 {
|
|
|
|
newPosition = 0
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
decrease := false
|
|
|
|
if chat.Position > int32(newPosition) {
|
|
|
|
decrease = true
|
2021-05-23 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 14:26:17 +00:00
|
|
|
for k, v := range o.config.CommunityDescription.Chats {
|
|
|
|
if k != chatID && newPosition == int(v.Position) && v.CategoryId == categoryID {
|
|
|
|
if oldCategoryID == categoryID {
|
|
|
|
if decrease {
|
|
|
|
v.Position++
|
|
|
|
} else {
|
|
|
|
v.Position--
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.Position++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
idx := -1
|
|
|
|
currChatID := ""
|
|
|
|
var sortedChatIDs []string
|
|
|
|
for i, k := range catChats {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 13:34:17 +00:00
|
|
|
|
2021-08-25 16:52:28 +00:00
|
|
|
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
|
2021-05-23 13:34:17 +00:00
|
|
|
} else {
|
2021-08-25 16:52:28 +00:00
|
|
|
changes.ChatsModified[sortedChatID] = &CommunityChatChanges{
|
2021-05-23 13:34:17 +00:00
|
|
|
MembersAdded: make(map[string]*protobuf.CommunityMember),
|
|
|
|
MembersRemoved: make(map[string]*protobuf.CommunityMember),
|
|
|
|
PositionModified: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) getCategoryChatCount(categoryID string) int {
|
|
|
|
result := 0
|
|
|
|
for _, chat := range o.config.CommunityDescription.Chats {
|
|
|
|
if chat.CategoryId == categoryID {
|
|
|
|
result = result + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Community) DeleteCategory(categoryID string) (*CommunityChanges, error) {
|
|
|
|
o.mutex.Lock()
|
|
|
|
defer o.mutex.Unlock()
|
|
|
|
|
|
|
|
if o.config.PrivateKey == nil {
|
|
|
|
return nil, ErrNotAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := o.config.CommunityDescription.Categories[categoryID]; !exists {
|
|
|
|
return nil, ErrCategoryNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := o.emptyCommunityChanges()
|
|
|
|
|
|
|
|
emptyCategoryChatCount := o.getCategoryChatCount("")
|
|
|
|
i := 0
|
|
|
|
for _, chat := range o.config.CommunityDescription.Chats {
|
|
|
|
if chat.CategoryId == categoryID {
|
|
|
|
i++
|
|
|
|
chat.CategoryId = ""
|
|
|
|
chat.Position = int32(emptyCategoryChatCount + i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o.SortCategoryChats(changes, "")
|
|
|
|
|
|
|
|
delete(o.config.CommunityDescription.Categories, categoryID)
|
|
|
|
|
|
|
|
changes.CategoriesRemoved = append(changes.CategoriesRemoved, categoryID)
|
|
|
|
|
|
|
|
// Reorder
|
|
|
|
s := make(sortSlice, 0, len(o.config.CommunityDescription.Categories))
|
|
|
|
for _, cat := range o.config.CommunityDescription.Categories {
|
|
|
|
s = append(s, sorterHelperIdx{
|
|
|
|
pos: cat.Position,
|
|
|
|
catID: cat.CategoryId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
o.setModifiedCategories(changes, s)
|
|
|
|
|
|
|
|
o.increaseClock()
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|