@@ -34,7 +34,7 @@ export class PrivateMessageManager {
3434 this . windows . set ( username , window ) ;
3535 this . windowContainer . appendChild ( window . element ) ;
3636
37- // Load PM history
37+ // Load PM history or create it if first time
3838 this . loadPMHistory ( username ) ;
3939
4040 // Focus input
@@ -230,8 +230,8 @@ export class PrivateMessageManager {
230230 renderPMMessages ( username ) {
231231 const window = this . windows . get ( username ) ;
232232 if ( ! window ) return ;
233-
234- const html = window . messages . map ( msg => {
233+ let msgs = Array . isArray ( window . messages ) ? window . messages : [ ] ;
234+ const html = msgs . map ( msg => {
235235 const date = new Date ( msg . time ) . toLocaleTimeString ( [ ] , {
236236 hour : '2-digit' ,
237237 minute : '2-digit'
@@ -257,36 +257,42 @@ export class PrivateMessageManager {
257257 try {
258258 // Generate conversation ID (sorted usernames for consistency)
259259 const conversationId = [ this . app . user , username ] . sort ( ) . join ( '_' ) ;
260-
261260 // Fetch from server - encode conversationId to handle special characters
262261 const res = await fetch ( `${ this . app . baseURL } /pm/${ encodeURIComponent ( conversationId ) } ?user=${ encodeURIComponent ( this . app . user ) } ` , {
263262 headers : this . app . getAuthHeaders ( false ) // No Content-Type for GET requests
264263 } ) ;
265-
266- if ( res . ok ) {
267- const data = await res . json ( ) ;
268- const window = this . windows . get ( username ) ;
264+ const window = this . windows . get ( username ) ;
265+
266+ if ( res . status === 404 ) {
267+ // First time PM; show empty convo (don't treat as error)
269268 if ( window ) {
270- window . messages = data . messages || [ ] ;
269+ window . messages = [ ] ;
271270 this . renderPMMessages ( username ) ;
272271 }
273- } else {
274- console . warn ( 'Failed to load PM history:' , res . status ) ;
275- // Fallback to local storage
276- const history = this . app . loadFromStorage ( `pm_history_ ${ username } ` ) || [ ] ;
277- const window = this . windows . get ( username ) ;
272+ return ;
273+ }
274+ if ( res . ok ) {
275+ let data = await res . json ( ) ;
276+ if ( ! Array . isArray ( data . messages ) ) data . messages = [ ] ;
278277 if ( window ) {
279- window . messages = history ;
278+ window . messages = data . messages ;
280279 this . renderPMMessages ( username ) ;
281280 }
281+ return ;
282+ }
283+
284+ // Non-404 errors, fallback to local storage or show as empty
285+ const history = await this . app . loadFromStorage ( `pm_history_${ username } ` ) || [ ] ;
286+ if ( window ) {
287+ window . messages = Array . isArray ( history ) ? history : [ ] ;
288+ this . renderPMMessages ( username ) ;
282289 }
283290 } catch ( e ) {
284291 console . warn ( 'Failed to load PM history:' , e ) ;
285- // Fallback to local storage
286- const history = this . app . loadFromStorage ( `pm_history_${ username } ` ) || [ ] ;
287292 const window = this . windows . get ( username ) ;
293+ const history = await this . app . loadFromStorage ( `pm_history_${ username } ` ) || [ ] ;
288294 if ( window ) {
289- window . messages = history ;
295+ window . messages = Array . isArray ( history ) ? history : [ ] ;
290296 this . renderPMMessages ( username ) ;
291297 }
292298 }
@@ -320,4 +326,4 @@ export class PrivateMessageManager {
320326 }
321327 return conversations ;
322328 }
323- }
329+ }
0 commit comments