Include lat/long in newsfeed url if available

This commit is contained in:
Nyall Dawson 2019-07-15 10:50:43 +10:00
parent f4ea33fef1
commit e783323fd7
3 changed files with 67 additions and 3 deletions

View File

@ -41,9 +41,32 @@ QgsNewsFeedParser::QgsNewsFeedParser( const QUrl &feedUrl, const QString &authcf
if ( after > 0 ) if ( after > 0 )
query.addQueryItem( QStringLiteral( "after" ), qgsDoubleToString( after, 0 ) ); query.addQueryItem( QStringLiteral( "after" ), qgsDoubleToString( after, 0 ) );
const QString lang = QgsSettings().value( QStringLiteral( "locale/userLocale" ), QStringLiteral( "en_US" ) ).toString().left( 2 ); QString feedLanguage = QgsSettings().value( QStringLiteral( "%1/lang" ).arg( mSettingsKey ), QString(), QgsSettings::Core ).toString();
if ( !lang.isEmpty() ) if ( feedLanguage.isEmpty() )
query.addQueryItem( QStringLiteral( "lang" ), lang ); {
feedLanguage = QgsSettings().value( QStringLiteral( "locale/userLocale" ), QStringLiteral( "en_US" ) ).toString().left( 2 );
}
if ( !feedLanguage.isEmpty() )
query.addQueryItem( QStringLiteral( "lang" ), feedLanguage );
bool latOk = false;
bool longOk = false;
const double feedLat = QgsSettings().value( QStringLiteral( "%1/latitude" ).arg( mSettingsKey ), QString(), QgsSettings::Core ).toDouble( &latOk );
const double feedLong = QgsSettings().value( QStringLiteral( "%1/longitude" ).arg( mSettingsKey ), QString(), QgsSettings::Core ).toDouble( &longOk );
if ( latOk && longOk )
{
// hack to allow testing using local files
if ( feedUrl.isLocalFile() )
{
query.addQueryItem( QStringLiteral( "lat" ), QString::number( static_cast< int >( feedLat ) ) );
query.addQueryItem( QStringLiteral( "lon" ), QString::number( static_cast< int >( feedLong ) ) );
}
else
{
query.addQueryItem( QStringLiteral( "lat" ), qgsDoubleToString( feedLat ) );
query.addQueryItem( QStringLiteral( "lon" ), qgsDoubleToString( feedLong ) );
}
}
// bit of a hack to allow testing using local files // bit of a hack to allow testing using local files
if ( feedUrl.isLocalFile() ) if ( feedUrl.isLocalFile() )

View File

@ -36,6 +36,7 @@ class TestQgsNewsFeedParser: public QObject
void testFetch(); void testFetch();
void testAutoExpiry(); void testAutoExpiry();
void testGeoFencing();
void testModel(); void testModel();
void testProxyModel(); void testProxyModel();
@ -220,6 +221,34 @@ void TestQgsNewsFeedParser::testAutoExpiry()
QVERIFY( !parser2.entries().at( 0 ).expiry.isValid() ); QVERIFY( !parser2.entries().at( 0 ).expiry.isValid() );
} }
void TestQgsNewsFeedParser::testGeoFencing()
{
QList< QgsNewsFeedParser::Entry > entries;
const QUrl url( QUrl::fromLocalFile( QStringLiteral( TEST_DATA_DIR ) + "/newsfeed/feed" ) );
const QString feedKey = QgsNewsFeedParser::keyForFeed( url.toString() );
QgsSettings().remove( feedKey, QgsSettings::Core );
QgsSettings().setValue( QStringLiteral( "%1/latitude" ).arg( feedKey ), 37.2343, QgsSettings::Core );
QgsSettings().setValue( QStringLiteral( "%1/longitude" ).arg( feedKey ), -115.8067, QgsSettings::Core );
QgsNewsFeedParser parser( url );
QSignalSpy spy( &parser, &QgsNewsFeedParser::entryAdded );
QVERIFY( parser.entries().isEmpty() );
QEventLoop loop;
connect( &parser, &QgsNewsFeedParser::fetched, this, [ =, &loop, &entries ]( const QList< QgsNewsFeedParser::Entry > &e )
{
entries = e;
loop.quit();
} );
parser.fetch();
loop.exec();
// check only geofenced entries are present (i.e. that request has included lat/lon params)
QCOMPARE( entries.count(), 1 );
QCOMPARE( entries.at( 0 ).title, QStringLiteral( "Secret docs leaked" ) );
}
void TestQgsNewsFeedParser::testModel() void TestQgsNewsFeedParser::testModel()
{ {
// test news feed model // test news feed model

View File

@ -0,0 +1,12 @@
[
{
"pk": 7,
"publish_from": 1557073748.136,
"publish_to": null,
"title": "Secret docs leaked",
"image": "",
"content": "<p>Super secret docs are leaked!</p>",
"url": "https://nsa.gov",
"sticky": false
}
]