diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-09-23 09:47:21 -0500 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-09-23 09:47:21 -0500 |
commit | a8c8286b88eae3e7db23555be90cb9673361be20 (patch) | |
tree | 34a7e50e776a4c2feae601f7aa08eb312da5c708 /src | |
parent | aabde2b401e725090232f203f23152ee1d50d566 (diff) | |
download | voidsky-a8c8286b88eae3e7db23555be90cb9673361be20.tar.zst |
Implement unfollow, unlike, unrepost
Diffstat (limited to 'src')
-rw-r--r-- | src/state/lib/api.ts | 68 | ||||
-rw-r--r-- | src/state/models/feed-view.ts | 12 | ||||
-rw-r--r-- | src/state/models/post-thread-view.ts | 12 | ||||
-rw-r--r-- | src/state/models/profile-view.ts | 4 | ||||
-rw-r--r-- | src/view/com/modals/ComposePost.tsx | 2 | ||||
-rw-r--r-- | src/view/com/profile/ProfileHeader.tsx | 4 |
6 files changed, 61 insertions, 41 deletions
diff --git a/src/state/lib/api.ts b/src/state/lib/api.ts index c0dc34fbb..8695d81bf 100644 --- a/src/state/lib/api.ts +++ b/src/state/lib/api.ts @@ -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, { diff --git a/src/state/models/feed-view.ts b/src/state/models/feed-view.ts index a7975b66a..5464f1400 100644 --- a/src/state/models/feed-view.ts +++ b/src/state/models/feed-view.ts @@ -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(() => { diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts index ce72201f5..fab0eb895 100644 --- a/src/state/models/post-thread-view.ts +++ b/src/state/models/post-thread-view.ts @@ -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(() => { diff --git a/src/state/models/profile-view.ts b/src/state/models/profile-view.ts index 24d49c112..7a85d06ae 100644 --- a/src/state/models/profile-view.ts +++ b/src/state/models/profile-view.ts @@ -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 diff --git a/src/view/com/modals/ComposePost.tsx b/src/view/com/modals/ComposePost.tsx index cdd312352..b1309ca83 100644 --- a/src/view/com/modals/ComposePost.tsx +++ b/src/view/com/modals/ComposePost.tsx @@ -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( diff --git a/src/view/com/profile/ProfileHeader.tsx b/src/view/com/profile/ProfileHeader.tsx index c5984ddff..8b30e0f92 100644 --- a/src/view/com/profile/ProfileHeader.tsx +++ b/src/view/com/profile/ProfileHeader.tsx @@ -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]}> |