@@ 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)
+ }
+}