2023-08-17 17:14:23 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2024-06-07 16:30:01 +00:00
|
|
|
"slices"
|
2023-08-17 17:14:23 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TokenPermissionState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
TokenPermissionApproved TokenPermissionState = iota
|
|
|
|
TokenPermissionAdditionPending
|
|
|
|
TokenPermissionUpdatePending
|
|
|
|
TokenPermissionRemovalPending
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommunityTokenPermission struct {
|
|
|
|
*protobuf.CommunityTokenPermission
|
|
|
|
State TokenPermissionState `json:"state,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCommunityTokenPermission(base *protobuf.CommunityTokenPermission) *CommunityTokenPermission {
|
|
|
|
return &CommunityTokenPermission{
|
|
|
|
CommunityTokenPermission: base,
|
|
|
|
State: TokenPermissionApproved,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommunityTokenPermission) Equals(other *CommunityTokenPermission) bool {
|
|
|
|
if p.Id != other.Id ||
|
|
|
|
p.Type != other.Type ||
|
|
|
|
len(p.TokenCriteria) != len(other.TokenCriteria) ||
|
|
|
|
len(p.ChatIds) != len(other.ChatIds) ||
|
|
|
|
p.IsPrivate != other.IsPrivate ||
|
|
|
|
p.State != other.State {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range p.TokenCriteria {
|
|
|
|
if !compareTokenCriteria(p.TokenCriteria[i], other.TokenCriteria[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.DeepEqual(p.ChatIds, other.ChatIds)
|
|
|
|
}
|
|
|
|
|
2024-06-07 16:30:01 +00:00
|
|
|
func (p *CommunityTokenPermission) HasChat(chatId string) bool {
|
|
|
|
return slices.Contains(p.ChatIds, chatId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CommunityTokenPermission) ChatIdsAsMap() map[string]struct{} {
|
|
|
|
chats := map[string]struct{}{}
|
|
|
|
for _, id := range p.GetChatIds() {
|
|
|
|
chats[id] = struct{}{}
|
|
|
|
}
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
2023-08-17 17:14:23 +00:00
|
|
|
func compareTokenCriteria(a, b *protobuf.TokenCriteria) bool {
|
|
|
|
if a == nil && b == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Type == b.Type &&
|
|
|
|
a.Symbol == b.Symbol &&
|
|
|
|
a.Name == b.Name &&
|
|
|
|
a.Amount == b.Amount &&
|
|
|
|
a.EnsPattern == b.EnsPattern &&
|
|
|
|
a.Decimals == b.Decimals &&
|
|
|
|
reflect.DeepEqual(a.ContractAddresses, b.ContractAddresses) &&
|
|
|
|
reflect.DeepEqual(a.TokenIds, b.TokenIds)
|
|
|
|
}
|