Merge branch 'main' into upload-image

zio/stable
João Ferreiro 2022-11-29 10:29:19 +00:00
commit 77ea3bfd0a
16 changed files with 255 additions and 137 deletions

View File

@ -1,6 +1,7 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'
import {MembershipsViewModel} from './memberships-view'
import {NotificationsViewModel} from './notifications-view'
export class MeModel {
did?: string
@ -9,9 +10,11 @@ export class MeModel {
description?: string
notificationCount: number = 0
memberships?: MembershipsViewModel
notifications: NotificationsViewModel
constructor(public rootStore: RootStoreModel) {
makeAutoObservable(this, {rootStore: false}, {autoBind: true})
this.notifications = new NotificationsViewModel(this.rootStore, {})
}
clear() {
@ -43,7 +46,12 @@ export class MeModel {
this.memberships = new MembershipsViewModel(this.rootStore, {
actor: this.did,
})
await this.memberships?.setup()
await this.memberships?.setup().catch(e => {
console.error('Failed to setup memberships model', e)
})
await this.notifications.setup().catch(e => {
console.error('Failed to setup notifications model', e)
})
} else {
this.clear()
}
@ -56,7 +64,12 @@ export class MeModel {
async fetchStateUpdate() {
const res = await this.rootStore.api.app.bsky.notification.getCount()
runInAction(() => {
const newNotifications = this.notificationCount !== res.data.count
this.notificationCount = res.data.count
if (newNotifications) {
// trigger pre-emptive fetch on new notifications
this.notifications.refresh()
}
})
}

View File

@ -1,6 +1,7 @@
import {makeAutoObservable} from 'mobx'
import {makeAutoObservable, runInAction} from 'mobx'
import * as ListNotifications from '../../third-party/api/src/client/types/app/bsky/notification/list'
import {RootStoreModel} from './root-store'
import {PostThreadViewModel} from './post-thread-view'
import {Declaration} from './_common'
import {hasProp} from '../lib/type-guards'
import {APP_BSKY_GRAPH} from '../../third-party/api'
@ -34,6 +35,9 @@ export class NotificationsViewItemModel implements GroupedNotification {
indexedAt: string = ''
additional?: NotificationsViewItemModel[]
// additional data
additionalPost?: PostThreadViewModel
constructor(
public rootStore: RootStoreModel,
reactKey: string,
@ -89,6 +93,13 @@ export class NotificationsViewItemModel implements GroupedNotification {
return this.reason === 'assertion'
}
get needsAdditionalData() {
if (this.isUpvote || this.isRepost || this.isTrend || this.isReply) {
return !this.additionalPost
}
return false
}
get isInvite() {
return (
this.isAssertion && this.record.assertion === APP_BSKY_GRAPH.AssertMember
@ -107,6 +118,27 @@ export class NotificationsViewItemModel implements GroupedNotification {
}
return ''
}
async fetchAdditionalData() {
if (!this.needsAdditionalData) {
return
}
let postUri
if (this.isReply) {
postUri = this.uri
} else if (this.isUpvote || this.isRead || this.isTrend) {
postUri = this.subjectUri
}
if (postUri) {
this.additionalPost = new PostThreadViewModel(this.rootStore, {
uri: postUri,
depth: 0,
})
await this.additionalPost.setup().catch(e => {
console.error('Failed to load post needed by notification', e)
})
}
}
}
export class NotificationsViewModel {
@ -171,7 +203,6 @@ export class NotificationsViewModel {
await this._pendingWork()
this._loadPromise = this._initialLoad(isRefreshing)
await this._loadPromise
this._updateReadState()
this._loadPromise = undefined
}
@ -208,6 +239,20 @@ export class NotificationsViewModel {
this._updatePromise = undefined
}
/**
* Update read/unread state
*/
async updateReadState() {
try {
await this.rootStore.api.app.bsky.notification.updateSeen({
seenAt: new Date().toISOString(),
})
this.rootStore.me.clearNotificationCount()
} catch (e) {
console.log('Failed to update notifications read state', e)
}
}
// state transitions
// =
@ -246,7 +291,7 @@ export class NotificationsViewModel {
limit: PAGE_SIZE,
})
const res = await this.rootStore.api.app.bsky.notification.list(params)
this._replaceAll(res)
await this._replaceAll(res)
this._xIdle()
} catch (e: any) {
this._xIdle(`Failed to load notifications: ${e.toString()}`)
@ -264,7 +309,7 @@ export class NotificationsViewModel {
before: this.loadMoreCursor,
})
const res = await this.rootStore.api.app.bsky.notification.list(params)
this._appendAll(res)
await this._appendAll(res)
this._xIdle()
} catch (e: any) {
this._xIdle(`Failed to load notifications: ${e.toString()}`)
@ -296,25 +341,40 @@ export class NotificationsViewModel {
}
}
private _replaceAll(res: ListNotifications.Response) {
this.notifications.length = 0
this._appendAll(res)
private async _replaceAll(res: ListNotifications.Response) {
return this._appendAll(res, true)
}
private _appendAll(res: ListNotifications.Response) {
private async _appendAll(res: ListNotifications.Response, replace = false) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
let counter = this.notifications.length
const promises = []
const itemModels: NotificationsViewItemModel[] = []
for (const item of groupNotifications(res.data.notifications)) {
this._append(counter++, item)
const itemModel = new NotificationsViewItemModel(
this.rootStore,
`item-${counter++}`,
item,
)
if (itemModel.needsAdditionalData) {
promises.push(itemModel.fetchAdditionalData())
}
itemModels.push(itemModel)
}
}
private _append(keyId: number, item: GroupedNotification) {
// TODO: validate .record
this.notifications.push(
new NotificationsViewItemModel(this.rootStore, `item-${keyId}`, item),
)
await Promise.all(promises).catch(e => {
console.error(
'Uncaught failure during notifications-view _appendAll()',
e,
)
})
runInAction(() => {
if (replace) {
this.notifications = itemModels
} else {
this.notifications = this.notifications.concat(itemModels)
}
})
}
private _updateAll(res: ListNotifications.Response) {
@ -330,17 +390,6 @@ export class NotificationsViewModel {
}
}
}
private async _updateReadState() {
try {
await this.rootStore.api.app.bsky.notification.updateSeen({
seenAt: new Date().toISOString(),
})
this.rootStore.me.clearNotificationCount()
} catch (e) {
console.log('Failed to update notifications read state', e)
}
}
}
function groupNotifications(

View File

@ -1,5 +1,5 @@
import {makeAutoObservable, runInAction} from 'mobx'
import * as GetPostThread from '../../third-party/api/src/client/types/app/bsky/feed/getPostThread'
import {AppBskyFeedGetPostThread as GetPostThread} from '../../third-party/api'
import {AtUri} from '../../third-party/uri'
import _omit from 'lodash.omit'
import {RootStoreModel} from './root-store'
@ -216,6 +216,7 @@ export class PostThreadViewModel {
isRefreshing = false
hasLoaded = false
error = ''
notFound = false
resolvedUri = ''
params: GetPostThread.QueryParams
@ -286,13 +287,15 @@ export class PostThreadViewModel {
this.isLoading = true
this.isRefreshing = isRefreshing
this.error = ''
this.notFound = false
}
private _xIdle(err: string = '') {
private _xIdle(err: any = undefined) {
this.isLoading = false
this.isRefreshing = false
this.hasLoaded = true
this.error = err
this.error = err ? err.toString() : ''
this.notFound = err instanceof GetPostThread.NotFoundError
}
// loader functions
@ -317,7 +320,7 @@ export class PostThreadViewModel {
this._replaceAll(res)
this._xIdle()
} catch (e: any) {
this._xIdle(`Failed to load thread: ${e.toString()}`)
this._xIdle(e)
}
}

View File

@ -10424,7 +10424,7 @@ var methodSchemaDict = {
required: ["useCount"],
properties: {
useCount: {
type: "number"
type: "integer"
}
},
$defs: {}
@ -10841,7 +10841,7 @@ var methodSchemaDict = {
description: "The NSID of the record type."
},
limit: {
type: "number",
type: "integer",
minimum: 1,
default: 50,
description: "The number of records to return. TODO-max number?"
@ -11306,16 +11306,16 @@ var methodSchemaDict = {
maxLength: 256
},
followersCount: {
type: "number"
type: "integer"
},
followsCount: {
type: "number"
type: "integer"
},
membersCount: {
type: "number"
type: "integer"
},
postsCount: {
type: "number"
type: "integer"
},
myState: {
type: "object",
@ -11403,7 +11403,7 @@ var methodSchemaDict = {
type: "object",
properties: {
limit: {
type: "number",
type: "integer",
maximum: 100
},
cursor: {
@ -11536,7 +11536,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -11661,7 +11661,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
}
}
@ -11818,7 +11818,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -11889,16 +11889,16 @@ var methodSchemaDict = {
]
},
replyCount: {
type: "number"
type: "integer"
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -12066,16 +12066,16 @@ var methodSchemaDict = {
]
},
replyCount: {
type: "number"
type: "integer"
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -12207,7 +12207,7 @@ var methodSchemaDict = {
type: "string"
},
depth: {
type: "number"
type: "integer"
}
}
},
@ -12265,7 +12265,7 @@ var methodSchemaDict = {
$ref: "#/$defs/post"
},
replyCount: {
type: "number"
type: "integer"
},
replies: {
type: "array",
@ -12274,13 +12274,13 @@ var methodSchemaDict = {
}
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -12445,7 +12445,7 @@ var methodSchemaDict = {
$ref: "#/$defs/post"
},
replyCount: {
type: "number"
type: "integer"
},
replies: {
type: "array",
@ -12454,13 +12454,13 @@ var methodSchemaDict = {
}
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -12578,7 +12578,12 @@ var methodSchemaDict = {
enum: ["app.bsky.system.actorUser", "app.bsky.system.actorScene"]
}
}
}
},
errors: [
{
name: "NotFound"
}
]
},
"app.bsky.feed.getRepostedBy": {
lexicon: 1,
@ -12595,7 +12600,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -12726,7 +12731,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -12797,16 +12802,16 @@ var methodSchemaDict = {
]
},
replyCount: {
type: "number"
type: "integer"
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -12977,16 +12982,16 @@ var methodSchemaDict = {
]
},
replyCount: {
type: "number"
type: "integer"
},
repostCount: {
type: "number"
type: "integer"
},
upvoteCount: {
type: "number"
type: "integer"
},
downvoteCount: {
type: "number"
type: "integer"
},
indexedAt: {
type: "string",
@ -13128,7 +13133,7 @@ var methodSchemaDict = {
enum: ["up", "down"]
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -13365,7 +13370,7 @@ var methodSchemaDict = {
type: "boolean"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -13582,7 +13587,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -13727,7 +13732,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -13872,7 +13877,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -14017,7 +14022,7 @@ var methodSchemaDict = {
type: "string"
},
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -14160,7 +14165,7 @@ var methodSchemaDict = {
required: ["count"],
properties: {
count: {
type: "number"
type: "integer"
}
},
$defs: {}
@ -14175,7 +14180,7 @@ var methodSchemaDict = {
type: "object",
properties: {
limit: {
type: "number",
type: "integer",
maximum: 100
},
before: {
@ -14563,11 +14568,11 @@ var recordSchemaDict = {
required: ["start", "end"],
properties: {
start: {
type: "number",
type: "integer",
minimum: 0
},
end: {
type: "number",
type: "integer",
minimum: 0
}
}
@ -14620,11 +14625,11 @@ var recordSchemaDict = {
required: ["start", "end"],
properties: {
start: {
type: "number",
type: "integer",
minimum: 0
},
end: {
type: "number",
type: "integer",
minimum: 0
}
}
@ -15315,10 +15320,18 @@ function toKnownErr29(e) {
// src/client/types/app/bsky/feed/getPostThread.ts
var getPostThread_exports = {};
__export(getPostThread_exports, {
NotFoundError: () => NotFoundError,
toKnownErr: () => toKnownErr30
});
var NotFoundError = class extends XRPCError {
constructor(src) {
super(src.status, src.error, src.message);
}
};
function toKnownErr30(e) {
if (e instanceof XRPCError) {
if (e.error === "NotFound")
return new NotFoundError(e);
}
return e;
}

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
import { Headers } from '@atproto/xrpc';
import { Headers, XRPCError } from '@atproto/xrpc';
export interface QueryParams {
uri: string;
depth?: number;
@ -61,4 +61,7 @@ export interface Response {
headers: Headers;
data: OutputSchema;
}
export declare class NotFoundError extends XRPCError {
constructor(src: XRPCError);
}
export declare function toKnownErr(e: any): any;

File diff suppressed because one or more lines are too long

View File

@ -65,8 +65,8 @@ export function Component({
return (
<View style={s.flex1}>
<Text style={[s.textCenter, s.bold, s.f16]}>Edit my profile</Text>
<BottomSheetScrollView style={styles.inner}>
<Text style={styles.title}>Edit my profile</Text>
{error !== '' && (
<View style={s.mb10}>
<ErrorMessage message={error} />
@ -114,6 +114,12 @@ const styles = StyleSheet.create({
inner: {
padding: 14,
},
title: {
textAlign: 'center',
fontWeight: 'bold',
fontSize: 24,
marginBottom: 18,
},
group: {
marginBottom: 10,
},

View File

@ -35,7 +35,7 @@ export const Modal = observer(function Modal() {
} else {
bottomSheetRef.current?.close()
}
}, [store.shell.isModalActive, bottomSheetRef])
}, [store.shell.isModalActive, bottomSheetRef, store.shell.activeModal?.name])
let snapPoints: (string | number)[] = CLOSED_SNAPPOINTS
let element

View File

@ -43,7 +43,7 @@ export const Feed = observer(function Feed({
onPressTryAgain={onPressTryAgain}
/>
)}
{view.hasContent && (
{view.hasLoaded && (
<FlatList
data={view.notifications}
keyExtractor={item => item._reactKey}
@ -53,7 +53,7 @@ export const Feed = observer(function Feed({
onEndReached={onEndReached}
/>
)}
{view.isEmpty && (
{view.hasLoaded && view.isEmpty && (
<EmptyState icon="bell" message="No notifications yet!" />
)}
</View>

View File

@ -4,11 +4,12 @@ import {StyleSheet, Text, View} from 'react-native'
import {AtUri} from '../../../third-party/uri'
import {FontAwesomeIcon, Props} from '@fortawesome/react-native-fontawesome'
import {NotificationsViewItemModel} from '../../../state/models/notifications-view'
import {PostThreadViewModel} from '../../../state/models/post-thread-view'
import {s, colors} from '../../lib/styles'
import {ago, pluralize} from '../../../lib/strings'
import {UpIconSolid} from '../../lib/icons'
import {UserAvatar} from '../util/UserAvatar'
import {PostText} from '../post/PostText'
import {ErrorMessage} from '../util/ErrorMessage'
import {Post} from '../post/Post'
import {Link} from '../util/Link'
import {InviteAccepter} from './InviteAccepter'
@ -42,16 +43,22 @@ export const FeedItem = observer(function FeedItem({
}
}, [item])
if (item.additionalPost?.notFound) {
// don't render anything if the target post was deleted or unfindable
return <View />
}
if (item.isReply) {
return (
<Link
style={[
styles.outerMinimal,
item.isRead ? undefined : styles.outerUnread,
]}
href={itemHref}
title={itemTitle}>
<Post uri={item.uri} />
<Link href={itemHref} title={itemTitle}>
<Post
uri={item.uri}
initView={item.additionalPost}
style={[
styles.outerMinimal,
item.isRead ? undefined : styles.outerUnread,
]}
/>
</Link>
)
}
@ -170,7 +177,7 @@ export const FeedItem = observer(function FeedItem({
</Text>
</View>
{item.isUpvote || item.isRepost || item.isTrend ? (
<PostText uri={item.subjectUri} style={[s.gray5]} />
<AdditionalPostText additionalPost={item.additionalPost} />
) : (
<></>
)}
@ -181,17 +188,24 @@ export const FeedItem = observer(function FeedItem({
<InviteAccepter item={item} />
</View>
)}
{item.isReply ? (
<View style={s.pt5}>
<Post uri={item.uri} />
</View>
) : (
<></>
)}
</Link>
)
})
function AdditionalPostText({
additionalPost,
}: {
additionalPost?: PostThreadViewModel
}) {
if (!additionalPost) {
return <View />
}
if (additionalPost.error) {
return <ErrorMessage message={additionalPost.error} />
}
return <Text style={[s.gray5]}>{additionalPost.thread?.record.text}</Text>
}
const styles = StyleSheet.create({
outer: {
backgroundColor: colors.white,
@ -207,8 +221,9 @@ const styles = StyleSheet.create({
marginBottom: 0,
},
outerUnread: {
backgroundColor: colors.unreadNotifBg,
borderWidth: 1,
borderColor: colors.blue2,
borderColor: colors.blue1,
},
layout: {
flexDirection: 'row',

View File

@ -88,6 +88,15 @@ export const PostThreadItem = observer(function PostThreadItem({
)
}
if (deleted) {
return (
<View style={[styles.outer, s.p20, s.flexRow]}>
<FontAwesomeIcon icon={['far', 'trash-can']} style={[s.gray4]} />
<Text style={[s.gray5, s.ml10]}>This post has been deleted.</Text>
</View>
)
}
if (item._isHighlightedPost) {
return (
<>

View File

@ -1,8 +1,15 @@
import React, {useState, useEffect} from 'react'
import {
ActivityIndicator,
StyleProp,
StyleSheet,
Text,
View,
ViewStyle,
} from 'react-native'
import {observer} from 'mobx-react-lite'
import {AtUri} from '../../../third-party/uri'
import * as PostType from '../../../third-party/api/src/client/types/app/bsky/feed/post'
import {ActivityIndicator, StyleSheet, Text, View} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {PostThreadViewModel} from '../../../state/models/post-thread-view'
import {Link} from '../util/Link'
@ -15,19 +22,27 @@ import {UserAvatar} from '../util/UserAvatar'
import {useStores} from '../../../state'
import {s, colors} from '../../lib/styles'
export const Post = observer(function Post({uri}: {uri: string}) {
export const Post = observer(function Post({
uri,
initView,
style,
}: {
uri: string
initView?: PostThreadViewModel
style?: StyleProp<ViewStyle>
}) {
const store = useStores()
const [view, setView] = useState<PostThreadViewModel | undefined>()
const [view, setView] = useState<PostThreadViewModel | undefined>(initView)
const [deleted, setDeleted] = useState(false)
useEffect(() => {
if (view?.params.uri === uri) {
if (initView || view?.params.uri === uri) {
return // no change needed? or trigger refresh?
}
const newView = new PostThreadViewModel(store, {uri, depth: 0})
setView(newView)
newView.setup().catch(err => console.error('Failed to fetch post', err))
}, [uri, view?.params.uri, store])
}, [initView, uri, view?.params.uri, store])
// deleted
// =
@ -109,7 +124,7 @@ export const Post = observer(function Post({uri}: {uri: string}) {
}
return (
<Link style={styles.outer} href={itemHref} title={itemTitle}>
<Link style={[styles.outer, style]} href={itemHref} title={itemTitle}>
<View style={styles.layout}>
<View style={styles.layoutAvi}>
<Link href={authorHref} title={authorTitle}>

View File

@ -41,6 +41,8 @@ export const colors = {
green3: '#20bc07',
green4: '#148203',
green5: '#082b03',
unreadNotifBg: '#ebf6ff',
}
export const gradients = {

View File

@ -7,43 +7,33 @@ import {NotificationsViewModel} from '../../state/models/notifications-view'
import {ScreenParams} from '../routes'
export const Notifications = ({navIdx, visible}: ScreenParams) => {
const [hasSetup, setHasSetup] = useState<boolean>(false)
const [notesView, setNotesView] = useState<
NotificationsViewModel | undefined
>()
const store = useStores()
useEffect(() => {
let aborted = false
if (!visible) {
return
}
console.log('Updating notifications feed')
store.me.refreshMemberships() // needed for the invite notifications
if (hasSetup) {
console.log('Updating notifications feed')
notesView?.update()
} else {
store.nav.setTitle(navIdx, 'Notifications')
const newNotesView = new NotificationsViewModel(store, {})
setNotesView(newNotesView)
newNotesView.setup().then(() => {
if (aborted) return
setHasSetup(true)
store.me.notifications
.update()
.catch(e => {
console.error('Error while updating notifications feed', e)
})
}
return () => {
aborted = true
}
.then(() => {
store.me.notifications.updateReadState()
})
store.nav.setTitle(navIdx, 'Notifications')
}, [visible, store])
const onPressTryAgain = () => {
notesView?.refresh()
store.me.notifications.refresh()
}
return (
<View style={{flex: 1}}>
<ViewHeader title="Notifications" />
{notesView && <Feed view={notesView} onPressTryAgain={onPressTryAgain} />}
<Feed view={store.me.notifications} onPressTryAgain={onPressTryAgain} />
</View>
)
}

View File

@ -321,7 +321,7 @@ export const MobileShell: React.FC = observer(() => {
<Btn
icon={isAtHome ? 'home-solid' : 'home'}
onPress={onPressHome}
onLongPress={doNewTab('/')}
onLongPress={TABS_ENABLED ? doNewTab('/') : undefined}
/>
{TABS_ENABLED ? (
<Btn
@ -333,7 +333,7 @@ export const MobileShell: React.FC = observer(() => {
<Btn
icon={isAtNotifications ? 'bell-solid' : 'bell'}
onPress={onPressNotifications}
onLongPress={doNewTab('/notifications')}
onLongPress={TABS_ENABLED ? doNewTab('/notifications') : undefined}
notificationCount={store.me.notificationCount}
/>
<Btn