2023-07-04 13:48:52 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
"github.com/status-im/status-go/api/multiformat"
|
2023-07-04 13:48:52 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2023-11-15 15:58:15 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common/shard"
|
2023-07-04 13:48:52 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
|
|
|
"github.com/status-im/status-go/protocol/urls"
|
2023-10-25 16:49:18 +00:00
|
|
|
"github.com/status-im/status-go/services/utils"
|
2023-07-04 13:48:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CommunityURLData struct {
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
MembersCount uint32 `json:"membersCount"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
TagIndices []uint32 `json:"tagIndices"`
|
2023-07-26 12:14:06 +00:00
|
|
|
CommunityID string `json:"communityId"`
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommunityChannelURLData struct {
|
|
|
|
Emoji string `json:"emoji"`
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Color string `json:"color"`
|
2023-07-26 12:14:06 +00:00
|
|
|
ChannelUUID string `json:"channelUuid"`
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContactURLData struct {
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
Description string `json:"description"`
|
2023-07-26 12:14:06 +00:00
|
|
|
PublicKey string `json:"publicKey"`
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type URLDataResponse struct {
|
2023-10-13 12:25:34 +00:00
|
|
|
Community *CommunityURLData `json:"community"`
|
|
|
|
Channel *CommunityChannelURLData `json:"channel"`
|
|
|
|
Contact *ContactURLData `json:"contact"`
|
2023-11-15 15:58:15 +00:00
|
|
|
Shard *shard.Shard `json:"shard,omitempty"`
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const baseShareURL = "https://status.app"
|
2023-11-10 15:00:03 +00:00
|
|
|
const userPath = "u#"
|
|
|
|
const userWithDataPath = "u/"
|
|
|
|
const communityPath = "c#"
|
|
|
|
const communityWithDataPath = "c/"
|
|
|
|
const channelPath = "cc/"
|
|
|
|
|
|
|
|
const sharedURLUserPrefix = baseShareURL + "/" + userPath
|
|
|
|
const sharedURLUserPrefixWithData = baseShareURL + "/" + userWithDataPath
|
|
|
|
const sharedURLCommunityPrefix = baseShareURL + "/" + communityPath
|
|
|
|
const sharedURLCommunityPrefixWithData = baseShareURL + "/" + communityWithDataPath
|
|
|
|
const sharedURLChannelPrefixWithData = baseShareURL + "/" + channelPath
|
|
|
|
|
2023-07-04 13:48:52 +00:00
|
|
|
const channelUUIDRegExp = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$"
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
var channelRegExp = regexp.MustCompile(channelUUIDRegExp)
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func decodeCommunityID(serialisedPublicKey string) (string, error) {
|
|
|
|
deserializedCommunityID, err := multiformat.DeserializeCompressedKey(serialisedPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
communityID, err := common.HexToPubkey(deserializedCommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.EncodeHex(crypto.CompressPubkey(communityID)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializePublicKey(compressedKey types.HexBytes) (string, error) {
|
2023-11-02 05:56:06 +00:00
|
|
|
return utils.SerializePublicKey(compressedKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func deserializePublicKey(compressedKey string) (types.HexBytes, error) {
|
2023-10-25 16:49:18 +00:00
|
|
|
return utils.DeserializePublicKey(compressedKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareCommunityURLWithChatKey(communityID types.HexBytes) (string, error) {
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(communityID)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/c#%s", baseShareURL, shortKey), nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseCommunityURLWithChatKey(urlData string) (*URLDataResponse, error) {
|
|
|
|
communityID, err := decodeCommunityID(urlData)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &URLDataResponse{
|
2023-11-10 16:33:37 +00:00
|
|
|
Community: &CommunityURLData{
|
|
|
|
CommunityID: communityID,
|
|
|
|
TagIndices: []uint32{},
|
|
|
|
},
|
|
|
|
Shard: nil,
|
2023-07-04 13:48:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) prepareEncodedCommunityData(community *communities.Community) (string, string, error) {
|
|
|
|
communityProto := &protobuf.Community{
|
|
|
|
DisplayName: community.Identity().DisplayName,
|
|
|
|
Description: community.DescriptionText(),
|
|
|
|
MembersCount: uint32(community.MembersCount()),
|
|
|
|
Color: community.Identity().GetColor(),
|
|
|
|
TagIndices: community.TagsIndices(),
|
|
|
|
}
|
|
|
|
|
|
|
|
communityData, err := proto.Marshal(communityProto)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlDataProto := &protobuf.URLData{
|
|
|
|
Content: communityData,
|
2023-10-12 19:21:49 +00:00
|
|
|
Shard: community.Shard().Protobuffer(),
|
2023-08-14 10:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
urlData, err := proto.Marshal(urlDataProto)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(community.ID())
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2023-08-14 10:25:42 +00:00
|
|
|
|
|
|
|
encodedData, err := urls.EncodeDataURL(urlData)
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodedData, shortKey, nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (string, error) {
|
|
|
|
community, err := m.GetCommunityByID(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if community == nil {
|
|
|
|
return "", fmt.Errorf("community with communityID %s not found", communityID)
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
data, shortKey, err := m.prepareEncodedCommunityData(community)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
return fmt.Sprintf("%s/c/%s#%s", baseShareURL, data, shortKey), nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseCommunityURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
|
|
|
communityID, err := deserializePublicKey(chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlData, err := urls.DecodeDataURL(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlDataProto protobuf.URLData
|
|
|
|
err = proto.Unmarshal(urlData, &urlDataProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var communityProto protobuf.Community
|
2023-08-14 10:25:42 +00:00
|
|
|
err = proto.Unmarshal(urlDataProto.Content, &communityProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-08 08:13:36 +00:00
|
|
|
var tagIndices []uint32
|
|
|
|
if communityProto.TagIndices != nil {
|
|
|
|
tagIndices = communityProto.TagIndices
|
|
|
|
} else {
|
|
|
|
tagIndices = []uint32{}
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:48:52 +00:00
|
|
|
return &URLDataResponse{
|
2023-10-13 12:25:34 +00:00
|
|
|
Community: &CommunityURLData{
|
2023-07-04 13:48:52 +00:00
|
|
|
DisplayName: communityProto.DisplayName,
|
|
|
|
Description: communityProto.Description,
|
|
|
|
MembersCount: communityProto.MembersCount,
|
|
|
|
Color: communityProto.Color,
|
2023-12-08 08:13:36 +00:00
|
|
|
TagIndices: tagIndices,
|
2023-07-26 12:14:06 +00:00
|
|
|
CommunityID: types.EncodeHex(communityID),
|
2023-07-04 13:48:52 +00:00
|
|
|
},
|
2023-11-15 15:58:15 +00:00
|
|
|
Shard: shard.FromProtobuff(urlDataProto.Shard),
|
2023-07-04 13:48:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareCommunityChannelURLWithChatKey(request *requests.CommunityChannelShareURL) (string, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(request.CommunityID)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := regexp.MatchString(channelUUIDRegExp, request.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return "", fmt.Errorf("channelID should be UUID, got %s", request.ChannelID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, request.ChannelID, shortKey), nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseCommunityChannelURLWithChatKey(channelID string, publicKey string) (*URLDataResponse, error) {
|
2023-07-04 13:48:52 +00:00
|
|
|
valid, err := regexp.MatchString(channelUUIDRegExp, channelID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return nil, fmt.Errorf("channelID should be UUID, got %s", channelID)
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
communityID, err := decodeCommunityID(publicKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &URLDataResponse{
|
2023-11-10 16:33:37 +00:00
|
|
|
Community: &CommunityURLData{
|
|
|
|
CommunityID: communityID,
|
|
|
|
TagIndices: []uint32{},
|
|
|
|
},
|
|
|
|
Channel: &CommunityChannelURLData{
|
|
|
|
ChannelUUID: channelID,
|
|
|
|
},
|
|
|
|
Shard: nil,
|
2023-07-04 13:48:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) prepareEncodedCommunityChannelData(community *communities.Community, channel *protobuf.CommunityChat, channelID string) (string, string, error) {
|
|
|
|
communityProto := &protobuf.Community{
|
|
|
|
DisplayName: community.Identity().DisplayName,
|
|
|
|
Description: community.DescriptionText(),
|
|
|
|
MembersCount: uint32(community.MembersCount()),
|
|
|
|
Color: community.Identity().GetColor(),
|
|
|
|
TagIndices: community.TagsIndices(),
|
|
|
|
}
|
|
|
|
|
|
|
|
channelProto := &protobuf.Channel{
|
|
|
|
DisplayName: channel.Identity.DisplayName,
|
|
|
|
Description: channel.Identity.Description,
|
|
|
|
Emoji: channel.Identity.Emoji,
|
|
|
|
Color: channel.GetIdentity().Color,
|
|
|
|
Community: communityProto,
|
|
|
|
Uuid: channelID,
|
|
|
|
}
|
|
|
|
|
|
|
|
channelData, err := proto.Marshal(channelProto)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlDataProto := &protobuf.URLData{
|
|
|
|
Content: channelData,
|
2023-10-12 19:21:49 +00:00
|
|
|
Shard: community.Shard().Protobuffer(),
|
2023-08-14 10:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
urlData, err := proto.Marshal(urlDataProto)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(community.ID())
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2023-08-14 10:25:42 +00:00
|
|
|
encodedData, err := urls.EncodeDataURL(urlData)
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodedData, shortKey, nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareCommunityChannelURLWithData(request *requests.CommunityChannelShareURL) (string, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := regexp.MatchString(channelUUIDRegExp, request.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return "nil", fmt.Errorf("channelID should be UUID, got %s", request.ChannelID)
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := m.GetCommunityByID(request.CommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
channel := community.Chats()[request.ChannelID]
|
|
|
|
if channel == nil {
|
|
|
|
return "", fmt.Errorf("channel with channelID %s not found", request.ChannelID)
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
data, shortKey, err := m.prepareEncodedCommunityChannelData(community, channel, request.ChannelID)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, data, shortKey), nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseCommunityChannelURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
|
|
|
communityID, err := deserializePublicKey(chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlData, err := urls.DecodeDataURL(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlDataProto protobuf.URLData
|
|
|
|
err = proto.Unmarshal(urlData, &urlDataProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var channelProto protobuf.Channel
|
2023-08-14 10:25:42 +00:00
|
|
|
err = proto.Unmarshal(urlDataProto.Content, &channelProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-08 08:13:36 +00:00
|
|
|
var tagIndices []uint32
|
|
|
|
if channelProto.Community.TagIndices != nil {
|
|
|
|
tagIndices = channelProto.Community.TagIndices
|
|
|
|
} else {
|
|
|
|
tagIndices = []uint32{}
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:48:52 +00:00
|
|
|
return &URLDataResponse{
|
2023-10-13 12:25:34 +00:00
|
|
|
Community: &CommunityURLData{
|
2023-07-04 13:48:52 +00:00
|
|
|
DisplayName: channelProto.Community.DisplayName,
|
|
|
|
Description: channelProto.Community.Description,
|
|
|
|
MembersCount: channelProto.Community.MembersCount,
|
|
|
|
Color: channelProto.Community.Color,
|
2023-12-08 08:13:36 +00:00
|
|
|
TagIndices: tagIndices,
|
2023-07-26 12:14:06 +00:00
|
|
|
CommunityID: types.EncodeHex(communityID),
|
2023-07-04 13:48:52 +00:00
|
|
|
},
|
2023-10-13 12:25:34 +00:00
|
|
|
Channel: &CommunityChannelURLData{
|
2023-07-04 13:48:52 +00:00
|
|
|
Emoji: channelProto.Emoji,
|
|
|
|
DisplayName: channelProto.DisplayName,
|
|
|
|
Description: channelProto.Description,
|
|
|
|
Color: channelProto.Color,
|
2023-07-26 12:14:06 +00:00
|
|
|
ChannelUUID: channelProto.Uuid,
|
2023-07-04 13:48:52 +00:00
|
|
|
},
|
2023-11-15 15:58:15 +00:00
|
|
|
Shard: shard.FromProtobuff(urlDataProto.Shard),
|
2023-07-04 13:48:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareUserURLWithChatKey(contactID string) (string, error) {
|
|
|
|
publicKey, err := common.HexToPubkey(contactID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(crypto.CompressPubkey(publicKey))
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/u#%s", baseShareURL, shortKey), nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseUserURLWithChatKey(urlData string) (*URLDataResponse, error) {
|
|
|
|
pubKeyBytes, err := deserializePublicKey(urlData)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := crypto.DecompressPubkey(pubKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
serializedPublicKey, err := multiformat.SerializeLegacyKey(common.PubkeyToHex(pubKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &URLDataResponse{
|
2023-11-10 16:33:37 +00:00
|
|
|
Contact: &ContactURLData{
|
|
|
|
PublicKey: serializedPublicKey,
|
|
|
|
},
|
2023-07-04 13:48:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareUserURLWithENS(contactID string) (string, error) {
|
2023-10-24 10:15:32 +00:00
|
|
|
contact := m.GetContactByID(contactID)
|
|
|
|
if contact == nil {
|
2023-07-04 13:48:52 +00:00
|
|
|
return "", ErrContactNotFound
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/u#%s", baseShareURL, contact.EnsName), nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseUserURLWithENS(ensName string) (*URLDataResponse, error) {
|
2023-07-04 13:48:52 +00:00
|
|
|
// TODO: fetch contact by ens name
|
|
|
|
return nil, fmt.Errorf("not implemented yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) prepareEncodedUserData(contact *Contact) (string, string, error) {
|
2023-08-14 10:25:42 +00:00
|
|
|
pk, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
shortKey, err := serializePublicKey(crypto.CompressPubkey(pk))
|
2023-08-14 10:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-07-04 13:48:52 +00:00
|
|
|
userProto := &protobuf.User{
|
|
|
|
DisplayName: contact.DisplayName,
|
2023-10-13 12:25:34 +00:00
|
|
|
Description: contact.Bio,
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userData, err := proto.Marshal(userProto)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlDataProto := &protobuf.URLData{
|
|
|
|
Content: userData,
|
2023-07-26 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
urlData, err := proto.Marshal(urlDataProto)
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-08-14 10:25:42 +00:00
|
|
|
encodedData, err := urls.EncodeDataURL(urlData)
|
2023-07-26 12:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodedData, shortKey, nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareUserURLWithData(contactID string) (string, error) {
|
2023-10-24 10:15:32 +00:00
|
|
|
contact := m.GetContactByID(contactID)
|
|
|
|
if contact == nil {
|
2023-07-04 13:48:52 +00:00
|
|
|
return "", ErrContactNotFound
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
data, shortKey, err := m.prepareEncodedUserData(contact)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-07-26 12:14:06 +00:00
|
|
|
return fmt.Sprintf("%s/u/%s#%s", baseShareURL, data, shortKey), nil
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func parseUserURLWithData(data string, chatKey string) (*URLDataResponse, error) {
|
2023-08-14 10:25:42 +00:00
|
|
|
urlData, err := urls.DecodeDataURL(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlDataProto protobuf.URLData
|
|
|
|
err = proto.Unmarshal(urlData, &urlDataProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var userProto protobuf.User
|
2023-08-14 10:25:42 +00:00
|
|
|
err = proto.Unmarshal(urlDataProto.Content, &userProto)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &URLDataResponse{
|
2023-10-13 12:25:34 +00:00
|
|
|
Contact: &ContactURLData{
|
2023-07-04 13:48:52 +00:00
|
|
|
DisplayName: userProto.DisplayName,
|
|
|
|
Description: userProto.Description,
|
2023-07-26 12:14:06 +00:00
|
|
|
PublicKey: chatKey,
|
2023-07-04 13:48:52 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
func IsStatusSharedURL(url string) bool {
|
|
|
|
return strings.HasPrefix(url, sharedURLUserPrefix) ||
|
|
|
|
strings.HasPrefix(url, sharedURLUserPrefixWithData) ||
|
|
|
|
strings.HasPrefix(url, sharedURLCommunityPrefix) ||
|
|
|
|
strings.HasPrefix(url, sharedURLCommunityPrefixWithData) ||
|
|
|
|
strings.HasPrefix(url, sharedURLChannelPrefixWithData)
|
2023-10-13 12:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
func splitSharedURLData(data string) (string, string, error) {
|
|
|
|
const count = 2
|
|
|
|
contents := strings.SplitN(data, "#", count)
|
|
|
|
if len(contents) != count {
|
|
|
|
return "", "", fmt.Errorf("url should contain at least one `#` separator")
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
2023-11-10 15:00:03 +00:00
|
|
|
return contents[0], contents[1], nil
|
|
|
|
}
|
2023-07-04 13:48:52 +00:00
|
|
|
|
2023-11-10 16:33:37 +00:00
|
|
|
func ParseSharedURL(url string) (*URLDataResponse, error) {
|
2023-07-04 13:48:52 +00:00
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if strings.HasPrefix(url, sharedURLUserPrefix) {
|
|
|
|
chatKey := strings.TrimPrefix(url, sharedURLUserPrefix)
|
|
|
|
if strings.HasPrefix(chatKey, "zQ3sh") {
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseUserURLWithChatKey(chatKey)
|
2023-11-10 15:00:03 +00:00
|
|
|
}
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseUserURLWithENS(chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if strings.HasPrefix(url, sharedURLUserPrefixWithData) {
|
|
|
|
trimmedURL := strings.TrimPrefix(url, sharedURLUserPrefixWithData)
|
|
|
|
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseUserURLWithData(encodedData, chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if strings.HasPrefix(url, sharedURLCommunityPrefix) {
|
|
|
|
chatKey := strings.TrimPrefix(url, sharedURLCommunityPrefix)
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseCommunityURLWithChatKey(chatKey)
|
2023-11-10 15:00:03 +00:00
|
|
|
}
|
2023-07-04 13:48:52 +00:00
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if strings.HasPrefix(url, sharedURLCommunityPrefixWithData) {
|
|
|
|
trimmedURL := strings.TrimPrefix(url, sharedURLCommunityPrefixWithData)
|
|
|
|
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
|
2023-07-04 13:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseCommunityURLWithData(encodedData, chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if strings.HasPrefix(url, sharedURLChannelPrefixWithData) {
|
|
|
|
trimmedURL := strings.TrimPrefix(url, sharedURLChannelPrefixWithData)
|
|
|
|
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
if channelRegExp.MatchString(encodedData) {
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseCommunityChannelURLWithChatKey(encodedData, chatKey)
|
2023-11-10 15:00:03 +00:00
|
|
|
}
|
2023-11-10 16:33:37 +00:00
|
|
|
return parseCommunityChannelURLWithData(encodedData, chatKey)
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:00:03 +00:00
|
|
|
return nil, fmt.Errorf("not a status shared url")
|
2023-07-04 13:48:52 +00:00
|
|
|
}
|