Skip to content

Commit 118270a

Browse files
committed
feat(plaxt): fetch user id and username for the owner of PMS
1 parent 70747c7 commit 118270a

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

handler/plex.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func NewPlexClient(config PlexConfig) *PlexClient {
7373
plaxtUrl: plaxtUrl,
7474
sections: make(map[string]plex.Directory, 0),
7575
sessions: make(map[string]sessionData),
76+
friends: make(map[string]plexUser),
7677
}
7778
}
7879

@@ -137,14 +138,12 @@ func (c *PlexClient) GetUserId(token string) (id int) {
137138
}
138139

139140
// refresh the list of friends
140-
c.findFriend("")
141+
c.findFriend(0)
141142
for _, friend := range c.friends {
142143
if friend.Token == token {
143-
if id64, err := friend.Id.Int64(); err == nil {
144-
id = int(id64)
145-
if !isCacheEnabled {
146-
break
147-
}
144+
id = friend.Id
145+
if !isCacheEnabled {
146+
break
148147
}
149148
}
150149
if isCacheEnabled {
@@ -179,15 +178,23 @@ func (c *PlexClient) GetAccountInfo(token string) (user plex.UserPlexTV) {
179178
defer c.mu.RUnlock()
180179
}
181180

181+
var err error
182182
c.client.Token = token
183-
user, _ = c.client.MyAccount()
183+
user, err = c.client.MyAccount()
184+
if err == nil {
185+
c.friends[strconv.Itoa(user.ID)] = plexUser{
186+
Id: user.ID,
187+
Username: user.Username,
188+
Token: token,
189+
}
190+
}
184191
return
185192
}
186193

187-
func (c *PlexClient) findFriend(id string) (isFound bool) {
188-
if id != "" {
194+
func (c *PlexClient) findFriend(id int) (isFound bool) {
195+
if id > 0 {
189196
for _, friend := range c.friends {
190-
if friend.Id.String() == id {
197+
if friend.Id == id {
191198
isFound = true
192199
return
193200
}
@@ -206,15 +213,16 @@ func (c *PlexClient) findFriend(id string) (isFound bool) {
206213
return
207214
}
208215

209-
c.friends = make(map[string]plexUser, len(response.Friends))
210216
for _, friend := range response.Friends {
211217
userId := strconv.Itoa(friend.UserId)
212-
c.friends[userId] = plexUser{
213-
Id: json.Number(userId),
214-
Username: friend.Username,
215-
Token: friend.AccessToken,
218+
if _, ok := c.friends[userId]; !ok {
219+
c.friends[userId] = plexUser{
220+
Id: friend.UserId,
221+
Username: friend.Username,
222+
Token: friend.AccessToken,
223+
}
216224
}
217-
if userId == id {
225+
if friend.UserId == id {
218226
isFound = true
219227
}
220228
}
@@ -226,11 +234,12 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
226234
return
227235
}
228236

237+
token := r.Header.Get(headerToken)
229238
clientUuid := r.Header.Get(headerClientIdentity)
230239
ratingKey := r.URL.Query().Get("ratingKey")
231240
playbackTime := r.URL.Query().Get("time")
232241
state := r.URL.Query().Get("state")
233-
if clientUuid == "" || ratingKey == "" || playbackTime == "" || state == "" {
242+
if token == "" || clientUuid == "" || ratingKey == "" || playbackTime == "" || state == "" {
234243
return
235244
}
236245

@@ -332,18 +341,27 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
332341
return
333342
}
334343

335-
userId := session.metadata.User.ID
344+
userId := c.GetUserId(token)
345+
userIdStr := strconv.Itoa(userId)
336346
username := session.metadata.User.Title
337347
if c.findFriend(userId) {
338-
username = c.friends[userId].Username
348+
username = c.friends[userIdStr].Username
349+
} else {
350+
// if not a friend and not in cache
351+
user := c.GetAccountInfo(token)
352+
if user.ID == userId {
353+
username = user.Username
354+
}
339355
}
340356

341357
webhook := plexhooks.PlexResponse{
342358
Event: event,
343359
Owner: true,
344360
User: false,
345361
Account: plexhooks.Account{
362+
Id: userId,
346363
Title: username,
364+
Thumb: session.metadata.User.Thumb,
347365
},
348366
Server: plexhooks.Server{
349367
Uuid: serverIdentifier,

handler/structs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"encoding/json"
54
"time"
65

76
"github.com/jrudio/go-plex-client"
@@ -27,7 +26,7 @@ type sessionData struct {
2726
}
2827

2928
type plexUser struct {
30-
Id json.Number
29+
Id int
3130
Username string
3231
Token string
3332
}

0 commit comments

Comments
 (0)