1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Tasks for task_executor
//!
//! This module contains a bunch of functions, each of which represents a background
//! task behind Message Queue executed by task_executor.

use futures::TryStreamExt;
use pulsar::{Consumer, Pulsar, SubType, TokioExecutor};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use sea_orm::sea_query::Expr;
use sea_orm::{entity::*, ConnectionTrait, Database, DatabaseConnection, DbErr, QueryFilter};
use serde_json::json;
use tokio::time::Duration;

use super::email::{self, check_email_exist};
use crate::config::mq::*;
use crate::config::user::SEND_EMAIL_LIMIT;
use crate::config::BACKEND_TEST_MODE;
use crate::db::{content_post, prelude::*, user_collection, user_follow, user_like};
use crate::models::{pulsar::*, search::*};
use crate::routes::trending::select_trending;

pub trait Typesense {
    fn build_get(&self, uri: &str) -> reqwest::RequestBuilder;
    fn build_post(&self, uri: &str) -> reqwest::RequestBuilder;
    fn build_delete(&self, uri: &str) -> reqwest::RequestBuilder;
    fn build_patch(&self, uri: &str) -> reqwest::RequestBuilder;
}

impl Typesense for reqwest::Client {
    fn build_get(&self, uri: &str) -> reqwest::RequestBuilder {
        let typesense_api_key: &str = &TYPESENSE_API_KEY;
        let typesense_addr: String = TYPESENSE_ADDR.to_owned();
        self.get(typesense_addr + uri)
            .header("X-TYPESENSE-API-KEY", typesense_api_key)
    }

    fn build_post(&self, uri: &str) -> reqwest::RequestBuilder {
        let typesense_api_key: &str = &TYPESENSE_API_KEY;
        let typesense_addr: String = TYPESENSE_ADDR.to_owned();
        self.post(typesense_addr + uri)
            .header("X-TYPESENSE-API-KEY", typesense_api_key)
    }

    fn build_delete(&self, uri: &str) -> reqwest::RequestBuilder {
        let typesense_api_key: &str = &TYPESENSE_API_KEY;
        let typesense_addr: String = TYPESENSE_ADDR.to_owned();
        self.delete(typesense_addr + uri)
            .header("X-TYPESENSE-API-KEY", typesense_api_key)
    }

    fn build_patch(&self, uri: &str) -> reqwest::RequestBuilder {
        let typesense_api_key: &str = &TYPESENSE_API_KEY;
        let typesense_addr: String = TYPESENSE_ADDR.to_owned();
        self.patch(typesense_addr + uri)
            .header("X-TYPESENSE-API-KEY", typesense_api_key)
    }
}

async fn create_typesense_collections() -> Result<(), reqwest::Error> {
    //create typesense collections
    let collection_burrows = json!({
        "name": "burrows",
        "fields": [
            {"name": "burrow_id", "type": "int64"},
            {"name": "title", "type": "string", "locale": "zh"},
            {"name": "description", "type": "string", "locale": "zh"},
        ]
    });
    let collection_posts = json!({
        "name": "posts",
        "fields": [
            {"name": "post_id", "type": "int64"},
            {"name": "burrow_id", "type": "int64"},
            {"name": "title", "type": "string", "locale": "zh"},
            {"name": "section", "type": "string[]", "facet": true},
            {"name": "tag", "type": "string[]", "facet": true},
        ]
    });
    let collection_replies = json!({
        "name": "replies",
        "fields": [
            {"name": "post_id", "type": "int64", "facet": true},
            {"name": "reply_id", "type": "int32", "index": false , "optional": true},
            {"name": "burrow_id", "type": "int64"},
            {"name": "content", "type": "string", "locale": "zh"},
        ]
    });
    let client = reqwest::Client::new();
    for each in [collection_burrows, collection_posts, collection_replies].iter() {
        match client.build_post("/collections").json(&each).send().await {
            Ok(r) => match r.status().as_u16() {
                201 => {
                    log::warn!(
                        "Collection {} created successfully.",
                        each["name"].as_str().unwrap()
                    );
                }
                400 => {
                    log::warn!(
                        "Create collection {} failed with bad request. {}",
                        each["name"].as_str().unwrap(),
                        r.text().await.unwrap()
                    );
                    panic!(
                        "Bad Request - The request could not be understood due to malformed syntax."
                    )
                }
                409 => {
                    log::warn!(
                        "Collection {} already exists. Skip creation.",
                        each["name"].as_str().unwrap()
                    );
                }
                _ => {
                    log::warn!(
                        "{} Create collection {} failed with response. {}",
                        r.status().as_u16(),
                        each["name"].as_str().unwrap(),
                        r.text().await.unwrap()
                    );
                    panic!("Unknown error when creating collections.")
                }
            },
            Err(e) => panic!("Err when create typesense collections,{:?}", e),
        }
    }
    Ok(())
}

pub async fn pulsar_typesense() -> Result<(), pulsar::Error> {
    // setup pulsar consumer
    let pulsar_addr: String = PULSAR_ADDR.to_owned();
    let addr = pulsar_addr;
    let topic = "persistent://public/default/search".to_string();
    let builder = Pulsar::builder(addr, TokioExecutor);
    let pulsar: Pulsar<_> = builder.build().await?;
    let mut consumer: Consumer<PulsarSearchData, _> = pulsar
        .consumer()
        .with_topic(topic)
        .with_subscription_type(SubType::Exclusive)
        .build()
        .await?;

    match create_typesense_collections().await {
        Ok(_) => {
            log::warn!("Search engine successfully initialized");
        }
        Err(e) => {
            panic!("Failed to initialize search engine: {:?}", e);
        }
    };
    let client = reqwest::Client::new();
    while let Some(msg) = consumer.try_next().await? {
        consumer.ack(&msg).await?;
        // println!("metadata: {:?},id: {:?}", msg.metadata(), msg.message_id());
        let data = match msg.deserialize() {
            Ok(data) => data,
            Err(e) => {
                log::error!("could not deserialize message: {:?}", e);
                continue;
            }
        };
        match data {
            PulsarSearchData::CreateBurrow(burrow) => {
                let data: TypesenseBurrowData = burrow.into();
                match client
                    .build_post("/collections/burrows/documents")
                    .json(&data)
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        201 => log::info!("[PULSAR-SEARCH] 201: Add new burrow."),
                        409 => log::info!("[PULSAR-SEARCH] 409: Burrow already exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to add new burrow. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("add new burrow failed {:?}", e),
                }
            }
            PulsarSearchData::CreatePost(post) => {
                let data: TypesensePostData = post.into();

                match client
                    .build_post("/collections/posts/documents")
                    .json(&data)
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        201 => log::info!("[PULSAR-SEARCH] 201: Add new post."),
                        409 => log::info!("[PULSAR-SEARCH] 409: Post already exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to add new post. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("add new post failed {:?}", e),
                }
            }
            PulsarSearchData::CreateReply(reply) => {
                let data: TypesenseReplyData = reply.into();

                match client
                    .build_post("/collections/replies/documents")
                    .json(&data)
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        201 => log::info!("[PULSAR-SEARCH] 201: Add new reply."),
                        409 => log::info!("[PULSAR-SEARCH] 409: Reply already exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to add new reply. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("add new reply failed {:?}", e),
                }
            }
            PulsarSearchData::UpdateBurrow(burrow) => {
                let burrow_id = burrow.burrow_id;
                let data: TypesenseBurrowData = burrow.into();
                match client
                    .build_get(&format!("/collections/burrows/documents/{}", burrow_id))
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        200 => {
                            let response = r.json::<TypesenseBurrowData>().await.unwrap();
                            if response.update_time < data.update_time {
                                match client
                                    .build_patch(&format!(
                                        "/collections/burrows/documents/{}",
                                        burrow_id
                                    ))
                                    .json(&data)
                                    .send()
                                    .await
                                {
                                    Ok(r) => match r.status().as_u16() {
                                        201 => log::info!("[PULSAR-SEARCH] 201: Update burrow."),
                                        _ => log::error!(
                                            "[PULSAR-SEARCH] {}:  Failed to update burrow. {}",
                                            r.status().as_u16(),
                                            r.text().await.unwrap()
                                        ),
                                    },
                                    Err(e) => {
                                        log::error!("[PULSAR-SEARCH] Update burrow failed {:?}", e)
                                    }
                                }
                            } else {
                                log::info!("[PULSAR-SEARCH] Burrow is up to date.");
                            }
                        }
                        404 => log::info!("[PULSAR-SEARCH] 404: Burrow does not exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to update burrow. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("[PULSAR-SEARCH] Update burrow failed {:?}", e),
                }
            }
            PulsarSearchData::UpdatePost(post) => {
                let post_id = post.post_id;
                let data: TypesensePostData = post.into();
                match client
                    .build_get(&format!("/collections/posts/documents/{}", post_id))
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        200 => {
                            let response = r.json::<TypesensePostData>().await.unwrap();
                            if response.update_time < data.update_time {
                                match client
                                    .build_patch(&format!(
                                        "/collections/posts/documents/{}",
                                        post_id
                                    ))
                                    .json(&data)
                                    .send()
                                    .await
                                {
                                    Ok(r) => match r.status().as_u16() {
                                        201 => log::info!("[PULSAR-SEARCH] 201: Update post."),
                                        _ => log::error!(
                                            "[PULSAR-SEARCH] {}:  Failed to update post. {}",
                                            r.status().as_u16(),
                                            r.text().await.unwrap()
                                        ),
                                    },
                                    Err(e) => {
                                        log::error!("[PULSAR-SEARCH] Update post failed {:?}", e)
                                    }
                                }
                            } else {
                                log::info!("[PULSAR-SEARCH] Post is up to date.");
                            }
                        }
                        404 => log::info!("[PULSAR-SEARCH] 404: post does not exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to update post. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("[PULSAR-SEARCH] Update post failed {:?}", e),
                }
            }
            PulsarSearchData::UpdateReply(reply) => {
                let post_id = reply.post_id;
                let reply_id = reply.reply_id;
                let data: TypesenseReplyData = reply.into();
                match client
                    .build_get(&format!(
                        "/collections/replies/documents/{}-{}",
                        post_id, reply_id
                    ))
                    .send()
                    .await
                {
                    Ok(r) => match r.status().as_u16() {
                        200 => {
                            let response = r.json::<TypesenseReplyData>().await.unwrap();
                            if response.update_time < data.update_time {
                                match client
                                    .build_patch(&format!(
                                        "/collections/replies/documents/{}-{}",
                                        post_id, reply_id
                                    ))
                                    .json(&data)
                                    .send()
                                    .await
                                {
                                    Ok(r) => match r.status().as_u16() {
                                        201 => log::info!("[PULSAR-SEARCH] 201: Update reply."),
                                        _ => log::error!(
                                            "[PULSAR-SEARCH] {}:  Failed to update reply. {}",
                                            r.status().as_u16(),
                                            r.text().await.unwrap()
                                        ),
                                    },
                                    Err(e) => {
                                        log::error!("[PULSAR-SEARCH] Update reply failed {:?}", e)
                                    }
                                }
                            } else {
                                log::info!("[PULSAR-SEARCH] Reply is up to date.");
                            }
                        }
                        404 => log::info!("[PULSAR-SEARCH] 404: reply does not exist."),
                        _ => log::error!(
                            "[PULSAR-SEARCH] {}:  Failed to update reply. {}",
                            r.status().as_u16(),
                            r.text().await.unwrap()
                        ),
                    },
                    Err(e) => log::error!("[PULSAR-SEARCH] Update reply failed {:?}", e),
                }
            }
            PulsarSearchData::DeleteBurrow(burrow_id) => {
                match client
                    .build_delete(&format!("/collections/burrows/documents/{}", burrow_id))
                    .send()
                    .await
                {
                    Ok(_) => log::info!("[PULSAR-SEARCH] Delete burrow successfully"),
                    Err(e) => log::error!("[PULSAR-SEARCH] Delete burrow failed: {:?}", e),
                }
            }
            PulsarSearchData::DeletePost(post_id) => {
                match client
                    .build_delete(&format!("/collections/posts/documents/{}", post_id))
                    .send()
                    .await
                {
                    Ok(_) => println!("[PULSAR-SEARCH] Delete post successfully"),
                    Err(e) => println!("[PULSAR-SEARCH] Delete post failed: {:?}", e),
                }
            }
            PulsarSearchData::DeleteReply(post_id, reply_id) => {
                match client
                    .build_delete(&format!(
                        "/collections/replies/documents/{}-{}",
                        post_id, reply_id
                    ))
                    .send()
                    .await
                {
                    Ok(_) => println!("[PULSAR-SEARCH] Delete reply successfully"),
                    Err(e) => println!("[PULSAR-SEARCH] Delete reply failed: {:?}", e),
                }
            }
        }
    }
    Ok(())
}

pub async fn pulsar_relation() -> Result<(), pulsar::Error> {
    // setup pulsar consumer
    let pulsar_addr: String = PULSAR_ADDR.to_owned();
    let addr = pulsar_addr;
    let topic = "persistent://public/default/relation".to_string();
    let builder = Pulsar::builder(addr, TokioExecutor);
    let pulsar: Pulsar<_> = builder.build().await?;
    let mut consumer: Consumer<PulsarRelationData, _> = pulsar
        .consumer()
        .with_topic(topic)
        .with_subscription_type(SubType::Exclusive)
        .build()
        .await?;
    let postgres_addr: &str = &POSTGRES_ADDR;
    let db: DatabaseConnection = match Database::connect(postgres_addr).await {
        Ok(db) => db,
        Err(e) => {
            log::error!("[PULSAR-RELATION] Database Error {:?}", e);
            panic!("pulsar relation database connection failed");
        }
    };
    while let Some(msg) = consumer.try_next().await? {
        consumer.ack(&msg).await?;
        let data = match msg.deserialize() {
            Ok(data) => data,
            Err(e) => {
                log::error!("[PULSAR-RELATION] Could not deserialize message: {:?}", e);
                continue;
            }
        };
        match data {
            PulsarRelationData::ActivateLike(uid, post_id) => {
                let like = user_like::ActiveModel {
                    uid: Set(uid),
                    post_id: Set(post_id),
                };
                match db
                    .transaction::<_, (), DbErr>(|txn| {
                        Box::pin(async move {
                            like.insert(txn).await?;
                            let update_res = ContentPost::update_many()
                                .col_expr(
                                    content_post::Column::LikeNum,
                                    Expr::col(content_post::Column::LikeNum).add(1),
                                )
                                .filter(content_post::Column::PostId.eq(post_id))
                                .exec(txn)
                                .await?;
                            if update_res.rows_affected != 1 {
                                return Err(DbErr::RecordNotFound("post not found".to_string()));
                            }
                            Ok(())
                        })
                    })
                    .await
                {
                    Ok(_) => log::info!("[PULSAR-RELATION] Insert like success"),
                    Err(e) => log::error!("[PULSAR-RELATION] Insert like failed {:?}", e),
                }
            }
            PulsarRelationData::DeactivateLike(uid, post_id) => {
                let like = user_like::ActiveModel {
                    uid: Set(uid),
                    post_id: Set(post_id),
                };
                match like.delete(&db).await {
                    Ok(res) => {
                        log::info!(
                            "[PULSAR-RELATION] Delete like success {}",
                            res.rows_affected
                        );
                        if res.rows_affected == 1 {
                            let _ = ContentPost::update_many()
                                .col_expr(
                                    content_post::Column::LikeNum,
                                    Expr::col(content_post::Column::LikeNum).sub(1),
                                )
                                .filter(content_post::Column::PostId.eq(post_id))
                                .exec(&db)
                                .await;
                        }
                    }
                    Err(e) => log::error!("[PULSAR-RELATION] Delete like failed {:?}", e),
                }
            }
            PulsarRelationData::ActivateCollection(uid, post_id) => {
                let collection = user_collection::ActiveModel {
                    uid: Set(uid),
                    post_id: Set(post_id),
                    ..Default::default()
                };
                match db
                    .transaction::<_, (), DbErr>(|txn| {
                        Box::pin(async move {
                            collection.insert(txn).await?;
                            let update_res = ContentPost::update_many()
                                .col_expr(
                                    content_post::Column::CollectionNum,
                                    Expr::col(content_post::Column::CollectionNum).add(1),
                                )
                                .filter(content_post::Column::PostId.eq(post_id))
                                .exec(txn)
                                .await?;
                            if update_res.rows_affected != 1 {
                                return Err(DbErr::RecordNotFound("post not found".to_string()));
                            }
                            Ok(())
                        })
                    })
                    .await
                {
                    Ok(_) => log::info!("[PULSAR-RELATION] Insert collection success"),
                    Err(e) => log::error!("[PULSAR-RELATION] Insert collection failed {:?}", e),
                }
            }
            PulsarRelationData::DeactivateCollection(uid, post_id) => {
                let collection = user_collection::ActiveModel {
                    uid: Set(uid),
                    post_id: Set(post_id),
                    ..Default::default()
                };
                match collection.delete(&db).await {
                    Ok(res) => {
                        log::info!(
                            "[PULSAR-RELATION] Delete collection success {}",
                            res.rows_affected
                        );
                        if res.rows_affected == 1 {
                            let _ = ContentPost::update_many()
                                .col_expr(
                                    content_post::Column::CollectionNum,
                                    Expr::col(content_post::Column::CollectionNum).sub(1),
                                )
                                .filter(content_post::Column::PostId.eq(post_id))
                                .exec(&db)
                                .await;
                        }
                    }
                    Err(e) => log::error!("[PULSAR-RELATION] Delete collection failed {:?}", e),
                }
            }
            PulsarRelationData::ActivateFollow(uid, burrow_id) => {
                let follow = user_follow::ActiveModel {
                    uid: Set(uid),
                    burrow_id: Set(burrow_id),
                    ..Default::default()
                };
                match db
                    .transaction::<_, (), DbErr>(|txn| {
                        Box::pin(async move {
                            follow.insert(txn).await?;
                            let res = Burrow::find_by_id(burrow_id).one(txn).await?;
                            match res {
                                Some(_) => Ok(()),
                                None => Err(DbErr::RecordNotFound("burrow not found".to_string())),
                            }
                        })
                    })
                    .await
                {
                    Ok(_) => log::info!("[PULSAR-RELATION] Insert follow success"),
                    Err(e) => log::error!("[PULSAR-RELATION] Insert follow failed {:?}", e),
                }
            }
            PulsarRelationData::DeactivateFollow(uid, burrow_id) => {
                let follow = user_follow::ActiveModel {
                    uid: Set(uid),
                    burrow_id: Set(burrow_id),
                    ..Default::default()
                };
                match follow.delete(&db).await {
                    Ok(res) => {
                        log::info!(
                            "[PULSAR-RELATION] Delete follow success {}",
                            res.rows_affected
                        );
                    }
                    Err(e) => log::error!("[PULSAR-RELATION] Delete follow failed {:?}", e),
                }
            }
        }
    }

    Ok(())
}

pub async fn generate_trending() -> redis::RedisResult<()> {
    // setup pulsar consumer
    let redis_addr: String = REDIS_ADDR.to_owned();
    let addr = redis_addr;
    let client = redis::Client::open(addr)?;
    let mut kv_conn = client.get_async_connection().await?;
    let postgres_addr: &str = &POSTGRES_ADDR;
    let pg_con: DatabaseConnection = match Database::connect(postgres_addr).await {
        Ok(db) => db,
        Err(e) => {
            log::error!("[PULSAR-TRENDING] Database Error{:?}", e);
            panic!("pulsar trending database connection failed");
        }
    };
    let seconds = 900;
    let mut interval = tokio::time::interval(Duration::from_secs(seconds));
    interval.tick().await;
    loop {
        interval.tick().await;
        match select_trending(&pg_con, &mut kv_conn).await {
            Ok(trending) => {
                log::info!("[PULSAR-TRENDING] Get Trending: {}", trending);
            }
            Err(e) => {
                log::error!("[PULSAR-TRENDING] Error: {}", e);
            }
        }
    }
}

async fn get_set_redis(
    kv_conn: &mut redis::aio::Connection,
    email: &str,
    verification_code: &str,
) -> Result<String, redis::RedisError> {
    let get_res: Option<String> = redis::cmd("GET").arg(email).query_async(kv_conn).await?;
    let op_times = 1 + match get_res {
        Some(res) => {
            let values: Vec<&str> = res.split(':').collect();
            values[0].parse::<usize>().unwrap()
        }
        None => 0,
    };
    // check request rate
    if op_times > SEND_EMAIL_LIMIT {
        let e = (redis::ErrorKind::ExtensionError, "RateLimit").into();
        return Err(e);
    } else {
        let _: String = redis::cmd("SETEX")
            .arg(email)
            .arg(EMAIL_TOKEN_EX)
            .arg(op_times.to_string() + ":" + verification_code)
            .query_async(kv_conn)
            .await?;
    }
    Ok("Success".to_string())
}

pub async fn pulsar_email() -> Result<(), pulsar::Error> {
    // setup pulsar consumer
    let redis_addr: String = REDIS_ADDR.to_owned();
    let addr = redis_addr;
    let client = match redis::Client::open(addr) {
        Ok(c) => c,
        Err(e) => {
            panic!("[PULSAR-EMAIL] Redis Error: {:?}", e);
        }
    };
    let mut kv_conn = match client.get_async_connection().await {
        Ok(c) => c,
        Err(e) => {
            panic!("[PULSAR-EMAIL] Redis Error: {:?}", e);
        }
    };
    let pulsar_addr: String = PULSAR_ADDR.to_owned();
    let addr = pulsar_addr;
    let topic = "persistent://public/default/email".to_string();
    let builder = Pulsar::builder(addr, TokioExecutor);
    let pulsar: Pulsar<_> = builder.build().await?;
    let mut consumer: Consumer<PulsarSendEmail, _> = pulsar
        .consumer()
        .with_topic(topic)
        .with_subscription_type(SubType::Exclusive)
        .build()
        .await?;
    while let Some(msg) = consumer.try_next().await? {
        consumer.ack(&msg).await?;
        let data = match msg.deserialize() {
            Ok(data) => data,
            Err(e) => {
                log::error!("[PULSAR-RELATION] Could not deserialize message: {:?}", e);
                continue;
            }
        };
        let (email, repeat_times) = match data {
            PulsarSendEmail::Sign { email } => (email, 6),
            PulsarSendEmail::Reset { email } => (email, 10),
        };
        if *BACKEND_TEST_MODE {
            let verification_code = "6".repeat(repeat_times);
            match get_set_redis(&mut kv_conn, &email, &verification_code).await {
                Ok(_) => log::info!("[PULSAR-EMAIL] Redis get & set success, Email send success"),
                Err(e) => match e.kind() {
                    redis::ErrorKind::ExtensionError => {
                        log::info!("[PULSAR-EMAIL] User sent too many emails, refuse to send");
                        continue;
                    }
                    _ => {
                        log::error!("[PULSAR-EMAIL] Redis get/set failed {:?}", e);
                        continue;
                    }
                },
            }
        } else if check_email_exist(&email).await.0 {
            // generate verification code
            let verification_code: String = std::iter::repeat(())
                .map(|()| thread_rng().sample(Alphanumeric))
                .map(char::from)
                .take(repeat_times)
                .collect();
            // println!("{}", verification_code);
            match get_set_redis(&mut kv_conn, &email, &verification_code).await {
                Ok(_) => {
                    log::info!("[PULSAR-EMAIL] Redis get & set success");
                    match email::send(email, verification_code).await {
                        Ok(res) => {
                            log::info!("[PULSAR-EMAIL] Email send success, response: {}", res);
                            // println!("{}", res);
                        }
                        Err(e) => {
                            log::error!("[PULSAR-EMAIL] Email send failed: {}", e);
                        }
                    };
                }
                Err(e) => match e.kind() {
                    redis::ErrorKind::ExtensionError => {
                        log::info!("[PULSAR-EMAIL] User sent too many emails, refuse to send");
                        continue;
                    }
                    _ => {
                        log::error!("[PULSAR-EMAIL] Redis get/set failed {:?}", e);
                        continue;
                    }
                },
            }
        } else {
            let verification_code = "6".repeat(repeat_times);
            let set_redis_result: Result<String, redis::RedisError> = redis::cmd("SETEX")
                .arg(&email)
                .arg(EMAIL_TOKEN_EX)
                .arg((SEND_EMAIL_LIMIT + 1).to_string() + ":" + &verification_code)
                .query_async(&mut kv_conn)
                .await;
            match set_redis_result {
                Ok(_) => log::info!("[PULSAR-EMAIL] Redis set success"),
                Err(e) => log::error!("[PULSAR-EMAIL] Redis set failed {:?}", e),
            }
        }
    }
    Ok(())
}