Skip to content

Commit e298125

Browse files
committed
fix: urgent providers fix
1 parent 7eeb1cb commit e298125

File tree

4 files changed

+423
-287
lines changed

4 files changed

+423
-287
lines changed

libraries/nestjs-libraries/src/integrations/social/lemmy.provider.ts

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import dayjs from 'dayjs';
1010
import { Integration } from '@prisma/client';
1111
import { AuthService } from '@gitroom/helpers/auth/auth.service';
1212
import { LemmySettingsDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/lemmy.dto';
13-
import { groupBy } from 'lodash';
1413
import { Tool } from '@gitroom/nestjs-libraries/integrations/tool.decorator';
1514

1615
export class LemmyProvider extends SocialAbstract implements SocialProvider {
@@ -121,14 +120,7 @@ export class LemmyProvider extends SocialAbstract implements SocialProvider {
121120
}
122121
}
123122

124-
async post(
125-
id: string,
126-
accessToken: string,
127-
postDetails: PostDetails<LemmySettingsDto>[],
128-
integration: Integration
129-
): Promise<PostResponse[]> {
130-
const [firstPost, ...restPosts] = postDetails;
131-
123+
private async getJwtAndService(integration: Integration): Promise<{ jwt: string; service: string }> {
132124
const body = JSON.parse(
133125
AuthService.fixedDecryption(integration.customInstanceDetails!)
134126
);
@@ -146,6 +138,18 @@ export class LemmyProvider extends SocialAbstract implements SocialProvider {
146138
})
147139
).json();
148140

141+
return { jwt, service: body.service };
142+
}
143+
144+
async post(
145+
id: string,
146+
accessToken: string,
147+
postDetails: PostDetails<LemmySettingsDto>[],
148+
integration: Integration
149+
): Promise<PostResponse[]> {
150+
const [firstPost] = postDetails;
151+
const { jwt, service } = await this.getJwtAndService(integration);
152+
149153
const valueArray: PostResponse[] = [];
150154

151155
for (const lemmy of firstPost.settings.subreddit) {
@@ -159,8 +163,8 @@ export class LemmyProvider extends SocialAbstract implements SocialProvider {
159163
: {}),
160164
nsfw: false,
161165
});
162-
const { post_view, ...all } = await (
163-
await fetch(body.service + '/api/v3/post', {
166+
const { post_view } = await (
167+
await fetch(service + '/api/v3/post', {
164168
body: JSON.stringify({
165169
community_id: +lemmy.value.id,
166170
name: lemmy.value.title,
@@ -188,41 +192,68 @@ export class LemmyProvider extends SocialAbstract implements SocialProvider {
188192

189193
valueArray.push({
190194
postId: post_view.post.id,
191-
releaseURL: body.service + '/post/' + post_view.post.id,
195+
releaseURL: service + '/post/' + post_view.post.id,
192196
id: firstPost.id,
193197
status: 'published',
194198
});
199+
}
195200

196-
for (const comment of restPosts) {
197-
const { comment_view } = await (
198-
await fetch(body.service + '/api/v3/comment', {
199-
body: JSON.stringify({
200-
post_id: post_view.post.id,
201-
content: comment.message,
202-
}),
203-
method: 'POST',
204-
headers: {
205-
Authorization: `Bearer ${jwt}`,
206-
'Content-Type': 'application/json',
207-
},
208-
})
209-
).json();
201+
return [
202+
{
203+
id: firstPost.id,
204+
postId: valueArray.map((p) => String(p.postId)).join(','),
205+
releaseURL: valueArray.map((p) => p.releaseURL).join(','),
206+
status: 'published',
207+
},
208+
];
209+
}
210210

211-
valueArray.push({
212-
postId: comment_view.post.id,
213-
releaseURL: body.service + '/comment/' + comment_view.comment.id,
214-
id: comment.id,
215-
status: 'published',
216-
});
217-
}
211+
async comment(
212+
id: string,
213+
postId: string,
214+
lastCommentId: string | undefined,
215+
accessToken: string,
216+
postDetails: PostDetails<LemmySettingsDto>[],
217+
integration: Integration
218+
): Promise<PostResponse[]> {
219+
const [commentPost] = postDetails;
220+
const { jwt, service } = await this.getJwtAndService(integration);
221+
222+
// postId can be comma-separated if posted to multiple communities
223+
const postIds = postId.split(',');
224+
const valueArray: PostResponse[] = [];
225+
226+
for (const singlePostId of postIds) {
227+
const { comment_view } = await (
228+
await fetch(service + '/api/v3/comment', {
229+
body: JSON.stringify({
230+
post_id: +singlePostId,
231+
content: commentPost.message,
232+
}),
233+
method: 'POST',
234+
headers: {
235+
Authorization: `Bearer ${jwt}`,
236+
'Content-Type': 'application/json',
237+
},
238+
})
239+
).json();
240+
241+
valueArray.push({
242+
postId: String(comment_view.comment.id),
243+
releaseURL: service + '/comment/' + comment_view.comment.id,
244+
id: commentPost.id,
245+
status: 'published',
246+
});
218247
}
219248

220-
return Object.values(groupBy(valueArray, (p) => p.id)).map((p) => ({
221-
id: p[0].id,
222-
postId: p.map((p) => String(p.postId)).join(','),
223-
releaseURL: p.map((p) => p.releaseURL).join(','),
224-
status: 'published',
225-
}));
249+
return [
250+
{
251+
id: commentPost.id,
252+
postId: valueArray.map((p) => p.postId).join(','),
253+
releaseURL: valueArray.map((p) => p.releaseURL).join(','),
254+
status: 'published',
255+
},
256+
];
226257
}
227258

228259
@Tool({
@@ -241,27 +272,11 @@ export class LemmyProvider extends SocialAbstract implements SocialProvider {
241272
id: string,
242273
integration: Integration
243274
) {
244-
const body = JSON.parse(
245-
AuthService.fixedDecryption(integration.customInstanceDetails!)
246-
);
247-
248-
const { jwt } = await (
249-
await fetch(body.service + '/api/v3/user/login', {
250-
body: JSON.stringify({
251-
username_or_email: body.identifier,
252-
password: body.password,
253-
}),
254-
method: 'POST',
255-
headers: {
256-
'Content-Type': 'application/json',
257-
},
258-
})
259-
).json();
275+
const { jwt, service } = await this.getJwtAndService(integration);
260276

261277
const { communities } = await (
262278
await fetch(
263-
body.service +
264-
`/api/v3/search?type_=Communities&sort=Active&q=${data.word}`,
279+
service + `/api/v3/search?type_=Communities&sort=Active&q=${data.word}`,
265280
{
266281
headers: {
267282
Authorization: `Bearer ${jwt}`,

libraries/nestjs-libraries/src/integrations/social/nostr.provider.ts

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getPublicKey, Relay, finalizeEvent, SimplePool } from 'nostr-tools';
1111

1212
import WebSocket from 'ws';
1313
import { AuthService } from '@gitroom/helpers/auth/auth.service';
14+
import { Integration } from '@prisma/client';
1415

1516
// @ts-ignore
1617
global.WebSocket = WebSocket;
@@ -158,43 +159,77 @@ export class NostrProvider extends SocialAbstract implements SocialProvider {
158159
}
159160
}
160161

162+
private buildContent(post: PostDetails): string {
163+
const mediaContent = post.media?.map((m) => m.path).join('\n\n') || '';
164+
return mediaContent
165+
? `${post.message}\n\n${mediaContent}`
166+
: post.message;
167+
}
168+
161169
async post(
162170
id: string,
163171
accessToken: string,
164172
postDetails: PostDetails[]
165173
): Promise<PostResponse[]> {
166174
const { password } = AuthService.verifyJWT(accessToken) as any;
175+
const [firstPost] = postDetails;
167176

168-
let lastId = '';
169-
const ids: PostResponse[] = [];
170-
for (const post of postDetails) {
171-
const textEvent = finalizeEvent(
172-
{
173-
kind: 1, // Text note
174-
content:
175-
post.message + '\n\n' + post.media?.map((m) => m.path).join('\n\n'),
176-
tags: [
177-
...(lastId
178-
? [
179-
['e', lastId, '', 'reply'],
180-
['p', id],
181-
]
182-
: []),
183-
], // Include delegation token in the event
184-
created_at: Math.floor(Date.now() / 1000),
185-
},
186-
password
187-
);
177+
const textEvent = finalizeEvent(
178+
{
179+
kind: 1, // Text note
180+
content: this.buildContent(firstPost),
181+
tags: [],
182+
created_at: Math.floor(Date.now() / 1000),
183+
},
184+
password
185+
);
186+
187+
const eventId = await this.publish(id, textEvent);
188188

189-
lastId = await this.publish(id, textEvent);
190-
ids.push({
191-
id: post.id,
192-
postId: String(lastId),
193-
releaseURL: `https://primal.net/e/${lastId}`,
189+
return [
190+
{
191+
id: firstPost.id,
192+
postId: String(eventId),
193+
releaseURL: `https://primal.net/e/${eventId}`,
194194
status: 'completed',
195-
});
196-
}
195+
},
196+
];
197+
}
198+
199+
async comment(
200+
id: string,
201+
postId: string,
202+
lastCommentId: string | undefined,
203+
accessToken: string,
204+
postDetails: PostDetails[],
205+
integration: Integration
206+
): Promise<PostResponse[]> {
207+
const { password } = AuthService.verifyJWT(accessToken) as any;
208+
const [commentPost] = postDetails;
209+
const replyToId = lastCommentId || postId;
210+
211+
const textEvent = finalizeEvent(
212+
{
213+
kind: 1, // Text note
214+
content: this.buildContent(commentPost),
215+
tags: [
216+
['e', replyToId, '', 'reply'],
217+
['p', id],
218+
],
219+
created_at: Math.floor(Date.now() / 1000),
220+
},
221+
password
222+
);
197223

198-
return ids;
224+
const eventId = await this.publish(id, textEvent);
225+
226+
return [
227+
{
228+
id: commentPost.id,
229+
postId: String(eventId),
230+
releaseURL: `https://primal.net/e/${eventId}`,
231+
status: 'completed',
232+
},
233+
];
199234
}
200235
}

0 commit comments

Comments
 (0)