Clean up the custom-feed model
parent
9c02fbb925
commit
2fd3b4ca04
|
@ -1,5 +1,5 @@
|
||||||
import {AppBskyFeedDefs, AtUri} from '@atproto/api'
|
import {AppBskyFeedDefs} from '@atproto/api'
|
||||||
import {makeAutoObservable} from 'mobx'
|
import {makeAutoObservable, runInAction} from 'mobx'
|
||||||
import {RootStoreModel} from 'state/models/root-store'
|
import {RootStoreModel} from 'state/models/root-store'
|
||||||
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
||||||
|
|
||||||
|
@ -25,13 +25,8 @@ export class CustomFeedModel {
|
||||||
|
|
||||||
// local actions
|
// local actions
|
||||||
// =
|
// =
|
||||||
set toggleSaved(value: boolean) {
|
|
||||||
if (this.data.viewer) {
|
|
||||||
this.data.viewer.saved = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get getUri() {
|
get uri() {
|
||||||
return this.data.uri
|
return this.data.uri
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,99 +45,65 @@ export class CustomFeedModel {
|
||||||
return this.data.viewer?.like
|
return this.data.viewer?.like
|
||||||
}
|
}
|
||||||
|
|
||||||
private toggleLiked(s?: string) {
|
|
||||||
if (this.data.viewer) {
|
|
||||||
if (this.data.viewer.like) {
|
|
||||||
this.data.viewer.like = undefined
|
|
||||||
} else {
|
|
||||||
this.data.viewer.like = s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private incrementLike() {
|
|
||||||
if (this.data.likeCount) {
|
|
||||||
this.data.likeCount += 1
|
|
||||||
} else {
|
|
||||||
this.data.likeCount = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private decrementLike() {
|
|
||||||
if (this.data.likeCount) {
|
|
||||||
this.data.likeCount -= 1
|
|
||||||
} else {
|
|
||||||
this.data.likeCount = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private rewriteData(data: AppBskyFeedDefs.GeneratorView) {
|
|
||||||
this.data = data
|
|
||||||
}
|
|
||||||
|
|
||||||
// public apis
|
// public apis
|
||||||
// =
|
// =
|
||||||
|
|
||||||
|
async save() {
|
||||||
|
await this.rootStore.agent.app.bsky.feed.saveFeed({
|
||||||
|
feed: this.uri,
|
||||||
|
})
|
||||||
|
runInAction(() => {
|
||||||
|
this.data.viewer = this.data.viewer || {}
|
||||||
|
this.data.viewer.saved = true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async unsave() {
|
||||||
|
await this.rootStore.agent.app.bsky.feed.unsaveFeed({
|
||||||
|
feed: this.uri,
|
||||||
|
})
|
||||||
|
runInAction(() => {
|
||||||
|
this.data.viewer = this.data.viewer || {}
|
||||||
|
this.data.viewer.saved = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async like() {
|
async like() {
|
||||||
try {
|
try {
|
||||||
const res = await this.rootStore.agent.app.bsky.feed.like.create(
|
const res = await this.rootStore.agent.like(this.data.uri, this.data.cid)
|
||||||
{
|
runInAction(() => {
|
||||||
repo: this.rootStore.me.did,
|
this.data.viewer = this.data.viewer || {}
|
||||||
},
|
this.data.viewer.like = res.uri
|
||||||
{
|
this.data.likeCount = (this.data.likeCount || 0) + 1
|
||||||
subject: {
|
})
|
||||||
uri: this.data.uri,
|
|
||||||
cid: this.data.cid,
|
|
||||||
},
|
|
||||||
createdAt: new Date().toISOString(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
this.toggleLiked(res.uri)
|
|
||||||
this.incrementLike()
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.rootStore.log.error('Failed to like feed', e)
|
this.rootStore.log.error('Failed to like feed', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async unlike() {
|
async unlike() {
|
||||||
|
if (!this.data.viewer.like) {
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await this.rootStore.agent.app.bsky.feed.like.delete({
|
await this.rootStore.agent.deleteLike(this.data.viewer.like!)
|
||||||
repo: this.rootStore.me.did,
|
runInAction(() => {
|
||||||
rkey: new AtUri(this.data.viewer?.like!).rkey,
|
this.data.viewer = this.data.viewer || {}
|
||||||
|
this.data.viewer.like = undefined
|
||||||
|
this.data.likeCount = (this.data.likeCount || 1) - 1
|
||||||
})
|
})
|
||||||
this.toggleLiked()
|
|
||||||
this.decrementLike()
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.rootStore.log.error('Failed to unlike feed', e)
|
this.rootStore.log.error('Failed to unlike feed', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getView(store: RootStoreModel, uri: string) {
|
|
||||||
const res = await store.agent.app.bsky.feed.getFeedGenerator({
|
|
||||||
feed: uri,
|
|
||||||
})
|
|
||||||
const view = res.data.view
|
|
||||||
return view
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkIsValid() {
|
|
||||||
const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({
|
|
||||||
feed: this.data.uri,
|
|
||||||
})
|
|
||||||
return res.data.isValid
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkIsOnline() {
|
|
||||||
const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({
|
|
||||||
feed: this.data.uri,
|
|
||||||
})
|
|
||||||
return res.data.isOnline
|
|
||||||
}
|
|
||||||
|
|
||||||
async reload() {
|
async reload() {
|
||||||
const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({
|
const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({
|
||||||
feed: this.data.uri,
|
feed: this.data.uri,
|
||||||
})
|
})
|
||||||
this.rewriteData(res.data.view)
|
runInAction(() => {
|
||||||
|
this.data = res.data.view
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize() {
|
serialize() {
|
||||||
|
|
|
@ -184,10 +184,7 @@ export class SavedFeedsModel {
|
||||||
|
|
||||||
async save(algoItem: CustomFeedModel) {
|
async save(algoItem: CustomFeedModel) {
|
||||||
try {
|
try {
|
||||||
await this.rootStore.agent.app.bsky.feed.saveFeed({
|
await algoItem.save()
|
||||||
feed: algoItem.getUri,
|
|
||||||
})
|
|
||||||
algoItem.toggleSaved = true
|
|
||||||
this.addFeed(algoItem)
|
this.addFeed(algoItem)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.rootStore.log.error('Failed to save feed', e)
|
this.rootStore.log.error('Failed to save feed', e)
|
||||||
|
@ -195,16 +192,13 @@ export class SavedFeedsModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
async unsave(algoItem: CustomFeedModel) {
|
async unsave(algoItem: CustomFeedModel) {
|
||||||
const uri = algoItem.getUri
|
const uri = algoItem.uri
|
||||||
try {
|
try {
|
||||||
await this.rootStore.agent.app.bsky.feed.unsaveFeed({
|
await algoItem.unsave()
|
||||||
feed: uri,
|
|
||||||
})
|
|
||||||
algoItem.toggleSaved = false
|
|
||||||
this.removeFeed(uri)
|
this.removeFeed(uri)
|
||||||
this.removePinnedFeed(uri)
|
this.removePinnedFeed(uri)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.rootStore.log.error('Failed to unsanve feed', e)
|
this.rootStore.log.error('Failed to unsave feed', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -188,7 +188,7 @@ export function UserAvatar({
|
||||||
accessibilityRole="image"
|
accessibilityRole="image"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<DefaultAvatar size={size} />
|
<DefaultAvatar type={type} size={size} />
|
||||||
)}
|
)}
|
||||||
<View style={[styles.editButtonContainer, pal.btn]}>
|
<View style={[styles.editButtonContainer, pal.btn]}>
|
||||||
<FontAwesomeIcon
|
<FontAwesomeIcon
|
||||||
|
|
|
@ -170,6 +170,7 @@ export function PostEmbeds({
|
||||||
AppBskyEmbedRecord.isView(embed) &&
|
AppBskyEmbedRecord.isView(embed) &&
|
||||||
AppBskyFeedDefs.isGeneratorView(embed.record)
|
AppBskyFeedDefs.isGeneratorView(embed.record)
|
||||||
) {
|
) {
|
||||||
|
// TODO memoize this?
|
||||||
return (
|
return (
|
||||||
<CustomFeed
|
<CustomFeed
|
||||||
item={new CustomFeedModel(store, embed.record)}
|
item={new CustomFeedModel(store, embed.record)}
|
||||||
|
|
|
@ -118,7 +118,7 @@ export const HomeScreen = withAuthRequired(
|
||||||
key={String(2 + index + 1)}
|
key={String(2 + index + 1)}
|
||||||
testID="customFeed"
|
testID="customFeed"
|
||||||
isPageFocused={selectedPage === 2 + index}
|
isPageFocused={selectedPage === 2 + index}
|
||||||
feed={new PostsFeedModel(store, 'custom', {feed: f.getUri})}
|
feed={new PostsFeedModel(store, 'custom', {feed: f.uri})}
|
||||||
renderEmptyState={renderFollowingEmptyState}
|
renderEmptyState={renderFollowingEmptyState}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue