Fix: link share_id to geo_id so /watch returns correct position
This commit is contained in:
@@ -12,7 +12,7 @@ import '../models/log.dart';
|
|||||||
class DatabaseProvider {
|
class DatabaseProvider {
|
||||||
late Connection _dbConnection;
|
late Connection _dbConnection;
|
||||||
|
|
||||||
final Map<String, bool> _shareLinks = {};
|
final Map<String, String> _shareLinks = {};
|
||||||
final _uuid = const Uuid();
|
final _uuid = const Uuid();
|
||||||
|
|
||||||
Future<void> initialize() async {
|
Future<void> initialize() async {
|
||||||
@@ -284,6 +284,28 @@ Future<Geoposition?> getLatestPosition() async {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Geoposition?> getPositionById(String id) async {
|
||||||
|
final results = await _dbConnection.execute(
|
||||||
|
Sql.named('''
|
||||||
|
SELECT id, x_value, y_value, last_update, expires_at
|
||||||
|
FROM geopositions
|
||||||
|
WHERE id = @id AND expires_at > NOW()
|
||||||
|
'''),
|
||||||
|
parameters: {'id': id},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (results.isEmpty) return null;
|
||||||
|
|
||||||
|
final row = results.first;
|
||||||
|
return Geoposition(
|
||||||
|
id: row[0].toString(),
|
||||||
|
xValue: double.parse(row[1].toString()),
|
||||||
|
yValue: double.parse(row[2].toString()),
|
||||||
|
lastUpdate: DateTime.parse(row[3].toString()),
|
||||||
|
expiresAt: DateTime.parse(row[4].toString()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> cleanupExpired() async {
|
Future<void> cleanupExpired() async {
|
||||||
await _dbConnection.execute(
|
await _dbConnection.execute(
|
||||||
Sql.named('DELETE FROM geopositions WHERE expires_at < NOW()'),
|
Sql.named('DELETE FROM geopositions WHERE expires_at < NOW()'),
|
||||||
@@ -292,14 +314,14 @@ Future<Geoposition?> getLatestPosition() async {
|
|||||||
|
|
||||||
// ==================== Share operations ====================
|
// ==================== Share operations ====================
|
||||||
|
|
||||||
String createShareId() {
|
String createShareId(String geoId) {
|
||||||
final uniqueId = _uuid.v4();
|
final uniqueId = _uuid.v4();
|
||||||
_shareLinks[uniqueId] = true;
|
_shareLinks[uniqueId] = geoId;
|
||||||
return uniqueId;
|
return uniqueId;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValidShareId(String uniqueId) {
|
String? getGeoIdByShareId(String uniqueId) {
|
||||||
return _shareLinks[uniqueId] == true;
|
return _shareLinks[uniqueId];
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Log operations ====================
|
// ==================== Log operations ====================
|
||||||
|
|||||||
@@ -61,13 +61,14 @@ class AuthRoutes {
|
|||||||
|
|
||||||
Future<Response> _watch(Request request, String login) async {
|
Future<Response> _watch(Request request, String login) async {
|
||||||
final uniqueId = request.url.queryParameters['unique_id'];
|
final uniqueId = request.url.queryParameters['unique_id'];
|
||||||
|
final geoId = database.getGeoIdByShareId(uniqueId!);
|
||||||
|
|
||||||
if (!database.isValidShareId(uniqueId!)) {
|
if (geoId == null) {
|
||||||
await database.createLog(login, 'Accessed invalid share link');
|
await database.createLog(login, 'Accessed invalid share link');
|
||||||
return Response(404, body: jsonEncode({'error': 'Share link not found'}), headers: {'Content-Type': 'application/json'});
|
return Response(404, body: jsonEncode({'error': 'Share link not found'}), headers: {'Content-Type': 'application/json'});
|
||||||
}
|
}
|
||||||
|
|
||||||
final position = await database.getLatestPosition();
|
final position = await database.getPositionById(geoId);
|
||||||
|
|
||||||
if (position == null) {
|
if (position == null) {
|
||||||
await database.createLog(login, 'Accessed share link - no position');
|
await database.createLog(login, 'Accessed share link - no position');
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class GeoRoutes {
|
|||||||
final y = data['y'];
|
final y = data['y'];
|
||||||
|
|
||||||
final position = await database.createPosition(x, y);
|
final position = await database.createPosition(x, y);
|
||||||
final shareId = database.createShareId();
|
final shareId = database.createShareId(position.id);
|
||||||
|
|
||||||
await database.createLog(login, 'Created share link geo_id=${position.id}');
|
await database.createLog(login, 'Created share link geo_id=${position.id}');
|
||||||
return Response(201, body: jsonEncode({
|
return Response(201, body: jsonEncode({
|
||||||
|
|||||||
Reference in New Issue
Block a user