@@ -39,6 +39,7 @@ type PlexClient struct {
3939 serverIdentifier * string
4040 sections map [string ]plex.Directory
4141 sessions map [string ]sessionData
42+ friends map [string ]plexUser
4243
4344 mu sync.RWMutex
4445}
@@ -126,17 +127,29 @@ func (c *PlexClient) GetUserId(token string) (id int) {
126127 var err error
127128 ctx := context .Background ()
128129 cacheKey := fmt .Sprintf ("%s:token:%s" , cachePrefixPlex , token )
129- id , err = redisClient .Get (ctx , cacheKey ).Int ()
130- if err == nil {
131- return id
130+
131+ isCacheEnabled := redisClient != nil
132+ if isCacheEnabled {
133+ id , err = redisClient .Get (ctx , cacheKey ).Int ()
134+ if err == nil {
135+ return id
136+ }
132137 }
133138
134- response := c .GetSharedServers ()
135- for _ , friend := range response .Friends {
136- key := fmt .Sprintf ("%s:token:%s" , cachePrefixPlex , friend .AccessToken )
137- redisClient .Set (ctx , key , friend .UserId , 0 )
138- if friend .AccessToken == token {
139- id = friend .UserId
139+ // refresh the list of friends
140+ c .findFriend ("" )
141+ for _ , friend := range c .friends {
142+ if friend .Token == token {
143+ if id64 , err := friend .Id .Int64 (); err == nil {
144+ id = int (id64 )
145+ if ! isCacheEnabled {
146+ break
147+ }
148+ }
149+ }
150+ if isCacheEnabled {
151+ key := fmt .Sprintf ("%s:token:%s" , cachePrefixPlex , friend .Token )
152+ redisClient .Set (ctx , key , friend .Id , 0 )
140153 }
141154 }
142155 if id > 0 {
@@ -145,24 +158,14 @@ func (c *PlexClient) GetUserId(token string) (id int) {
145158
146159 user := c .GetAccountInfo (token )
147160 if user .ID > 0 {
148- redisClient .Set (ctx , cacheKey , user .ID , 0 )
149161 id = user .ID
162+ if isCacheEnabled {
163+ redisClient .Set (ctx , cacheKey , user .ID , 0 )
164+ }
150165 }
151166 return
152167}
153168
154- func (c * PlexClient ) GetSharedServers () (response plex.SharedServersResponse ) {
155- c .mu .RLock ()
156- defer c .mu .RUnlock ()
157-
158- identifier := c .getServerIdentifier ()
159- if identifier == "" {
160- return
161- }
162- response , _ = c .client .GetSharedServers (identifier )
163- return
164- }
165-
166169func (c * PlexClient ) GetAccountInfo (token string ) (user plex.UserPlexTV ) {
167170 if c .client .Token != token {
168171 c .mu .Lock ()
@@ -181,6 +184,43 @@ func (c *PlexClient) GetAccountInfo(token string) (user plex.UserPlexTV) {
181184 return
182185}
183186
187+ func (c * PlexClient ) findFriend (id string ) (isFound bool ) {
188+ if id != "" {
189+ for _ , friend := range c .friends {
190+ if friend .Id .String () == id {
191+ isFound = true
192+ return
193+ }
194+ }
195+ }
196+
197+ c .mu .RLock ()
198+ defer c .mu .RUnlock ()
199+
200+ identifier := c .getServerIdentifier ()
201+ if identifier == "" {
202+ return
203+ }
204+ response , err := c .client .GetSharedServers (identifier )
205+ if err != nil {
206+ return
207+ }
208+
209+ c .friends = make (map [string ]plexUser , len (response .Friends ))
210+ for _ , friend := range response .Friends {
211+ userId := strconv .Itoa (friend .UserId )
212+ c .friends [userId ] = plexUser {
213+ Id : json .Number (userId ),
214+ Username : friend .Username ,
215+ Token : friend .AccessToken ,
216+ }
217+ if userId == id {
218+ isFound = true
219+ }
220+ }
221+ return
222+ }
223+
184224func (c * PlexClient ) syncTimelineWithPlaxt (r * http.Request ) {
185225 if c .plaxtUrl == "" || c .client .Token == "" {
186226 return
@@ -290,12 +330,18 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request) {
290330 return
291331 }
292332
333+ userId := session .metadata .User .ID
334+ username := session .metadata .User .Title
335+ if c .findFriend (userId ) {
336+ username = c .friends [userId ].Username
337+ }
338+
293339 webhook := plexhooks.PlexResponse {
294340 Event : event ,
295341 Owner : true ,
296342 User : false ,
297343 Account : plexhooks.Account {
298- Title : session . metadata . User . Title ,
344+ Title : username ,
299345 },
300346 Server : plexhooks.Server {
301347 Uuid : serverIdentifier ,
0 commit comments