Implement unfollow, unlike, unrepost

zio/stable
Paul Frazee 2022-09-23 09:47:21 -05:00
parent aabde2b401
commit a8c8286b88
6 changed files with 61 additions and 41 deletions

View File

@ -53,8 +53,12 @@ export async function like(adx: ServiceClient, user: string, uri: string) {
)
}
export async function unlike(adx: ServiceClient, user: string, uri: string) {
throw new Error('TODO')
export async function unlike(adx: ServiceClient, likeUri: string) {
const likeUrip = new AdxUri(likeUri)
return await adx.todo.social.like.delete({
did: likeUrip.hostname,
tid: likeUrip.recordKey,
})
}
export async function repost(adx: ServiceClient, user: string, uri: string) {
@ -67,8 +71,12 @@ export async function repost(adx: ServiceClient, user: string, uri: string) {
)
}
export async function unrepost(adx: ServiceClient, user: string, uri: string) {
throw new Error('TODO')
export async function unrepost(adx: ServiceClient, repostUri: string) {
const repostUrip = new AdxUri(repostUri)
return await adx.todo.social.repost.delete({
did: repostUrip.hostname,
tid: repostUrip.recordKey,
})
}
export async function follow(
@ -85,12 +93,12 @@ export async function follow(
)
}
export async function unfollow(
adx: ServiceClient,
user: string,
subject: {did: string},
) {
throw new Error('TODO')
export async function unfollow(adx: ServiceClient, followUri: string) {
const followUrip = new AdxUri(followUri)
return await adx.todo.social.follow.delete({
did: followUrip.hostname,
tid: followUrip.recordKey,
})
}
export async function updateProfile(
@ -108,36 +116,42 @@ interface FetchHandlerResponse {
}
async function fetchHandler(
httpUri: string,
httpMethod: string,
httpHeaders: Record<string, string>,
httpReqBody: any,
reqUri: string,
reqMethod: string,
reqHeaders: Record<string, string>,
reqBody: any,
): Promise<FetchHandlerResponse> {
httpHeaders['Authorization'] = 'did:test:alice' // DEBUG
reqHeaders['Authorization'] = 'did:test:alice' // DEBUG
const reqMimeType = reqHeaders['Content-Type'] || reqHeaders['content-type']
if (reqMimeType && reqMimeType.startsWith('application/json')) {
reqBody = JSON.stringify(reqBody)
}
const res = await RNFetchBlob.fetch(
/** @ts-ignore method coersion, it's fine -prf */
httpMethod,
httpUri,
httpHeaders,
httpReqBody,
reqMethod,
reqUri,
reqHeaders,
reqBody,
)
const status = res.info().status
const headers = (res.info().headers || {}) as Record<string, string>
const mimeType = headers['Content-Type'] || headers['content-type']
const resStatus = res.info().status
const resHeaders = (res.info().headers || {}) as Record<string, string>
const resMimeType = resHeaders['Content-Type'] || resHeaders['content-type']
let resBody
if (mimeType) {
if (mimeType.startsWith('application/json')) {
if (resMimeType) {
if (resMimeType.startsWith('application/json')) {
resBody = res.json()
} else if (mimeType.startsWith('text/')) {
} else if (resMimeType.startsWith('text/')) {
resBody = res.text()
} else {
resBody = res.base64()
}
}
return {
status,
headers,
status: resStatus,
headers: resHeaders,
body: resBody,
}
// const res = await fetch(httpUri, {

View File

@ -59,13 +59,17 @@ export class FeedViewItemModel implements GetFeedView.FeedItem {
async toggleLike() {
if (this.myState.like) {
await apilib.unlike(this.rootStore.api, 'alice.test', this.uri)
await apilib.unlike(this.rootStore.api, this.myState.like)
runInAction(() => {
this.likeCount--
this.myState.like = undefined
})
} else {
const res = await apilib.like(this.rootStore.api, 'alice.test', this.uri)
const res = await apilib.like(
this.rootStore.api,
'did:test:alice',
this.uri,
)
runInAction(() => {
this.likeCount++
this.myState.like = res.uri
@ -75,7 +79,7 @@ export class FeedViewItemModel implements GetFeedView.FeedItem {
async toggleRepost() {
if (this.myState.repost) {
await apilib.unrepost(this.rootStore.api, 'alice.test', this.uri)
await apilib.unrepost(this.rootStore.api, this.myState.repost)
runInAction(() => {
this.repostCount--
this.myState.repost = undefined
@ -83,7 +87,7 @@ export class FeedViewItemModel implements GetFeedView.FeedItem {
} else {
const res = await apilib.repost(
this.rootStore.api,
'alice.test',
'did:test:alice',
this.uri,
)
runInAction(() => {

View File

@ -95,13 +95,17 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
async toggleLike() {
if (this.myState.like) {
await apilib.unlike(this.rootStore.api, 'alice.test', this.uri)
await apilib.unlike(this.rootStore.api, this.myState.like)
runInAction(() => {
this.likeCount--
this.myState.like = undefined
})
} else {
const res = await apilib.like(this.rootStore.api, 'alice.test', this.uri)
const res = await apilib.like(
this.rootStore.api,
'did:test:alice',
this.uri,
)
runInAction(() => {
this.likeCount++
this.myState.like = res.uri
@ -111,7 +115,7 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
async toggleRepost() {
if (this.myState.repost) {
await apilib.unrepost(this.rootStore.api, 'alice.test', this.uri)
await apilib.unrepost(this.rootStore.api, this.myState.repost)
runInAction(() => {
this.repostCount--
this.myState.repost = undefined
@ -119,7 +123,7 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
} else {
const res = await apilib.repost(
this.rootStore.api,
'alice.test',
'did:test:alice',
this.uri,
)
runInAction(() => {

View File

@ -74,9 +74,7 @@ export class ProfileViewModel {
throw new Error('Not logged in')
}
if (this.myState.follow) {
await apilib.unfollow(this.rootStore.api, this.rootStore.me.did, {
did: this.did,
})
await apilib.unfollow(this.rootStore.api, this.myState.follow)
runInAction(() => {
this.followersCount--
this.myState.follow = undefined

View File

@ -48,7 +48,7 @@ export function Component({replyTo}: {replyTo?: string}) {
return false
}
try {
await apilib.post(store.api, 'alice.test', text, replyTo)
await apilib.post(store.api, 'did:test:alice', text, replyTo)
} catch (e: any) {
console.error(`Failed to create post: ${e.toString()}`)
setError(

View File

@ -30,7 +30,7 @@ export const ProfileHeader = observer(function ProfileHeader({
view?.toggleFollowing().then(
() => {
Toast.show(
`${view.myState.hasFollowed ? 'Following' : 'No longer following'} ${
`${view.myState.follow ? 'Following' : 'No longer following'} ${
view.displayName || view.name
}`,
{
@ -104,7 +104,7 @@ export const ProfileHeader = observer(function ProfileHeader({
style={[styles.mainBtn, styles.btn]}>
<Text style={[s.fw400, s.f14]}>Edit Profile</Text>
</TouchableOpacity>
) : view.myState.hasFollowed ? (
) : view.myState.follow ? (
<TouchableOpacity
onPress={onPressToggleFollow}
style={[styles.mainBtn, styles.btn]}>