Skip to content

Commit

Permalink
Refactor visitor type to use Option
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Dec 29, 2023
1 parent 570f0f4 commit 5431c1d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/app_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ struct VistEvent {
ip: String,
country: String,
member: Membership,
vt: VisitorType,
vt: Option<VisitorType>,
}

#[derive(Serialize_repr, Debug, PartialEq)]
#[derive(Serialize_repr, Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum VisitorType {
Referer = 1,
Expand Down Expand Up @@ -76,11 +76,11 @@ impl Context {

pub async fn boring_visitor(
&self,
v_type: VisitorType,
v_type: Option<VisitorType>,
domain: &str,
headers: &HeaderMap,
) -> Result<(&str, i64, i64, i64), anyhow::Error> {
if v_type == VisitorType::Referer && domain.eq(&*SYSTEM_DOMAIN) {
if v_type.is_some_and(|v| v == VisitorType::Referer) && domain.eq(&*SYSTEM_DOMAIN) {
return Err(anyhow!("system domain"));
}
if let Some(id) = self.domain2id.get(domain) {
Expand All @@ -97,7 +97,9 @@ impl Context {
let visitor_key = format!("{}_{}_{:?}", ip, id, v_type);
let visitor_cache = self.cache.get(&visitor_key).await;

if visitor_cache.is_none() {
if v_type.is_some_and(|v| [VisitorType::Referer, VisitorType::Badge].contains(&v))
&& visitor_cache.is_none()
{
self.cache
.set(visitor_key, (), Some(Duration::from_secs(60 * 60 * 4)))
.await;
Expand All @@ -110,7 +112,7 @@ impl Context {
.get(id)
.unwrap_or(&(0, NaiveDateTime::from_timestamp(0, 0)))
.to_owned();
if matches!(v_type, VisitorType::Referer) {
if v_type.is_some_and(|v| v == VisitorType::Referer) {
if visitor_cache.is_none() {
dist_r.0 += 1;
dist_r.1 = now_shanghai();
Expand All @@ -125,7 +127,7 @@ impl Context {
.get(id)
.unwrap_or(&(0, NaiveDateTime::from_timestamp(0, 0)))
.to_owned();
if matches!(v_type, VisitorType::Badge) {
if v_type.is_some_and(|v| v == VisitorType::Badge) {
if visitor_cache.is_none() {
dist_uv.0 += 1;
dist_uv.1 = now_shanghai();
Expand Down
12 changes: 6 additions & 6 deletions src/app_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ pub async fn show_badge(
headers: HeaderMap,
Extension(ctx): Extension<DynContext>,
) -> Response {
let mut v_type = crate::app_model::VisitorType::Badge;
let mut v_type = Some(crate::app_model::VisitorType::Badge);

let domain_referrer = get_domain_from_referrer(&headers).unwrap_or("".to_string());
if domain_referrer.ne(&domain) {
if domain.eq("[domain]") {
domain = domain_referrer;
} else {
v_type = crate::app_model::VisitorType::ICON;
v_type = None;
}
}

Expand All @@ -86,7 +86,7 @@ pub async fn show_favicon(
Extension(ctx): Extension<DynContext>,
) -> Response {
let tend = ctx
.boring_visitor(crate::app_model::VisitorType::ICON, &domain, &headers)
.boring_visitor(Some(crate::app_model::VisitorType::ICON), &domain, &headers)
.await;
if tend.is_err() {
return (
Expand All @@ -105,7 +105,7 @@ pub async fn show_icon(
Extension(ctx): Extension<DynContext>,
) -> Response {
let tend = ctx
.boring_visitor(crate::app_model::VisitorType::ICON, &domain, &headers)
.boring_visitor(Some(crate::app_model::VisitorType::ICON), &domain, &headers)
.await;
if tend.is_err() {
return (
Expand Down Expand Up @@ -139,7 +139,7 @@ pub async fn home_page(
if domain.is_ok() {
let _ = ctx
.boring_visitor(
crate::app_model::VisitorType::Referer,
Some(crate::app_model::VisitorType::Referer),
&domain.unwrap(),
&headers,
)
Expand Down Expand Up @@ -255,7 +255,7 @@ pub async fn rank_page(
if domain.is_ok() {
let _ = ctx
.boring_visitor(
crate::app_model::VisitorType::Referer,
Some(crate::app_model::VisitorType::Referer),
&domain.unwrap(),
&headers,
)
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<div class="text-center font-size-14 mt-10">
QQ交流群:136231667(仅限会员加入)
<br>
<img referrerpolicy="no-referrer" class="mt-5" height="30rem" src="/api/badge/boringbay.com">
<img class="mt-5" height="30rem" src="/api/badge/boringbay.com">
</div>

<div class="text-center font-weight-bolder font-size-14 mt-10 mb-5">
Expand Down

0 comments on commit 5431c1d

Please sign in to comment.