~edwargix/tallyard

bd092db54c469107d39c1376b5bafefe4c75b409 — David Florness 4 years ago 21b778e
Move device display name code out of main
1 files changed, 29 insertions(+), 17 deletions(-)

M cmd/tallyard/main.go
M cmd/tallyard/main.go => cmd/tallyard/main.go +29 -17
@@ 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)
	}
}