Skip to content

Commit 767df3d

Browse files
committed
core: Send User-Agent header in requests
This is required for the server to automatically mark sent messages as read. See `sent_by_human` in `zulip/zerver/models.py`. Fixes: zulip#440
1 parent 01e4f14 commit 767df3d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/api/core.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'dart:convert';
22
import 'dart:io';
33

4+
import 'package:device_info_plus/device_info_plus.dart';
45
import 'package:http/http.dart' as http;
6+
import 'package:package_info_plus/package_info_plus.dart';
57

68
import '../log.dart';
79
import '../model/localizations.dart';
@@ -69,6 +71,28 @@ class ApiConnection {
6971
}
7072
}
7173

74+
Future<void> addUserAgent(http.BaseRequest request) async {
75+
final packageInfo = await PackageInfo.fromPlatform();
76+
final deviceInfoPlugin = DeviceInfoPlugin();
77+
78+
final String applicationVersion = packageInfo.version;
79+
final String systemName;
80+
final String systemVersion;
81+
if (Platform.isIOS) {
82+
final deviceInfo = await deviceInfoPlugin.iosInfo;
83+
systemName = deviceInfo.systemName;
84+
systemVersion = deviceInfo.systemVersion;
85+
} else if (Platform.isAndroid) {
86+
final deviceInfo = await deviceInfoPlugin.androidInfo;
87+
systemName = "Android";
88+
systemVersion = deviceInfo.version.release;
89+
} else {
90+
systemName = "Desktop";
91+
systemVersion = "?";
92+
}
93+
request.headers['User-Agent'] = 'ZulipMobile/$applicationVersion ($systemName $systemVersion)';
94+
}
95+
7296
final http.Client _client;
7397

7498
bool _isOpen = true;
@@ -80,6 +104,7 @@ class ApiConnection {
80104
assert(debugLog("${request.method} ${request.url}"));
81105

82106
addAuth(request);
107+
await addUserAgent(request);
83108

84109
final http.StreamedResponse response;
85110
try {

0 commit comments

Comments
 (0)