Enable fetching status context

This commit is contained in:
Nat 2021-05-20 15:40:12 -03:00
parent f0c422681e
commit 5c42133e53
2 changed files with 72 additions and 31 deletions

View File

@ -17,6 +17,8 @@ import TimelineViewJsx from "src/components/posts/timeline-view";
import BackBarJsx from "src/components/navigation/back-bar"; import BackBarJsx from "src/components/navigation/back-bar";
import { TouchableWithoutFeedback } from "react-native-gesture-handler"; import { TouchableWithoutFeedback } from "react-native-gesture-handler";
import * as requests from "src/requests";
const TEST_IMAGE = "https://cache.desktopnexus.com/thumbseg/2255/2255124-bigthumbnail.jpg"; const TEST_IMAGE = "https://cache.desktopnexus.com/thumbseg/2255/2255124-bigthumbnail.jpg";
const TEST_CONTEXT = { const TEST_CONTEXT = {
@ -115,7 +117,7 @@ function chunkWhile(arr, fun) {
return parts; return parts;
} }
function threadify(descendants, parentID) { function threadify(descendants) {
/* /*
* Take a list of descendants and sort them into a 2D matrix. * Take a list of descendants and sort them into a 2D matrix.
* The first item is the direct descendant of parentID post and the rest * The first item is the direct descendant of parentID post and the rest
@ -123,8 +125,7 @@ function threadify(descendants, parentID) {
* way Instagram displays conversations in comments. * way Instagram displays conversations in comments.
* i.e. [[first level comment, ...descendants]] * i.e. [[first level comment, ...descendants]]
*/ */
if (descendants.length == 0) {
if (descendants == []) {
return []; return [];
} }
@ -164,9 +165,9 @@ function threadify(descendants, parentID) {
} }
} }
}); });
} }
return sorted; return sorted;
} }
const CommentJsx = (props) => { const CommentJsx = (props) => {
@ -180,17 +181,22 @@ const CommentJsx = (props) => {
return ( return (
<View style = { styles.container }> <View style = { styles.container }>
<Image <Image
source = { { uri: props.data.avatar } } source = { { uri: props.data.account.avatar } }
style = { styles.avatar } /> style = { styles.avatar } />
<View style = { styles.contentContainer }> <View style = { styles.contentContainer }>
<Text style = { styles.content }> <Text style = { styles.content }>
<Text style = { styles.bold }>{ props.data.username }</Text>&nbsp; <Text style = { styles.bold }>{ props.data.account.acct }</Text>&nbsp;
{ props.data.content } { props.data.content }
</Text> </Text>
<View style = { styles.commentActions }> <View style = { styles.commentActions }>
<View> <View>
<Text style = { styles.actionText }> <Text style = { styles.actionText }>
{ timeToAge((new Date()).getTime(), props.data.created_at) } {
timeToAge(
Date.now(),
(new Date(props.data.created_at)).getTime()
)
}
</Text> </Text>
</View> </View>
<TouchableWithoutFeedback> <TouchableWithoutFeedback>
@ -213,17 +219,32 @@ const CommentJsx = (props) => {
const ViewCommentsJsx = (props) => { const ViewCommentsJsx = (props) => {
let [state, setState] = useState({ let [state, setState] = useState({
postData: undefined, postData: props.navigation.getParam("postData", null),
loaded: false, loaded: false,
reply: "" reply: ""
}); });
useEffect(() => { useEffect(() => {
AsyncStorage.getItem("@user_profile").then((profileJSON) => { let profile, instance, accessToken;
setState({ ...state, AsyncStorage
descendants: threadify(TEST_CONTEXT.descendants), .multiGet([
postData: props.navigation.getParam("postData"), "@user_profile",
profile: JSON.parse(profileJSON), "@user_instance",
"@user_token",
]).then(([profilePair, instancePair, tokenPair]) => {
profile = JSON.parse(profilePair[1]);
instance = instancePair[1];
accessToken = JSON.parse(tokenPair[1]).access_token;
return requests
.fetchStatusContext(instance, state.postData.id, accessToken)
})
.then(context => {
setState({...state,
descendants: threadify(context.descendants),
profile,
instance,
accessToken,
loaded: true, loaded: true,
}); });
}); });
@ -243,8 +264,8 @@ const ViewCommentsJsx = (props) => {
data = { state.postData } /> data = { state.postData } />
</View> </View>
<View> <View>
{ { state.descendants.length != 0
state.descendants.map((thread, i) => { ? state.descendants.map((thread, i) => {
const comment = thread[0]; const comment = thread[0];
const subs = thread.slice(1); const subs = thread.slice(1);
return ( return (
@ -265,6 +286,11 @@ const ViewCommentsJsx = (props) => {
</View> </View>
); );
}) })
: <View style = { styles.emptyMessage.container }>
<Text style = { styles.emptyMessage.text }>
No comments
</Text>
</View>
} }
</View> </View>
</View> </View>
@ -364,7 +390,17 @@ const styles = {
commentSubmit: { commentSubmit: {
width: 30, width: 30,
height: 30, height: 30,
} },
emptyMessage: {
container: {
paddingTop: 30,
paddingBottom: 30,
},
text: {
textAlign: "center",
color: "#666",
},
},
}; };
export default ViewCommentsJsx; export default ViewCommentsJsx;

View File

@ -81,6 +81,11 @@ export async function fetchAccountStatuses(domain, id, token) {
return resp.json(); return resp.json();
} }
export async function fetchStatusContext(domain, id, token) {
const resp = await get(`https://${domain}/api/v1/statuses/${id}/context`, token);
return resp.json();
}
export async function fetchFollowing(domain, id, token) { export async function fetchFollowing(domain, id, token) {
const resp = await get(`https://${domain}/api/v1/accounts/${id}/following`, token); const resp = await get(`https://${domain}/api/v1/accounts/${id}/following`, token);
return resp.json(); return resp.json();