@@ -10,7 +10,6 @@ import dayjs from 'dayjs';
1010import { Integration } from '@prisma/client' ;
1111import { AuthService } from '@gitroom/helpers/auth/auth.service' ;
1212import { LemmySettingsDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/lemmy.dto' ;
13- import { groupBy } from 'lodash' ;
1413import { Tool } from '@gitroom/nestjs-libraries/integrations/tool.decorator' ;
1514
1615export 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 } ` ,
0 commit comments