Browse Source

fix: add props.lastUpdated field for last update time

Gerald 8 years ago
parent
commit
21ec4b4515

+ 8 - 3
src/background/sync/base.js

@@ -1,5 +1,5 @@
 import { debounce, normalizeKeys, request, noop } from 'src/common';
-import { objectPurify } from 'src/common/object';
+import { objectSet, objectPurify } from 'src/common/object';
 import { getEventEmitter, getOption, setOption, hookOptions } from '../utils';
 import {
   getScripts,
@@ -385,6 +385,7 @@ export const BaseService = serviceFactory({
               if (obj.version === 2) {
                 data.config = obj.config;
                 data.custom = obj.custom;
+                data.props = obj.props;
               } else if (obj.version === 1) {
                 if (obj.more) {
                   data.custom = obj.more.custom;
@@ -392,6 +393,9 @@ export const BaseService = serviceFactory({
                     enabled: obj.more.enabled,
                     shouldUpdate: obj.more.update,
                   });
+                  data.props = objectPurify({
+                    lastUpdated: obj.more.lastUpdated,
+                  });
                 }
               }
             } catch (e) {
@@ -399,8 +403,7 @@ export const BaseService = serviceFactory({
             }
             // Invalid data
             if (!data.code) return;
-            const { modified } = info;
-            data.modified = modified;
+            if (info.modified) objectSet(data, 'props.lastModified', info.modified);
             const position = +info.position;
             if (position) data.position = position;
             if (!getOption('syncScriptStatus') && data.config) {
@@ -419,6 +422,7 @@ export const BaseService = serviceFactory({
             //   code,
             //   custom: script.custom,
             //   config: script.config,
+            //   props: objectPick(script.props, ['lastUpdated']),
             // };
             // XXX use version 1 to be compatible with Violentmonkey on other platforms
             const data = {
@@ -428,6 +432,7 @@ export const BaseService = serviceFactory({
                 custom: local.custom,
                 enabled: local.config.enabled,
                 update: local.config.shouldUpdate,
+                lastUpdated: local.props.lastUpdated,
               },
             };
             remoteMetaData.info[local.props.uri] = {

+ 5 - 2
src/background/utils/db.js

@@ -459,7 +459,7 @@ export function getExportData(ids, withValues) {
 
 export function parseScript(data) {
   const {
-    id, code, message, isNew, config, custom,
+    id, code, message, isNew, config, custom, props,
   } = data;
   const meta = parseMeta(code);
   if (!meta.name) return Promise.reject(i18n('msgInvalidScript'));
@@ -486,12 +486,15 @@ export function parseScript(data) {
       removed: 0, // force reset `removed` since this is an installation
     });
     script.custom = Object.assign({}, script.custom, custom);
+    script.props = Object.assign({}, script.props, {
+      lastModified: Date.now(),
+      lastUpdated: Date.now(),
+    }, props);
     script.meta = meta;
     if (!meta.homepageURL && !script.custom.homepageURL && isRemote(data.from)) {
       script.custom.homepageURL = data.from;
     }
     if (isRemote(data.url)) script.custom.lastInstallURL = data.url;
-    objectSet(script, 'props.lastModified', data.modified || Date.now());
     const position = +data.position;
     if (position) objectSet(script, 'props.position', position);
     buildPathMap(script);

+ 8 - 0
src/common/object.js

@@ -49,3 +49,11 @@ export function objectPurify(obj) {
   }
   return obj;
 }
+
+export function objectPick(obj, keys) {
+  return keys.reduce((res, key) => {
+    const value = obj ? obj[key] : null;
+    if (value != null) res[key] = value;
+    return res;
+  }, {});
+}

+ 9 - 7
src/options/views/script-item.vue

@@ -9,9 +9,9 @@
         <a class="ellipsis" :href="`mailto:${author.email}`" v-if="author.email" v-text="author.name"></a>
         <span class="ellipsis" v-else v-text="author.name"></span>
       </tooltip>
-      <tooltip :title="lastModified.title" align="end">
+      <tooltip :title="lastUpdated.title" align="end">
         <span v-text="script.meta.version ? `v${script.meta.version}` : ''"></span>
-        <span class="secondary" v-text="lastModified.show"></span>
+        <span class="secondary" v-text="lastUpdated.show"></span>
       </tooltip>
       <div v-if="script.config.removed" v-text="i18n('labelRemoved')"></div>
       <div v-if="script.config.removed">
@@ -134,11 +134,13 @@ export default {
     description() {
       return this.script.custom.description || getLocaleString(this.script.meta, 'description');
     },
-    lastModified() {
-      const { lastModified } = this.script.props;
+    lastUpdated() {
+      const { props } = this.script;
+      // XXX use `lastModified` as a fallback for scripts without `lastUpdated`
+      const lastUpdated = props.lastUpdated || props.lastModified;
       const ret = {};
-      if (lastModified) {
-        let delta = (Date.now() - lastModified) / 1000 / 60;
+      if (lastUpdated) {
+        let delta = (Date.now() - lastUpdated) / 1000 / 60;
         const units = [
           ['min', 60],
           ['h', 24],
@@ -152,7 +154,7 @@ export default {
           delta /= step;
           return false;
         });
-        const date = new Date(lastModified);
+        const date = new Date(lastUpdated);
         ret.title = this.i18n('labelLastUpdatedAt', date.toLocaleString());
         ret.show = `${delta | 0}${unitInfo[0]}`;
       }