Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starting to add comment tree paging #70

Merged
merged 9 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lemmy-js-client",
"description": "A javascript / typescript client for Lemmy",
"version": "0.17.0-rc.33",
"version": "0.17.0-rc.37",
"author": "Dessalines <tyhou13@gmx.com>",
"license": "AGPL-3.0",
"main": "./dist/index.js",
Expand All @@ -19,23 +19,23 @@
"@sniptt/monads": "^0.5.10",
"@types/node": "^17.0.33",
"@types/node-fetch": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"class-transformer": "^0.5.1",
"eslint": "^8.15.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint": "^8.20.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"lint-staged": "^12.4.1",
"node-fetch": "^3.2.4",
"prettier": "^2.6.2",
"node-fetch": "^3.2.9",
"prettier": "^2.7.1",
"prettier-plugin-import-sort": "^0.0.7",
"prettier-plugin-organize-imports": "^2.3.4",
"prettier-plugin-organize-imports": "^3.0.0",
"prettier-plugin-packagejson": "^2.2.18",
"reflect-metadata": "^0.1.13",
"sortpack": "^2.2.0",
"sortpack": "^2.3.0",
"typedoc": "^0.21.6",
"typedoc-plugin-sourcefile-url": "^1.0.6",
"typescript": "^4.6.4"
"typescript": "^4.7.4"
},
"types": "./dist/index.d.ts",
"lint-staged": {
Expand Down
42 changes: 19 additions & 23 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
GetCommentsResponse,
ListCommentReports,
ListCommentReportsResponse,
MarkCommentAsRead,
RemoveComment,
ResolveCommentReport,
SaveComment,
Expand Down Expand Up @@ -66,6 +65,7 @@ import {
Login,
LoginResponse,
MarkAllAsRead,
MarkCommentReplyAsRead,
MarkPersonMentionAsRead,
MarkPrivateMessageAsRead,
PasswordChange,
Expand Down Expand Up @@ -578,7 +578,7 @@ export class LemmyHttp {
*
* `HTTP.POST /comment/mark_as_read`
*/
async markCommentAsRead(form: MarkCommentAsRead) {
async markCommentReplyAsRead(form: MarkCommentReplyAsRead) {
return this.wrapper(
HttpType.Post,
"/comment/mark_as_read",
Expand Down Expand Up @@ -1098,20 +1098,14 @@ export class LemmyHttp {
): Promise<ResponseType> {
if (type_ == HttpType.Get) {
let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
return (
fetch(getUrl, {
method: "GET",
headers: this.headers,
})
// TODO test this
.then(
d =>
d
.text()
.then(a =>
deserialize(responseClass, a)
) as Promise<ResponseType>
)
return fetch(getUrl, {
method: "GET",
headers: this.headers,
}).then(
d =>
d
.text()
.then(a => deserialize(responseClass, a)) as Promise<ResponseType>
);
} else {
return fetch(this.buildFullUrl(endpoint), {
Expand All @@ -1121,18 +1115,20 @@ export class LemmyHttp {
...this.headers,
},
body: serialize(form),
}).then(d => d.json() as Promise<ResponseType>);
}).then(
d =>
d
.text()
.then(a => deserialize(responseClass, a)) as Promise<ResponseType>
);
}
}
}

function encodeGetParams<BodyType>(p: BodyType): string {
// Necessary to remove the Options
let serialized = JSON.parse(serialize(p));
return (
Object.entries(serialized)
// TODO test this, it might serialize the undefineds
.map(kv => kv.map(encodeURIComponent).join("="))
.join("&")
);
return Object.entries(serialized)
.map(kv => kv.map(encodeURIComponent).join("="))
.join("&");
}
1 change: 1 addition & 0 deletions src/interfaces/aggregates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ export interface CommentAggregates {
score: number;
upvotes: number;
downvotes: number;
child_count: number;
}
29 changes: 14 additions & 15 deletions src/interfaces/api/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Option } from "@sniptt/monads";
import { Expose, Transform, Type } from "class-transformer";
import "reflect-metadata";
import { toOption, toUndefined } from "../../utils";
import { ListingType, SortType } from "../others";
import { CommentSortType, ListingType } from "../others";
import { CommentReportView, CommentView } from "../views";

export class CreateComment {
Expand Down Expand Up @@ -73,19 +73,6 @@ export class RemoveComment {
}
}

/**
* Only the recipient can do this.
*/
export class MarkCommentAsRead {
comment_id: number;
read: boolean;
auth: string;

constructor(init: MarkCommentAsRead) {
Object.assign(this, init);
}
}

export class SaveComment {
comment_id: number;
save: boolean;
Expand Down Expand Up @@ -133,7 +120,11 @@ export class GetComments {
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
sort: Option<SortType>;
sort: Option<CommentSortType>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
max_depth: Option<number>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
Expand All @@ -153,6 +144,14 @@ export class GetComments {
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
post_id: Option<number>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
parent_id: Option<number>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
saved_only: Option<boolean>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
Expand Down
26 changes: 21 additions & 5 deletions src/interfaces/api/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Option } from "@sniptt/monads";
import { Expose, Transform, Type } from "class-transformer";
import "reflect-metadata";
import { toOption, toUndefined } from "../../utils";
import { SortType } from "../others";
import { CommentSortType, SortType } from "../others";
import {
CommentReplyView,
CommentView,
CommunityModeratorView,
PersonMentionView,
Expand Down Expand Up @@ -275,8 +276,8 @@ export class GetPersonDetailsResponse {
}

export class GetRepliesResponse {
@Type(() => CommentView)
replies: CommentView[];
@Type(() => CommentReplyView)
replies: CommentReplyView[];
}

export class GetPersonMentionsResponse {
Expand Down Expand Up @@ -346,7 +347,7 @@ export class GetReplies {
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
sort: Option<SortType>;
sort: Option<CommentSortType>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
Expand All @@ -370,7 +371,7 @@ export class GetPersonMentions {
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
sort: Option<SortType>;
sort: Option<CommentSortType>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
Expand Down Expand Up @@ -405,6 +406,21 @@ export class PersonMentionResponse {
person_mention_view: PersonMentionView;
}

export class MarkCommentReplyAsRead {
comment_reply_id: number;
read: boolean;
auth: string;

constructor(init: MarkCommentReplyAsRead) {
Object.assign(this, init);
}
}

export class CommentReplyResponse {
@Type(() => CommentReplyView)
comment_reply_view: CommentReplyView;
}

/**
* Permanently deletes your posts and comments
*/
Expand Down
12 changes: 8 additions & 4 deletions src/interfaces/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "reflect-metadata";
import { toOption, toUndefined } from "../../utils";
import { ListingType, SiteMetadata, SortType } from "../others";
import {
CommentView,
CommunityModeratorView,
CommunityView,
PostReportView,
Expand Down Expand Up @@ -43,7 +42,14 @@ export class PostResponse {
}

export class GetPost {
id: number;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
id: Option<number>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
comment_id: Option<number>;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
Expand All @@ -59,8 +65,6 @@ export class GetPostResponse {
post_view: PostView;
@Type(() => CommunityView)
community_view: CommunityView;
@Type(() => CommentView)
comments: CommentView[];
@Type(() => CommunityModeratorView)
moderators: CommunityModeratorView[];
online: number;
Expand Down
37 changes: 33 additions & 4 deletions src/interfaces/others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export enum UserOperation {
EditComment,
DeleteComment,
RemoveComment,
MarkCommentAsRead,
SaveComment,
CreateCommentLike,
GetPosts,
Expand All @@ -39,6 +38,7 @@ export enum UserOperation {
GetReplies,
GetPersonMentions,
MarkPersonMentionAsRead,
MarkCommentReplyAsRead,
GetModlog,
BanFromCommunity,
AddModToCommunity,
Expand Down Expand Up @@ -89,18 +89,25 @@ export enum UserOperation {
}

/**
* Different sort types used in lemmy.
* Different post sort types used in lemmy.
*/
export enum SortType {
/**
* Posts sorted by the most recent comment.
* Posts sorted by hot, but bumped by new comments up to 2 days
*/
Active = "Active",
/**
* Posts sorted by the published time.
* Posts sorted by a decaying rank.
*/
Hot = "Hot",
/**
* Posts sorted by the published time.
*/
New = "New",
/**
* Posts sorted by the published time ascending
*/
Old = "Old",
/**
* The top posts for this last day.
*/
Expand Down Expand Up @@ -131,6 +138,28 @@ export enum SortType {
NewComments = "NewComments",
}

/**
* Different comment sort types used in lemmy.
*/
export enum CommentSortType {
/**
* Comments sorted by a decaying rank.
*/
Hot = "Hot",
/**
* Comments sorted by top score.
*/
Top = "Top",
/**
* Comments sorted by new.
*/
New = "New",
/**
* Comments sorted by old.
*/
Old = "Old",
}

/**
* The different listing types for post and comment fetches.
*/
Expand Down
14 changes: 9 additions & 5 deletions src/interfaces/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,8 @@ export class Comment {
id: number;
creator_id: number;
post_id: number;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
@Expose()
parent_id: Option<number>;
content: string;
removed: boolean;
read: boolean; // Whether the recipient has read the comment or not
published: string;
@Transform(({ value }) => toOption(value), { toClassOnly: true })
@Transform(({ value }) => toUndefined(value), { toPlainOnly: true })
Expand All @@ -458,6 +453,7 @@ export class Comment {
deleted: boolean;
ap_id: string;
local: boolean;
path: string;
}

export class PersonMention {
Expand All @@ -468,6 +464,14 @@ export class PersonMention {
published: string;
}

export class CommentReply {
id: number;
recipient_id: number;
comment_id: number;
read: boolean;
published: string;
}

export class RegistrationApplication {
id: number;
local_user_id: number;
Expand Down
Loading