11/**
22 * @typedef {import('@converse/skeletor').Model } Model
33 */
4- import log from " @converse/log" ;
4+ import log from ' @converse/log' ;
55import _converse from '../../shared/_converse.js' ;
66import api from '../../shared/api/index.js' ;
7- import converse from " ../../shared/api/public.js" ;
7+ import converse from ' ../../shared/api/public.js' ;
88import { createStanza , fetchVCard } from './utils.js' ;
99
1010const { Strophe, dayjs, u, stx } = converse . env ;
@@ -40,8 +40,8 @@ export default {
4040 * // Failure, e is your error object
4141 * }).
4242 */
43- async set ( jid , data ) {
44- if ( ! jid ) throw Error ( " No jid provided for the VCard data" ) ;
43+ async set ( jid , data ) {
44+ if ( ! jid ) throw Error ( ' No jid provided for the VCard data' ) ;
4545 api . waitUntil ( 'VCardsInitialized' ) ;
4646
4747 let vcard = _converse . state . vcards . get ( jid ) ;
@@ -72,10 +72,10 @@ export default {
7272 </PHOTO>
7373 </vCard>` ;
7474 try {
75- result = await api . sendIQ ( createStanza ( " set" , jid , vcard_el ) ) ;
75+ result = await api . sendIQ ( createStanza ( ' set' , jid , vcard_el ) ) ;
7676 } catch ( e ) {
7777 if ( old_vcard_attrs ) vcard . save ( old_vcard_attrs ) ;
78- throw ( e ) ;
78+ throw e ;
7979 }
8080
8181 vcard = await api . vcard . update ( jid , true ) ;
@@ -115,42 +115,35 @@ export default {
115115
116116 if ( typeof model === "string" ) return fetchVCard ( model ) ;
117117
118- const error_date = model . get ( "vcard_error" ) ;
119- if ( error_date ) {
120- // For a VCard fetch that returned an error, we check how long ago
121- // it was fetched. If it was longer ago than the last 21 days plus
122- // some jitter (to prevent an IQ fetch flood), we try again.
123- const { random, round } = Math ;
124- const subtract_flag = round ( random ( ) ) ;
125- const recent_date = dayjs ( )
126- . subtract ( 21 , "days" )
127- . subtract ( round ( random ( ) * 24 ) * subtract_flag , "hours" )
128- . add ( round ( random ( ) * 24 ) * ( ! subtract_flag ? 1 : 0 ) , "hours" ) ;
118+ const jid = model . get ( 'jid' ) ;
119+ const vcard_updated = model . get ( "vcard_updated" ) ;
120+ const vcard_error = model . get ( "vcard_error" ) ;
129121
130- const tried_recently = dayjs ( error_date ) . isAfter ( recent_date ) ;
131- if ( ! force && tried_recently ) return null ;
132- }
122+ if ( vcard_updated || vcard_error ) {
123+ const muc = _converse . state . chatboxes . get ( jid ) ;
124+ if ( ! muc || ! force ) return null ;
133125
134- const vcard_updated = model . get ( "vcard_updated" ) ;
135- if ( vcard_updated ) {
136- // For a successful VCard fetch, we check how long ago it was fetched.
137- // If it was longer ago than the last 7 days plus some jitter
138- // (to prevent an IQ fetch flood), we try again.
126+ // We don't get XEP-0153 presence updates for MUCs, so we need
127+ // to refresh from time to time.
128+ // We check how long ago it was fetched. If it was longer ago than
129+ // the last 7 days plus some jitter (to prevent an IQ fetch flood),
130+ // we try again.
139131 const { random, round } = Math ;
140132 const subtract_flag = round ( random ( ) ) ;
141133 const recent_date = dayjs ( )
142134 . subtract ( 7 , "days" )
143135 . subtract ( round ( random ( ) * 24 ) * subtract_flag , "hours" )
144136 . add ( round ( random ( ) * 24 ) * ( ! subtract_flag ? 1 : 0 ) , "hours" ) ;
137+
145138 const updated_recently = dayjs ( vcard_updated ) . isAfter ( recent_date ) ;
146139 if ( ! force && updated_recently ) return null ;
147140 }
148141
149- const jid = model . get ( "jid" ) ;
150142 if ( ! jid ) {
151- log . error ( " No JID to get vcard for" ) ;
143+ log . error ( ' No JID to get vcard for' ) ;
152144 return null ;
153145 }
146+
154147 return fetchVCard ( jid ) ;
155148 } ,
156149
@@ -171,7 +164,7 @@ export default {
171164 * api.vcard.update(chatbox);
172165 * });
173166 */
174- async update ( model , force ) {
167+ async update ( model , force ) {
175168 api . waitUntil ( 'VCardsInitialized' ) ;
176169 const data = await this . get ( model , force ) ;
177170 if ( data === null ) {
@@ -184,10 +177,10 @@ export default {
184177 return ;
185178 }
186179 if ( Object . keys ( data ) . length ) {
187- delete data [ 'stanza' ]
180+ delete data [ 'stanza' ] ;
188181 u . safeSave ( model , data ) ;
189182 }
190183 return model ;
191- }
192- }
193- }
184+ } ,
185+ } ,
186+ } ;
0 commit comments