7
7
#include " creds/abstractcredentials.h"
8
8
#include " account.h"
9
9
10
+ #include < QJsonArray>
11
+
10
12
namespace {
11
13
static constexpr int MAX_ALLOWED_FAILED_AUTHENTICATION_ATTEMPTS = 3 ;
12
14
static constexpr int PING_INTERVAL = 30 * 1000 ;
15
+
16
+ static constexpr QLatin1String NOTIFY_FILE_ID_PREFIX = QLatin1String{" notify_file_id " };
13
17
}
14
18
15
19
namespace OCC {
@@ -102,7 +106,9 @@ void PushNotifications::onWebSocketTextMessageReceived(const QString &message)
102
106
{
103
107
qCInfo (lcPushNotifications) << " Received push notification:" << message;
104
108
105
- if (message == " notify_file" ) {
109
+ if (message.startsWith (NOTIFY_FILE_ID_PREFIX)) {
110
+ handleNotifyFileId (message);
111
+ } else if (message == " notify_file" ) {
106
112
handleNotifyFile ();
107
113
} else if (message == " notify_activity" ) {
108
114
handleNotifyActivity ();
@@ -124,7 +130,7 @@ void PushNotifications::onWebSocketError(QAbstractSocket::SocketError error)
124
130
return ;
125
131
}
126
132
127
- qCWarning (lcPushNotifications) << " Websocket error on with account" << _account->url () << error;
133
+ qCWarning (lcPushNotifications) << " Websocket error on with account" << _account->displayName () << _account-> url () << error;
128
134
closeWebSocket ();
129
135
emit connectionLost ();
130
136
}
@@ -153,7 +159,7 @@ bool PushNotifications::tryReconnectToWebSocket()
153
159
154
160
void PushNotifications::onWebSocketSslErrors (const QList<QSslError> &errors)
155
161
{
156
- qCWarning (lcPushNotifications) << " Websocket ssl errors on with account" << _account->url () << errors;
162
+ qCWarning (lcPushNotifications) << " Websocket ssl errors on with account" << _account->displayName () << _account-> url () << errors;
157
163
closeWebSocket ();
158
164
emit authenticationFailed ();
159
165
}
@@ -164,7 +170,7 @@ void PushNotifications::openWebSocket()
164
170
const auto capabilities = _account->capabilities ();
165
171
const auto webSocketUrl = capabilities.pushNotificationsWebSocketUrl ();
166
172
167
- qCInfo (lcPushNotifications) << " Open connection to websocket on" << webSocketUrl << " for account" << _account->url ();
173
+ qCInfo (lcPushNotifications) << " Open connection to websocket on" << webSocketUrl << " for account" << _account->displayName () << _account-> url ();
168
174
connect (_webSocket, QOverload<QAbstractSocket::SocketError>::of (&QWebSocket::errorOccurred), this , &PushNotifications::onWebSocketError);
169
175
connect (_webSocket, &QWebSocket::sslErrors, this , &PushNotifications::onWebSocketSslErrors);
170
176
_webSocket->open (webSocketUrl);
@@ -184,6 +190,8 @@ void PushNotifications::handleAuthenticated()
184
190
{
185
191
qCInfo (lcPushNotifications) << " Authenticated successful on websocket" ;
186
192
_failedAuthenticationAttemptsCount = 0 ;
193
+ qCDebug (lcPushNotifications) << " Requesting opt-in to 'notify_file_id' notifications" ;
194
+ _webSocket->sendTextMessage (" listen notify_file_id" );
187
195
_isReady = true ;
188
196
startPingTimer ();
189
197
emit ready ();
@@ -202,6 +210,35 @@ void PushNotifications::handleNotifyFile()
202
210
emitFilesChanged ();
203
211
}
204
212
213
+ void PushNotifications::handleNotifyFileId (const QString &message)
214
+ {
215
+ qCInfo (lcPushNotifications) << " File-ID push notification arrived" ;
216
+
217
+ QList<qint64> fileIds{};
218
+ QJsonParseError parseError;
219
+ const auto fileIdsJson = message.mid (NOTIFY_FILE_ID_PREFIX.length ());
220
+ const auto jsonDoc = QJsonDocument::fromJson (fileIdsJson.toUtf8 (), &parseError);
221
+
222
+ if (parseError.error != QJsonParseError::NoError) {
223
+ qCWarning (lcPushNotifications).nospace () << " could not parse received list of file IDs error=" << parseError.error << " errorString=" << parseError.errorString () << " fileIdsJson=" << fileIdsJson;
224
+ return ;
225
+ }
226
+
227
+ if (const auto jsonArray = jsonDoc.array (); jsonDoc.isArray ()) {
228
+ for (const auto & fileid : jsonArray) {
229
+ if (const auto fid = fileid.toInteger (); fileid.isDouble ()) {
230
+ fileIds.push_back (fid);
231
+ }
232
+ }
233
+ }
234
+
235
+ if (fileIds.empty ()) {
236
+ return ;
237
+ }
238
+
239
+ emit fileIdsChanged (_account, fileIds);
240
+ }
241
+
205
242
void PushNotifications::handleInvalidCredentials ()
206
243
{
207
244
qCInfo (lcPushNotifications) << " Invalid credentials submitted to websocket" ;
0 commit comments