From 9318b1c3cedae4ee8dfad30280a13048883979de Mon Sep 17 00:00:00 2001 From: Bruce Liu Date: Sat, 15 Oct 2022 15:32:07 -0700 Subject: [PATCH] update services, fix json and ui drag --- dist/styles/global.css | 2 +- src/components/settings/services/miniflux.tsx | 1 - .../settings/services/nextcloud.tsx | 19 ++--- src/scripts/i18n/cs.json | 4 +- src/scripts/models/services/miniflux.ts | 79 +++++++++---------- src/scripts/models/services/nextcloud.ts | 51 ++++++------ 6 files changed, 67 insertions(+), 89 deletions(-) diff --git a/dist/styles/global.css b/dist/styles/global.css index 8b6e63f..89bb2c1 100644 --- a/dist/styles/global.css +++ b/dist/styles/global.css @@ -237,7 +237,7 @@ body.darwin .btn-group .seperator { height: var(--navHeight); line-height: var(--navHeight); } -body.darwin.not-fullscreen #root > nav .btn-group .btn:first-of-type { +body.darwin.not-fullscreen #root > nav .btn-group:first-of-type { margin-left: 72px; } #root > nav .btn-group .btn.system { diff --git a/src/components/settings/services/miniflux.tsx b/src/components/settings/services/miniflux.tsx index 32bdc04..221ab41 100644 --- a/src/components/settings/services/miniflux.tsx +++ b/src/components/settings/services/miniflux.tsx @@ -17,7 +17,6 @@ import { } from "@fluentui/react" import DangerButton from "../../utils/danger-button" import { urlTest } from "../../../scripts/utils" -import LiteExporter from "./lite-exporter" import { MinifluxConfigs } from "../../../scripts/models/services/miniflux" type MinifluxConfigsTabState = { diff --git a/src/components/settings/services/nextcloud.tsx b/src/components/settings/services/nextcloud.tsx index 11d1f26..b8fbe25 100644 --- a/src/components/settings/services/nextcloud.tsx +++ b/src/components/settings/services/nextcloud.tsx @@ -18,7 +18,6 @@ import { } from "@fluentui/react" import DangerButton from "../../utils/danger-button" import { urlTest } from "../../../scripts/utils" -import LiteExporter from "./lite-exporter" type NextcloudConfigsTabState = { existing: boolean @@ -129,17 +128,14 @@ class NextcloudConfigsTab extends React.Component< )} - - - + /> @@ -161,7 +157,7 @@ class NextcloudConfigsTab extends React.Component< - + - {this.state.existing && ( - - )} ) diff --git a/src/scripts/i18n/cs.json b/src/scripts/i18n/cs.json index 7794962..f5c59de 100644 --- a/src/scripts/i18n/cs.json +++ b/src/scripts/i18n/cs.json @@ -41,8 +41,8 @@ "refresh": "Načíst znovu", "markAllRead": "Označit vše jako přečtené", "notifications": "Notifikace", - "view": "Zobrazit" - "settings": "Nastavení" + "view": "Zobrazit", + "settings": "Nastavení", "minimize": "Maximalizovat", "maximize": "Minimalizovat" }, diff --git a/src/scripts/models/services/miniflux.ts b/src/scripts/models/services/miniflux.ts index b4d2955..f949507 100644 --- a/src/scripts/models/services/miniflux.ts +++ b/src/scripts/models/services/miniflux.ts @@ -128,18 +128,18 @@ export const minifluxServiceHooks: ServiceHooks = { // fetch entries from after the last fetched id (if exists) // limit by quantity and maximum safe integer (id) // NOTE: miniflux endpoint /entries default order with "published at", and does not offer "created_at" - // but does offer id sort, directly correlated with "created". some feeds give strange published_at. + // but does offer id sort, directly correlated with "created". some feeds give strange published_at. fetchItems: () => async (_, getState) => { const state = getState() const configs = state.service as MinifluxConfigs - let items: Entry[] = new Array() + const items: Entry[] = new Array() let entriesResponse: Entries // parameters configs.lastId = configs.lastId ?? 0 // intermediate - const quantity = 100 + const quantity = 125 let continueId: number do { @@ -147,37 +147,28 @@ export const minifluxServiceHooks: ServiceHooks = { if (continueId) { entriesResponse = await fetchAPI( configs, - `entries? - order=id - &direction=desc - &after_entry_id=${configs.lastId} - &before_entry_id=${continueId} - &limit=${quantity}` + `entries?order=id&direction=desc&after_entry_id=${configs.lastId}&before_entry_id=${continueId}&limit=${quantity}` ).then(response => response.json()) } else { entriesResponse = await fetchAPI( configs, - `entries? - order=id - &direction=desc - &after_entry_id=${configs.lastId} - &limit=${quantity}` + `entries?order=id&direction=desc&after_entry_id=${configs.lastId}&limit=${quantity}` ).then(response => response.json()) } - items = entriesResponse.entries.concat(items) + items.push(...entriesResponse.entries) continueId = items[items.length - 1].id } catch { break } } while ( entriesResponse.entries && - entriesResponse.total === 100 && + entriesResponse.total >= quantity && items.length < configs.fetchLimit ) // break/return nothing if no new items acquired - if (items.length == 0) return [[], configs] + if (items.length === 0) return [[], configs] configs.lastId = items[0].id // get sources that possess ref/id given by service, associate new items @@ -245,7 +236,10 @@ export const minifluxServiceHooks: ServiceHooks = { configs, "entries?starred=true" ).then(response => response.json()) - const [unread, starred] = await Promise.all([unreadPromise, starredPromise]) + const [unread, starred] = await Promise.all([ + unreadPromise, + starredPromise, + ]) return [ new Set(unread.entries.map((entry: Entry) => String(entry.id))), @@ -257,9 +251,9 @@ export const minifluxServiceHooks: ServiceHooks = { if (!item.serviceRef) return const body = `{ - "entry_ids": [${item.serviceRef}], - "status": "read" - }` + "entry_ids": [${item.serviceRef}], + "status": "read" + }` const response = await fetchAPI( getState().service as MinifluxConfigs, @@ -275,9 +269,9 @@ export const minifluxServiceHooks: ServiceHooks = { if (!item.serviceRef) return const body = `{ - "entry_ids": [${item.serviceRef}], - "status": "unread" - }` + "entry_ids": [${item.serviceRef}], + "status": "unread" + }` await fetchAPI( getState().service as MinifluxConfigs, "entries", @@ -296,7 +290,8 @@ export const minifluxServiceHooks: ServiceHooks = { // if null, state consulted for context sids markAllRead: (sids, date, before) => async (_, getState) => { - let refs: string[] + const state = getState() + const configs = state.service as MinifluxConfigs if (date) { const predicates: lf.Predicate[] = [ @@ -311,26 +306,24 @@ export const minifluxServiceHooks: ServiceHooks = { .from(db.items) .where(query) .exec() - refs = rows.map(row => row["serviceRef"]) + const refs = rows.map(row => row["serviceRef"]) + const body = `{ + "entry_ids": [${refs}], + "status": "read" + }` + await fetchAPI(configs, "entries", "PUT", body) } else { - const state = getState() - const items = state.feeds[state.page.feedId].iids - .map(iid => state.items[iid]) - .filter(item => item.serviceRef && !item.hasRead) - refs = items.map(item => item.serviceRef) + const sources = state.sources + await Promise.all( + sids.map(sid => + fetchAPI( + configs, + `feeds/${sources[sid]?.serviceRef}/mark-all-as-read`, + "PUT" + ) + ) + ) } - - const body = `{ - "entry_ids": [${refs}], - "status": "read" - }` - - await fetchAPI( - getState().service as MinifluxConfigs, - "entries", - "PUT", - body - ) }, star: (item: RSSItem) => async (_, getState) => { diff --git a/src/scripts/models/services/nextcloud.ts b/src/scripts/models/services/nextcloud.ts index 86ff545..cd56bd1 100644 --- a/src/scripts/models/services/nextcloud.ts +++ b/src/scripts/models/services/nextcloud.ts @@ -98,7 +98,10 @@ export const nextcloudServiceHooks: ServiceHooks = { source.iconurl = s.faviconLink source.serviceRef = String(s.id) if (s.folderId && groupsByTagId.has(String(s.folderId))) { - groupsMap.set(String(s.id), groupsByTagId.get(String(s.folderId))) + groupsMap.set( + String(s.id), + groupsByTagId.get(String(s.folderId)) + ) } return source }) @@ -107,7 +110,7 @@ export const nextcloudServiceHooks: ServiceHooks = { syncItems: () => async (_, getState) => { const configs = getState().service as NextcloudConfigs - const [unreadResponse, starredResponse]= await Promise.all([ + const [unreadResponse, starredResponse] = await Promise.all([ fetchAPI(configs, "/items?getRead=false&type=3&batchSize=-1"), fetchAPI(configs, "/items?getRead=true&type=2&batchSize=-1"), ]) @@ -133,19 +136,14 @@ export const nextcloudServiceHooks: ServiceHooks = { //first sync let min = Number.MAX_SAFE_INTEGER do { - try { - const response = await fetchAPI( - configs, - "/items?getRead=true&type=3&batchSize=125&offset=" + min - ) - if (response.status !== 200) throw APIError() - lastFetched = await response.json() - items = [ ...items, ...lastFetched.items] - min = lastFetched.items.reduce((m, n) => Math.min(m, n.id), min) - } catch (error) { - console.log(error) - break - } + const response = await fetchAPI( + configs, + "/items?getRead=true&type=3&batchSize=125&offset=" + min + ) + if (response.status !== 200) throw APIError() + lastFetched = await response.json() + items = [...items, ...lastFetched.items] + min = lastFetched.items.reduce((m, n) => Math.min(m, n.id), min) } while ( lastFetched.items && lastFetched.items.length >= 125 && @@ -155,18 +153,14 @@ export const nextcloudServiceHooks: ServiceHooks = { //incremental sync const response = await fetchAPI( configs, - "/items/updated?lastModified="+configs.lastModified+"&type=3" + "/items/updated?lastModified=" + + configs.lastModified + + "&type=3" ) if (response.status !== 200) throw APIError() lastFetched = (await response.json()).items - items.push( - ...lastFetched.filter( - i => i.id > configs.lastId - ) - ) - + items.push(...lastFetched.filter(i => i.id > configs.lastId)) } - const previousLastModified = configs.lastModified configs.lastModified = items.reduce( (m, n) => Math.max(m, n.lastModified), configs.lastModified @@ -175,7 +169,6 @@ export const nextcloudServiceHooks: ServiceHooks = { (m, n) => Math.max(m, n.id), configs.lastId ) - console.log("last modified from "+ previousLastModified + " to " + configs.lastModified) configs.lastModified++ //+1 to avoid fetching articles with same lastModified next time if (items.length > 0) { const fidMap = new Map() @@ -184,7 +177,7 @@ export const nextcloudServiceHooks: ServiceHooks = { fidMap.set(source.serviceRef, source) } } - + const parsedItems = new Array() items.forEach(i => { if (i.body === null || i.url === null) return @@ -196,8 +189,8 @@ export const nextcloudServiceHooks: ServiceHooks = { source: source.sid, title: i.title, link: i.url, - date: new Date(i.pubDate*1000), - fetchedDate: new Date(i.pubDate*1000), + date: new Date(i.pubDate * 1000), + fetchedDate: new Date(), content: i.body, snippet: dom.documentElement.textContent.trim(), creator: i.author, @@ -207,7 +200,7 @@ export const nextcloudServiceHooks: ServiceHooks = { notify: false, serviceRef: String(i.id), } as RSSItem - if (i.enclosureLink ) { + if (i.enclosureLink) { item.thumb = i.enclosureLink } else { let baseEl = dom.createElement("base") @@ -235,7 +228,7 @@ export const nextcloudServiceHooks: ServiceHooks = { "POST", [i.id] ) - + parsedItems.push(item) }) return [parsedItems, configs]