Skip to content

Commit 64866f3

Browse files
committed
fix: Correct query for near home instant notification
1 parent c689e55 commit 64866f3

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

website/workers/mail-notification.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,37 +206,32 @@ private function getUsersToNotifyForMove(Move $move): array {
206206
SQL;
207207

208208
// Get users with home location near the move who want notifications
209+
// Use the same pre-computed gk_geokrety_near_users_homes table that DailyDigest uses
210+
// This avoids distance calculation bugs with st_dwithin and coordinate units
209211
$homeLocationSql = <<<'SQL'
210-
SELECT DISTINCT u.id
211-
FROM geokrety.gk_users u
212-
WHERE u.email_invalid = 0
213-
AND u.home_latitude IS NOT NULL
214-
AND u.home_longitude IS NOT NULL
215-
AND u.observation_area > 0
216-
AND public.st_dwithin(
217-
public.st_setsrid(public.st_makepoint(?, ?), 4326),
218-
public.st_setsrid(public.st_makepoint(u.home_longitude, u.home_latitude), 4326),
219-
(u.observation_area * 1000)::double precision
220-
)
212+
SELECT DISTINCT c_user_id AS id
213+
FROM geokrety.gk_geokrety_near_users_homes
214+
WHERE geokret = ?
215+
AND missing = ?
221216
AND EXISTS (
222217
SELECT 1
223218
FROM geokrety.gk_users_settings s
224-
WHERE s.user = u.id
219+
WHERE s.user = c_user_id
225220
AND s.name = 'INSTANT_NOTIFICATIONS'
226221
AND s.value = 'true'
227222
)
228223
AND NOT EXISTS (
229224
SELECT 1
230225
FROM geokrety.gk_users_settings s
231-
WHERE s.user = u.id
226+
WHERE s.user = c_user_id
232227
AND s.name = 'INSTANT_NOTIFICATIONS_MOVES_AROUND_HOME'
233228
AND s.value = 'false'
234229
)
235230
SQL;
236231

237232
$ownerResult = $db->exec($ownerSql, [$move->geokret->id]);
238233
$watchersResult = $db->exec($watchersSql, [$move->geokret->id]);
239-
$homeLocationResult = $db->exec($homeLocationSql, [$move->lon, $move->lat]);
234+
$homeLocationResult = $db->exec($homeLocationSql, [$move->geokret->id, Geokret::GEOKRETY_PRESENT_IN_CACHE]);
240235

241236
// Build array with user IDs and a flag indicating if it's a home-location notification
242237
$users = [];

0 commit comments

Comments
 (0)