Import Group Bundles and Sending Group Messages
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2021-04-22 14:13:15 -07:00
parent 347bb27310
commit 624f56f6f8
4 changed files with 60 additions and 19 deletions

View File

@ -5,8 +5,8 @@ import (
"cwtch.im/cwtch/model" "cwtch.im/cwtch/model"
"cwtch.im/cwtch/peer" "cwtch.im/cwtch/peer"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"git.openprivacy.ca/flutter/libcwtch-go/features"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"strings" "strings"
) )
@ -16,6 +16,8 @@ const tofuBundlePrefix = "tofubundle:"
const groupPrefix = "torv3" const groupPrefix = "torv3"
const groupExperiment = "tapir-groups-experiment" const groupExperiment = "tapir-groups-experiment"
const importBundlePrefix = "importBundle"
const ( const (
// ServerList is a json encoded list of servers // ServerList is a json encoded list of servers
ServerList = event.Field("ServerList") ServerList = event.Field("ServerList")
@ -95,20 +97,28 @@ func (gf *GroupFunctionality) HandleImportString(peer peer.CwtchPeer, importStri
if strings.HasPrefix(importString, tofuBundlePrefix) { if strings.HasPrefix(importString, tofuBundlePrefix) {
bundle := strings.Split(importString, "||") bundle := strings.Split(importString, "||")
gf.HandleImportString(peer, bundle[0][11:]) err := gf.HandleImportString(peer, bundle[0][len(tofuBundlePrefix):])
gf.HandleImportString(peer, bundle[1]) // if the server import failed then abort the whole process..
return nil if strings.HasSuffix(err.Error(), "success") == false {
return features.ConstructResponse(importBundlePrefix, err.Error())
}
return gf.HandleImportString(peer, bundle[1])
} else if strings.HasPrefix(importString, serverPrefix) { } else if strings.HasPrefix(importString, serverPrefix) {
// Server Key Bundles are prefixed with // Server Key Bundles are prefixed with
bundle, err := base64.StdEncoding.DecodeString(importString[7:]) bundle, err := base64.StdEncoding.DecodeString(importString[len(serverPrefix):])
if err == nil { if err == nil {
return peer.AddServer(string(bundle)) if err = peer.AddServer(string(bundle)); err != nil {
return features.ConstructResponse(importBundlePrefix, err.Error())
}
return features.ConstructResponse(importBundlePrefix, "success")
} }
return err return features.ConstructResponse(importBundlePrefix, err.Error())
} else if strings.HasPrefix(importString, groupPrefix) { } else if strings.HasPrefix(importString, groupPrefix) {
//eg: torv3JFDWkXExBsZLkjvfkkuAxHsiLGZBk0bvoeJID9ItYnU=EsEBCiBhOWJhZDU1OTQ0NWI3YmM2N2YxYTM5YjkzMTNmNTczNRIgpHeNaG+6jy750eDhwLO39UX4f2xs0irK/M3P6mDSYQIaOTJjM2ttb29ibnlnaGoyenc2cHd2N2Q1N3l6bGQ3NTNhdW8zdWdhdWV6enB2ZmFrM2FoYzRiZHlkCiJAdVSSVgsksceIfHe41OJu9ZFHO8Kwv3G6F5OK3Hw4qZ6hn6SiZjtmJlJezoBH0voZlCahOU7jCOg+dsENndZxAA== //eg: torv3JFDWkXExBsZLkjvfkkuAxHsiLGZBk0bvoeJID9ItYnU=EsEBCiBhOWJhZDU1OTQ0NWI3YmM2N2YxYTM5YjkzMTNmNTczNRIgpHeNaG+6jy750eDhwLO39UX4f2xs0irK/M3P6mDSYQIaOTJjM2ttb29ibnlnaGoyenc2cHd2N2Q1N3l6bGQ3NTNhdW8zdWdhdWV6enB2ZmFrM2FoYzRiZHlkCiJAdVSSVgsksceIfHe41OJu9ZFHO8Kwv3G6F5OK3Hw4qZ6hn6SiZjtmJlJezoBH0voZlCahOU7jCOg+dsENndZxAA==
return peer.ImportGroup(importString) if err := peer.ImportGroup(importString); err != nil {
return features.ConstructResponse(importBundlePrefix, err.Error())
}
return features.ConstructResponse(importBundlePrefix, "success")
} }
return features.ConstructResponse(importBundlePrefix, "invalid_group_invite_prefix")
return errors.New("invalid_group_invite_prefix")
} }

43
lib.go
View File

@ -240,12 +240,6 @@ func SendProfileEvent(onion string, eventJson string) {
if model.Authorization(new_event.Data[event.Authorization]) == model.AuthApproved { if model.Authorization(new_event.Data[event.Authorization]) == model.AuthApproved {
peer.PeerWithOnion(new_event.Data[event.RemotePeer]) peer.PeerWithOnion(new_event.Data[event.RemotePeer])
} }
case event.SendMessageToGroup:
// TODO Uncomment and integrate once contacts/messages are loaded.
//groupHandler,err := groups.ExperimentGate(utils.ReadGlobalSettings().Experiments)
//if err == nil {
// err = groupHandler.SendMessage(peer, new_event.Data[event.GroupID], new_event.Data[event.Data])
//}
default: default:
// rebroadcast catch all // rebroadcast catch all
log.Infof("Received Event %v for %v but no libCwtch handler found, relaying the event directly", new_event, onion) log.Infof("Received Event %v for %v but no libCwtch handler found, relaying the event directly", new_event, onion)
@ -430,7 +424,9 @@ func GetMessage(profileOnion, handle string, message_index int) string {
ph := utils.NewPeerHelper(profile) ph := utils.NewPeerHelper(profile)
var message model.Message var message model.Message
if ph.IsGroup(handle) { if ph.IsGroup(handle) {
message = profile.GetGroup(handle).Timeline.Messages[message_index] if len(profile.GetGroup(handle).Timeline.Messages) > message_index {
message = profile.GetGroup(handle).Timeline.Messages[message_index]
}
} else { } else {
message = profile.GetContact(handle).Timeline.Messages[message_index] message = profile.GetContact(handle).Timeline.Messages[message_index]
} }
@ -459,8 +455,18 @@ func c_SendMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, h
SendMessage(profile, handle, msg) SendMessage(profile, handle, msg)
} }
func SendMessage(profile, handle, msg string) { func SendMessage(profileOnion, handle, msg string) {
application.GetPeer(profile).SendMessageToPeer(handle, msg) profile := application.GetPeer(profileOnion)
ph := utils.NewPeerHelper(profile)
if ph.IsGroup(handle) {
groupHandler, err := groups.ExperimentGate(utils.ReadGlobalSettings().Experiments)
if err == nil {
groupHandler.SendMessage(profile, handle, msg)
}
} else {
contactHandler, _ := contact.FunctionalityGate(utils.ReadGlobalSettings().Experiments)
contactHandler.SendMessage(profile, handle, msg)
}
} }
//export c_ResetTor //export c_ResetTor
@ -496,5 +502,24 @@ func CreateGroup(profile string, server string, name string) {
} }
} }
//export c_ImportBundle
func c_ImportBundle(profile_ptr *C.char, profile_len C.int, bundle_ptr *C.char, bundle_len C.int) {
profile := C.GoStringN(profile_ptr, profile_len)
name := C.GoStringN(bundle_ptr, bundle_len)
ImportBundle(profile, name)
}
// ImportGroupInviteString takes in a handle to a profile and an invite string which could have one of many
// different formats (e.g. a group invite, a server key bundle, or a combination)
// TODO: extend to also handle AddContact flow...
func ImportBundle(profileOnion string, bundle string) {
profile := application.GetPeer(profileOnion)
groupHandler, err := groups.ExperimentGate(utils.ReadGlobalSettings().Experiments)
if err == nil {
err = groupHandler.HandleImportString(profile, bundle)
eventHandler.Push(event.NewEvent(event.AppError, map[event.Field]string{event.Data: err.Error()}))
}
}
// Leave as is, needed by ffi // Leave as is, needed by ffi
func main() {} func main() {}

View File

@ -11,4 +11,5 @@ type Contact struct {
Unread int `json:"numUnread"` Unread int `json:"numUnread"`
LastMessage string `json:"lastMsgTime"` LastMessage string `json:"lastMsgTime"`
IsGroup bool `json:"isGroup"` IsGroup bool `json:"isGroup"`
GroupServer string `json:"groupServer"`
} }

View File

@ -154,11 +154,15 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
}) })
} }
// We compile and send the groups regardless of the experiment flag, and hide them in the UI
for _, groupId := range profile.GetGroups() { for _, groupId := range profile.GetGroups() {
group := profile.GetGroup(groupId) group := profile.GetGroup(groupId)
ph := NewPeerHelper(profile) ph := NewPeerHelper(profile)
cpicPath := ph.GetProfilePic(groupId) cpicPath := ph.GetProfilePic(groupId)
// todo hack for now, auto accept groups
profile.AcceptInvite(groupId)
authorization := model.AuthUnknown authorization := model.AuthUnknown
if group.Accepted { if group.Accepted {
authorization = model.AuthApproved authorization = model.AuthApproved
@ -175,6 +179,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
Unread: 0, Unread: 0,
LastMessage: strconv.Itoa(getLastMessageTime(&group.Timeline)), LastMessage: strconv.Itoa(getLastMessageTime(&group.Timeline)),
IsGroup: true, IsGroup: true,
GroupServer: group.GroupServer,
}) })
} }