Skip to content

Commit

Permalink
feat: ✨ add feature #2
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideSegullo committed Apr 12, 2022
1 parent da5124d commit 916e746
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
36 changes: 36 additions & 0 deletions code/src/tweets/authors.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { UserV2 } from 'twitter-api-v2';
import { Author, AuthorDocument } from 'src/schemas/author.schema';
import { mapAuthor } from 'src/utils';

@Injectable()
export class AuthorsService {
constructor(
@InjectModel(Author.name) private authorModel: Model<AuthorDocument>,
) {}

async createOrUpdate(
authorRaw: UserV2,
bitsongAddress: string,
): Promise<string> {
const author: Author = mapAuthor(authorRaw, bitsongAddress);

try {
const oldAuthorModel = await this.authorModel.findOneAndUpdate(
{ authorId: author.authorId },
{ address: author.address },
{ new: true },
);

return oldAuthorModel._id.toString();
} catch (error) {
console.error("Author doesn't exist: ", error);

const authorModel = await new this.authorModel(author).save();

return authorModel._id.toString();
}
}
}
3 changes: 2 additions & 1 deletion code/src/tweets/tweets.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TweetsService } from './tweets.service';
import { Tweet, TweetSchema } from 'src/schemas/tweet.schema';
import { TwitterService } from './twitter.service';
import { Author, AuthorSchema } from 'src/schemas/author.schema';
import { AuthorsService } from './authors.service';

@Module({
imports: [
Expand All @@ -12,6 +13,6 @@ import { Author, AuthorSchema } from 'src/schemas/author.schema';
{ name: Author.name, schema: AuthorSchema },
]),
],
providers: [TweetsService, TwitterService],
providers: [TweetsService, TwitterService, AuthorsService],
})
export class TweetsModule {}
20 changes: 13 additions & 7 deletions code/src/tweets/tweets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Tweet, TweetDocument } from 'src/schemas/tweet.schema';
import { ApiV2Includes, TweetV2 } from 'twitter-api-v2';
import { Author, AuthorDocument } from 'src/schemas/author.schema';
import { mapAuthor, mapTweet, searchValidAddress } from 'src/utils';
import { mapTweet, searchValidAddress } from 'src/utils';
import { AuthorsService } from './authors.service';

@Injectable()
export class TweetsService {
constructor(
@InjectModel(Tweet.name) private tweetModel: Model<TweetDocument>,
@InjectModel(Author.name) private authorModel: Model<AuthorDocument>,
private authorsService: AuthorsService,
) {}

async create(payload: TweetV2, includes: ApiV2Includes): Promise<Tweet> {
console.log('fetching... id: ', payload.id);

if (includes.users && payload.entities) {
try {
const bitsongAddress = searchValidAddress(payload.text);

if (!bitsongAddress) {
console.log('invalid address');
return;
}

Expand All @@ -35,17 +38,20 @@ export class TweetsService {
);

if (authorRaw && validHashtag && validMention) {
const author: Author = mapAuthor(authorRaw, bitsongAddress);

const authorModel = await new this.authorModel(author).save();
const authorId = await this.authorsService.createOrUpdate(
authorRaw,
bitsongAddress,
);

const tweet: Tweet = mapTweet(payload, authorModel._id.toString());
const tweet: Tweet = mapTweet(payload, authorId);

const createdTweet = new this.tweetModel(tweet);

const tweetDocument = await createdTweet.save();

return tweetDocument;
} else {
console.log('invalid tweet');
}
} catch (error) {
console.error(error);
Expand Down
12 changes: 5 additions & 7 deletions code/src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ export const searchValidAddress = (text: string, prefix = 'bitsong') => {
const regex = new RegExp(`${prefix}1[a-zA-Z0-9]{38}`, 'gm');
let address: string;

let m: RegExpExecArray;
const matches = text.match(regex);

if ((m = regex.exec(text)) !== null) {
if (m.length === 1) {
m.forEach((match) => {
address = match;
});
}
if (matches.length === 1) {
matches.forEach((match) => {
address = match;
});
}

return address;
Expand Down
2 changes: 0 additions & 2 deletions code/src/utils/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { UserV2, TweetV2 } from 'twitter-api-v2';
export const mapAuthor = (user: UserV2, address: string): Author => {
const months = differenceByMonths(user.created_at);

console.log(user.created_at, months);

return {
authorId: user.id,
twitterCreatedAt: user.created_at,
Expand Down

0 comments on commit 916e746

Please sign in to comment.