Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Albums: Add option to sort albums/states chronologically by date of assigned photos #3832

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

gokcedilek
Copy link
Collaborator

@gokcedilek gokcedilek commented Oct 17, 2023

#3763

  • When a photo is added, update the album oldest / newest fields if needed
  • When a photo is removed, re-compute the album oldest / newest if needed and update the fields
  • When a user updates the date of a photo that is the newest or oldest in an album, check if it is still the newest or oldest, re-compute the album oldest / newest if needed and update the fields
  • Make sure the code and the queries are optimized

@lastzero
Copy link
Member

lastzero commented Jan 9, 2024

My apologies for the late feedback on your changes! I hope you had a good start into the new year :)

Since empty albums should not have an oldest or newest date, it might be a good idea to change the entity time values to pointers (so that they can be set to nil if needed):

AlbumOldest	 *time.Time	 `json:"Oldest" yaml:"Oldest"`
AlbumNewest 	 *time.Time	 `json:"Newest" yaml:"Newest"`

Based on the currently implemented functionality, it would seem best to (also) update the new album date fields in the following place (or move this function to the entity package)?

// UpdateAlbumDates updates the year, month and day of the album based on the indexed photo metadata.
func UpdateAlbumDates() error {
mutex.Index.Lock()
defer mutex.Index.Unlock()
switch DbDialect() {
case MySQL:
return UnscopedDb().Exec(`UPDATE albums INNER JOIN (
SELECT photo_path, MAX(taken_at_local) AS taken_max
FROM photos WHERE taken_src = 'meta' AND photos.photo_quality >= 3 AND photos.deleted_at IS NULL
GROUP BY photo_path
) AS p ON albums.album_path = p.photo_path
SET albums.album_year = YEAR(taken_max), albums.album_month = MONTH(taken_max), albums.album_day = DAY(taken_max)
WHERE albums.album_type = 'folder' AND albums.album_path IS NOT NULL AND p.taken_max IS NOT NULL`).Error
default:
return nil
}
}

This function is automatically executed in regular intervals by the meta worker so that the updates happen automatically in the background:

if err := query.UpdateAlbumDates(); err != nil {

That's particularly useful for albums that are based on search filters, e.g. the "state", "folder" and "moment" types (for manually managed albums, an additional update statement can be run in the entity model so that these albums are updated immediately after performing changes).

The SQL query needed to determine and update the dates in UpdateAlbumDates() would be different for each album type (e.g. manual or state). To cover all types, a similar approach to the cover query below could theoretically be taken:

// AlbumCoverByUID returns an album cover file based on the uid.
func AlbumCoverByUID(uid string, public bool) (file entity.File, err error) {

However, from what I know, we only need to take care of the "album", "state" and "folder" types, and for those it should be possible to determine the oldest and newest date without running a regular search query for each individual album, which might otherwise cause quite a bit of overhead:

if photos, _, err := search.Photos(f); err != nil {

Happy to answer any follow-up questions you may have 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants