@crievo/omni 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/tools/omnichat.mjs +19 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crievo/omni",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "OmniVideo no seu computador: o CLI omni, o studio local e as ferramentas do pipeline.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1033,10 +1033,16 @@ async function status() {
1033
1033
  const pause = readJson(PAUSE_FILE);
1034
1034
  if (isPaused(pause, Date.now())) s.pause = pause;
1035
1035
  if (cloudOwns()) {
1036
- await Promise.all(videosWithRule().map(d => pullRecord(d).catch(() => {})));
1037
- /* the cloud answers: its pause and its token are the ones that count */
1036
+ /* the cloud answers: its pause and its token are the ones that count; its posts' updated_at say
1037
+ which records changed since the mirror, and only those are pulled */
1038
1038
  try {
1039
1039
  const c = await cloudApi("GET", "/api/status");
1040
+ const at = new Map((c.posts ?? []).map(p => [String(p.media_id), p.updated_at]));
1041
+ await Promise.all(
1042
+ videosWithRule()
1043
+ .filter(d => at.has(String(mediaOf(d))))
1044
+ .map(d => pullRecord(d, at.get(String(mediaOf(d)))).catch(() => {})),
1045
+ );
1040
1046
  s.cloud = { ready: true, active: !!c.active, expires: c.account?.expires ?? null };
1041
1047
  if (!c.active) s.cloud.reason = "a cloud não está respondendo (cloud push)";
1042
1048
  if (c.account?.paused_until && Date.parse(c.account.paused_until) > Date.now())
@@ -1167,19 +1173,26 @@ function syncRule(dir) {
1167
1173
  }
1168
1174
  const flushCloud = () => Promise.all(pendingCloud.splice(0));
1169
1175
 
1170
- /* the cloud's record mirrored into omnichat-record.json: the studio and the log read the file */
1171
- async function pullRecord(dir) {
1176
+ /* The cloud's record mirrored into omnichat-record.json: the studio and the log read the file.
1177
+ `cloudAt`: the post's updated_at from /api/status; when the mirror already has that version, nothing is
1178
+ asked (the studio's status runs every few minutes, and each record that leaves Neon counts against the
1179
+ free plan's monthly transfer). */
1180
+ async function pullRecord(dir, cloudAt = null) {
1172
1181
  if (!cloudOwns()) return;
1173
1182
  const media = mediaOf(dir);
1174
1183
  if (!media) return;
1184
+ const file = recordFile(dir);
1185
+ if (cloudAt && readJson(file)?.cloud_updated_at === cloudAt) return;
1175
1186
  try {
1176
1187
  const out = await cloudApi("GET", `/api/posts/${media}`);
1177
- const file = recordFile(dir);
1178
1188
  const all = readJson(file) ?? {};
1179
1189
  /* only a change is written: the write is a studio notice, and the notice makes the page read (and
1180
1190
  pull) again, so an unchanged record written anyway loops against the cloud */
1181
- if (JSON.stringify(all.instagram ?? null) === JSON.stringify(out.record ?? null)) return;
1191
+ const at = out.updated_at ?? null;
1192
+ if (JSON.stringify(all.instagram ?? null) === JSON.stringify(out.record ?? null) && all.cloud_updated_at === at)
1193
+ return;
1182
1194
  all.instagram = out.record;
1195
+ all.cloud_updated_at = at;
1183
1196
  delete all.cloud;
1184
1197
  writeJson(file, all);
1185
1198
  } catch (e) {