Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d049efa
move branch
xlcrr Sep 15, 2022
fa484b1
first commit
xlcrr Sep 18, 2022
d4332f9
stash
xlcrr Oct 28, 2022
c6dfe57
update existing data
xlcrr Oct 29, 2022
c1c379c
update migrations to auto load states + cities
xlcrr Apr 28, 2023
2be0885
update daily report tweet with flags
xlcrr Apr 28, 2023
1c028c4
Merge pull request #609 from OpenLitterMap/weekyOlm/57/updates
xlcrr Apr 28, 2023
261b2ee
add littercoin merchant page
brazilsunshine Apr 29, 2023
02b082a
stasho
brazilsunshine Apr 29, 2023
9edee40
get data per minute
xlcrr Apr 30, 2023
fe60419
add data per minute
xlcrr Apr 30, 2023
4139e13
fix floats
xlcrr Apr 30, 2023
4fa5f3a
add approved to merchants
brazilsunshine Apr 30, 2023
c465be4
deleted log
brazilsunshine Apr 30, 2023
4ea0bba
fixed bugs
brazilsunshine Apr 30, 2023
0b54f68
merge master into PR
xlcrr Apr 30, 2023
1f86788
Add slack notification
xlcrr Apr 30, 2023
b2630b4
Merge pull request #611 from OpenLitterMap/brazilsunshine-littercoin/…
xlcrr Apr 30, 2023
7763427
change package.lock
xlcrr Apr 30, 2023
839f4ff
remove package.lock
xlcrr Apr 30, 2023
30dead1
npm install
xlcrr Apr 30, 2023
7a9464c
merchants page mobile friendly
brazilsunshine May 4, 2023
ebc16ce
stash
xlcrr May 5, 2023
bc884a4
merge master into PR
xlcrr May 5, 2023
955fb93
merge master into PR
xlcrr May 5, 2023
77ba31c
deleted package-lock.json
brazilsunshine May 6, 2023
5038efe
compiled
brazilsunshine May 6, 2023
fa2691b
compiled for production
brazilsunshine May 6, 2023
a723dfb
enable admins to go back 1 photo and search for any photo by ID
xlcrr May 6, 2023
8f95abe
added packahe-lock.json
brazilsunshine May 6, 2023
e57f023
compile for production
xlcrr May 6, 2023
7a32993
remove log
xlcrr May 6, 2023
5da1583
change github actions
xlcrr May 6, 2023
1bdea1a
Updated laravel.yml
GeniJaho May 6, 2023
0bf40d2
updated laravel.yml
brazilsunshine May 6, 2023
1fa9d75
fixed padding
brazilsunshine May 6, 2023
df8f713
Merge pull request #613 from OpenLitterMap/feature/allow-admins-updat…
xlcrr May 6, 2023
d808336
stash
brazilsunshine May 6, 2023
af92884
stash
brazilsunshine May 6, 2023
8b1ce06
compile for production
brazilsunshine May 6, 2023
e7c5f8a
Merge pull request #612 from brazilsunshine/littercoin/mobile-friendly
xlcrr May 6, 2023
ef0fb0e
refactor to single controller with improved validation, logic and com…
xlcrr May 6, 2023
910253a
update comments
xlcrr May 6, 2023
c8fc344
Update code
xlcrr May 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install NPM assets
run: npm install --silent
run: npm install --silent --force
- name: Compile NPM assets
run: npm run dev --silent
- name: Execute tests (Unit and Feature tests) via PHPUnit
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Locations/UpdateTotalPhotosForLocationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UpdateTotalPhotosForLocationAction
* @param string $cityId City
* @param int $increaseBy can be negative, value will be subtracted, but not below 0
*/
public function run(string $countryId, string $stateId, string $cityId, int $increaseBy = 1)
public function run (string $countryId, string $stateId, string $cityId, int $increaseBy = 1)
{
$this->updateValue("country:$countryId", $increaseBy);

Expand All @@ -34,7 +34,7 @@ public function run(string $countryId, string $stateId, string $cityId, int $inc
* @param $hashName
* @param $value
*/
protected function updateValue($hashName, $value)
protected function updateValue ($hashName, $value)
{
// Separate if conditions to skip redis check when $value is positive
if ($value < 0) {
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Photos/AddTagsToPhotoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class AddTagsToPhotoAction
{
/**
* Adds tags to the photo.
* Add pre-defined tags to the photo.
*
* @param Photo $photo
* @param $tags
Expand Down
10 changes: 5 additions & 5 deletions app/Actions/Photos/DeleteTagsFromPhotoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DeleteTagsFromPhotoAction
*
* @return array
*/
public function run(Photo $photo): array
public function run (Photo $photo): array
{
$photo->refresh();

Expand All @@ -29,11 +29,11 @@ public function run(Photo $photo): array
return compact('litter', 'brands', 'custom', 'all');
}

private function deleteLitter(Photo $photo): int
private function deleteLitter (Photo $photo): int
{
$categories = collect($photo->categories())
->filter(function ($category) use ($photo) {
return $category !== 'brands' && !!$photo->$category;
return ($category !== 'brands' && !!$photo->$category);
});

$total = $categories->sum(function ($category) use ($photo) {
Expand All @@ -47,7 +47,7 @@ private function deleteLitter(Photo $photo): int
return $total;
}

private function deleteBrands(Photo $photo): int
private function deleteBrands (Photo $photo): int
{
if (!$photo->brands) {
return 0;
Expand All @@ -60,7 +60,7 @@ private function deleteBrands(Photo $photo): int
return $total;
}

private function deleteCustomTags(Photo $photo): int
private function deleteCustomTags (Photo $photo): int
{
$total = $photo->customTags->count();

Expand Down
143 changes: 143 additions & 0 deletions app/Actions/Photos/Update/UpdatePhotoDecreaseScores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace App\Actions\Photos\Update;

use App\Models\Photo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UpdatePhotoDecreaseScores
{
/**
* When a User wants to update one of the Photos,
*
* We need to reset the scores that were already rewarded to:
* - User
* - Team
* - Locations
*/
public function run (Photo $photo, $userId, $diff)
{
// photo.result_string will be re-written

// Reverse IncrementLocation.php and UpdateUserCategories
$decrementCounts = $this->decrementLocation($photo);

// Reverse IncreaseTeamTotalLitter
$this->decreaseTeamTotalLitter($photo, $decrementCounts);

// What if Littercoin was mined?

// $this->decrementUserCategoryScores($photo, $decrementCounts);

// UpdateUserTimeSeries needs to be refactored before its included here
}

/**
* Reverse of IncrementLocation.php
*/
private function decrementLocation ($photo)
{
$decrementTotalLitter = 0;
$decrementTotalBrands = 0;

// Decrease for each Category including Brands
foreach ($photo->tags as $category => $tags)
{
if ($category === 'brands')
{
$decrementTotalBrands = ($photo->brands->total() * -1);

foreach ($photo->tags['brands'] as $brand => $quantity)
{
$decrementBrand = ($quantity * -1);

// User
Redis::hincrby("user:$photo->user_id", $brand, $decrementBrand);

// Locations
Redis::hincrby("country:$photo->country_id", $brand, $decrementBrand);
Redis::hincrby("state:$photo->state_id", $brand, $decrementBrand);
Redis::hincrby("city:$photo->city_id", $brand, $decrementBrand);
}
}
else
{
$decrementCategory = ($photo->$category->total() * -1);

$decrementTotalLitter += $decrementCategory;

// User
Redis::hincrby("user:$photo->user_id", $category, $decrementCategory);

// Locations
Redis::hincrby("country:$photo->country_id", $category, $decrementCategory);
Redis::hincrby("state:$photo->state_id", $category, $decrementCategory);
Redis::hincrby("city:$photo->city_id", $category, $decrementCategory);
}
}

// Decrease total_litter
if ($decrementTotalLitter !== 0)
{
// User
Redis::hincrby("user:$photo->user_id", "total_litter", $decrementTotalLitter);

// Locations
Redis::hincrby("country:$photo->country_id", "total_litter", $decrementTotalLitter);
Redis::hincrby("state:$photo->state_id", "total_litter", $decrementTotalLitter);
Redis::hincrby("city:$photo->city_id", "total_litter", $decrementTotalLitter);
}

if ($decrementTotalBrands !== 0)
{
// User
Redis::hincrby("user:$photo->user_id", "total_brands", $decrementTotalLitter);

// Locations
Redis::hincrby("country:$photo->country_id", "total_brands", $decrementTotalBrands);
Redis::hincrby("state:$photo->state_id", "total_brands", $decrementTotalBrands);
Redis::hincrby("city:$photo->city_id", "total_brands", $decrementTotalBrands);
}

// Doesn't really make sense to do this again and again
// We should consider refactoring how this value is added during upload
// Reverse of UpdateTotalPhotosForLocationAction

// User
Redis::hincrby("user:$photo->user_id", "total_photos", -1);

// Locations
Redis::hincrby("country:$photo->country_id", "total_photos", -1);
Redis::hincrby("state:$photo->state_id", "total_photos", -1);
Redis::hincrby("city:$photo->city_id", "total_photos", -1);

return compact('decrementTotalLitter', 'decrementTotalBrands');
}

/**
* Reverse of IncreaseTeamTotalLitter
*/
private function decreaseTeamTotalLitter ($photo, $decrementCounts)
{
if (!$photo->team) {
return;
}

$decrementTotal = $decrementCounts['decrementTotalLitter'] + $decrementCounts['decrementTotalBrands'];

// We need to move these counts to Redis
$photo->team->total_litter += $decrementTotal;
$photo->team->save();

DB::table('team_user')
->where([
'team_id' => $photo->team_id,
'user_id' => $photo->user_id
])
->update([
'total_litter' => DB::raw('ifnull(total_litter, 0) - ' . $decrementTotal),
'updated_at' => now()
]);
}
}
110 changes: 87 additions & 23 deletions app/Console/Commands/Twitter/DailyReportTweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,22 @@

namespace App\Console\Commands\Twitter;

use App\Helpers\Twitter;
use Carbon\Carbon;
use App\Models\Photo;
use Spatie\Emoji\Emoji;
use App\Helpers\Twitter;
use App\Models\User\User;
use App\Models\Littercoin;
use App\Models\Location\Country;
use Illuminate\Console\Command;
use Abraham\TwitterOAuth\TwitterOAuth;


class DailyReportTweet extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'twitter:daily-report';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a daily report about OLM to Twitter OLM_bot account';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function handle ()
{
$startOfYesterday = Carbon::yesterday()->startOfDay();
$endOfYesterday = Carbon::yesterday()->endOfDay();
Expand All @@ -43,7 +28,7 @@ public function handle()
->count();

// total uploads/photos
$photos = Photo::whereDate('created_at', '>=', $startOfYesterday)
$todaysPhotosCount = Photo::whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday)
->count();

Expand All @@ -53,12 +38,91 @@ public function handle()

$totalUsers = User::count();

// total tags
$tags = Photo::whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday)
->sum('total_litter');

// new locations

// total littercoin
$littercoinCount = Littercoin::whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday)
->count();

// top countries
$photos = Photo::select('id', 'created_at', 'country_id', 'total_litter')
->whereDate('created_at', '>=', $startOfYesterday)
->whereDate('created_at', '<=', $endOfYesterday)
->get();

$countryIds = [];

foreach ($photos as $photo)
{
if (!array_key_exists($photo->country_id, $countryIds))
{
$countryIds[$photo->country_id] = 0;
}

$countryIds[$photo->country_id] += $photo->total_litter;
}

arsort($countryIds);

$first_three_keys = array_slice(array_keys($countryIds), 0, 3);

$firstFlag = false;
$secondFlag = false;
$thirdFlag = false;

if (isset($first_three_keys[0])) {
$firstCountryId = $first_three_keys[0];

$firstCountry = Country::select('id', 'shortcode')
->where('id', $firstCountryId)
->first();

$firstFlag = Emoji::countryFlag($firstCountry->shortcode);
}

if (isset($first_three_keys[1])) {
$secondCountryId = $first_three_keys[1];

$secondCountry = Country::select('id', 'shortcode')
->where('id', $secondCountryId)
->first();

$secondFlag = Emoji::countryFlag($secondCountry->shortcode);
}

if (isset($first_three_keys[2])) {
$thirdCountryId = $first_three_keys[2];

$thirdCountry = Country::select('id', 'shortcode')
->where('id', $thirdCountryId)
->first();

$thirdFlag = Emoji::countryFlag($thirdCountry->shortcode);
}

$message = "Today we signed up $users users and uploaded $todaysPhotosCount photos from $countries countries!";
$message .= " We added $tags tags.";
$message .= " We now have $totalUsers users!";
$message .= " $littercoinCount littercoin were mined.";

if ($firstFlag) {
$message .= " 1st $firstFlag ";

if ($secondFlag) {
$message .= " 2nd $secondFlag ";

if ($thirdFlag) {
$message .= " 3rd $thirdFlag";
}
}
}

$message = "Today we signed up $users users and uploaded $photos photos from $countries countries!";
$message .= " We now have $totalUsers users! #openlittermap #OLMbot 🌍";
$message .= " #openlittermap #OLMbot 🌍";

Twitter::sendTweet($message);
}
Expand Down
7 changes: 3 additions & 4 deletions app/Events/NewCityAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ class NewCityAdded implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;

public $city, $state, $country, $now, $cityId, $lat, $lon;
public $city, $state, $country, $now, $cityId, $lat, $lon, $photoId;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct ($city, $state, $country, $now, $cityId = null, $lat = null, $lon = null)
public function __construct ($city, $state, $country, $now, $cityId = null, $lat = null, $lon = null, $photoId = null)
{
$this->city = $city;
$this->state = $state;
Expand All @@ -29,6 +27,7 @@ public function __construct ($city, $state, $country, $now, $cityId = null, $lat
$this->cityId = $cityId;
$this->lat = $lat;
$this->lon = $lon;
$this->photoId = $photoId;
}

/**
Expand Down
Loading