Skip to content

Commit

Permalink
fix(plaxt): treat 0% as 100% if a playback was over 90%
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Mar 9, 2022
1 parent 58585f0 commit df8c2f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
43 changes: 29 additions & 14 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ func (u *plexUser) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, u)
}

func (a sessionData) Equal(b sessionData) bool {
if a.progress != b.progress {
return false
}
if a.lastEvent != b.lastEvent {
return false
}
if a.status != b.status {
return false
}
if len(a.guids) != len(b.guids) {
return false
}
return true
}

func (c *PlexClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.EscapedPath()
switch {
Expand Down Expand Up @@ -274,11 +290,15 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
return
}
session := c.sessions[sessionKey]
sessionChanged := false

progress := int(math.Round(float64(viewOffset) / float64(session.metadata.Duration) * 100.0))
if session.status != sessionUnplayed && progress <= 0 {
return
if progress == 0 {
if session.progress >= watchedThreshold {
// time would become 0 once a playback session was finished
progress = 100
} else if session.status != sessionUnplayed {
return
}
}

serverIdentifier := c.getServerIdentifier()
Expand Down Expand Up @@ -310,7 +330,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
})
}
session.guids = externalGuids
sessionChanged = true
} else {
externalGuids = session.guids
}
Expand All @@ -327,10 +346,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
} else {
event = webhookEventPlay
}
case "buffering":
if progress >= watchedThreshold && session.status == sessionPlaying {
event = webhookEventScrobble
}
case "paused":
if progress >= watchedThreshold && session.status == sessionPlaying {
event = webhookEventScrobble
Expand All @@ -348,16 +363,16 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
return
} else if event == webhookEventScrobble {
session.status = sessionWatched
sessionChanged = true
go clearCachedMetadata(ratingKey, r.Header.Get(headerToken))
go clearCachedMetadata(ratingKey, token)
} else if event == webhookEventStop {
go clearCachedMetadata(ratingKey, r.Header.Get(headerToken))
go clearCachedMetadata(ratingKey, token)
} else if session.status == sessionUnplayed {
session.status = sessionPlaying
sessionChanged = true
}
if sessionChanged || event != session.lastEvent {
session.lastEvent = event

session.lastEvent = event
session.progress = progress
if !session.Equal(c.sessions[sessionKey]) {
c.sessions[sessionKey] = session
} else {
return
Expand Down
1 change: 1 addition & 0 deletions handler/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type sessionData struct {
guids []plexhooks.ExternalGuid
lastEvent string
status sessionStatus
progress int
}

type plexUser struct {
Expand Down

0 comments on commit df8c2f2

Please sign in to comment.