@@ -101,20 +101,20 @@ func (u *plexUser) UnmarshalBinary(data []byte) error {
101101 return json .Unmarshal (data , u )
102102}
103103
104- func (a sessionData ) Equal (b sessionData ) bool {
104+ func (a sessionData ) Check (b sessionData ) (bool , bool ) {
105+ if a .status != b .status {
106+ return true , true
107+ }
105108 if a .progress != b .progress {
106- return false
109+ return true , false
107110 }
108111 if a .lastEvent != b .lastEvent {
109- return false
110- }
111- if a .status != b .status {
112- return false
112+ return true , false
113113 }
114114 if len (a .guids ) != len (b .guids ) {
115- return false
115+ return true , false
116116 }
117- return true
117+ return false , false
118118}
119119
120120func (c * PlexClient ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
@@ -285,6 +285,9 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
285285 return
286286 }
287287 session := c .sessions [sessionKey ]
288+ if session .status == sessionWatched {
289+ return
290+ }
288291
289292 progress := int (math .Round (float64 (viewOffset ) / float64 (session .metadata .Duration ) * 100.0 ))
290293 if progress == 0 {
@@ -296,21 +299,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
296299 }
297300 }
298301
299- serverIdentifier := c .getServerIdentifier ()
300- if serverIdentifier == "" {
301- return
302- }
303- var section plex.Directory
304- sectionId := session .metadata .LibrarySectionID .String ()
305- if c .getLibrarySection (sectionId ) {
306- section = c .sections [sectionId ]
307- if section .Type != "show" && section .Type != "movie" {
308- return
309- }
310- } else {
311- return
312- }
313-
314302 externalGuids := make ([]plexhooks.ExternalGuid , 0 )
315303 if session .guids == nil {
316304 metadata := c .getMetadata (ratingKey )
@@ -354,21 +342,41 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
354342 event = webhookEventStop
355343 }
356344 }
357- if event == "" || session . status == sessionWatched {
345+ if event == "" {
358346 return
359- } else if event == webhookEventScrobble {
360- session .status = sessionWatched
347+ }
348+ switch event {
349+ case webhookEventPlay , webhookEventResume :
350+ session .status = sessionPlaying
351+ case webhookEventPause :
352+ session .status = sessionStopped
353+ case webhookEventStop :
354+ session .status = sessionStopped
361355 go clearCachedMetadata (ratingKey , user .Id )
362- } else if event == webhookEventStop {
356+ case webhookEventScrobble :
357+ session .status = sessionWatched
363358 go clearCachedMetadata (ratingKey , user .Id )
364- } else if session .status == sessionUnplayed {
365- session .status = sessionPlaying
366359 }
367-
368360 session .lastEvent = event
369361 session .progress = progress
370- if ! session .Equal (c .sessions [sessionKey ]) {
362+ if shouldUpdate , shouldScrobble := session .Check (c .sessions [sessionKey ]); shouldUpdate {
371363 c .sessions [sessionKey ] = session
364+ if ! shouldScrobble {
365+ return
366+ }
367+ }
368+
369+ serverIdentifier := c .getServerIdentifier ()
370+ if serverIdentifier == "" {
371+ return
372+ }
373+ var section plex.Directory
374+ sectionId := session .metadata .LibrarySectionID .String ()
375+ if c .getLibrarySection (sectionId ) {
376+ section = c .sections [sectionId ]
377+ if section .Type != "show" && section .Type != "movie" {
378+ return
379+ }
372380 } else {
373381 return
374382 }
0 commit comments