refactor: Restructure twitter app with beforeRequest hook

This commit is contained in:
Faruk AYDIN
2022-10-18 11:13:53 +02:00
committed by Ali BARIN
parent bc402883b3
commit a5e114ac68
14 changed files with 119 additions and 113 deletions

View File

@@ -6,6 +6,7 @@ export default defineTrigger({
key: 'myFollowers',
pollInterval: 15,
description: 'Will be triggered when you have a new follower.',
dedupeStrategy: 'unique',
substeps: [
{
key: 'chooseConnection',
@@ -18,6 +19,6 @@ export default defineTrigger({
],
async run($) {
return await myFollowers($, $.flow.lastInternalId);
return await myFollowers($);
},
});

View File

@@ -3,13 +3,12 @@ import getCurrentUser from '../../common/get-current-user';
import getUserByUsername from '../../common/get-user-by-username';
import getUserFollowers from '../../common/get-user-followers';
const myFollowers = async ($: IGlobalVariable, lastInternalId?: string) => {
const myFollowers = async ($: IGlobalVariable) => {
const { username } = await getCurrentUser($);
const user = await getUserByUsername($, username as string);
const tweets = await getUserFollowers($, {
userId: user.id,
lastInternalId,
});
return tweets;
};

View File

@@ -4,10 +4,9 @@ import {
ITriggerOutput,
} from '@automatisch/types';
import qs from 'qs';
import generateRequest from '../../common/generate-request';
import { omitBy, isEmpty } from 'lodash';
const searchTweets = async ($: IGlobalVariable) => {
const fetchTweets = async ($: IGlobalVariable) => {
const searchTerm = $.step.parameters.searchTerm as string;
let response;
@@ -29,10 +28,7 @@ const searchTweets = async ($: IGlobalVariable) => {
queryParams.toString() ? `?${queryParams.toString()}` : ''
}`;
response = await generateRequest($, {
requestPath,
method: 'GET',
});
response = await $.http.get(requestPath);
if (response.integrationError) {
tweets.error = response.integrationError;
@@ -58,8 +54,14 @@ const searchTweets = async ($: IGlobalVariable) => {
}
} while (response.data.meta.next_token && !$.execution.testRun);
return tweets;
};
const searchTweets = async ($: IGlobalVariable) => {
const tweets = await fetchTweets($);
tweets.data.sort((tweet, nextTweet) => {
return (tweet.raw.id as number) - (nextTweet.raw.id as number);
return Number(nextTweet.meta.internalId) - Number(tweet.meta.internalId);
});
return tweets;