-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_threads_api.ts
174 lines (157 loc) · 4.81 KB
/
mock_threads_api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// mock_threads_api.ts
import type {
ThreadsContainer,
ThreadsPost,
ThreadsProfile,
PublishingLimit,
ThreadsPostRequest,
ThreadsListResponse,
} from "./types.ts";
export class MockThreadsAPI implements MockThreadsAPI {
private containers: Map<string, ThreadsContainer> = new Map();
private posts: Map<string, ThreadsPost> = new Map();
private users: Map<string, ThreadsProfile> = new Map();
private publishingLimits: Map<string, PublishingLimit> = new Map();
private errorMode = false;
constructor() {
// Initialize with some sample data
this.users.set("12345", {
id: "12345",
username: "testuser",
name: "Test User",
threadsProfilePictureUrl: "https://example.com/profile.jpg",
threadsBiography: "This is a test user",
});
this.publishingLimits.set("12345", {
quota_usage: 10,
config: {
quota_total: 250,
quota_duration: 86400,
},
});
}
setErrorMode(mode: boolean) {
this.errorMode = mode;
}
createThreadsContainer(
request: ThreadsPostRequest
): Promise<string | { id: string; permalink: string }> {
if (this.errorMode) {
return Promise.reject(new Error("Failed to create Threads container"));
}
const containerId = `container_${Math.random().toString(36).substring(7)}`;
const permalink = `https://www.threads.net/@${request.userId}/post/${containerId}`;
const container: ThreadsContainer = {
id: containerId,
permalink,
status: "FINISHED",
};
this.containers.set(containerId, container);
// Create a post immediately when creating a container
const postId = `post_${Math.random().toString(36).substring(7)}`;
const post: ThreadsPost = {
id: postId,
media_product_type: "THREADS",
media_type: request.mediaType,
permalink,
owner: { id: request.userId },
username: "testuser",
text: request.text || "",
timestamp: new Date().toISOString(),
shortcode: postId,
is_quote_post: false,
hasReplies: false,
isReply: false,
isReplyOwnedByMe: false,
};
this.posts.set(postId, post);
// Always return an object with both id and permalink
return Promise.resolve({ id: containerId, permalink });
}
publishThreadsContainer(
_userId: string,
_accessToken: string,
containerId: string,
getPermalink: boolean = false
): Promise<string | { id: string; permalink: string }> {
if (this.errorMode) {
return Promise.reject(new Error("Failed to publish Threads container"));
}
const container = this.containers.get(containerId);
if (!container) {
return Promise.reject(new Error("Container not found"));
}
// Find the post associated with this container
const existingPost = Array.from(this.posts.values()).find(
(post) => post.permalink === container.permalink
);
if (!existingPost) {
return Promise.reject(
new Error("Post not found for the given container")
);
}
return Promise.resolve(
getPermalink
? { id: existingPost.id, permalink: existingPost.permalink || "" }
: existingPost.id
);
}
createCarouselItem(
request: Omit<ThreadsPostRequest, "mediaType"> & {
mediaType: "IMAGE" | "VIDEO";
}
): Promise<string | { id: string }> {
const itemId = `item_${Math.random().toString(36).substring(7)}`;
const container: ThreadsContainer = {
id: itemId,
permalink: `https://www.threads.net/@${request.userId}/post/${itemId}`,
status: "FINISHED",
};
this.containers.set(itemId, container);
return Promise.resolve({ id: itemId });
}
getPublishingLimit(
userId: string,
_accessToken: string
): Promise<PublishingLimit> {
if (this.errorMode) {
return Promise.reject(new Error("Failed to get publishing limit"));
}
const limit = this.publishingLimits.get(userId);
if (!limit) {
return Promise.reject(new Error("Publishing limit not found"));
}
return Promise.resolve(limit);
}
getThreadsList(
userId: string,
_accessToken: string,
options?: {
since?: string;
until?: string;
limit?: number;
after?: string;
before?: string;
}
): Promise<ThreadsListResponse> {
const threads = Array.from(this.posts.values())
.filter((post) => post.owner.id === userId)
.slice(0, options?.limit || 25);
return Promise.resolve({
data: threads as ThreadsPost[],
paging: {
cursors: {
before: "BEFORE_CURSOR",
after: "AFTER_CURSOR",
},
},
});
}
getSingleThread(mediaId: string, _accessToken: string): Promise<ThreadsPost> {
const post = this.posts.get(mediaId);
if (!post) {
return Promise.reject(new Error("Thread not found"));
}
return Promise.resolve(post as ThreadsPost);
}
}