beamsocial 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -118,7 +118,7 @@ export class Client {
118
118
  });
119
119
  let user;
120
120
  user = new User(res.data.id);
121
- user.__load(res.data, this.token, this.baseURL);
121
+ user.__load(res.data, this.session, this.baseURL);
122
122
  return user;
123
123
  }
124
124
  catch (err) {
@@ -154,7 +154,7 @@ export class Client {
154
154
  });
155
155
  const data = res.data;
156
156
  const post = new Post(data.id);
157
- post.__load(data, this.token, this.baseURL);
157
+ post.__load(data, this.session, this.baseURL);
158
158
  return post;
159
159
  }
160
160
  catch (err) {
@@ -186,7 +186,7 @@ export class Client {
186
186
  });
187
187
  const data = res.data;
188
188
  const post = new Post(data.id);
189
- post.__load(data, this.token, this.baseURL);
189
+ post.__load(data, this.session, this.baseURL);
190
190
  return post;
191
191
  }
192
192
  catch (err) {
@@ -223,7 +223,7 @@ export class Client {
223
223
  const posts = [];
224
224
  for (const p of data) {
225
225
  let new_p = new Post(p.id);
226
- new_p.__load(p, this.token, this.baseURL);
226
+ new_p.__load(p, this.session, this.baseURL);
227
227
  posts.push(new_p);
228
228
  }
229
229
  return posts;
@@ -1,6 +1,8 @@
1
1
  import type { RawPost, RawComment, RawFile } from "./types/interactions";
2
2
  import { User } from "./users.js";
3
+ import { Session } from "./auth.js";
3
4
  export declare class Post {
5
+ private __session?;
4
6
  private __token?;
5
7
  private __url?;
6
8
  id: string;
@@ -8,11 +10,11 @@ export declare class Post {
8
10
  content: string;
9
11
  attachments: RawFile[];
10
12
  visibility: "everyone" | "followers" | "friends" | "me";
11
- likes: string[];
12
- reposts: string[];
13
- comments: RawComment[];
13
+ likes: number;
14
+ reposts: number;
15
+ comments: number;
14
16
  constructor(id: string);
15
- __load(data: RawPost, __token?: string, __url?: string): void;
17
+ __load(data: RawPost, __session?: Session, __url?: string): void;
16
18
  like(): Promise<void>;
17
19
  unlike(): Promise<void>;
18
20
  repost(): Promise<void>;
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import axios from "axios";
10
11
  import { User } from "./users.js";
11
12
  export class Post {
12
13
  constructor(id) {
@@ -14,16 +15,17 @@ export class Post {
14
15
  this.content = '';
15
16
  this.attachments = [];
16
17
  this.visibility = "followers";
17
- this.likes = [];
18
- this.reposts = [];
19
- this.comments = [];
18
+ this.likes = 0;
19
+ this.reposts = 0;
20
+ this.comments = 0;
20
21
  this.id = id;
21
22
  }
22
- __load(data, __token, __url) {
23
- this.__token = __token;
23
+ __load(data, __session, __url) {
24
+ this.__token = (__session === null || __session === void 0 ? void 0 : __session.token) || this.__token;
25
+ this.__session = __session;
24
26
  this.__url = __url + '/posts/' + data.id;
25
27
  this.author = new User(data.author.id);
26
- this.author.__load(data.author, __token, __url + '/users/' + data.author.name);
28
+ this.author.__load(data.author, __session, __url);
27
29
  this.content = data.content;
28
30
  this.attachments = data.attachments;
29
31
  this.visibility = data.visibility;
@@ -32,10 +34,73 @@ export class Post {
32
34
  this.comments = data.comments;
33
35
  }
34
36
  like() {
35
- return __awaiter(this, void 0, void 0, function* () { });
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ var _a;
39
+ try {
40
+ const res = yield axios.post(this.__url + `/like`, {}, {
41
+ headers: {
42
+ Authorization: `Bearer ${this.__token}`,
43
+ },
44
+ });
45
+ this.likes += 1;
46
+ (_a = this.__session) === null || _a === void 0 ? void 0 : _a.profile.likes.push(this.__session.id);
47
+ return;
48
+ }
49
+ catch (err) {
50
+ if (axios.isAxiosError(err) && err.response) {
51
+ const res = err.response;
52
+ if (res.status == 401) {
53
+ throw new Error('Not logged in');
54
+ }
55
+ else if (res.status == 403) {
56
+ throw new Error('Private account');
57
+ }
58
+ else if (res.status == 404) {
59
+ throw new Error('User not found');
60
+ }
61
+ else {
62
+ throw new Error('Error following user');
63
+ }
64
+ }
65
+ else {
66
+ throw err;
67
+ }
68
+ }
69
+ });
36
70
  }
37
71
  unlike() {
38
- return __awaiter(this, void 0, void 0, function* () { });
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ try {
74
+ const res = yield axios.post(this.__url + `/like`, {}, {
75
+ headers: {
76
+ Authorization: `Bearer ${this.__token}`,
77
+ },
78
+ });
79
+ this.likes -= 1;
80
+ this.__session.profile.likes = this.__session.profile.likes.filter((id) => id !== this.__session.id);
81
+ return;
82
+ }
83
+ catch (err) {
84
+ if (axios.isAxiosError(err) && err.response) {
85
+ const res = err.response;
86
+ if (res.status == 401) {
87
+ throw new Error('Not logged in');
88
+ }
89
+ else if (res.status == 403) {
90
+ throw new Error('Private account');
91
+ }
92
+ else if (res.status == 404) {
93
+ throw new Error('User not found');
94
+ }
95
+ else {
96
+ throw new Error('Error following user');
97
+ }
98
+ }
99
+ else {
100
+ throw err;
101
+ }
102
+ }
103
+ });
39
104
  }
40
105
  repost() {
41
106
  return __awaiter(this, void 0, void 0, function* () { });
@@ -14,7 +14,7 @@ export type RawComment = {
14
14
  post_id: string;
15
15
  content: string;
16
16
  visibility: "everyone" | "followers" | "friends" | "me";
17
- likes: string[];
17
+ likes: number;
18
18
  };
19
19
  export type RawPost = {
20
20
  id: string;
@@ -22,7 +22,7 @@ export type RawPost = {
22
22
  content: string;
23
23
  attachments: RawFile[];
24
24
  visibility: "everyone" | "followers" | "friends" | "me";
25
- likes: string[];
26
- reposts: string[];
27
- comments: RawComment[];
25
+ likes: number;
26
+ reposts: number;
27
+ comments: number;
28
28
  };
@@ -6,6 +6,7 @@ export type Profile = {
6
6
  update_date: string;
7
7
  followers: string[];
8
8
  following: string[];
9
+ likes: string[];
9
10
  status: string | null;
10
11
  birthday: string | null;
11
12
  pronouns: string | null;
@@ -1,7 +1,9 @@
1
- import type { Profile } from "./types/profiles";
1
+ import type { Profile } from "./types/profiles.js";
2
+ import { Session } from "./auth.js";
2
3
  export declare class User {
3
4
  private __token?;
4
5
  private __url?;
6
+ private __session?;
5
7
  id: string;
6
8
  name: string;
7
9
  display_name: string | null;
@@ -15,7 +17,7 @@ export declare class User {
15
17
  account_type: string | null;
16
18
  level: number;
17
19
  constructor(id: string);
18
- __load(data: Profile, __token?: string, __url?: string): void;
20
+ __load(data: Profile, __session?: Session, __url?: string): void;
19
21
  follow(): Promise<void>;
20
22
  unfollow(): Promise<void>;
21
23
  block(): Promise<void>;
@@ -23,9 +23,10 @@ export class User {
23
23
  this.level = 0;
24
24
  this.id = id;
25
25
  }
26
- __load(data, __token, __url) {
27
- this.__token = __token;
28
- this.__url = __url;
26
+ __load(data, __session, __url) {
27
+ this.__token = (__session === null || __session === void 0 ? void 0 : __session.token) || this.__token;
28
+ this.__session = __session;
29
+ this.__url = __url + '/users/' + data.name;
29
30
  this.name = data.name;
30
31
  this.display_name = data.display_name;
31
32
  this.creation_date = data.creation_date;
@@ -46,6 +47,7 @@ export class User {
46
47
  Authorization: `Bearer ${this.__token}`,
47
48
  },
48
49
  });
50
+ this.followers.push(this.__session.id);
49
51
  return;
50
52
  }
51
53
  catch (err) {
@@ -78,6 +80,7 @@ export class User {
78
80
  Authorization: `Bearer ${this.__token}`,
79
81
  },
80
82
  });
83
+ this.followers = this.followers.filter(item => item !== this.__session.id);
81
84
  return;
82
85
  }
83
86
  catch (err) {
@@ -104,12 +107,14 @@ export class User {
104
107
  }
105
108
  block() {
106
109
  return __awaiter(this, void 0, void 0, function* () {
110
+ var _a;
107
111
  try {
108
112
  const res = yield axios.post(this.__url + `/block`, {}, {
109
113
  headers: {
110
114
  Authorization: `Bearer ${this.__token}`,
111
115
  },
112
116
  });
117
+ (_a = this.__session) === null || _a === void 0 ? void 0 : _a.settings.blocklist.push(this.id);
113
118
  return;
114
119
  }
115
120
  catch (err) {
@@ -139,6 +144,9 @@ export class User {
139
144
  Authorization: `Bearer ${this.__token}`,
140
145
  },
141
146
  });
147
+ if (this.__session && this.__session.settings) {
148
+ this.__session.settings.blocklist = this.__session.settings.blocklist.filter(item => item !== this.id);
149
+ }
142
150
  return;
143
151
  }
144
152
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beamsocial",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "API-wrapper pour Beam",
6
6
  "main": "index.js",