From bd092db54c469107d39c1376b5bafefe4c75b409 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 14 Mar 2021 15:18:58 -0400 Subject: [PATCH] Move device display name code out of main --- cmd/tallyard/main.go | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/cmd/tallyard/main.go b/cmd/tallyard/main.go index 3032cf5..aa993b1 100644 --- a/cmd/tallyard/main.go +++ b/cmd/tallyard/main.go @@ -9,6 +9,7 @@ import ( log "github.com/sirupsen/logrus" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" "tallyard.xyz/election" "tallyard.xyz/math" @@ -75,23 +76,7 @@ func main() { panic(err) } - resp, err := client.GetDeviceInfo(authInfo.DeviceID) - if resp.DisplayName == "" { - displayName := fmt.Sprintf("tallyard %s", election.Version) - hostname, err := os.Hostname() - if err == nil { - log.Debugf("hostname: %s", hostname) - displayName = fmt.Sprintf("%s - %s", displayName, hostname) - } else { - log.Debugf("couldn't get hostname: %s", err) - } - err = client.SetDeviceInfo(authInfo.DeviceID, &mautrix.ReqDeviceInfo{ - DisplayName: displayName, - }) - if err != nil { - log.Warnf("couldn't set device display name: %s", err) - } - } + setDeviceName(client, authInfo.DeviceID) // setup the elections store elections, err := election.GetElectionsMap(client.UserID) @@ -197,3 +182,30 @@ func debugEventHook(_ mautrix.EventSource, evt *event.Event) { evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) } + +func setDeviceName(client *mautrix.Client, deviceID id.DeviceID) { + resp, err := client.GetDeviceInfo(deviceID) + if err != nil { + log.Debugf("couldn't get device %s info: %s", deviceID, err) + return + } + if resp.DisplayName != "" { + log.Debugf("device %s display name already set to '%s'", deviceID, resp.DisplayName) + // device name has already been set + return + } + displayName := fmt.Sprintf("tallyard %s", election.Version) + hostname, err := os.Hostname() + if err == nil { + log.Debugf("hostname: %s", hostname) + displayName = fmt.Sprintf("%s - %s", displayName, hostname) + } else { + log.Debugf("couldn't get hostname: %s", err) + } + err = client.SetDeviceInfo(deviceID, &mautrix.ReqDeviceInfo{ + DisplayName: displayName, + }) + if err != nil { + log.Warnf("couldn't set device display name: %s", err) + } +} -- 2.38.4