Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
add post editing
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed Jul 7, 2024
1 parent 884472a commit dee3a08
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 198 deletions.
4 changes: 2 additions & 2 deletions src/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ export class socket extends EventEmitter<{
) return;
if (packet.cmd) return;

const post = new post({
const p = new post({
api_token: this.opts.api_token,
api_url: this.opts.api_url,
data: packet.val as unknown as api_post,
});

this.emit('post', post);
this.emit('post', p);
});
}

Expand Down
37 changes: 37 additions & 0 deletions src/interfaces/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export interface post_report_options {
comment: string;
}

/** post update options */
export interface post_update_options {
/** new content */
content?: string;
/** new attachments */
attachments?: string[];
/** nonce */
nonce?: string;
}

/** check if a value is a post */
export function is_api_post(obj: unknown): obj is api_post {
if (obj === null || typeof obj !== 'object') return false;
Expand Down Expand Up @@ -211,4 +221,31 @@ export class post {
});
}
}

/** edit the post */
async update(opts: post_update_options) {
const resp = await fetch(`${this.api_url}/posts/?id=${this.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'token': this.api_token,
},
body: JSON.stringify({
attachments: opts.attachments ?? this.attachments,
content: opts.content ?? this.content,
nonce: opts.nonce ?? undefined,
}),
});

const data = await resp.json();

if (!resp.ok || data.error) {
throw new Error('failed to update post', {
cause: data,
});
}

this.raw = data;
this.assign_data();
}
}
6 changes: 6 additions & 0 deletions tests/internal/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
assertEquals,
assertRejects,
assertThrows,
} from 'jsr:@std/[email protected]';
export { mockFetch } from 'jsr:@c4spar/[email protected]';
62 changes: 33 additions & 29 deletions tests/internal/post.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import { assertEquals } from 'jsr:@std/[email protected]';
import { type post, type api_post, post_type } from '../../src/interfaces/post.ts';
import {
type api_post,
type post,
post_type,
} from '../../src/interfaces/post.ts';

export function post_is_api_post(post: post, api_post: api_post) {
assertEquals(post.id, api_post._id);
assertEquals(post.deleted, api_post.isDeleted);
assertEquals(post.content, api_post.p);
assertEquals(post.pinned, api_post.pinned);
assertEquals(post.id, api_post.post_id);
assertEquals(post.chat_id, api_post.post_origin);
assertEquals(post.timestamp, api_post.t.e);
assertEquals(post.type, api_post.type);
assertEquals(post.username, api_post.u);
if (api_post.bridged && post.bridged) {
post_is_api_post(post.bridged, api_post.bridged);
} else {
assertEquals(post.bridged, undefined);
}
assertEquals(post.id, api_post._id);
assertEquals(post.deleted, api_post.isDeleted);
assertEquals(post.content, api_post.p);
assertEquals(post.pinned, api_post.pinned);
assertEquals(post.id, api_post.post_id);
assertEquals(post.chat_id, api_post.post_origin);
assertEquals(post.timestamp, api_post.t.e);
assertEquals(post.type, api_post.type);
assertEquals(post.username, api_post.u);
if (api_post.bridged && post.bridged) {
post_is_api_post(post.bridged, api_post.bridged);
} else {
assertEquals(post.bridged, undefined);
}
}

export const regular_post: api_post = {
_id: 'test',
attachments: [],
isDeleted: false,
p: 'test',
pinned: false,
post_id: 'test',
post_origin: 'test',
t: {
e: 0,
},
type: post_type.normal,
u: 'test',
_id: 'test',
attachments: [],
isDeleted: false,
p: 'test',
pinned: false,
post_id: 'test',
post_origin: 'test',
t: {
e: 0,
},
type: post_type.normal,
u: 'test',
};

export const bridged_post: api_post = {
...regular_post,
bridged: regular_post,
...regular_post,
bridged: regular_post,
};
Loading

0 comments on commit dee3a08

Please sign in to comment.