Skip to content

Commit

Permalink
Implemented User Content Querying
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectronicsArchiver committed Oct 8, 2022
1 parent ef6ec28 commit ff2c125
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 24 deletions.
36 changes: 36 additions & 0 deletions Source/API/User/Content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import { user as endpoint } from '../../Endpoints/mod.ts'
import { query } from '../../Fetch.js'


const { log } = console;


export default async function ( userId , options ){

const { content = 'all' } = options;


const { profile } = await
fetch(userId,content);


if(content === 'all'){

const response = await
fetch(userId,'collabs');

profile.content.content.collabs
= response.profile.content.content.collabs ?? [];
}

return profile;
}


async function fetch ( userId , content ){
return await query({
url : endpoint.user( userId ) ,
parameters : { content }
});
}
19 changes: 19 additions & 0 deletions Source/API/User/Id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { user as endpoint } from '../../Endpoints/mod.ts'
import { query } from '../../Fetch.js'


const { log } = console;


export default async function ( username ){

const response = await query({
url : endpoint.id ,
parameters : { username }
});

const { user_id } = response;

return user_id;
}
37 changes: 37 additions & 0 deletions Source/API/User/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import { UserContentQuery } from '../../Types/UserContentQuery.ts'
import { Profile } from '../../Types/Profile.ts'

import fetchContent from './Content.js'
import fetchId from './Id.js'



/**
* Resolves the users id from their username.
*/

export async function id (

username : string

) : number {

return await
fetchId ( username );
}


/**
* Queries for a users profile and the content specified.
*/

export async function content (

query : UserContentQuery

) : Profile {

return await
fetchContent ( query ) as Profile;
}
7 changes: 5 additions & 2 deletions Source/Endpoints/Base.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@


export const domain
= 'https://devrant.com/api/devrant';
= 'https://devrant.com/api';

export const images
= 'https://avatars.devrant.com';

export const rants
= `${ domain }/rants`;
= `${ domain }/devrant/rants`;

export const random
= `${ rants }/surprise`;
Expand Down
21 changes: 12 additions & 9 deletions Source/Endpoints/User.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@

import { users } from './Base.js'
import { domain , users } from './Base.js'


export const id =
`${ domain }/get-user-id`;

export const user = ( id ) =>
`${ users }/${ id }`;

export const edit
= `${ users }/me/edit-profile`;
export const edit =
`${ users }/me/edit-profile`;

export const authenticate
= `${ users }/auth-token`;
export const authenticate =
`${ users }/auth-token`;

export const notifications
= `${ users }/me/notif-feed`;
export const notifications =
`${ users }/me/notif-feed`;

export const avatar
= `${ users }/me/avatar`;
export const avatar =
`${ users }/me/avatar`;

export const subscribe = ( id ) =>
`${ user(id) }/subscribe`;
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions Source/Fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

const { error , log } = console;


export async function query ( options ){

let { url , parameters } = options;

url = new URL(url);

parameters ??= {};
parameters.app = 3;


const { searchParams } = url;

for(const [ parameter , value ] of Object.entries(parameters))
searchParams.set(parameter,value);

log(url.href);

const response = await fetch(url);

log(response);

if(response.ok)
return await response.json();

throw new Error(
`\n${ response.status } : ${ response.statusText }` +
`\nUrl : ${ response.url }`
)
}
30 changes: 30 additions & 0 deletions Source/Types/Collaboration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { Image } from './Image.ts'
import { Post } from './Post.ts'


export interface Collaboration extends Post {

attached_image : '' | Image ,

user_avatar_lg : Avatar ,

num_comments : number ,

c_type_long : string ,

user_dpp : number ,

c_type : number ,

tags : string [] ,

text : string ,

rt : number ,

rc : number ,

id : number ,

}
2 changes: 1 addition & 1 deletion Source/Types/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Post } from './Post.ts'


export interface Rant extends Post {
export interface Comment extends Post {

body : string

Expand Down
4 changes: 1 addition & 3 deletions Source/Types/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Avatar } from './Avatar.ts'
import { Link } from './Link.ts'


export interface Rant {
export interface Post {

created_time : number ,

Expand All @@ -15,8 +15,6 @@ export interface Rant {

user_id : number ,

edited : bool ,

links ? : Link [] ,

score : number
Expand Down
8 changes: 5 additions & 3 deletions Source/Types/Rant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { Post } from './Post.ts'

export interface Rant extends Post {

user_avatar_lg : Avatar ,

attached_image : '' | Image ,

user_avatar_lg : Avatar ,

num_comments : number ,

special : bool ,

edited : bool ,

tags : string [] ,

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

rc : number ,

id : number ,
id : number

}
11 changes: 11 additions & 0 deletions Source/Types/UserContentQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { UserContentType } from './UserContentType.ts'


export interface UserContentQuery {

content : UserContentType ,

userId : string

}
12 changes: 6 additions & 6 deletions Source/Types/UserContentType.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

export enum UserContentType {

Collaborations ,
Collaborations = 'collabs' ,

Everything ,
Everything = 'all' ,

Favorites ,
Favorites = 'favorites' ,

Comments ,
Comments = 'comments' ,

Rants ,
Rants = 'rants' ,

Likes
Likes = 'likes'

}
2 changes: 2 additions & 0 deletions Source/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export * as User from './API/User/mod.ts'

0 comments on commit ff2c125

Please sign in to comment.