diff --git a/database.sql b/database.sql index 709d9f35ea..b26a9d57cc 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-dev (Siberian Iris) --- DB_UPDATE_VERSION 1437 +-- DB_UPDATE_VERSION 1438 -- ------------------------------------------ @@ -970,6 +970,7 @@ CREATE TABLE IF NOT EXISTS `photo` ( `title` varchar(255) NOT NULL DEFAULT '' COMMENT '', `desc` text COMMENT '', `album` varchar(255) NOT NULL DEFAULT '' COMMENT 'The name of the album to which the photo belongs', + `photo-type` tinyint unsigned COMMENT 'User avatar, user banner, contact avatar, contact banner or default', `filename` varchar(255) NOT NULL DEFAULT '' COMMENT '', `type` varchar(30) NOT NULL DEFAULT 'image/jpeg', `height` smallint unsigned NOT NULL DEFAULT 0 COMMENT '', @@ -993,6 +994,7 @@ CREATE TABLE IF NOT EXISTS `photo` ( INDEX `uid_album_scale_created` (`uid`,`album`(32),`scale`,`created`), INDEX `uid_album_resource-id_created` (`uid`,`album`(32),`resource-id`,`created`), INDEX `resource-id` (`resource-id`), + INDEX `uid_photo-type` (`uid`,`photo-type`), FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE RESTRICT, FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='photo storage'; diff --git a/doc/database/db_photo.md b/doc/database/db_photo.md index b641ae4596..a93cf65f91 100644 --- a/doc/database/db_photo.md +++ b/doc/database/db_photo.md @@ -6,35 +6,36 @@ photo storage Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| ------------- | ------------------------------------------------------ | ------------------ | ---- | --- | ------------------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| uid | Owner User id | mediumint unsigned | NO | | 0 | | -| contact-id | contact.id | int unsigned | NO | | 0 | | -| guid | A unique identifier for this photo | char(16) | NO | | | | -| resource-id | | char(32) | NO | | | | -| hash | hash value of the photo | char(32) | YES | | NULL | | -| created | creation date | datetime | NO | | 0001-01-01 00:00:00 | | -| edited | last edited date | datetime | NO | | 0001-01-01 00:00:00 | | -| title | | varchar(255) | NO | | | | -| desc | | text | YES | | NULL | | -| album | The name of the album to which the photo belongs | varchar(255) | NO | | | | -| filename | | varchar(255) | NO | | | | -| type | | varchar(30) | NO | | image/jpeg | | -| height | | smallint unsigned | NO | | 0 | | -| width | | smallint unsigned | NO | | 0 | | -| datasize | | int unsigned | NO | | 0 | | -| data | | mediumblob | NO | | NULL | | -| scale | | tinyint unsigned | NO | | 0 | | -| profile | | boolean | NO | | 0 | | -| allow_cid | Access Control - list of allowed contact.id '<19><78>' | mediumtext | YES | | NULL | | -| allow_gid | Access Control - list of allowed groups | mediumtext | YES | | NULL | | -| deny_cid | Access Control - list of denied contact.id | mediumtext | YES | | NULL | | -| deny_gid | Access Control - list of denied groups | mediumtext | YES | | NULL | | -| accessible | Make photo publicly accessible, ignoring permissions | boolean | NO | | 0 | | -| backend-class | Storage backend class | tinytext | YES | | NULL | | -| backend-ref | Storage backend data reference | text | YES | | NULL | | -| updated | | datetime | NO | | 0001-01-01 00:00:00 | | +| Field | Description | Type | Null | Key | Default | Extra | +| ------------- | ------------------------------------------------------------------- | ------------------ | ---- | --- | ------------------- | -------------- | +| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | +| uid | Owner User id | mediumint unsigned | NO | | 0 | | +| contact-id | contact.id | int unsigned | NO | | 0 | | +| guid | A unique identifier for this photo | char(16) | NO | | | | +| resource-id | | char(32) | NO | | | | +| hash | hash value of the photo | char(32) | YES | | NULL | | +| created | creation date | datetime | NO | | 0001-01-01 00:00:00 | | +| edited | last edited date | datetime | NO | | 0001-01-01 00:00:00 | | +| title | | varchar(255) | NO | | | | +| desc | | text | YES | | NULL | | +| album | The name of the album to which the photo belongs | varchar(255) | NO | | | | +| photo-type | User avatar, user banner, contact avatar, contact banner or default | tinyint unsigned | YES | | NULL | | +| filename | | varchar(255) | NO | | | | +| type | | varchar(30) | NO | | image/jpeg | | +| height | | smallint unsigned | NO | | 0 | | +| width | | smallint unsigned | NO | | 0 | | +| datasize | | int unsigned | NO | | 0 | | +| data | | mediumblob | NO | | NULL | | +| scale | | tinyint unsigned | NO | | 0 | | +| profile | | boolean | NO | | 0 | | +| allow_cid | Access Control - list of allowed contact.id '<19><78>' | mediumtext | YES | | NULL | | +| allow_gid | Access Control - list of allowed groups | mediumtext | YES | | NULL | | +| deny_cid | Access Control - list of denied contact.id | mediumtext | YES | | NULL | | +| deny_gid | Access Control - list of denied groups | mediumtext | YES | | NULL | | +| accessible | Make photo publicly accessible, ignoring permissions | boolean | NO | | 0 | | +| backend-class | Storage backend class | tinytext | YES | | NULL | | +| backend-ref | Storage backend data reference | text | YES | | NULL | | +| updated | | datetime | NO | | 0001-01-01 00:00:00 | | Indexes ------------ @@ -48,6 +49,7 @@ Indexes | uid_album_scale_created | uid, album(32), scale, created | | uid_album_resource-id_created | uid, album(32), resource-id, created | | resource-id | resource-id | +| uid_photo-type | uid, photo-type | Foreign Keys ------------ diff --git a/include/api.php b/include/api.php index 211c2f6ecc..c5c863bdd1 100644 --- a/include/api.php +++ b/include/api.php @@ -4115,8 +4115,8 @@ function api_fr_photos_list($type) $r = DBA::toArray(DBA::p( "SELECT `resource-id`, MAX(scale) AS `scale`, `album`, `filename`, `type`, MAX(`created`) AS `created`, MAX(`edited`) AS `edited`, MAX(`desc`) AS `desc` FROM `photo` - WHERE `uid` = ? AND NOT `album` IN (?, ?) GROUP BY `resource-id`, `album`, `filename`, `type`", - local_user(), Photo::CONTACT_PHOTOS, DI::l10n()->t(Photo::CONTACT_PHOTOS) + WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) GROUP BY `resource-id`, `album`, `filename`, `type`", + local_user(), Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER )); $typetoext = [ 'image/jpeg' => 'jpg', @@ -4212,7 +4212,7 @@ function api_fr_photo_create_update($type) // now let's upload the new media in create-mode if ($mode == "create") { $media = $_FILES['media']; - $data = save_media_to_database("photo", $media, $type, $album, trim($allow_cid), trim($deny_cid), trim($allow_gid), trim($deny_gid), $desc, $visibility); + $data = save_media_to_database("photo", $media, $type, $album, trim($allow_cid), trim($deny_cid), trim($allow_gid), trim($deny_gid), $desc, Photo::DEFAULT, $visibility); // return success of updating or error message if (!is_null($data)) { @@ -4265,7 +4265,7 @@ function api_fr_photo_create_update($type) if (!empty($_FILES['media'])) { $nothingtodo = false; $media = $_FILES['media']; - $data = save_media_to_database("photo", $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, 0, $visibility, $photo_id); + $data = save_media_to_database("photo", $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, Photo::DEFAULT, $visibility, $photo_id); if (!is_null($data)) { return api_format_data("photo_update", $type, $data); } @@ -4408,7 +4408,7 @@ function api_account_update_profile_image($type) $media = $_FILES['media']; } // save new profile image - $data = save_media_to_database("profileimage", $media, $type, DI::l10n()->t('Profile Photos'), "", "", "", "", "", $is_default_profile); + $data = save_media_to_database("profileimage", $media, $type, DI::l10n()->t('Profile Photos'), "", "", "", "", "", Photo::USER_AVATAR); // get filetype if (is_array($media['type'])) { @@ -4427,7 +4427,7 @@ function api_account_update_profile_image($type) // change specified profile or all profiles to the new resource-id if ($is_default_profile) { $condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], api_user()]; - Photo::update(['profile' => false], $condition); + Photo::update(['profile' => false, 'photo-type' => Photo::DEFAULT], $condition); } else { $fields = ['photo' => DI::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $fileext, 'thumb' => DI::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $fileext]; @@ -4534,7 +4534,7 @@ function check_acl_input($acl_string) * @param string $allow_gid * @param string $deny_gid * @param string $desc - * @param integer $profile + * @param integer $phototype * @param boolean $visibility * @param string $photo_id * @return array @@ -4545,7 +4545,7 @@ function check_acl_input($acl_string) * @throws NotFoundException * @throws UnauthorizedException */ -function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, $profile = 0, $visibility = false, $photo_id = null) +function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, $phototype = 0, $visibility = false, $photo_id = null) { $visitor = 0; $src = ""; @@ -4623,13 +4623,13 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ // upload normal image (scales 0, 1, 2) logger::info("photo upload: starting new photo upload"); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 0, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 0 (original size) failed"); } if ($width > 640 || $height > 640) { $Image->scaleDown(640); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 1, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 1 (640x640) failed"); } @@ -4637,7 +4637,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 320 || $height > 320) { $Image->scaleDown(320); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 2, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: image upload with scale 2 (320x320) failed"); } @@ -4649,7 +4649,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 300 || $height > 300) { $Image->scaleDown(300); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 4, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 4, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 4 (300x300) failed"); } @@ -4657,7 +4657,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 80 || $height > 80) { $Image->scaleDown(80); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 5, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 5, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 5 (80x80) failed"); } @@ -4665,7 +4665,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $ if ($width > 48 || $height > 48) { $Image->scaleDown(48); - $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 6, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); + $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 6, $phototype, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); if (!$r) { logger::notice("photo upload: profile image upload with scale 6 (48x48) failed"); } diff --git a/mod/fbrowser.php b/mod/fbrowser.php index c68b44a1ed..b9f9b4c7f6 100644 --- a/mod/fbrowser.php +++ b/mod/fbrowser.php @@ -46,10 +46,10 @@ function fbrowser_content(App $a) $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10"; if (DI::args()->getArgc() == 2) { - $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `album` IN (?, ?)", + $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)", local_user(), - Photo::CONTACT_PHOTOS, - DI::l10n()->t(Photo::CONTACT_PHOTOS) + Photo::CONTACT_AVATAR, + Photo::CONTACT_BANNER )); $albums = array_column($photos, 'album'); @@ -64,11 +64,11 @@ function fbrowser_content(App $a) $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`, min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created` - FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `album` IN (?, ?) + FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `photo-type` IN (?, ?) GROUP BY `resource-id` $sql_extra2", local_user(), - Photo::CONTACT_PHOTOS, - DI::l10n()->t(Photo::CONTACT_PHOTOS) + Photo::CONTACT_AVATAR, + Photo::CONTACT_BANNER )); function _map_files1($rr) diff --git a/mod/photos.php b/mod/photos.php index 05767d4a4c..f58e532249 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -725,7 +725,7 @@ function photos_post(App $a) $resource_id = Photo::newResource(); - $r = Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); + $r = Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0 , Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); if (!$r) { Logger::info('image store failed'); @@ -735,13 +735,13 @@ function photos_post(App $a) if ($width > 640 || $height > 640) { $image->scaleDown(640); - Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); + Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); $smallest = 1; } if ($width > 320 || $height > 320) { $image->scaleDown(320); - Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); + Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); $smallest = 2; } @@ -1546,11 +1546,11 @@ function photos_content(App $a) // Default - show recent photos with upload link (if applicable) //$o = ''; $total = 0; - $r = DBA::toArray(DBA::p("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = ? AND NOT `album` IN (?, ?) + $r = DBA::toArray(DBA::p("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sql_extra GROUP BY `resource-id`", $user['uid'], - Photo::CONTACT_PHOTOS, - DI::l10n()->t(Photo::CONTACT_PHOTOS) + Photo::CONTACT_AVATAR, + Photo::CONTACT_BANNER )); if (DBA::isResult($r)) { $total = count($r); @@ -1561,11 +1561,11 @@ function photos_content(App $a) $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`, ANY_VALUE(`created`) AS `created` FROM `photo` - WHERE `uid` = ? AND NOT `album` IN (?, ?) + WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT ? , ?", $user['uid'], - Photo::CONTACT_PHOTOS, - DI::l10n()->t(Photo::CONTACT_PHOTOS), + Photo::CONTACT_AVATAR, + Photo::CONTACT_BANNER, $pager->getStart(), $pager->getItemsPerPage() )); diff --git a/mod/wall_upload.php b/mod/wall_upload.php index 100c948088..f915954f0a 100644 --- a/mod/wall_upload.php +++ b/mod/wall_upload.php @@ -226,7 +226,7 @@ function wall_upload_post(App $a, $desktopmode = true) $defperm = '<' . $default_cid . '>'; - $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, 0, $defperm); + $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0, Photo::DEFAULT, $defperm); if (!$r) { $msg = DI::l10n()->t('Image upload failed.'); @@ -240,7 +240,7 @@ function wall_upload_post(App $a, $desktopmode = true) if ($width > 640 || $height > 640) { $Image->scaleDown(640); - $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $defperm); + $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, Photo::DEFAULT, $defperm); if ($r) { $smallest = 1; } @@ -248,7 +248,7 @@ function wall_upload_post(App $a, $desktopmode = true) if ($width > 320 || $height > 320) { $Image->scaleDown(320); - $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $defperm); + $r = Photo::store($Image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, Photo::DEFAULT, $defperm); if ($r && ($smallest == 0)) { $smallest = 2; } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 918eab384f..60b303bd54 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1933,7 +1933,7 @@ class Contact if (!empty($cids)) { // Delete possibly existing cached user contact avatars - Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]); + Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'photo-type' => Photo::CONTACT_AVATAR]); } } diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 54316d92ef..1fdd55926d 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -46,6 +46,12 @@ class Photo { const CONTACT_PHOTOS = 'Contact Photos'; + const DEFAULT = 0; + const USER_AVATAR = 10; + const USER_BANNER = 11; + const CONTACT_AVATAR = 20; + const CONTACT_BANNER = 21; + /** * Select rows from the photo table and returns them as array * @@ -322,7 +328,7 @@ class Photo * @return boolean True on success * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = "", $allow_gid = "", $deny_cid = "", $deny_gid = "", $desc = "") + public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $type = self::DEFAULT, $allow_cid = "", $allow_gid = "", $deny_cid = "", $deny_gid = "", $desc = "") { $photo = self::selectFirst(["guid"], ["`resource-id` = ? AND `guid` != ?", $rid, ""]); if (DBA::isResult($photo)) { @@ -372,7 +378,8 @@ class Photo "datasize" => strlen($Image->asString()), "data" => $data, "scale" => $scale, - "profile" => $profile, + "photo-type" => $type, + "profile" => $type == self::USER_AVATAR, "allow_cid" => $allow_cid, "allow_gid" => $allow_gid, "deny_cid" => $deny_cid, @@ -476,7 +483,7 @@ class Photo $micro = ""; $photo = DBA::selectFirst( - "photo", ["resource-id"], ["uid" => $uid, "contact-id" => $cid, "scale" => 4, "album" => self::CONTACT_PHOTOS] + "photo", ["resource-id"], ["uid" => $uid, "contact-id" => $cid, "scale" => 4, "photo-type" => self::CONTACT_AVATAR] ); if (!empty($photo['resource-id'])) { $resource_id = $photo["resource-id"]; @@ -529,7 +536,7 @@ class Photo Logger::info('Avatar is resized', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'type' => $Image->getType()]); } - $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 4); + $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 4, self::CONTACT_AVATAR); if ($r === false) { $photo_failure = true; @@ -537,7 +544,7 @@ class Photo $Image->scaleDown(80); - $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 5); + $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 5, self::CONTACT_AVATAR); if ($r === false) { $photo_failure = true; @@ -545,7 +552,7 @@ class Photo $Image->scaleDown(48); - $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 6); + $r = self::store($Image, $uid, $cid, $resource_id, $filename, self::CONTACT_PHOTOS, 6, self::CONTACT_AVATAR); if ($r === false) { $photo_failure = true; @@ -651,20 +658,20 @@ class Photo // At this time we just store the data in the cache $albums = DBA::toArray(DBA::p("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created` FROM `photo` - WHERE `uid` = ? AND NOT `album` IN (?, ?) $sql_extra + WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sql_extra GROUP BY `album` ORDER BY `created` DESC", $uid, - self::CONTACT_PHOTOS, - DI::l10n()->t(self::CONTACT_PHOTOS) + self::CONTACT_AVATAR, + self::CONTACT_BANNER )); } else { // This query doesn't do the count and is much faster $albums = DBA::toArray(DBA::p("SELECT DISTINCT(`album`), '' AS `total` FROM `photo` USE INDEX (`uid_album_scale_created`) - WHERE `uid` = ? AND NOT `album` IN (?, ?) $sql_extra", + WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sql_extra", $uid, - self::CONTACT_PHOTOS, - DI::l10n()->t(self::CONTACT_PHOTOS) + self::CONTACT_AVATAR, + self::CONTACT_BANNER )); } DI::cache()->set($key, $albums, Duration::DAY); @@ -998,7 +1005,7 @@ class Photo $smallest = 0; - $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 0, 0, $defperm); + $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 0, self::DEFAULT, $defperm); if (!$r) { Logger::notice('Photo could not be stored'); return []; @@ -1006,7 +1013,7 @@ class Photo if ($width > 640 || $height > 640) { $Image->scaleDown(640); - $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 1, 0, $defperm); + $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 1, self::DEFAULT, $defperm); if ($r) { $smallest = 1; } @@ -1014,7 +1021,7 @@ class Photo if ($width > 320 || $height > 320) { $Image->scaleDown(320); - $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 2, 0, $defperm); + $r = Photo::store($Image, $user['uid'], 0, $resource_id, $filename, $album, 2, self::DEFAULT, $defperm); if ($r && ($smallest == 0)) { $smallest = 2; } diff --git a/src/Model/User.php b/src/Model/User.php index e27a950adb..fbee73c786 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -1184,7 +1184,7 @@ class User } if (!$photo_failure) { - Photo::update(['profile' => 1], ['resource-id' => $resource_id]); + Photo::update(['profile' => true, 'photo-type' => Photo::USER_AVATAR], ['resource-id' => $resource_id]); } } diff --git a/src/Module/Settings/Profile/Photo/Crop.php b/src/Module/Settings/Profile/Photo/Crop.php index ba96e0032f..d9f04d8de4 100644 --- a/src/Module/Settings/Profile/Photo/Crop.php +++ b/src/Module/Settings/Profile/Photo/Crop.php @@ -100,7 +100,7 @@ class Crop extends BaseSettings $base_image['filename'], DI::l10n()->t('Profile Photos'), 4, - 1 + Photo::USER_AVATAR ); if ($r === false) { notice(DI::l10n()->t('Image size reduction [%s] failed.', '300')); @@ -116,7 +116,7 @@ class Crop extends BaseSettings $base_image['filename'], DI::l10n()->t('Profile Photos'), 5, - 1 + Photo::USER_AVATAR ); if ($r === false) { notice(DI::l10n()->t('Image size reduction [%s] failed.', '80')); @@ -132,7 +132,7 @@ class Crop extends BaseSettings $base_image['filename'], DI::l10n()->t('Profile Photos'), 6, - 1 + Photo::USER_AVATAR ); if ($r === false) { notice(DI::l10n()->t('Image size reduction [%s] failed.', '48')); diff --git a/src/Worker/Expire.php b/src/Worker/Expire.php index d64a63a843..7a636499bd 100644 --- a/src/Worker/Expire.php +++ b/src/Worker/Expire.php @@ -59,7 +59,7 @@ class Expire Logger::notice('start expiry'); - $r = DBA::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0"); + $r = DBA::select('user', ['uid', 'username'], ["`expire` != ?", 0]); while ($row = DBA::fetch($r)) { Logger::info('Calling expiry', ['user' => $row['uid'], 'username' => $row['username']]); Worker::add(['priority' => $a->getQueueValue('priority'), 'created' => $a->getQueueValue('created'), 'dont_fork' => true], diff --git a/src/Worker/ExpireConversations.php b/src/Worker/ExpireConversations.php index aedf7cea51..8980a192ac 100644 --- a/src/Worker/ExpireConversations.php +++ b/src/Worker/ExpireConversations.php @@ -36,7 +36,7 @@ class ExpireConversations return; } - DBA::p("DELETE FROM `conversation` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days); + DBA::e("DELETE FROM `conversation` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days); } } diff --git a/src/Worker/RemoveUnusedAvatars.php b/src/Worker/RemoveUnusedAvatars.php index 7fdbeb7ecc..e8f615f62f 100644 --- a/src/Worker/RemoveUnusedAvatars.php +++ b/src/Worker/RemoveUnusedAvatars.php @@ -45,7 +45,7 @@ class RemoveUnusedAvatars $contacts = DBA::select('contact', ['id'], $condition); while ($contact = DBA::fetch($contacts)) { Contact::update(['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]); - Photo::delete(['contact-id' => $contact['id'], 'album' => Photo::CONTACT_PHOTOS]); + Photo::delete(['contact-id' => $contact['id'], 'photo-type' => [Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER]]); if ((++$count % 1000) == 0) { if (!Worker::isInMaintenanceWindow()) { Logger::notice('We are outside of the maintenance window, quitting'); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 8203ae5202..e333c136f4 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1437); + define('DB_UPDATE_VERSION', 1438); } return [ @@ -1023,6 +1023,7 @@ return [ "title" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "desc" => ["type" => "text", "comment" => ""], "album" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "The name of the album to which the photo belongs"], + "photo-type" => ["type" => "tinyint unsigned", "comment" => "User avatar, user banner, contact avatar, contact banner or default"], "filename" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "type" => ["type" => "varchar(30)", "not null" => "1", "default" => "image/jpeg"], "height" => ["type" => "smallint unsigned", "not null" => "1", "default" => "0", "comment" => ""], @@ -1048,6 +1049,7 @@ return [ "uid_album_scale_created" => ["uid", "album(32)", "scale", "created"], "uid_album_resource-id_created" => ["uid", "album(32)", "resource-id", "created"], "resource-id" => ["resource-id"], + "uid_photo-type" => ["uid", "photo-type"], ] ], "post" => [ diff --git a/update.php b/update.php index 49287cd0b2..1144013344 100644 --- a/update.php +++ b/update.php @@ -192,8 +192,8 @@ function update_1330() } // Update attachments and photos - if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") || - !DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) { + if (!DBA::e("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") || + !DBA::e("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) { return Update::FAILED; }; @@ -1005,4 +1005,11 @@ function update_1435() while ($contact = DBA::fetch($contacts)) { Contact\User::insertForContactArray($contact); } -} \ No newline at end of file +} + +function update_1438() +{ + DBA::update('photo', ['photo-type' => Photo::USER_AVATAR], ['profile' => true]); + DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]); + DBA::update('photo', ['photo-type' => Photo::DEFAULT], ["NOT `profile` AND (`contact-id` IS NULL OR `contact-id` = ?) AND `photo-type` IS NULL AND `album` != ?", 0, Photo::CONTACT_PHOTOS]); +}