Merge pull request 'Update Settings and Server Status on Reconnect' (#9) from reconnect-fixes into trunk

Reviewed-on: cwtch.im/libcwtch-go#9
Reviewed-by: erinn <erinn@openprivacy.ca>
This commit is contained in:
erinn 2021-06-30 13:47:59 -07:00
commit f8eedcae3f
3 changed files with 44 additions and 22 deletions

47
lib.go
View File

@ -70,7 +70,6 @@ func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) int8 {
func StartCwtch(appDir string, torPath string) int { func StartCwtch(appDir string, torPath string) int {
log.SetLevel(log.LevelInfo) log.SetLevel(log.LevelInfo)
log.Infof("StartCwtch(...)") log.Infof("StartCwtch(...)")
// Quick hack check that we're being called with the correct params // Quick hack check that we're being called with the correct params
// On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker // On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker
@ -187,34 +186,47 @@ func ReconnectCwtchForeground() {
eventHandler.Push(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: onion, event.Created: event.False, "Reload": event.True})) eventHandler.Push(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: onion, event.Created: event.False, "Reload": event.True}))
} }
for onion := range peerList { settings := utils.ReadGlobalSettings()
for profileOnion := range peerList {
// fix peerpeercontact message counts // fix peerpeercontact message counts
contactList := application.GetPeer(onion).GetContacts() contactList := application.GetPeer(profileOnion).GetContacts()
for _, handle := range contactList { for _, handle := range contactList {
totalMessages := application.GetPeer(onion).GetContact(handle).Timeline.Len() + len(application.GetPeer(onion).GetContact(handle).UnacknowledgedMessages) totalMessages := application.GetPeer(profileOnion).GetContact(handle).Timeline.Len() + len(application.GetPeer(profileOnion).GetContact(handle).UnacknowledgedMessages)
eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{ eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{
event.Identity: onion, event.Identity: profileOnion,
event.RemotePeer: handle, event.RemotePeer: handle,
event.Data: strconv.Itoa(totalMessages), event.Data: strconv.Itoa(totalMessages),
})) }))
} }
// fix peergroupcontact message counts // Group Experiment Refresh
groupList := application.GetPeer(onion).GetGroups() groupHandler, err := groups.ExperimentGate(settings.Experiments)
for _, groupID := range groupList { if err == nil {
totalMessages := application.GetPeer(onion).GetGroup(groupID).Timeline.Len() + len(application.GetPeer(onion).GetGroup(groupID).UnacknowledgedMessages) // fix peergroupcontact message counts
eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{ groupList := application.GetPeer(profileOnion).GetGroups()
event.Identity: onion, for _, groupID := range groupList {
event.GroupID: groupID, totalMessages := application.GetPeer(profileOnion).GetGroup(groupID).Timeline.Len() + len(application.GetPeer(profileOnion).GetGroup(groupID).UnacknowledgedMessages)
event.Data: strconv.Itoa(totalMessages), eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{
})) event.Identity: profileOnion,
event.GroupID: groupID,
event.Data: strconv.Itoa(totalMessages),
}))
}
serverListForOnion := groupHandler.GetServerInfoList(application.GetPeer(profileOnion))
serversListBytes, _ := json.Marshal(serverListForOnion)
eventHandler.Push(event.NewEvent(groups.UpdateServerInfo, map[event.Field]string{"ProfileOnion": profileOnion, groups.ServerList: string(serversListBytes)}))
} }
} }
settingsJson, _ := json.Marshal(settings)
application.GetPrimaryBus().Publish(event.NewEvent(utils.UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)}))
application.GetPrimaryBus().Publish(event.NewEvent(utils.CwtchStarted, map[event.Field]string{})) application.GetPrimaryBus().Publish(event.NewEvent(utils.CwtchStarted, map[event.Field]string{}))
application.QueryACNStatus() application.QueryACNStatus()
application.QueryACNVersion() application.QueryACNVersion()
} }
//export c_SendAppEvent //export c_SendAppEvent
@ -540,7 +552,6 @@ func GetMessage(profileOnion, handle string, message_index int) string {
return string(bytes) return string(bytes)
} }
//export c_SendMessage //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) { 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) profile := C.GoStringN(profile_ptr, profile_len)

View File

@ -83,7 +83,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
eh.startHandlingPeer(onion) eh.startHandlingPeer(onion)
} }
tag,isTagged := profile.GetAttribute(app.AttributeTag) tag, isTagged := profile.GetAttribute(app.AttributeTag)
if isTagged { if isTagged {
e.Data[app.AttributeTag] = tag e.Data[app.AttributeTag] = tag
} else { } else {
@ -197,10 +197,13 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
authorization = model.AuthApproved authorization = model.AuthApproved
} }
// Use the server state when assessing group state
state := profile.GetContact(group.GroupServer).State
contacts = append(contacts, Contact{ contacts = append(contacts, Contact{
Name: ph.GetNick(groupId), Name: ph.GetNick(groupId),
Onion: group.GroupID, Onion: group.GroupID,
Status: group.State, Status: state,
Picture: cpicPath, Picture: cpicPath,
Authorization: string(authorization), Authorization: string(authorization),
SaveHistory: event.SaveHistoryConfirmed, SaveHistory: event.SaveHistoryConfirmed,

View File

@ -3,6 +3,7 @@ package utils
import ( import (
"cwtch.im/cwtch/event" "cwtch.im/cwtch/event"
"cwtch.im/cwtch/storage/v1" "cwtch.im/cwtch/storage/v1"
"sync"
"encoding/json" "encoding/json"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
@ -18,6 +19,7 @@ const (
) )
var GlobalSettingsFile v1.FileStore var GlobalSettingsFile v1.FileStore
var lock sync.Mutex
const GlobalSettingsFilename = "ui.globals" const GlobalSettingsFilename = "ui.globals"
const saltFile = "SALT" const saltFile = "SALT"
@ -44,11 +46,13 @@ var DefaultGlobalSettings = GlobalSettings{
StateRootPane: 0, StateRootPane: 0,
FirstTime: true, FirstTime: true,
BlockUnknownConnections: false, BlockUnknownConnections: false,
UIColumnModePortrait: "DualpaneMode.Single", UIColumnModePortrait: "DualpaneMode.Single",
UIColumnModeLandscape: "DualpaneMode.CopyPortrait", UIColumnModeLandscape: "DualpaneMode.CopyPortrait",
} }
func InitGlobalSettingsFile(directory string, password string) error { func InitGlobalSettingsFile(directory string, password string) error {
lock.Lock()
defer lock.Unlock()
var key [32]byte var key [32]byte
salt, err := ioutil.ReadFile(path.Join(directory, saltFile)) salt, err := ioutil.ReadFile(path.Join(directory, saltFile))
if err != nil { if err != nil {
@ -75,6 +79,8 @@ func InitGlobalSettingsFile(directory string, password string) error {
} }
func ReadGlobalSettings() *GlobalSettings { func ReadGlobalSettings() *GlobalSettings {
lock.Lock()
defer lock.Unlock()
settings := DefaultGlobalSettings settings := DefaultGlobalSettings
if GlobalSettingsFile == nil { if GlobalSettingsFile == nil {
@ -100,6 +106,8 @@ func ReadGlobalSettings() *GlobalSettings {
} }
func WriteGlobalSettings(globalSettings GlobalSettings) { func WriteGlobalSettings(globalSettings GlobalSettings) {
lock.Lock()
defer lock.Unlock()
bytes, _ := json.Marshal(globalSettings) bytes, _ := json.Marshal(globalSettings)
// override first time setting // override first time setting
globalSettings.FirstTime = true globalSettings.FirstTime = true