From 043774223ab6fd140916f70145c81f1ab5da42b2 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 30 Jun 2021 13:28:16 -0700 Subject: [PATCH] Update Settings and Server Status on Reconnect --- lib.go | 47 ++++++++++++++++++++++++++----------------- utils/eventHandler.go | 7 +++++-- utils/settings.go | 12 +++++++++-- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/lib.go b/lib.go index 0db1a47..4b2bcb3 100644 --- a/lib.go +++ b/lib.go @@ -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 { log.SetLevel(log.LevelInfo) - log.Infof("StartCwtch(...)") // 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 @@ -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})) } - for onion := range peerList { + settings := utils.ReadGlobalSettings() + + for profileOnion := range peerList { // fix peerpeercontact message counts - contactList := application.GetPeer(onion).GetContacts() + contactList := application.GetPeer(profileOnion).GetContacts() 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{ - event.Identity: onion, + event.Identity: profileOnion, event.RemotePeer: handle, - event.Data: strconv.Itoa(totalMessages), + event.Data: strconv.Itoa(totalMessages), })) } - - // fix peergroupcontact message counts - groupList := application.GetPeer(onion).GetGroups() - for _, groupID := range groupList { - totalMessages := application.GetPeer(onion).GetGroup(groupID).Timeline.Len() + len(application.GetPeer(onion).GetGroup(groupID).UnacknowledgedMessages) - eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{ - event.Identity: onion, - event.GroupID: groupID, - event.Data: strconv.Itoa(totalMessages), - })) + + // Group Experiment Refresh + groupHandler, err := groups.ExperimentGate(settings.Experiments) + if err == nil { + // fix peergroupcontact message counts + groupList := application.GetPeer(profileOnion).GetGroups() + for _, groupID := range groupList { + totalMessages := application.GetPeer(profileOnion).GetGroup(groupID).Timeline.Len() + len(application.GetPeer(profileOnion).GetGroup(groupID).UnacknowledgedMessages) + 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.QueryACNStatus() application.QueryACNVersion() + } //export c_SendAppEvent @@ -540,7 +552,6 @@ func GetMessage(profileOnion, handle string, message_index 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) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index f0024ec..2108c90 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -83,7 +83,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { eh.startHandlingPeer(onion) } - tag,isTagged := profile.GetAttribute(app.AttributeTag) + tag, isTagged := profile.GetAttribute(app.AttributeTag) if isTagged { e.Data[app.AttributeTag] = tag } else { @@ -197,10 +197,13 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { authorization = model.AuthApproved } + // Use the server state when assessing group state + state := profile.GetContact(group.GroupServer).State + contacts = append(contacts, Contact{ Name: ph.GetNick(groupId), Onion: group.GroupID, - Status: group.State, + Status: state, Picture: cpicPath, Authorization: string(authorization), SaveHistory: event.SaveHistoryConfirmed, diff --git a/utils/settings.go b/utils/settings.go index 2356559..67db308 100644 --- a/utils/settings.go +++ b/utils/settings.go @@ -3,6 +3,7 @@ package utils import ( "cwtch.im/cwtch/event" "cwtch.im/cwtch/storage/v1" + "sync" "encoding/json" "git.openprivacy.ca/openprivacy/log" @@ -18,6 +19,7 @@ const ( ) var GlobalSettingsFile v1.FileStore +var lock sync.Mutex const GlobalSettingsFilename = "ui.globals" const saltFile = "SALT" @@ -44,11 +46,13 @@ var DefaultGlobalSettings = GlobalSettings{ StateRootPane: 0, FirstTime: true, BlockUnknownConnections: false, - UIColumnModePortrait: "DualpaneMode.Single", - UIColumnModeLandscape: "DualpaneMode.CopyPortrait", + UIColumnModePortrait: "DualpaneMode.Single", + UIColumnModeLandscape: "DualpaneMode.CopyPortrait", } func InitGlobalSettingsFile(directory string, password string) error { + lock.Lock() + defer lock.Unlock() var key [32]byte salt, err := ioutil.ReadFile(path.Join(directory, saltFile)) if err != nil { @@ -75,6 +79,8 @@ func InitGlobalSettingsFile(directory string, password string) error { } func ReadGlobalSettings() *GlobalSettings { + lock.Lock() + defer lock.Unlock() settings := DefaultGlobalSettings if GlobalSettingsFile == nil { @@ -100,6 +106,8 @@ func ReadGlobalSettings() *GlobalSettings { } func WriteGlobalSettings(globalSettings GlobalSettings) { + lock.Lock() + defer lock.Unlock() bytes, _ := json.Marshal(globalSettings) // override first time setting globalSettings.FirstTime = true