Skip to content

Commit

Permalink
feat(dao): add tweet update/edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Feb 13, 2019
1 parent 01b8865 commit 0325e42
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ const CLI = () => {
.string('p')
;
})
.command('update <id> <message>', 'edit/update an existing tweet', (y) => {
return y
.alias('p', 'photo')
.describe('p', 'the photo ID to associate with the tweet')
.string('p')
;
})
;
})
.argv;
Expand Down Expand Up @@ -301,6 +308,13 @@ const CLI = () => {
console.log('');
};

const doUpdateStreamPost = async (id: string, message: string, photo?: string) => {
const client = getClient();
const response = await client.stream().update(id, message, photo);
console.log(colors.green('Updated tweet ' + response.post.id));
console.log('');
};

const processArgs = async (args) => {
try {
switch (args._[0]) {
Expand Down Expand Up @@ -362,6 +376,10 @@ const CLI = () => {
await doStreamPost(args.message, args.replyTo, args.photo);
break;
}
case 'update': {
await doUpdateStreamPost(args.id, args.message, args.photo);
break;
}
}
break;
}
Expand All @@ -372,7 +390,7 @@ const CLI = () => {
}
process.exit(0);
} catch (err) {
console.log(colors.red('Failed: ' + err.message));
console.log(colors.red('Failed: ' + (err.toString? err.toString() : err.message)));
process.exit(1);
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/dao/StreamDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ export class StreamDAO extends AbstractDAO {
return StreamResponse.fromRest(result.data);
});
}

public async update(id: string, message: string, photo?: string) {
const options = new TwitarrHTTPOptions()
.withData({
text: message,
});
if (!Util.isEmpty(photo)) {
options.data.photo = photo;
}
return this.http.post('/api/v2/tweet/' + id, options).then((result) => {
return StreamResponse.fromRest(result.data);
});
}
}
10 changes: 10 additions & 0 deletions test/dao/StreamDAO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ describe('dao/StreamDAO', () => {
done();
});
});
describe('#update', () => {
it('update(id, message)', async (done) => {
const ret = await dao.update('5c63275ad86b930ad6739cb8', 'this is no longer a twitter post!');
expect(ret).toBeDefined();
expect(ret).toBeInstanceOf(StreamResponse);
expect(ret.posts[0]).toBeDefined();
expect(ret.posts.length).toEqual(1);
done();
});
});
});
4 changes: 4 additions & 0 deletions test/rest/MockHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export class MockHTTP extends AbstractHTTP {
if (options.data.text === 'this is a twitter post!' && Util.isEmpty(options.data.parent, options.data.photo)) {
return jsonOK(require('../data/stream-mentions-rangerrick-include-author.json'));
}
break;
}
case '/api/v2/tweet/5c63275ad86b930ad6739cb8': {
return jsonOK(require('../data/stream-mentions-rangerrick-include-author.json'));
}
}

Expand Down

0 comments on commit 0325e42

Please sign in to comment.