feat: Introduce my tweet trigger with execute step mutation
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
46321f19f4
commit
b42cb759a5
@@ -1,9 +1,12 @@
|
|||||||
import Authentication from './authentication';
|
import Authentication from './authentication';
|
||||||
|
import Triggers from './triggers';
|
||||||
|
|
||||||
export default class Twitter {
|
export default class Twitter {
|
||||||
authenticationClient: any
|
authenticationClient: any
|
||||||
|
triggers: any
|
||||||
|
|
||||||
constructor(connectionData: any) {
|
constructor(connectionData: any) {
|
||||||
this.authenticationClient = new Authentication(connectionData);
|
this.authenticationClient = new Authentication(connectionData);
|
||||||
|
this.triggers = new Triggers(connectionData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
packages/backend/src/apps/twitter/triggers.ts
Normal file
9
packages/backend/src/apps/twitter/triggers.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import MyTweet from './triggers/my-tweet';
|
||||||
|
|
||||||
|
export default class Triggers {
|
||||||
|
myTweet: any
|
||||||
|
|
||||||
|
constructor(connectionData: any) {
|
||||||
|
this.myTweet = new MyTweet(connectionData)
|
||||||
|
}
|
||||||
|
}
|
24
packages/backend/src/apps/twitter/triggers/my-tweet.ts
Normal file
24
packages/backend/src/apps/twitter/triggers/my-tweet.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import TwitterApi from 'twitter-api-v2';
|
||||||
|
|
||||||
|
export default class MyTweet {
|
||||||
|
client: any
|
||||||
|
|
||||||
|
constructor(connectionData: any) {
|
||||||
|
this.client = new TwitterApi({
|
||||||
|
appKey: connectionData.consumerKey,
|
||||||
|
appSecret: connectionData.consumerSecret,
|
||||||
|
accessToken: connectionData.accessToken,
|
||||||
|
accessSecret: connectionData.accessSecret
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async run() {
|
||||||
|
const response = await this.client.currentUser();
|
||||||
|
const username = response.screen_name;
|
||||||
|
|
||||||
|
const userTimeline = await this.client.v1.userTimelineByUsername(username);
|
||||||
|
const fetchedTweets = userTimeline.tweets;
|
||||||
|
|
||||||
|
return fetchedTweets[0];
|
||||||
|
}
|
||||||
|
}
|
36
packages/backend/src/graphql/mutations/execute-step.ts
Normal file
36
packages/backend/src/graphql/mutations/execute-step.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||||
|
import App from '../../models/app';
|
||||||
|
import Connection from '../../models/connection';
|
||||||
|
import Step from '../../models/step';
|
||||||
|
import stepType from '../types/step';
|
||||||
|
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
id: string,
|
||||||
|
data: object
|
||||||
|
}
|
||||||
|
const executeStepResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||||
|
let step = await Step.query().findOne({
|
||||||
|
id: params.id
|
||||||
|
})
|
||||||
|
|
||||||
|
let connection = await Connection.query().findOne({
|
||||||
|
id: step.connectionId
|
||||||
|
})
|
||||||
|
|
||||||
|
const appClass = (await import(`../../apps/${step.appKey}`)).default;
|
||||||
|
const appInstance = new appClass(connection.data);
|
||||||
|
await appInstance.triggers[step.key].run();
|
||||||
|
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
const executeStep = {
|
||||||
|
type: stepType,
|
||||||
|
args: {
|
||||||
|
id: { type: GraphQLNonNull(GraphQLString) }
|
||||||
|
},
|
||||||
|
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => executeStepResolver(params, req)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default executeStep;
|
@@ -6,6 +6,7 @@ import resetConnection from './mutations/reset-connection';
|
|||||||
import verifyConnection from './mutations/verify-connection';
|
import verifyConnection from './mutations/verify-connection';
|
||||||
import deleteConnection from './mutations/delete-connection';
|
import deleteConnection from './mutations/delete-connection';
|
||||||
import createStep from './mutations/create-step';
|
import createStep from './mutations/create-step';
|
||||||
|
import executeStep from './mutations/execute-step';
|
||||||
|
|
||||||
const rootMutation = new GraphQLObjectType({
|
const rootMutation = new GraphQLObjectType({
|
||||||
name: 'Mutation',
|
name: 'Mutation',
|
||||||
@@ -17,6 +18,7 @@ const rootMutation = new GraphQLObjectType({
|
|||||||
verifyConnection,
|
verifyConnection,
|
||||||
deleteConnection,
|
deleteConnection,
|
||||||
createStep,
|
createStep,
|
||||||
|
executeStep
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user