|
@@ -53,6 +53,8 @@ export default {
|
|
* }).
|
|
* }).
|
|
*/
|
|
*/
|
|
async set (jid, data) {
|
|
async set (jid, data) {
|
|
|
|
+ api.waitUntil('VCardsInitialized');
|
|
|
|
+
|
|
if (!jid) {
|
|
if (!jid) {
|
|
throw Error("No jid provided for the VCard data");
|
|
throw Error("No jid provided for the VCard data");
|
|
}
|
|
}
|
|
@@ -101,35 +103,40 @@ export default {
|
|
* });
|
|
* });
|
|
*/
|
|
*/
|
|
async get(model, force) {
|
|
async get(model, force) {
|
|
|
|
+ api.waitUntil("VCardsInitialized");
|
|
|
|
+
|
|
if (typeof model === "string") return fetchVCard(model);
|
|
if (typeof model === "string") return fetchVCard(model);
|
|
|
|
|
|
- // For a VCard fetch that returned an error, we
|
|
|
|
- // check how long ago it was fetched. If it was longer ago than
|
|
|
|
- // the last 7 days plus some jitter (to prevent an IQ fetch flood),
|
|
|
|
- // we try again.
|
|
|
|
- const { random, round } = Math;
|
|
|
|
- const subtract_flag = round(random());
|
|
|
|
const error_date = model.get("vcard_error");
|
|
const error_date = model.get("vcard_error");
|
|
- const tried_recently =
|
|
|
|
- error_date &&
|
|
|
|
- dayjs(error_date).isBefore(
|
|
|
|
- dayjs().subtract(7, "days")
|
|
|
|
- .subtract(round(random() * 24)*subtract_flag, "hours")
|
|
|
|
- .add(round(random() * 24)*(!subtract_flag ? 1 : 0), "hours")
|
|
|
|
- );
|
|
|
|
- if (tried_recently) return null;
|
|
|
|
|
|
+ if (error_date) {
|
|
|
|
+ // For a VCard fetch that returned an error, we check how long ago
|
|
|
|
+ // it was fetched. If it was longer ago than the last 21 days plus
|
|
|
|
+ // some jitter (to prevent an IQ fetch flood), we try again.
|
|
|
|
+ const { random, round } = Math;
|
|
|
|
+ const subtract_flag = round(random());
|
|
|
|
+ const recent_date = dayjs()
|
|
|
|
+ .subtract(21, "days")
|
|
|
|
+ .subtract(round(random() * 24) * subtract_flag, "hours")
|
|
|
|
+ .add(round(random() * 24) * (!subtract_flag ? 1 : 0), "hours");
|
|
|
|
+
|
|
|
|
+ const tried_recently = dayjs(error_date).isAfter(recent_date);
|
|
|
|
+ if (!force && tried_recently) return null;
|
|
|
|
+ }
|
|
|
|
|
|
- // For a successful VCard fetch, we check how long ago it was fetched.
|
|
|
|
- // If it was longer ago than the last 7 days plus some jitter
|
|
|
|
- // (to prevent an IQ fetch flood), we try again.
|
|
|
|
const vcard_updated = model.get("vcard_updated");
|
|
const vcard_updated = model.get("vcard_updated");
|
|
- const updated_recently =
|
|
|
|
- dayjs(vcard_updated).isBefore(
|
|
|
|
- dayjs().subtract(7, "days")
|
|
|
|
- .subtract(round(random() * 24)*subtract_flag, "hours")
|
|
|
|
- .add(round(random() * 24)*(!subtract_flag ? 1 : 0), "hours")
|
|
|
|
- );
|
|
|
|
- if (!force && updated_recently) return null;
|
|
|
|
|
|
+ if (vcard_updated) {
|
|
|
|
+ // For a successful VCard fetch, we check how long ago it was fetched.
|
|
|
|
+ // If it was longer ago than the last 7 days plus some jitter
|
|
|
|
+ // (to prevent an IQ fetch flood), we try again.
|
|
|
|
+ const { random, round } = Math;
|
|
|
|
+ const subtract_flag = round(random());
|
|
|
|
+ const recent_date = dayjs()
|
|
|
|
+ .subtract(7, "days")
|
|
|
|
+ .subtract(round(random() * 24) * subtract_flag, "hours")
|
|
|
|
+ .add(round(random() * 24) * (!subtract_flag ? 1 : 0), "hours");
|
|
|
|
+ const updated_recently = dayjs(vcard_updated).isAfter(recent_date);
|
|
|
|
+ if (!force && updated_recently) return null;
|
|
|
|
+ }
|
|
|
|
|
|
const jid = model.get("jid");
|
|
const jid = model.get("jid");
|
|
if (!jid) {
|
|
if (!jid) {
|
|
@@ -157,7 +164,12 @@ export default {
|
|
* });
|
|
* });
|
|
*/
|
|
*/
|
|
async update (model, force) {
|
|
async update (model, force) {
|
|
|
|
+ api.waitUntil('VCardsInitialized');
|
|
const data = await this.get(model, force);
|
|
const data = await this.get(model, force);
|
|
|
|
+ if (data === null) {
|
|
|
|
+ log.debug('api.vcard.update: null data returned, not updating the vcard');
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
model = typeof model === 'string' ? _converse.state.vcards.get(model) : model;
|
|
model = typeof model === 'string' ? _converse.state.vcards.get(model) : model;
|
|
if (!model) {
|
|
if (!model) {
|
|
log.error(`Could not find a VCard model for ${model}`);
|
|
log.error(`Could not find a VCard model for ${model}`);
|
|
@@ -165,7 +177,7 @@ export default {
|
|
}
|
|
}
|
|
if (Object.keys(data).length) {
|
|
if (Object.keys(data).length) {
|
|
delete data['stanza']
|
|
delete data['stanza']
|
|
- model.save(data);
|
|
|
|
|
|
+ u.safeSave(model, data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|