Skip to content

Commit ff2c125

Browse files
Implemented User Content Querying
1 parent ef6ec28 commit ff2c125

14 files changed

+198
-24
lines changed

Source/API/User/Content.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
import { user as endpoint } from '../../Endpoints/mod.ts'
3+
import { query } from '../../Fetch.js'
4+
5+
6+
const { log } = console;
7+
8+
9+
export default async function ( userId , options ){
10+
11+
const { content = 'all' } = options;
12+
13+
14+
const { profile } = await
15+
fetch(userId,content);
16+
17+
18+
if(content === 'all'){
19+
20+
const response = await
21+
fetch(userId,'collabs');
22+
23+
profile.content.content.collabs
24+
= response.profile.content.content.collabs ?? [];
25+
}
26+
27+
return profile;
28+
}
29+
30+
31+
async function fetch ( userId , content ){
32+
return await query({
33+
url : endpoint.user( userId ) ,
34+
parameters : { content }
35+
});
36+
}

Source/API/User/Id.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import { user as endpoint } from '../../Endpoints/mod.ts'
3+
import { query } from '../../Fetch.js'
4+
5+
6+
const { log } = console;
7+
8+
9+
export default async function ( username ){
10+
11+
const response = await query({
12+
url : endpoint.id ,
13+
parameters : { username }
14+
});
15+
16+
const { user_id } = response;
17+
18+
return user_id;
19+
}

Source/API/User/mod.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import { UserContentQuery } from '../../Types/UserContentQuery.ts'
3+
import { Profile } from '../../Types/Profile.ts'
4+
5+
import fetchContent from './Content.js'
6+
import fetchId from './Id.js'
7+
8+
9+
10+
/**
11+
* Resolves the users id from their username.
12+
*/
13+
14+
export async function id (
15+
16+
username : string
17+
18+
) : number {
19+
20+
return await
21+
fetchId ( username );
22+
}
23+
24+
25+
/**
26+
* Queries for a users profile and the content specified.
27+
*/
28+
29+
export async function content (
30+
31+
query : UserContentQuery
32+
33+
) : Profile {
34+
35+
return await
36+
fetchContent ( query ) as Profile;
37+
}

Source/Endpoints/Base.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11

22

33
export const domain
4-
= 'https://devrant.com/api/devrant';
4+
= 'https://devrant.com/api';
5+
6+
export const images
7+
= 'https://avatars.devrant.com';
58

69
export const rants
7-
= `${ domain }/rants`;
10+
= `${ domain }/devrant/rants`;
811

912
export const random
1013
= `${ rants }/surprise`;

Source/Endpoints/User.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11

2-
import { users } from './Base.js'
2+
import { domain , users } from './Base.js'
33

44

5+
export const id =
6+
`${ domain }/get-user-id`;
7+
58
export const user = ( id ) =>
69
`${ users }/${ id }`;
710

8-
export const edit
9-
= `${ users }/me/edit-profile`;
11+
export const edit =
12+
`${ users }/me/edit-profile`;
1013

11-
export const authenticate
12-
= `${ users }/auth-token`;
14+
export const authenticate =
15+
`${ users }/auth-token`;
1316

14-
export const notifications
15-
= `${ users }/me/notif-feed`;
17+
export const notifications =
18+
`${ users }/me/notif-feed`;
1619

17-
export const avatar
18-
= `${ users }/me/avatar`;
20+
export const avatar =
21+
`${ users }/me/avatar`;
1922

2023
export const subscribe = ( id ) =>
2124
`${ user(id) }/subscribe`;
File renamed without changes.

Source/Fetch.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
const { error , log } = console;
3+
4+
5+
export async function query ( options ){
6+
7+
let { url , parameters } = options;
8+
9+
url = new URL(url);
10+
11+
parameters ??= {};
12+
parameters.app = 3;
13+
14+
15+
const { searchParams } = url;
16+
17+
for(const [ parameter , value ] of Object.entries(parameters))
18+
searchParams.set(parameter,value);
19+
20+
log(url.href);
21+
22+
const response = await fetch(url);
23+
24+
log(response);
25+
26+
if(response.ok)
27+
return await response.json();
28+
29+
throw new Error(
30+
`\n${ response.status } : ${ response.statusText }` +
31+
`\nUrl : ${ response.url }`
32+
)
33+
}

Source/Types/Collaboration.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import { Image } from './Image.ts'
3+
import { Post } from './Post.ts'
4+
5+
6+
export interface Collaboration extends Post {
7+
8+
attached_image : '' | Image ,
9+
10+
user_avatar_lg : Avatar ,
11+
12+
num_comments : number ,
13+
14+
c_type_long : string ,
15+
16+
user_dpp : number ,
17+
18+
c_type : number ,
19+
20+
tags : string [] ,
21+
22+
text : string ,
23+
24+
rt : number ,
25+
26+
rc : number ,
27+
28+
id : number ,
29+
30+
}

Source/Types/Comment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Post } from './Post.ts'
33

44

5-
export interface Rant extends Post {
5+
export interface Comment extends Post {
66

77
body : string
88

Source/Types/Post.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Avatar } from './Avatar.ts'
33
import { Link } from './Link.ts'
44

55

6-
export interface Rant {
6+
export interface Post {
77

88
created_time : number ,
99

@@ -15,8 +15,6 @@ export interface Rant {
1515

1616
user_id : number ,
1717

18-
edited : bool ,
19-
2018
links ? : Link [] ,
2119

2220
score : number

Source/Types/Rant.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { Post } from './Post.ts'
55

66
export interface Rant extends Post {
77

8-
user_avatar_lg : Avatar ,
9-
108
attached_image : '' | Image ,
119

10+
user_avatar_lg : Avatar ,
11+
1212
num_comments : number ,
1313

1414
special : bool ,
1515

16+
edited : bool ,
17+
1618
tags : string [] ,
1719

1820
text : string ,
@@ -21,6 +23,6 @@ export interface Rant extends Post {
2123

2224
rc : number ,
2325

24-
id : number ,
26+
id : number
2527

2628
}

Source/Types/UserContentQuery.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import { UserContentType } from './UserContentType.ts'
3+
4+
5+
export interface UserContentQuery {
6+
7+
content : UserContentType ,
8+
9+
userId : string
10+
11+
}

Source/Types/UserContentType.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22
export enum UserContentType {
33

4-
Collaborations ,
4+
Collaborations = 'collabs' ,
55

6-
Everything ,
6+
Everything = 'all' ,
77

8-
Favorites ,
8+
Favorites = 'favorites' ,
99

10-
Comments ,
10+
Comments = 'comments' ,
1111

12-
Rants ,
12+
Rants = 'rants' ,
1313

14-
Likes
14+
Likes = 'likes'
1515

1616
}

Source/mod.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export * as User from './API/User/mod.ts'

0 commit comments

Comments
 (0)