bug fixes for #30 and #31

This commit is contained in:
刘浩远 2020-07-06 14:54:25 +08:00
parent f3aac967eb
commit 27c81788e7
6 changed files with 39 additions and 24 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
dist/article/article.js text eol=lf

View File

@ -30,7 +30,9 @@
"log": { "log": {
"empty": "No notifications", "empty": "No notifications",
"fetchFailure": "Failed to load source \"{name}\".", "fetchFailure": "Failed to load source \"{name}\".",
"fetchSuccess": "Successfully fetched {count, plural, =1 {# article} other {# articles}}." "fetchSuccess": "Successfully fetched {count, plural, =1 {# article} other {# articles}}.",
"networkError": "A network error has occurred.",
"parseError": "An error has occurred when parsing the XML feed."
}, },
"nav": { "nav": {
"menu": "Menu", "menu": "Menu",

View File

@ -30,7 +30,9 @@
"log": { "log": {
"empty": "无消息", "empty": "无消息",
"fetchFailure": "无法加载订阅源“{name}”", "fetchFailure": "无法加载订阅源“{name}”",
"fetchSuccess": "成功加载 {count} 篇文章" "fetchSuccess": "成功加载 {count} 篇文章",
"networkError": "连接订阅源时出错",
"parseError": "解析XML信息流时出错"
}, },
"nav": { "nav": {
"menu": "菜单", "menu": "菜单",

View File

@ -1,6 +1,6 @@
import * as db from "../db" import * as db from "../db"
import { SourceActionTypes, INIT_SOURCES, ADD_SOURCE, DELETE_SOURCE } from "./source" import { SourceActionTypes, INIT_SOURCES, ADD_SOURCE, DELETE_SOURCE } from "./source"
import { ItemActionTypes, FETCH_ITEMS, RSSItem, MARK_READ, MARK_UNREAD, TOGGLE_STARRED, TOGGLE_HIDDEN, applyItemReduction, ItemState } from "./item" import { ItemActionTypes, FETCH_ITEMS, RSSItem, MARK_READ, MARK_UNREAD, TOGGLE_STARRED, TOGGLE_HIDDEN, applyItemReduction, ItemState, MARK_ALL_READ } from "./item"
import { ActionStatus, AppThunk, mergeSortedArrays } from "../utils" import { ActionStatus, AppThunk, mergeSortedArrays } from "../utils"
import { PageActionTypes, SELECT_PAGE, PageType, APPLY_FILTER } from "./page" import { PageActionTypes, SELECT_PAGE, PageType, APPLY_FILTER } from "./page"

View File

@ -2,7 +2,7 @@ import * as db from "../db"
import intl from "react-intl-universal" import intl from "react-intl-universal"
import { domParser, htmlDecode, ActionStatus, AppThunk } from "../utils" import { domParser, htmlDecode, ActionStatus, AppThunk } from "../utils"
import { RSSSource } from "./source" import { RSSSource } from "./source"
import { FeedActionTypes, INIT_FEED, LOAD_MORE } from "./feed" import { FeedActionTypes, INIT_FEED, LOAD_MORE, FilterType, initFeeds } from "./feed"
import Parser from "@yang991178/rss-parser" import Parser from "@yang991178/rss-parser"
export class RSSItem { export class RSSItem {
@ -38,19 +38,17 @@ export class RSSItem {
item.content = parsed.content || "" item.content = parsed.content || ""
item.snippet = htmlDecode(parsed.contentSnippet || "") item.snippet = htmlDecode(parsed.contentSnippet || "")
} }
if (parsed.thumb) item.thumb = parsed.thumb if (parsed.thumb) {
else if (parsed.image) { item.thumb = parsed.thumb
if (parsed.image.$ && parsed.image.$.url) { } else if (parsed.image && parsed.image.$ && parsed.image.$.url) {
item.thumb = parsed.image.$.url item.thumb = parsed.image.$.url
} else if (typeof parsed.image === "string") { } else if (parsed.image && typeof parsed.image === "string") {
item.thumb = parsed.image item.thumb = parsed.image
} } else if (parsed.mediaContent) {
}
else if (parsed.mediaContent) {
let images = parsed.mediaContent.filter(c => c.$ && c.$.medium === "image" && c.$.url) let images = parsed.mediaContent.filter(c => c.$ && c.$.medium === "image" && c.$.url)
if (images.length > 0) item.thumb = images[0].$.url if (images.length > 0) item.thumb = images[0].$.url
} }
if(!item.thumb) { if (!item.thumb) {
let dom = domParser.parseFromString(item.content, "text/html") let dom = domParser.parseFromString(item.content, "text/html")
let baseEl = dom.createElement('base') let baseEl = dom.createElement('base')
baseEl.setAttribute('href', item.link.split("/").slice(0, 3).join("/")) baseEl.setAttribute('href', item.link.split("/").slice(0, 3).join("/"))
@ -58,6 +56,9 @@ export class RSSItem {
let img = dom.querySelector("img") let img = dom.querySelector("img")
if (img && img.src) item.thumb = img.src if (img && img.src) item.thumb = img.src
} }
if (item.thumb && !item.thumb.startsWith("https:") && !item.thumb.startsWith("http:")) {
delete item.thumb
}
} }
} }
@ -65,7 +66,7 @@ export type ItemState = {
[_id: string]: RSSItem [_id: string]: RSSItem
} }
export const FETCH_ITEMS = 'FETCH_ITEMS' export const FETCH_ITEMS = "FETCH_ITEMS"
export const MARK_READ = "MARK_READ" export const MARK_READ = "MARK_READ"
export const MARK_ALL_READ = "MARK_ALL_READ" export const MARK_ALL_READ = "MARK_ALL_READ"
export const MARK_UNREAD = "MARK_UNREAD" export const MARK_UNREAD = "MARK_UNREAD"
@ -223,8 +224,8 @@ export function markRead(item: RSSItem): AppThunk {
export function markAllRead(sids: number[] = null): AppThunk { export function markAllRead(sids: number[] = null): AppThunk {
return (dispatch, getState) => { return (dispatch, getState) => {
let state = getState()
if (sids === null) { if (sids === null) {
let state = getState()
let feed = state.feeds[state.page.feedId] let feed = state.feeds[state.page.feedId]
sids = feed.sids sids = feed.sids
} }
@ -235,6 +236,9 @@ export function markAllRead(sids: number[] = null): AppThunk {
} }
}) })
dispatch(markAllReadDone(sids)) dispatch(markAllReadDone(sids))
if (!(state.page.filter.type & FilterType.ShowRead)) {
dispatch(initFeeds(true))
}
} }
} }

View File

@ -1,3 +1,4 @@
import intl from "react-intl-universal"
import { ThunkAction, ThunkDispatch } from "redux-thunk" import { ThunkAction, ThunkDispatch } from "redux-thunk"
import { AnyAction } from "redux" import { AnyAction } from "redux"
import { RootState } from "./reducer" import { RootState } from "./reducer"
@ -27,15 +28,20 @@ const rssParser = new Parser({
}) })
export async function parseRSS(url: string) { export async function parseRSS(url: string) {
let result: Response
try { try {
let result = await fetch(url, { credentials: "omit" }) result = await fetch(url, { credentials: "omit" })
if (result.ok) {
return await rssParser.parseString(await result.text())
} else {
throw new Error(result.statusText)
}
} catch { } catch {
throw new Error("A network error has occurred.") throw new Error(intl.get("log.networkError"))
}
if (result && result.ok) {
try {
return await rssParser.parseString(await result.text())
} catch {
throw new Error(intl.get("log.parseError"))
}
} else {
throw new Error(result.statusText)
} }
} }