Commit fbb24753 authored by shahriar's avatar shahriar
Browse files

fix bug

parent 24ac4281
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type LottoryResultDocument = LottoryResult & Document
export type LottoryResultDocument = LottoryResult & Document;
@Schema({timestamps:true})
@Schema({ timestamps: true })
export class LottoryResult {
@Prop()
username: string
username: string;
@Prop()
index: number
index: number;
}
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult)
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type AccountFollowersDocument = AccountFollowers & Document
export type AccountFollowersDocument = AccountFollowers & Document;
@Schema({timestamps:true})
@Schema({ timestamps: true })
export class AccountFollowers {
@Prop()
_id: Types.ObjectId
_id: Types.ObjectId;
@Prop()
username: string
username: string;
@Prop()
user_id: string
user_id: string;
@Prop()
full_name: string
full_name: string;
@Prop()
bussines_username: string
bussines_username: string;
@Prop({type:Object})
follower_obejct: Object
@Prop({ type: Object })
follower_obejct: Object;
@Prop()
follow_date: number
follow_date: number;
}
export const AccountFollowersSchema = SchemaFactory.createForClass(AccountFollowers)
export const AccountFollowersSchema =
SchemaFactory.createForClass(AccountFollowers);
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { GetUserScore } from './dto/get-user-score';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(private readonly appService: AppService) { }
@Get('get-comments')
async getCommentsFromIG() {
return await this.appService.getComments();
}
@Get('get-followers')
async getFollowers() {
return await this.appService.getFollowers();
}
@Post()
async getUserScore(@Body() getUserScoreDto: GetUserScore) {
return await this.appService.calculateUserScore(getUserScoreDto.username);
......@@ -23,7 +25,7 @@ export class AppController {
@Get('calculate-result')
async calculateResult() {
return await this.appService.getResults();
return await this.appService.getResults()
}
@Get('get-results')
......@@ -31,18 +33,19 @@ export class AppController {
return await this.appService.getFinalResults();
}
@Get('search2/:id')
async getUserResults(@Param('id') id: string) {
return await this.appService.getUserResult(id);
@Post('search')
async getUserResults(@Body('username') username: string) {
return await this.appService.getUserResult(username);
}
@Get('shuffle')
async shuffle() {
return await this.appService.getShuffleData();
return await this.appService.getShuffleData()
}
@Get('add-lottory-result')
async addResultDb() {
return await this.appService.addResultsToDB();
return await this.appService.addResultsToDB()
}
}
import { Module, OnApplicationBootstrap, } from '@nestjs/common';
import { Module, OnApplicationBootstrap } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AccountFollowersSchema } from './account.followers';
import { AppController } from './app.controller';
......@@ -10,15 +10,20 @@ import { ResultSchema } from './result.schema';
@Module({
imports: [
MongooseModule.forRoot('mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256'),
MongooseModule.forRoot(
'mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256',
),
MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]),
MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]),
MongooseModule.forFeature([{ name: 'Result', schema: ResultSchema }]),
MongooseModule.forFeature([{ name: 'LottoryResult', schema: LottoryResultSchema }]),
MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]),
MongooseModule.forFeature([
{ name: 'LottoryResult', schema: LottoryResultSchema },
]),
MongooseModule.forFeature([
{ name: 'AccountFollower', schema: AccountFollowersSchema },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { HttpException, Injectable, OnApplicationBootstrap } from '@nestjs/common';
import {
HttpException,
Injectable,
OnApplicationBootstrap,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { CommentDocument } from './comment.schema';
import { IFollower } from './interface/Ifollower';
import { IncomingComment } from './interface/IncomingComment';
import { RequestDocument } from './request.schema';
const Instagram = require('instagram-web-api')
const { username, password } = process.env
import * as _ from "lodash"
const Instagram = require('instagram-web-api');
const { username, password } = process.env;
import * as _ from 'lodash';
import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { AccountFollowersDocument } from './account.followers';
import { AccountFollowersDocument } from './account.followers';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
import { ResultDocument } from './result.schema';
import { LottoryResultDocument } from './LottoryResult.schema';
@Injectable()
export class AppService implements OnApplicationBootstrap {
client: any
client: any;
constructor(
@InjectModel('Request')
private requestModel: Model<RequestDocument>,
......@@ -28,66 +32,72 @@ export class AppService implements OnApplicationBootstrap {
@InjectModel('Result')
private resultModel: Model<ResultDocument>,
@InjectModel('LottoryResult')
private lotoryResultModel: Model<LottoryResultDocument>
) {
}
private lotoryResultModel: Model<LottoryResultDocument>,
) {}
async onApplicationBootstrap() {
this.client = await this.login("sohe.ibs", "kaka1374")
this.client = await this.login('sohe.ibs', 'kaka1374');
}
private async login(username, password) {
try {
const client = new Instagram({ username, password })
await client.login()
const client = new Instagram({ username, password });
await client.login();
console.log(`user logged in. details :\n
username: ${username},
password: ${password},
`)
`);
return client
}
catch (err) {
return client;
} catch (err) {
console.log(err);
}
}
async getFollowers(account_username: string = 'azadi.gold') {
async getFollowers(account_username = 'azadi.gold') {
try {
let hasNextPage: boolean = true
let requestCount = 0
let followersCount = 0
let cursor: string = ''
let reqList = await this.requestModel.find({ $and: [{ type: "follower" }, { account_username }] }).sort({ createdAt: -1 })
let hasNextPage = true;
let requestCount = 0;
let followersCount = 0;
let cursor = '';
const reqList = await this.requestModel
.find({ $and: [{ type: 'follower' }, { account_username }] })
.sort({ createdAt: -1 });
console.log("Request History:", reqList.length)
console.log('Request History:', reqList.length);
if (reqList.length != 0) {
// let nextCursor = await this.getFollowersNextCursor(client, reqList[0].cursor)
cursor = reqList[0].cursor
cursor = reqList[0].cursor;
}
while (hasNextPage) {
console.log("seted cursor", cursor)
console.log("sending request....")
let collectedFollower = await this.sendFollowerRequest(this.client, account_username, '')
console.log("request sended. request count:", requestCount);
requestCount++
cursor = collectedFollower.cursor
await this.requestModel.create({ _id: new Types.ObjectId(), cursor: cursor, type: "follower", account_username })
hasNextPage = collectedFollower.hasNextPage
console.log('seted cursor', cursor);
console.log('sending request....');
const collectedFollower = await this.sendFollowerRequest(
this.client,
account_username,
'',
);
console.log('request sended. request count:', requestCount);
requestCount++;
cursor = collectedFollower.cursor;
await this.requestModel.create({
_id: new Types.ObjectId(),
cursor: cursor,
type: 'follower',
account_username,
});
hasNextPage = collectedFollower.hasNextPage;
for await (const follower of collectedFollower.followers) {
let check = await this.followerModel.findOne({
const check = await this.followerModel.findOne({
$and: [
{ bussines_username: account_username },
{ username: follower.username, },
{ user_id: follower.user_id, },
]
})
{ username: follower.username },
{ user_id: follower.user_id },
],
});
if (!check) {
await this.followerModel.create({
_id: new Types.ObjectId(),
......@@ -96,39 +106,38 @@ export class AppService implements OnApplicationBootstrap {
full_name: follower.full_name,
bussines_username: account_username,
follower_object: follower.follower_obejct,
follow_date: this.getFollowerDateOfFollow(follower.username)
})
followersCount += 1
}
else {
return "already updated"
follow_date: this.getFollowerDateOfFollow(follower.username),
});
followersCount += 1;
} else {
return 'already updated';
}
}
console.log("================next request info==================");
console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage)
console.log(followersCount, "followers imported from pervios requests")
console.log("================ end of this Request Proccess ==================");
console.log('================next request info==================');
console.log('nextCursor:', cursor);
console.log('has a next page', hasNextPage);
console.log(followersCount, 'followers imported from pervios requests');
console.log(
'================ end of this Request Proccess ==================',
);
}
return {
status: "successfull",
totalAdded: followersCount
}
}
catch (err) {
console.log(err)
throw new HttpException(err.message, 500)
status: 'successfull',
totalAdded: followersCount,
};
} catch (err) {
console.log(err);
throw new HttpException(err.message, 500);
}
}
async getComments(postShortCode: string = 'CRWNkkchs2x') {
async getComments(postShortCode = 'CRWNkkchs2x') {
try {
let hasNextPage: boolean = true
let requestCount = 0
let commentCount = 0
let cursor: string = ''
let hasNextPage = true;
let requestCount = 0;
let commentCount = 0;
let cursor = '';
// let reqList = await this.requestModel.find({ $and: [{ type: "comment" }, { post_short_code: postShortCode }] }).sort({ createdAt: -1 })
// console.log("Request History:", reqList.length)
......@@ -139,22 +148,30 @@ export class AppService implements OnApplicationBootstrap {
// cursor = reqList[0].cursor
// }
while (hasNextPage) {
// console.log("seted cursor", cursor)
console.log("sending request....")
let collectedComments = await this.sendCommentRequest(this.client, postShortCode, '')
console.log("request sended. request count:", requestCount);
requestCount++
cursor = collectedComments.cursor
await this.requestModel.create({ _id: new Types.ObjectId(), cursor: cursor, type: "comment", post_short_code: postShortCode })
hasNextPage = collectedComments.hasNextPage
console.log('sending request....');
const collectedComments = await this.sendCommentRequest(
this.client,
postShortCode,
'',
);
console.log('request sended. request count:', requestCount);
requestCount++;
cursor = collectedComments.cursor;
await this.requestModel.create({
_id: new Types.ObjectId(),
cursor: cursor,
type: 'comment',
post_short_code: postShortCode,
});
hasNextPage = collectedComments.hasNextPage;
for await (const comment of collectedComments.comments) {
let check = await this.commentModel.findOne({
comment_id: comment.comment_id
})
const check = await this.commentModel.findOne({
comment_id: comment.comment_id,
});
if (!check) {
await this.commentModel.create({
_id: new Types.ObjectId(),
......@@ -163,64 +180,87 @@ export class AppService implements OnApplicationBootstrap {
owner_id: comment.owner_id,
text: comment.text,
comment_object: comment.commnet_object,
date: comment.date
})
commentCount += 1
}
else {
return "already updated"
date: comment.date,
});
commentCount += 1;
} else {
return 'already updated';
}
}
console.log("================nex request info==================");
console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage)
console.log(commentCount, "comment imported from pervios requests")
console.log("================ End of this Request Proccess ==================");
console.log('================nex request info==================');
console.log('nextCursor:', cursor);
console.log('has a next page', hasNextPage);
console.log(commentCount, 'comment imported from pervios requests');
console.log(
'================ End of this Request Proccess ==================',
);
}
return {
status: "successfull",
totalAdded: commentCount
}
}
catch (err) {
console.log(err)
throw new HttpException(err.message, 500)
status: 'successfull',
totalAdded: commentCount,
};
} catch (err) {
console.log(err);
throw new HttpException(err.message, 500);
}
}
async getCommentsNextCursor(client, cursor: string, postShortCode: string) {
console.log(`----------------------parameters------------------------------------------\n`, cursor, postShortCode);
let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: cursor })
console.log(`----------------------incomingComments------------------------------------------\n`, incomingComments);
return incomingComments.page_info.end_cursor
console.log(
`----------------------parameters------------------------------------------\n`,
cursor,
postShortCode,
);
const incomingComments = await client.getMediaComments({
shortcode: postShortCode,
first: '49',
after: cursor,
});
console.log(
`----------------------incomingComments------------------------------------------\n`,
incomingComments,
);
return incomingComments.page_info.end_cursor;
}
async getFollowersNextCursor(client, cursor: string) {
const azadiGoldUser = await client.getUserByUsername({ username: 'azadi.gold' })
const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor })
return followers.page_info.end_cursor
const azadiGoldUser = await client.getUserByUsername({
username: 'azadi.gold',
});
const followers = await client.getFollowers({
userId: azadiGoldUser.id,
after: cursor,
});
return followers.page_info.end_cursor;
}
async sendCommentRequest(client, postShortCode, cursor) {
try {
let comments: IncomingComment[] = new Array<IncomingComment>()
const comments: IncomingComment[] = new Array<IncomingComment>();
let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: '' })
const incomingComments = await client.getMediaComments({
shortcode: postShortCode,
first: '49',
after: '',
});
console.log("=============incoming comments=============", incomingComments);
console.log(
'=============incoming comments=============',
incomingComments,
);
for (const comment of incomingComments.edges) {
console.log("============data Object===============:", comment);
console.log('============data Object===============:', comment);
comments.push({
comment_id: comment.node.id,
text: comment.node.text,
date: comment.node.created_at,
owner_id: comment.node.owner.id,
owner_username: comment.node.owner.username,
commnet_object: comment.node
})
commnet_object: comment.node,
});
}
let pointer = incomingComments.page_info.end_cursor;
......@@ -231,37 +271,37 @@ export class AppService implements OnApplicationBootstrap {
} catch (e) {
console.log(`Pointer is not array!, dont need to be converted!`);
}
await this.delay(_.random(2000, 10000))
await this.delay(_.random(2000, 10000));
return {
comments,
cursor: '',
hasNextPage: incomingComments.page_info.has_next_page
}
}
catch (err) {
console.log(err)
throw new HttpException(err.message, 500)
hasNextPage: incomingComments.page_info.has_next_page,
};
} catch (err) {
console.log(err);
throw new HttpException(err.message, 500);
}
}
async sendFollowerRequest(client, username, cursor) {
try {
let Infollowers: IFollower[] = new Array<IFollower>()
await this.delay(_.random(2000, 10000))
const azadiGoldUser = await client.getUserByUsername({ username })
const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor })
const Infollowers: IFollower[] = new Array<IFollower>();
await this.delay(_.random(2000, 10000));
const azadiGoldUser = await client.getUserByUsername({ username });
const followers = await client.getFollowers({
userId: azadiGoldUser.id,
after: cursor,
});
console.log("=============incoming followers=============", followers);
console.log('=============incoming followers=============', followers);
for (const user of followers.data) {
Infollowers.push({
user_id: user.id,
username: user.username,
full_name: user.full_name,
follower_obejct: user
})
follower_obejct: user,
});
}
let pointer = followers.page_info.end_cursor;
// this will try to convert array to json stringify
......@@ -274,146 +314,153 @@ export class AppService implements OnApplicationBootstrap {
return {
followers: Infollowers,
cursor: '',
hasNextPage: followers.page_info.has_next_page
}
}
catch (err) {
console.log(err)
throw new HttpException(err.message, 500)
hasNextPage: followers.page_info.has_next_page,
};
} catch (err) {
console.log(err);
throw new HttpException(err.message, 500);
}
}
async delay(ms) {
// return await for better async stack trace support in case of errors.
console.log('delay time:', ms);
return await new Promise(resolve => setTimeout(resolve, ms));
return await new Promise((resolve) => setTimeout(resolve, ms));
}
async calculateUserScore(owner_username: string) {
let foundUserComments = await this.commentModel.find({ owner_username }).sort({ createdAt: -1 })
let isUserFollowPage = await this.checkUserFollowingStatus(owner_username)
let ownerUserFollowStatus: boolean
const foundUserComments = await this.commentModel
.find({ owner_username })
.sort({ createdAt: -1 });
const isUserFollowPage = await this.checkUserFollowingStatus(
owner_username,
);
let ownerUserFollowStatus: boolean;
if (isUserFollowPage) {
ownerUserFollowStatus = true
}
else {
ownerUserFollowStatus = false
ownerUserFollowStatus = true;
} else {
ownerUserFollowStatus = false;
}
let UseCleanComment = new CleanedComments()
UseCleanComment.mentions = new Array<MentionDocument>()
UseCleanComment.owner_username = owner_username
const UseCleanComment = new CleanedComments();
UseCleanComment.mentions = new Array<MentionDocument>();
UseCleanComment.owner_username = owner_username;
foundUserComments.forEach(comment => {
let rawComment = comment.text
let commentTextArray = rawComment.split(' ')
commentTextArray.forEach(commentSubStr => {
let check = false
foundUserComments.forEach((comment) => {
const rawComment = comment.text;
const commentTextArray = rawComment.split(' ');
commentTextArray.forEach((commentSubStr) => {
let check = false;
if (commentSubStr.includes('@')) {
if (UseCleanComment.mentions.length != 0) {
UseCleanComment.mentions.forEach(mention => {
if (commentSubStr == mention.mentioned_username || commentSubStr == owner_username) {
check = true
UseCleanComment.mentions.forEach((mention) => {
if (
commentSubStr == mention.mentioned_username ||
commentSubStr == owner_username
) {
check = true;
}
})
}
else {
});
} else {
if (commentSubStr == owner_username) {
check = true
check = true;
}
}
if (check == false) {
UseCleanComment.mentions.push({ mentioned_username: commentSubStr, date: comment.date })
UseCleanComment.mentions.push({
mentioned_username: commentSubStr,
date: comment.date,
});
}
}
})
})
});
});
let allUserMentions = new Array<UserAllMention>()
const allUserMentions = new Array<UserAllMention>();
for await (const mentionedUser of UseCleanComment.mentions) {
const newMentionData = new UserAllMention();
newMentionData.comment_status = new Array<CommentStatus>();
let newMentionData = new UserAllMention()
newMentionData.comment_status = new Array<CommentStatus>()
let foundAccount = await this.checkUserFollowingStatus(mentionedUser.mentioned_username.split('@')[1])
const foundAccount = await this.checkUserFollowingStatus(
mentionedUser.mentioned_username.split('@')[1],
);
if (!foundAccount) {
newMentionData.mentioned_username = mentionedUser.mentioned_username,
newMentionData.page_follow_date = null,
newMentionData.comment_date = mentionedUser.date,
newMentionData.comment_status.push(CommentStatus.notFollower)
allUserMentions.push(newMentionData)
}
else {
newMentionData.mentioned_username = foundAccount.username
newMentionData.mentioned_user_id = foundAccount.user_id
newMentionData.page_follow_date = foundAccount.follow_date
newMentionData.comment_date = mentionedUser.date
(newMentionData.mentioned_username = mentionedUser.mentioned_username),
(newMentionData.page_follow_date = null),
(newMentionData.comment_date = mentionedUser.date),
newMentionData.comment_status.push(CommentStatus.notFollower);
let checkUserFollowedPageBefore = await this.checkUserFollowedPageBefore(
newMentionData.comment_date, newMentionData.page_follow_date)
allUserMentions.push(newMentionData);
} else {
newMentionData.mentioned_username = foundAccount.username;
newMentionData.mentioned_user_id = foundAccount.user_id;
newMentionData.page_follow_date = foundAccount.follow_date;
newMentionData.comment_date = mentionedUser.date;
const checkUserFollowedPageBefore =
await this.checkUserFollowedPageBefore(
newMentionData.comment_date,
newMentionData.page_follow_date,
);
if (checkUserFollowedPageBefore == true) {
newMentionData.comment_status.push(CommentStatus.isAFollowerBefore)
newMentionData.comment_status.push(CommentStatus.isAFollowerBefore);
}
let checkUserMentionedBefore = await this.checkUserMentionedBefore(
newMentionData.mentioned_username, newMentionData.comment_date)
const checkUserMentionedBefore = await this.checkUserMentionedBefore(
newMentionData.mentioned_username,
newMentionData.comment_date,
);
if (checkUserMentionedBefore == true) {
newMentionData.comment_status.push(CommentStatus.isMentionedBefore)
newMentionData.comment_status.push(CommentStatus.isMentionedBefore);
}
if (checkUserFollowedPageBefore == false && checkUserMentionedBefore == false) {
newMentionData.comment_status.push(CommentStatus.isValid)
if (
checkUserFollowedPageBefore == false &&
checkUserMentionedBefore == false
) {
newMentionData.comment_status.push(CommentStatus.isValid);
}
allUserMentions.push(newMentionData)
allUserMentions.push(newMentionData);
}
}
return {
mentions: allUserMentions
}
mentions: allUserMentions,
};
}
async getResults() {
let foundUsernames = await this.commentModel.distinct('owner_username')
console.log("total users for ranking:", foundUsernames.length);
const foundUsernames = await this.commentModel.distinct('owner_username');
console.log('total users for ranking:', foundUsernames.length);
for await (const username of foundUsernames) {
let mentions = await this.calculateUserScore(username)
let valid_mentions = 0
let invalid_mentions = 0
let pending_mentions = 0
let valid_users = new Array<string>()
let inValid_users = new Array<string>()
let pending_users = new Array<string>()
mentions.mentions.forEach(mention => {
const mentions = await this.calculateUserScore(username);
let valid_mentions = 0;
let invalid_mentions = 0;
let pending_mentions = 0;
const valid_users = new Array<string>();
const inValid_users = new Array<string>();
const pending_users = new Array<string>();
mentions.mentions.forEach((mention) => {
if (mention.comment_status.includes(CommentStatus.isValid)) {
valid_mentions++
valid_users.push(mention.mentioned_username)
}
else if (mention.comment_status.includes(CommentStatus.isMentionedBefore)
|| mention.comment_status.includes(CommentStatus.isAFollowerBefore)) {
invalid_mentions++
inValid_users.push(mention.mentioned_username)
valid_mentions++;
valid_users.push(mention.mentioned_username);
} else if (
mention.comment_status.includes(CommentStatus.isMentionedBefore) ||
mention.comment_status.includes(CommentStatus.isAFollowerBefore)
) {
invalid_mentions++;
inValid_users.push(mention.mentioned_username);
} else if (mention.comment_status.includes(CommentStatus.notFollower)) {
pending_mentions++;
pending_users.push(mention.mentioned_username);
}
else if (mention.comment_status.includes(CommentStatus.notFollower)) {
pending_mentions++
pending_users.push(mention.mentioned_username)
}
})
});
await this.delay(_.random(500, 1000))
let foundUser = await this.resultModel.findOne({ username: username })
await this.delay(_.random(500, 1000));
const foundUser = await this.resultModel.findOne({ username: username });
if (!foundUser) {
await this.resultModel.create({
username: username,
......@@ -421,212 +468,211 @@ export class AppService implements OnApplicationBootstrap {
invalid_mentions,
pending_mentions,
score: valid_mentions + 1,
valid_users: valid_users
, inValid_users: inValid_users,
pending_users: pending_users
})
valid_users: valid_users,
inValid_users: inValid_users,
pending_users: pending_users,
});
} else {
await this.resultModel.updateOne({ _id: foundUser._id }, {
username: username,
valid_mentions,
invalid_mentions,
pending_mentions,
score: valid_mentions + 1,
valid_users: valid_users
, inValid_users: inValid_users,
pending_users: pending_users
})
await this.resultModel.updateOne(
{ _id: foundUser._id },
{
username: username,
valid_mentions,
invalid_mentions,
pending_mentions,
score: valid_mentions + 1,
valid_users: valid_users,
inValid_users: inValid_users,
pending_users: pending_users,
},
);
}
}
return "records updated successfully"
return 'records updated successfully';
}
async checkUserMentionedBefore(username, comment_date) {
let foundCommentsWithThisMention = await this.commentModel.find({
text: new RegExp(`@${username}`)
})
let isValid = false
const foundCommentsWithThisMention = await this.commentModel.find({
text: new RegExp(`@${username}`),
});
let isValid = false;
if (foundCommentsWithThisMention.length != 0) {
foundCommentsWithThisMention.forEach(comment => {
foundCommentsWithThisMention.forEach((comment) => {
if (comment_date > comment.date) {
isValid = true
isValid = true;
}
})
});
}
return isValid
return isValid;
}
async checkUserFollowingStatus(username: string) {
let res = await this.followerModel.findOne({ username: username })
return res
const res = await this.followerModel.findOne({ username: username });
return res;
}
async checkUserFollowedPageBefore(comment_date: number, followed_date: number) {
async checkUserFollowedPageBefore(
comment_date: number,
followed_date: number,
) {
if (comment_date < followed_date) {
return false
}
else {
return true
return false;
} else {
return true;
}
}
getFollowerDateOfFollow(username: string) {
let follower_objectResult: number
let follower_objectResult: number;
FollowerPrivateData.relationships_followers.forEach((follower_object) => {
if (follower_object.string_list_data[0]['value'].toString() === username.toString())
follower_objectResult = follower_object.string_list_data[0]['timestamp']
if (
follower_object.string_list_data[0]['value'].toString() ===
username.toString()
)
follower_objectResult =
follower_object.string_list_data[0]['timestamp'];
else {
follower_objectResult = Date.now()
follower_objectResult = Date.now();
}
})
});
return follower_objectResult
return follower_objectResult;
}
async getFinalResults() {
let results = await this.resultModel.find().sort({ score: -1 })
let last_update = await this.resultModel.find().sort({ updatedAt: -1 })
let last_create = await this.resultModel.find().sort({ createdAt: -1 })
let date: number
console.log(last_update[0]['updatedAt'])
console.log(last_create[0]['createdAt'])
const results = await this.resultModel.find().sort({ score: -1 });
const last_update = await this.resultModel.find().sort({ updatedAt: -1 });
const last_create = await this.resultModel.find().sort({ createdAt: -1 });
let date: number;
console.log(last_update[0]['updatedAt']);
console.log(last_create[0]['createdAt']);
if (last_update[0]['updatedAt'] >= last_create[0]['createdAt']) {
date = last_update[0]['updatedAt']
}
else {
date = last_create[0]['createdAt']
date = last_update[0]['updatedAt'];
} else {
date = last_create[0]['createdAt'];
}
let finalResult = new Array<ResultResponse>()
const finalResult = new Array<ResultResponse>();
for await (const userRes of results) {
let response: ResultResponse = new ResultResponse()
response.users = new Array<any>()
userRes.pending_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.notFollower })
})
userRes.inValid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.inValid })
})
userRes.valid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.isValid })
})
response.username = userRes.username,
response.valid_mentions = userRes.valid_mentions,
response.invalid_mentions = userRes.invalid_mentions,
response.pending_mentions = userRes.pending_mentions,
response.score = userRes.score
finalResult.push(response)
const response: ResultResponse = new ResultResponse();
response.users = new Array<any>();
userRes.pending_users.forEach((user) => {
response.users.push({
userId: user,
status: CommentStatus.notFollower,
});
});
userRes.inValid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.inValid });
});
userRes.valid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.isValid });
});
(response.username = userRes.username),
(response.valid_mentions = userRes.valid_mentions),
(response.invalid_mentions = userRes.invalid_mentions),
(response.pending_mentions = userRes.pending_mentions),
(response.score = userRes.score);
finalResult.push(response);
}
return {
finalResult,
last_update: date
}
last_update: date,
};
}
async getUserResult(username: string) {
let userRes = await this.resultModel.findOne({ username })
let userIndexs = await this.lotoryResultModel.find({username})
let response: ResultResponse = new ResultResponse()
response.users = new Array<any>()
response.lottory_chances_codes = new Array<string>()
userRes.pending_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.notFollower })
})
userRes.inValid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.inValid })
})
userRes.valid_users.forEach(user => {
response.users.push({ userId: user, status: CommentStatus.isValid })
})
response.username = userRes.username,
response.valid_mentions = userRes.valid_mentions,
response.invalid_mentions = userRes.invalid_mentions,
response.pending_mentions = userRes.pending_mentions,
response.score = userRes.score
userIndexs.forEach(index =>{
response.lottory_chances_codes.push(index.index.toString())
})
const userRes = await this.resultModel.findOne({ username });
const userIndexs = await this.lotoryResultModel.find({ username });
const response: ResultResponse = new ResultResponse();
response.users = new Array<any>();
response.lottory_chances_codes = new Array<string>();
userRes.pending_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.notFollower });
});
userRes.inValid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.inValid });
});
userRes.valid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.isValid });
});
(response.username = userRes.username),
(response.valid_mentions = userRes.valid_mentions),
(response.invalid_mentions = userRes.invalid_mentions),
(response.pending_mentions = userRes.pending_mentions),
(response.score = userRes.score);
userIndexs.forEach((index) => {
response.lottory_chances_codes.push(index.index.toString());
});
return {
response
}
response,
};
}
shuffle(array) {
var currentIndex = array.length, randomIndex;
let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
array[randomIndex],
array[currentIndex],
];
}
return array;
}
async getShuffleData() {
let comptitionArray = new Array<string>()
let foundUsernames = await this.resultModel.find()
const comptitionArray = new Array<string>();
const foundUsernames = await this.resultModel.find();
console.log(foundUsernames);
let score = 0
let score = 0;
for await (const user of foundUsernames) {
score += user.score
score += user.score;
for (let index = 0; index < user.score; index++) {
comptitionArray.push(user.username)
comptitionArray.push(user.username);
}
}
let res = this.shuffle(comptitionArray)
console.log("score is",score);
return res
}
const res = this.shuffle(comptitionArray);
console.log('score is', score);
return res;
}
async addResultsToDB(){
await this.lotoryResultModel.deleteMany()
let comptitionArray = new Array<any>()
let foundUsernames = await this.resultModel.find().sort({ score: -1 })
async addResultsToDB() {
await this.lotoryResultModel.deleteMany();
const comptitionArray = new Array<any>();
const foundUsernames = await this.resultModel.find().sort({ score: -1 });
console.log(foundUsernames);
let index = 1
let index = 1;
for await (const user of foundUsernames) {
for (let u = 0; u < user.score; u++) {
comptitionArray.push({username: user.username, index})
index++
comptitionArray.push({ username: user.username, index });
index++;
}
}
await this.lotoryResultModel.insertMany(comptitionArray)
return "successfull"
await this.lotoryResultModel.insertMany(comptitionArray);
return 'successfull';
}
}
export class ResultResponse {
username: string
valid_mentions: number
invalid_mentions: number
pending_mentions: number
score: number
users?: Array<any>
lottory_chances_codes :Array<string>
username: string;
valid_mentions: number;
invalid_mentions: number;
pending_mentions: number;
score: number;
users?: Array<any>;
lottory_chances_codes: Array<string>;
}
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type CommentDocument = Comment & Document
export type CommentDocument = Comment & Document;
@Schema({timestamps:true})
@Schema({ timestamps: true })
export class Comment {
@Prop()
_id: Types.ObjectId
_id: Types.ObjectId;
@Prop()
comment_id: string
comment_id: string;
@Prop()
text: string
text: string;
@Prop()
owner_username : string
owner_username: string;
@Prop()
owner_id : string
owner_id: string;
@Prop()
date: number
@Prop({type:Object})
comment_object : Object
date: number;
@Prop({ type: Object })
comment_object: Object;
}
export const CommentSchema = SchemaFactory.createForClass(Comment)
export const CommentSchema = SchemaFactory.createForClass(Comment);
export class GetUserScore {
username:string
}
\ No newline at end of file
username: string;
}
This source diff could not be displayed because it is too large. You can view the blob instead.
export class CleanedComments{
owner_username: string
mentions : Array<MentionDocument>
export class CleanedComments {
owner_username: string;
mentions: Array<MentionDocument>;
}
export class MentionDocument{
mentioned_username: string
date: number
}
\ No newline at end of file
export class MentionDocument {
mentioned_username: string;
date: number;
}
export interface IFollower{
user_id: string
username: string
full_name: string
follower_obejct: Object
follow_data?: number
}
\ No newline at end of file
export interface IFollower {
user_id: string;
username: string;
full_name: string;
follower_obejct: Object;
follow_data?: number;
}
export interface IncomingComment {
comment_id: string
owner_username: string
owner_id: string
text: string
date: number
commnet_object : Object
}
\ No newline at end of file
comment_id: string;
owner_username: string;
owner_id: string;
text: string;
date: number;
commnet_object: Object;
}
export class UserAllMention {
mentioned_username: string
mentioned_user_id: string
page_follow_date: number
comment_date: number
comment_status?: CommentStatus[]
mentioned_username: string;
mentioned_user_id: string;
page_follow_date: number;
comment_date: number;
comment_status?: CommentStatus[];
}
export enum CommentStatus {
isMentionedBefore ="isMentionedBefore", //"this username was mentioned already before your comment",
isAFollowerBefore = "isAFollowerBefore", //"this username was followed page before your comment",
notFollower = "notFollower", //"this username didnt follow page yet",
isValid = "isValid", //"your comment is valid"
inValid ="inValid (mentioned before or followed before)"
}
\ No newline at end of file
isMentionedBefore = 'isMentionedBefore', //"this username was mentioned already before your comment",
isAFollowerBefore = 'isAFollowerBefore', //"this username was followed page before your comment",
notFollower = 'notFollower', //"this username didnt follow page yet",
isValid = 'isValid', //"your comment is valid"
inValid = 'inValid (mentioned before or followed before)',
}
......@@ -2,14 +2,13 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule,{ cors: true });
const app = await NestFactory.create(AppModule, { cors: true });
app.enableCors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
})
});
await app.listen(3000);
}
bootstrap();
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type RequestDocument = Request & Document
export type RequestDocument = Request & Document;
@Schema({timestamps:true})
@Schema({ timestamps: true })
export class Request {
@Prop()
_id: Types.ObjectId
_id: Types.ObjectId;
@Prop()
cursor: string
cursor: string;
@Prop()
type: string
type: string;
@Prop()
post_short_code: string
post_short_code: string;
@Prop()
account_username: string
account_username: string;
}
export const RequestSchema = SchemaFactory.createForClass(Request)
export const RequestSchema = SchemaFactory.createForClass(Request);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type ResultDocument = Result & Document
export type ResultDocument = Result & Document;
@Schema({ timestamps: true })
export class Result {
@Prop()
username: string;
@Prop()
username: string
@Prop()
valid_mentions: number;
@Prop()
valid_mentions: number
@Prop()
invalid_mentions: number;
@Prop()
invalid_mentions: number
@Prop()
pending_mentions: number;
@Prop()
pending_mentions: number
@Prop()
score: number;
@Prop()
score: number
@Prop()
valid_users: Array<string>
@Prop()
inValid_users: Array<string>
@Prop()
pending_users: Array<string>
@Prop()
valid_users: Array<string>;
@Prop()
inValid_users: Array<string>;
@Prop()
pending_users: Array<string>;
}
export const ResultSchema = SchemaFactory.createForClass(Result)
export const ResultSchema = SchemaFactory.createForClass(Result);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment