Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
logType = flag.String("logtype", "console", "Type of log output (console or json)")
skipMedia = flag.Bool("skipmedia", false, "Do not attempt to download media in messages")
osName = flag.String("osname", "Mac OS 10", "Connection OSName in Whatsapp")
platformType = flag.String("platformtype", "DESKTOP", "Device platform type (DESKTOP, IPAD, ANDROID_TABLET, IOS_PHONE, ANDROID_PHONE, etc.)")
colorOutput = flag.Bool("color", false, "Enable colored output for console logs")
sslcert = flag.String("sslcertificate", "", "SSL Certificate File")
sslprivkey = flag.String("sslprivatekey", "", "SSL Certificate Private Key File")
Expand Down Expand Up @@ -225,6 +226,11 @@ func main() {
*osName = v
}

// Override platformType from environment variable if set
if v := os.Getenv("SESSION_PLATFORM_TYPE"); v != "" {
*platformType = v
}

if *versionFlag {
fmt.Printf("WuzAPI version %s\n", version)
os.Exit(0)
Expand Down
60 changes: 59 additions & 1 deletion wmiau.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,64 @@ func parseJID(arg string) (types.JID, bool) {
}
}

// getPlatformTypeEnum converts a platform type string to the corresponding DeviceProps enum
// Returns DESKTOP as default if the string doesn't match any known type
func getPlatformTypeEnum(platformType string) *waCompanionReg.DeviceProps_PlatformType {
platformType = strings.ToUpper(strings.TrimSpace(platformType))

switch platformType {
case "UNKNOWN":
return waCompanionReg.DeviceProps_UNKNOWN.Enum()
case "CHROME":
return waCompanionReg.DeviceProps_CHROME.Enum()
case "FIREFOX":
return waCompanionReg.DeviceProps_FIREFOX.Enum()
case "IE":
return waCompanionReg.DeviceProps_IE.Enum()
case "OPERA":
return waCompanionReg.DeviceProps_OPERA.Enum()
case "SAFARI":
return waCompanionReg.DeviceProps_SAFARI.Enum()
case "EDGE":
return waCompanionReg.DeviceProps_EDGE.Enum()
case "DESKTOP":
return waCompanionReg.DeviceProps_DESKTOP.Enum()
case "IPAD":
return waCompanionReg.DeviceProps_IPAD.Enum()
case "ANDROID_TABLET":
return waCompanionReg.DeviceProps_ANDROID_TABLET.Enum()
case "OHANA":
return waCompanionReg.DeviceProps_OHANA.Enum()
case "ALOHA":
return waCompanionReg.DeviceProps_ALOHA.Enum()
case "CATALINA":
return waCompanionReg.DeviceProps_CATALINA.Enum()
case "TCL_TV":
return waCompanionReg.DeviceProps_TCL_TV.Enum()
case "IOS_PHONE":
return waCompanionReg.DeviceProps_IOS_PHONE.Enum()
case "IOS_CATALYST":
return waCompanionReg.DeviceProps_IOS_CATALYST.Enum()
case "ANDROID_PHONE":
return waCompanionReg.DeviceProps_ANDROID_PHONE.Enum()
case "ANDROID_AMBIGUOUS":
return waCompanionReg.DeviceProps_ANDROID_AMBIGUOUS.Enum()
case "WEAR_OS":
return waCompanionReg.DeviceProps_WEAR_OS.Enum()
case "AR_WRIST":
return waCompanionReg.DeviceProps_AR_WRIST.Enum()
case "AR_DEVICE":
return waCompanionReg.DeviceProps_AR_DEVICE.Enum()
case "UWP":
return waCompanionReg.DeviceProps_UWP.Enum()
case "VR":
return waCompanionReg.DeviceProps_VR.Enum()
default:
log.Warn().Str("platformType", platformType).Msg("Unknown platform type, defaulting to DESKTOP")
return waCompanionReg.DeviceProps_DESKTOP.Enum()
}
Comment on lines +372 to +424
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getPlatformTypeEnum function can be made more concise and maintainable. The waCompanionReg package, which is generated from a protobuf definition, provides a DeviceProps_PlatformType_value map that translates platform type names (strings) to their integer values. Using this map instead of a large switch statement will simplify the code and ensure it automatically supports new platform types if the underlying protobuf definition is updated.

	platformType = strings.ToUpper(strings.TrimSpace(platformType))
	if val, ok := waCompanionReg.DeviceProps_PlatformType_value[platformType]; ok {
		enumVal := waCompanionReg.DeviceProps_PlatformType(val)
		return enumVal.Enum()
	}

	log.Warn().Str("platformType", platformType).Msg("Unknown platform type, defaulting to DESKTOP")
	return waCompanionReg.DeviceProps_DESKTOP.Enum()

}

func (s *server) startClient(userID string, textjid string, token string, subscriptions []string) {
log.Info().Str("userid", userID).Str("jid", textjid).Msg("Starting websocket connection to Whatsapp")

Expand Down Expand Up @@ -407,7 +465,7 @@ func (s *server) startClient(userID string, textjid string, token string, subscr
// Now we can use the client with the manager
clientManager.SetWhatsmeowClient(userID, client)

store.DeviceProps.PlatformType = waCompanionReg.DeviceProps_DESKTOP.Enum()
store.DeviceProps.PlatformType = getPlatformTypeEnum(*platformType)
store.DeviceProps.Os = osName

mycli := MyClient{client, 1, userID, token, subscriptions, s.db, s}
Expand Down
Loading