Skip to content

Commit df8c2f2

Browse files
committed
fix(plaxt): treat 0% as 100% if a playback was over 90%
1 parent 58585f0 commit df8c2f2

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

handler/plex.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ func (u *plexUser) UnmarshalBinary(data []byte) error {
102102
return json.Unmarshal(data, u)
103103
}
104104

105+
func (a sessionData) Equal(b sessionData) bool {
106+
if a.progress != b.progress {
107+
return false
108+
}
109+
if a.lastEvent != b.lastEvent {
110+
return false
111+
}
112+
if a.status != b.status {
113+
return false
114+
}
115+
if len(a.guids) != len(b.guids) {
116+
return false
117+
}
118+
return true
119+
}
120+
105121
func (c *PlexClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
106122
path := r.URL.EscapedPath()
107123
switch {
@@ -274,11 +290,15 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
274290
return
275291
}
276292
session := c.sessions[sessionKey]
277-
sessionChanged := false
278293

279294
progress := int(math.Round(float64(viewOffset) / float64(session.metadata.Duration) * 100.0))
280-
if session.status != sessionUnplayed && progress <= 0 {
281-
return
295+
if progress == 0 {
296+
if session.progress >= watchedThreshold {
297+
// time would become 0 once a playback session was finished
298+
progress = 100
299+
} else if session.status != sessionUnplayed {
300+
return
301+
}
282302
}
283303

284304
serverIdentifier := c.getServerIdentifier()
@@ -310,7 +330,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
310330
})
311331
}
312332
session.guids = externalGuids
313-
sessionChanged = true
314333
} else {
315334
externalGuids = session.guids
316335
}
@@ -327,10 +346,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
327346
} else {
328347
event = webhookEventPlay
329348
}
330-
case "buffering":
331-
if progress >= watchedThreshold && session.status == sessionPlaying {
332-
event = webhookEventScrobble
333-
}
334349
case "paused":
335350
if progress >= watchedThreshold && session.status == sessionPlaying {
336351
event = webhookEventScrobble
@@ -348,16 +363,16 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
348363
return
349364
} else if event == webhookEventScrobble {
350365
session.status = sessionWatched
351-
sessionChanged = true
352-
go clearCachedMetadata(ratingKey, r.Header.Get(headerToken))
366+
go clearCachedMetadata(ratingKey, token)
353367
} else if event == webhookEventStop {
354-
go clearCachedMetadata(ratingKey, r.Header.Get(headerToken))
368+
go clearCachedMetadata(ratingKey, token)
355369
} else if session.status == sessionUnplayed {
356370
session.status = sessionPlaying
357-
sessionChanged = true
358371
}
359-
if sessionChanged || event != session.lastEvent {
360-
session.lastEvent = event
372+
373+
session.lastEvent = event
374+
session.progress = progress
375+
if !session.Equal(c.sessions[sessionKey]) {
361376
c.sessions[sessionKey] = session
362377
} else {
363378
return

handler/structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type sessionData struct {
2323
guids []plexhooks.ExternalGuid
2424
lastEvent string
2525
status sessionStatus
26+
progress int
2627
}
2728

2829
type plexUser struct {

0 commit comments

Comments
 (0)