@@ -918,6 +918,8 @@ ChatPage::joinRoom(const QString &room)
918918 } catch (const lmdb::error &e) {
919919 emit showNotification (tr (" Failed to remove invite: %1" ).arg (e.what ()));
920920 }
921+
922+ room_list_->highlightSelectedRoom (QString::fromStdString (room_id));
921923 });
922924}
923925
@@ -1268,3 +1270,141 @@ ChatPage::decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescriptio
12681270 cache::storeSecret (secretName, decrypted);
12691271 }
12701272}
1273+
1274+ void
1275+ ChatPage::startChat (QString userid)
1276+ {
1277+ auto joined_rooms = cache::joinedRooms ();
1278+ auto room_infos = cache::getRoomInfo (joined_rooms);
1279+
1280+ for (std::string room_id : joined_rooms) {
1281+ if (room_infos[QString::fromStdString (room_id)].member_count == 2 ) {
1282+ auto room_members = cache::roomMembers (room_id);
1283+ if (std::find (room_members.begin (),
1284+ room_members.end (),
1285+ (userid).toStdString ()) != room_members.end ()) {
1286+ room_list_->highlightSelectedRoom (QString::fromStdString (room_id));
1287+ return ;
1288+ }
1289+ }
1290+ }
1291+
1292+ mtx::requests::CreateRoom req;
1293+ req.preset = mtx::requests::Preset::PrivateChat;
1294+ req.visibility = mtx::requests::Visibility::Private;
1295+ if (utils::localUser () != userid)
1296+ req.invite = {userid.toStdString ()};
1297+ emit ChatPage::instance ()->createRoom (req);
1298+ }
1299+
1300+ static QString
1301+ mxidFromSegments (QStringRef sigil, QStringRef mxid)
1302+ {
1303+ if (mxid.isEmpty ())
1304+ return " " ;
1305+
1306+ auto mxid_ = QUrl::fromPercentEncoding (mxid.toUtf8 ());
1307+
1308+ if (sigil == " user" ) {
1309+ return " @" + mxid_;
1310+ } else if (sigil == " roomid" ) {
1311+ return " !" + mxid_;
1312+ } else if (sigil == " room" ) {
1313+ return " #" + mxid_;
1314+ } else if (sigil == " group" ) {
1315+ return " +" + mxid_;
1316+ } else {
1317+ return " " ;
1318+ }
1319+ }
1320+
1321+ void
1322+ ChatPage::handleMatrixUri (const QByteArray &uri)
1323+ {
1324+ nhlog::ui ()->info (" Received uri! {}" , uri.toStdString ());
1325+ QUrl uri_{QString::fromUtf8 (uri)};
1326+
1327+ if (uri_.scheme () != " matrix" )
1328+ return ;
1329+
1330+ auto tempPath = uri_.path (QUrl::ComponentFormattingOption::FullyEncoded);
1331+ if (tempPath.startsWith (' /' ))
1332+ tempPath.remove (0 , 1 );
1333+ auto segments = tempPath.splitRef (' /' );
1334+
1335+ if (segments.size () != 2 && segments.size () != 4 )
1336+ return ;
1337+
1338+ auto sigil1 = segments[0 ];
1339+ auto mxid1 = mxidFromSegments (sigil1, segments[1 ]);
1340+ if (mxid1.isEmpty ())
1341+ return ;
1342+
1343+ QString mxid2;
1344+ if (segments.size () == 4 && segments[2 ] == " event" ) {
1345+ if (segments[3 ].isEmpty ())
1346+ return ;
1347+ else
1348+ mxid2 = " $" + QUrl::fromPercentEncoding (segments[3 ].toUtf8 ());
1349+ }
1350+
1351+ std::vector<std::string> vias;
1352+ QString action;
1353+
1354+ for (QString item : uri_.query (QUrl::ComponentFormattingOption::FullyEncoded).split (' &' )) {
1355+ nhlog::ui ()->info (" item: {}" , item.toStdString ());
1356+
1357+ if (item.startsWith (" action=" )) {
1358+ action = item.remove (" action=" );
1359+ } else if (item.startsWith (" via=" )) {
1360+ vias.push_back (
1361+ QUrl::fromPercentEncoding (item.remove (" via=" ).toUtf8 ()).toStdString ());
1362+ }
1363+ }
1364+
1365+ if (sigil1 == " user" ) {
1366+ if (action.isEmpty ()) {
1367+ view_manager_->activeTimeline ()->openUserProfile (mxid1);
1368+ } else if (action == " chat" ) {
1369+ this ->startChat (mxid1);
1370+ }
1371+ } else if (sigil1 == " roomid" ) {
1372+ auto joined_rooms = cache::joinedRooms ();
1373+ auto targetRoomId = mxid1.toStdString ();
1374+
1375+ for (auto roomid : joined_rooms) {
1376+ if (roomid == targetRoomId) {
1377+ room_list_->highlightSelectedRoom (mxid1);
1378+ break ;
1379+ }
1380+ }
1381+
1382+ if (action == " join" ) {
1383+ joinRoom (mxid1);
1384+ }
1385+ } else if (sigil1 == " room" ) {
1386+ auto joined_rooms = cache::joinedRooms ();
1387+ auto targetRoomAlias = mxid1.toStdString ();
1388+
1389+ for (auto roomid : joined_rooms) {
1390+ auto aliases = cache::client ()->getRoomAliases (roomid);
1391+ if (aliases) {
1392+ if (aliases->alias == targetRoomAlias) {
1393+ room_list_->highlightSelectedRoom (
1394+ QString::fromStdString (roomid));
1395+ break ;
1396+ }
1397+ }
1398+ }
1399+
1400+ if (action == " join" ) {
1401+ joinRoom (mxid1);
1402+ }
1403+ }
1404+ }
1405+
1406+ void
1407+ ChatPage::handleMatrixUri (const QUrl &uri)
1408+ {
1409+ handleMatrixUri (uri.toString (QUrl::ComponentFormattingOption::FullyEncoded).toUtf8 ());
1410+ }
0 commit comments