Skip to content

Commit

Permalink
basically completed without deeply test
Browse files Browse the repository at this point in the history
TODO: better UI
  • Loading branch information
cnwangjie committed Aug 30, 2018
1 parent ff60778 commit b0014b3
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 29 deletions.
50 changes: 49 additions & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,61 @@
"ui_title_down_btn": {
"message": "move list down"
},
"ui_authorize": {
"ui_auth_with_google": {
"message": "Authorize with Google account"
},
"ui_auth_with_github": {
"message": "Authorize with Github account"
},
"ui_use_boss": {
"message": "Use Better-Onetab Sync Service (unlimited storage)"
},
"ui_logged_uid": {
"message": "Has authorized with uid:"
},
"ui_sync_exists_conflict_header": {
"message": "There is a conflict when sync with server."
},
"ui_sync_lists_failed": {
"message": "sync tab lists failed"
},
"ui_sync_opts_failed": {
"message": "sync options failed"
},
"ui_sync_remote_time": {
"message": "remote update time"
},
"ui_sync_local_time": {
"message": "local update time"
},
"ui_sync_lists_item": {
"message": "remote lists items"
},
"ui_opts_diff": {
"message": "options difference"
},
"ui_sync_keep_local": {
"message": "keep local"
},
"ui_sync_keep_remote": {
"message": "keep remote"
},
"ui_sync_keep_both": {
"message": "keep both"
},
"ui_sync_use_local": {
"message": "use local"
},
"ui_sync_use_remote": {
"message": "use remote"
},
"ui_syncing": {
"message": "syncing"
},
"ui_conflict": {
"message": "conflict"
},
"ui_not_sync": {
"message": "not_sync_yet"
}
}
52 changes: 50 additions & 2 deletions src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,61 @@
"ui_title_down_btn": {
"message": "向下移动列表"
},
"ui_authorize": {
"message": "授权获取Google ID"
"ui_auth_with_google": {
"message": "通过 Google 账号授权"
},
"ui_auth_with_github": {
"message": "通过 Github 账号授权"
},
"ui_use_boss": {
"message": "使用 Better Onetab 同步服务 (无限容量)"
},
"ui_logged_uid": {
"message": "已授权为UID: "
},
"ui_sync_exists_conflict_header": {
"message": "同步时发生冲突。"
},
"ui_sync_lists_failed": {
"message": "同步列表失败"
},
"ui_sync_opts_failed": {
"message": "同步设置失败"
},
"ui_sync_remote_time": {
"message": "远程更新时间"
},
"ui_sync_local_time": {
"message": "本地更新时间"
},
"ui_sync_lists_item": {
"message": "远程列表"
},
"ui_opts_diff": {
"message": "远程设置"
},
"ui_sync_keep_local": {
"message": "保留本地"
},
"ui_sync_keep_remote": {
"message": "保留远程"
},
"ui_sync_keep_both": {
"message": "同时保留"
},
"ui_sync_use_local": {
"message": "使用本地"
},
"ui_sync_use_remote": {
"message": "使用远程"
},
"ui_syncing": {
"message": "同步中"
},
"ui_conflict": {
"message": "冲突"
},
"ui_not_sync": {
"message": "未同步"
}
}
2 changes: 1 addition & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const init = async () => {
const opts = window.opts = await storage.getOptions() || {}
_.defaults(opts, options.getDefaultOptions())
await storage.setOptions(opts)
if (boss.hasToken()) await boss.forceDownloadRemoteImmediate()
if (await boss.hasToken()) await boss.forceDownloadRemoteImmediate()
updateBrowserAction(opts.browserAction)
setupContextMenus(opts.pageContext)
browser.runtime.onMessage.addListener(async msg => {
Expand Down
20 changes: 14 additions & 6 deletions src/common/service/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ const hasToken = async () => {
return tokenKey in await browser.storage.local.get(tokenKey)
}

const getToken = async () => {
const {[tokenKey]: existedToken} = await browser.storage.local.get(tokenKey)
if (existedToken) return existedToken
const getToken = async (auth) => {
const {[tokenKey]: existedToken, sync_info} = await browser.storage.local.get([tokenKey, 'sync_info'])
if (auth === 'google' && sync_info && sync_info.googleId && existedToken
|| auth === 'github' && sync_info && sync_info.githubId && existedToken
|| !auth && existedToken) return existedToken
else if (!['google', 'github'].includes(auth)) throw new Error('[boss]: unsupported auth')
console.log('[boss]: getting token')
const lend = browser.identity.getRedirectURL()
const authUrl = apiUrl + '/auth/google'
const authUrl = apiUrl + `/auth/${auth}`
const uid = sync_info ? sync_info.uid : null
const uidPart = uid ? `;uid:${uid}` : ''
const url = authUrl + '?state=ext:' + encodeURIComponent(lend) + uidPart
console.log('[boss]: url', url)
const to = await new Promise((resolve, reject) => {
chrome.identity.launchWebAuthFlow({
url: authUrl + '?state=ext:' + encodeURIComponent(lend),
url,
interactive: true,
}, to => {
const err = chrome.runtime.lastError
Expand Down Expand Up @@ -207,7 +214,7 @@ const forceDownloadRemoteImmediate = async () => {

const forceUpdate = async ({lists, opts}) => {
const works = []
const conflict = await browser.storage.local.get('conflict')
const conflict = (await browser.storage.local.get('conflict')).conflict || {}
if (lists) {
delete conflict.lists
works.push(async () => {
Expand Down Expand Up @@ -309,6 +316,7 @@ const resolveConflict = async ({type, result}) => {
}

export default {
getToken,
getInfo,
hasToken,
forceUpdate,
Expand Down
6 changes: 3 additions & 3 deletions src/page/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export default {
},
computed: {
tooltip() {
return this.syncing ? 'syncing'
: this.conflict ? 'conflict'
return this.syncing ? __('ui_syncing')
: this.conflict ? __('ui_conflict')
: isFinite(this.lastUpdated) ? null
: 'not sync yet'
: __('ui_not_sync')
}
},
watch: {
Expand Down
40 changes: 24 additions & 16 deletions src/page/Options.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,33 @@
<v-divider></v-divider>
<v-card-text v-if="conflict">
<div>
<h2>There is a conflict when sync with server.</h2>
<h2>{{ __('ui_sync_exists_conflict_header') }}</h2>
<div v-if="conflict.lists">
<h3 class="warning--text">sync tab lists failed</h3>
<h3><span class="grey--text">remote update time:</span> {{ formatTime(conflict.lists.remote.time) }}</h3>
<h3><span class="grey--text">local update time:</span> {{ formatTime(conflict.lists.local.time) }}</h3>
<h3>remote lists items</h3>
<h3 class="warning--text">{{ __('ui_sync_lists_failed') }}</h3>
<h3><span class="grey--text">{{ __('ui_sync_remote_time') }}:</span> {{ formatTime(conflict.lists.remote.time) }}</h3>
<h3><span class="grey--text">{{ __('ui_sync_local_time') }}:</span> {{ formatTime(conflict.lists.local.time) }}</h3>
<h3>{{ __('ui_sync_lists_item') }}</h3>
<ul>
<li v-for="list, index in conflict.lists.remote.lists" :key="index">
{{ list.title || '(untitled list)' }}: {{ list.tabs.length }} tabs
</li>
</ul>
<v-btn small @click="resolveConflict('lists', 'local')">keep local</v-btn>
<v-btn small @click="resolveConflict('lists', 'remote')">keep remote</v-btn>
<v-btn small @click="resolveConflict('lists', 'both')">keep both</v-btn>
<v-btn small @click="resolveConflict('lists', 'local')">{{ __('ui_sync_keep_local') }}</v-btn>
<v-btn small @click="resolveConflict('lists', 'remote')">{{ __('ui_sync_keep_remote') }}</v-btn>
<v-btn small @click="resolveConflict('lists', 'both')">{{ __('ui_sync_keep_both') }}</v-btn>
</div>
<div v-if="conflict.opts">
<h3 class="warning--text">sync options failed</h3>
<h3><span class="grey--text">remote update time:</span> {{ formatTime(conflict.opts.remote.time) }}</h3>
<h3><span class="grey--text">local update time:</span> {{ formatTime(conflict.opts.local.time) }}</h3>
<h3>options difference</h3>
<h3 class="warning--text">{{ __('ui_sync_opts_failed') }}</h3>
<h3><span class="grey--text">{{ __('ui_sync_remote_time') }}:</span> {{ formatTime(conflict.opts.remote.time) }}</h3>
<h3><span class="grey--text">{{ __('ui_sync_local_time') }}:</span> {{ formatTime(conflict.opts.local.time) }}</h3>
<h3>{{ __('ui_opts_diff') }}</h3>
<ul>
<li v-for="value, key in conflict.opts" v-if="(key in options) && options[key] !== value" :key="key">
{{ key }}: {{ value }}
</li>
</ul>
<v-btn small @click="resolveConflict('opts', 'local')">use local</v-btn>
<v-btn small @click="resolveConflict('opts', 'remote')">use remote</v-btn>
<v-btn small @click="resolveConflict('opts', 'local')">{{ __('ui_sync_use_local') }}</v-btn>
<v-btn small @click="resolveConflict('opts', 'remote')">{{ __('ui_sync_use_remote') }}</v-btn>
</div>
</div>
</v-card-text>
Expand All @@ -93,7 +93,7 @@
</v-card-title>

<v-card-actions>
<v-btn color="primary" large @click="auth">{{ __('ui_authorize') }}
<v-btn color="primary" large @click="authWithGoogle">{{ __('ui_auth_with_google') }}
<v-icon dark right>fab fa-google</v-icon>
</v-btn>
</v-card-actions>
Expand Down Expand Up @@ -167,7 +167,15 @@ export default {
this.conflict = _.isEmpty(conflict) ? null : conflict
}
},
async auth() {
async authWithGoogle() {
await boss.getToken('google')
return this.afterFirstAuth()
},
async authWithGithub() {
await boss.getToken('github')
return this.afterFirstAuth()
},
async afterFirstAuth() {
this.bossinfo = await boss.getInfo()
await browser.storage.local.set({sync_info: this.bossinfo})
this.loadSyncInfo()
Expand Down

0 comments on commit b0014b3

Please sign in to comment.