2021-11-09 23:47:33 +00:00
|
|
|
package model
|
|
|
|
|
2021-11-17 22:34:13 +00:00
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/model/attr"
|
|
|
|
"cwtch.im/cwtch/model/constants"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
2021-11-09 23:47:33 +00:00
|
|
|
|
|
|
|
// AccessControl is a type determining client assigned authorization to a peer
|
|
|
|
type AccessControl struct {
|
|
|
|
Blocked bool // Any attempts from this handle to connect are blocked
|
|
|
|
Read bool // Allows a handle to access the conversation
|
|
|
|
Append bool // Allows a handle to append new messages to the conversation
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultP2PAccessControl - because in the year 2021, go does not support constant structs...
|
|
|
|
func DefaultP2PAccessControl() AccessControl {
|
|
|
|
return AccessControl{Read: true, Append: true, Blocked: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccessControlList represents an access control list for a conversation. Mapping handles to conversation
|
|
|
|
// functions
|
|
|
|
type AccessControlList map[string]AccessControl
|
|
|
|
|
2021-11-11 00:41:43 +00:00
|
|
|
// Serialize transforms the ACL into json.
|
2021-11-09 23:47:33 +00:00
|
|
|
func (acl *AccessControlList) Serialize() []byte {
|
|
|
|
data, _ := json.Marshal(acl)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:41:43 +00:00
|
|
|
// DeserializeAccessControlList takes in JSON and returns an AccessControlList
|
2021-11-09 23:47:33 +00:00
|
|
|
func DeserializeAccessControlList(data []byte) AccessControlList {
|
|
|
|
var acl AccessControlList
|
|
|
|
json.Unmarshal(data, &acl)
|
|
|
|
return acl
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:41:43 +00:00
|
|
|
// Attributes a type-driven encapsulation of an Attribute map.
|
2021-11-09 23:47:33 +00:00
|
|
|
type Attributes map[string]string
|
|
|
|
|
2021-11-11 00:41:43 +00:00
|
|
|
// Serialize transforms an Attributes map into a JSON struct
|
2021-11-09 23:47:33 +00:00
|
|
|
func (a *Attributes) Serialize() []byte {
|
|
|
|
data, _ := json.Marshal(a)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:41:43 +00:00
|
|
|
// DeserializeAttributes convers a JSON struct into an Attributes map
|
2021-11-09 23:47:33 +00:00
|
|
|
func DeserializeAttributes(data []byte) Attributes {
|
|
|
|
var attributes Attributes
|
|
|
|
json.Unmarshal(data, &attributes)
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversation encapsulates high-level information about a conversation, including the
|
|
|
|
// handle, any set attributes, the access control list associated with the message tree and the
|
|
|
|
// accepted status of the conversation (whether the user has consented into the conversation).
|
|
|
|
type Conversation struct {
|
|
|
|
ID int
|
|
|
|
Handle string
|
|
|
|
Attributes Attributes
|
|
|
|
ACL AccessControlList
|
|
|
|
Accepted bool
|
|
|
|
}
|
2021-11-17 22:34:13 +00:00
|
|
|
|
|
|
|
func (ci *Conversation) GetAttribute(scope attr.Scope, zone attr.Zone, key string) (string, bool) {
|
|
|
|
if value, exists := ci.Attributes[scope.ConstructScopedZonedPath(zone.ConstructZonedPath(key)).ToString()]; exists {
|
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *Conversation) IsGroup() bool {
|
|
|
|
if _, exists := ci.Attributes[attr.LocalScope.ConstructScopedZonedPath(attr.LegacyGroupZone.ConstructZonedPath(constants.GroupID)).ToString()]; exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *Conversation) IsServer() bool {
|
|
|
|
if _, exists := ci.Attributes[attr.PublicScope.ConstructScopedZonedPath(attr.ServerKeyZone.ConstructZonedPath(string(BundleType))).ToString()]; exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConversationMessage struct {
|
|
|
|
ID int
|
|
|
|
Body string
|
|
|
|
Attr Attributes
|
|
|
|
Signature string
|
|
|
|
ContentHash string
|
|
|
|
}
|