77#include " creds/abstractcredentials.h"
88#include " account.h"
99
10+ #include < QJsonArray>
11+
1012namespace {
1113static constexpr int MAX_ALLOWED_FAILED_AUTHENTICATION_ATTEMPTS = 3 ;
1214static constexpr int PING_INTERVAL = 30 * 1000 ;
15+
16+ static constexpr QLatin1String NOTIFY_FILE_ID_PREFIX = QLatin1String{" notify_file_id " };
1317}
1418
1519namespace OCC {
@@ -102,7 +106,9 @@ void PushNotifications::onWebSocketTextMessageReceived(const QString &message)
102106{
103107 qCInfo (lcPushNotifications) << " Received push notification:" << message;
104108
105- if (message == " notify_file" ) {
109+ if (message.startsWith (NOTIFY_FILE_ID_PREFIX)) {
110+ handleNotifyFileId (message);
111+ } else if (message == " notify_file" ) {
106112 handleNotifyFile ();
107113 } else if (message == " notify_activity" ) {
108114 handleNotifyActivity ();
@@ -124,7 +130,7 @@ void PushNotifications::onWebSocketError(QAbstractSocket::SocketError error)
124130 return ;
125131 }
126132
127- qCWarning (lcPushNotifications) << " Websocket error on with account" << _account->url () << error;
133+ qCWarning (lcPushNotifications) << " Websocket error on with account" << _account->displayName () << _account-> url () << error;
128134 closeWebSocket ();
129135 emit connectionLost ();
130136}
@@ -153,7 +159,7 @@ bool PushNotifications::tryReconnectToWebSocket()
153159
154160void PushNotifications::onWebSocketSslErrors (const QList<QSslError> &errors)
155161{
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;
157163 closeWebSocket ();
158164 emit authenticationFailed ();
159165}
@@ -164,7 +170,7 @@ void PushNotifications::openWebSocket()
164170 const auto capabilities = _account->capabilities ();
165171 const auto webSocketUrl = capabilities.pushNotificationsWebSocketUrl ();
166172
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 ();
168174 connect (_webSocket, QOverload<QAbstractSocket::SocketError>::of (&QWebSocket::errorOccurred), this , &PushNotifications::onWebSocketError);
169175 connect (_webSocket, &QWebSocket::sslErrors, this , &PushNotifications::onWebSocketSslErrors);
170176 _webSocket->open (webSocketUrl);
@@ -184,6 +190,8 @@ void PushNotifications::handleAuthenticated()
184190{
185191 qCInfo (lcPushNotifications) << " Authenticated successful on websocket" ;
186192 _failedAuthenticationAttemptsCount = 0 ;
193+ qCDebug (lcPushNotifications) << " Requesting opt-in to 'notify_file_id' notifications" ;
194+ _webSocket->sendTextMessage (" listen notify_file_id" );
187195 _isReady = true ;
188196 startPingTimer ();
189197 emit ready ();
@@ -202,6 +210,35 @@ void PushNotifications::handleNotifyFile()
202210 emitFilesChanged ();
203211}
204212
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+
205242void PushNotifications::handleInvalidCredentials ()
206243{
207244 qCInfo (lcPushNotifications) << " Invalid credentials submitted to websocket" ;
0 commit comments