Merge pull request 'approve contacts, send messages' (#20) from sendagain into trunk
continuous-integration/drone/push Build is passing Details

Reviewed-on: #20
This commit is contained in:
Sarah Jamie Lewis 2021-04-09 19:51:48 -07:00
commit b3cbd5905b
4 changed files with 70 additions and 11 deletions

2
go.mod
View File

@ -6,4 +6,4 @@ require (
cwtch.im/cwtch v0.6.4
git.openprivacy.ca/openprivacy/connectivity v1.3.3
git.openprivacy.ca/openprivacy/log v1.0.2
)
)

70
lib.go
View File

@ -309,7 +309,6 @@ func c_SelectProfile(onion_ptr *C.char, onion_len C.int) *C.char {
}
func SelectProfile(onion string) string {
log.Infof("Select Profile: %v", onion)
contactEventsQueue = event.NewQueue()
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
return ""
@ -338,6 +337,61 @@ func ContactEvents() string {
}
}
//export c_AcceptContact
func c_AcceptContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) {
AcceptContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen))
}
func AcceptContact(profile, handle string) {
err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthApproved)
if err == nil {
eventHandler.Push(event.NewEvent(event.PeerStateChange, map[event.Field]string{
//application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{
"ProfileOnion": profile,
event.RemotePeer: handle,
"authorization": string(model.AuthApproved),
}))
} else {
log.Errorf("error accepting contact: %s", err.Error())
}
}
//export c_BlockContact
func c_BlockContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) {
BlockContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen))
}
func BlockContact(profile, handle string) {
err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthBlocked)
if err == nil {
application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{
"ProfileOnion": profile,
event.RemotePeer: handle,
"authorization": string(model.AuthBlocked),
}))
} else {
log.Errorf("error blocking contact: %s", err.Error())
}
}
//export c_DebugResetContact
func c_DebugResetContact(profilePtr *C.char, profileLen C.int, handlePtr *C.char, handleLen C.int) {
DebugResetContact(C.GoStringN(profilePtr, profileLen), C.GoStringN(handlePtr, handleLen))
}
func DebugResetContact(profile, handle string) {
err := application.GetPeer(profile).SetContactAuthorization(handle, model.AuthUnknown)
if err == nil {
application.GetPrimaryBus().Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{
"ProfileOnion": profile,
event.RemotePeer: handle,
"authorization": string(model.AuthUnknown),
}))
} else {
log.Errorf("error resetting contact: %s", err.Error())
}
}
//export c_NumMessages
func c_NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n int) {
profile := C.GoStringN(profile_ptr, profile_len)
@ -347,7 +401,6 @@ func c_NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, h
func NumMessages(profile, handle string) (n int) {
n = len(application.GetPeer(profile).GetContact(handle).Timeline.Messages)
//log.Infof("NumMessagse(%s, %s) = %d", profile, handle, n)
return
}
@ -362,7 +415,6 @@ func c_GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, ha
func GetMessage(profile, handle string, message_index int) string {
message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index]
bytes, _ := json.Marshal(message)
log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes))
return string(bytes)
}
@ -379,5 +431,17 @@ func GetMessages(profile, handle string, start, end int) string {
return string(bytes)
}
//export c_SendMessage
func c_SendMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, msg_ptr *C.char, msg_len C.int) {
profile := C.GoStringN(profile_ptr, profile_len)
handle := C.GoStringN(handle_ptr, handle_len)
msg := C.GoStringN(msg_ptr, msg_len)
SendMessage(profile, handle, msg)
}
func SendMessage(profile, handle, msg string) {
application.GetPeer(profile).SendMessageToPeer(handle, msg)
}
// Leave as is, needed by ffi
func main() {}

View File

@ -201,14 +201,8 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
contact := peer.GetContact(ev.Event.Data[event.RemotePeer])
if cxnState == connections.AUTHENTICATED && contact == nil {
// Contact does not exist, change event to NewPeer
peer.AddContact(ev.Event.Data[event.RemotePeer], ev.Event.Data[event.RemotePeer], model.AuthUnknown)
contact = peer.GetContact(ev.Event.Data[event.RemotePeer])
ev.Event.EventType = event.PeerCreated
err := EnrichNewPeer(ev.Event.Data[event.RemotePeer], ph, ev)
if err != nil {
return ""
}
return ""
}
if contact != nil {

View File

@ -264,6 +264,7 @@ func EnrichNewPeer(handle string, ph *PeerHelper, ev *EventProfileEnvelope) erro
if contact != nil {
lastRead := ph.InitLastReadTime(contact.Onion)
ev.Event.Data["unread"] = strconv.Itoa(ph.CountUnread(contact.Timeline.GetMessages(), lastRead))
ev.Event.Data["numMessages"] = strconv.Itoa(contact.Timeline.Len())
ev.Event.Data["picture"] = ph.GetProfilePic(handle)
ev.Event.Data["nick"] = ph.GetNick(handle)