Created
October 31, 2020 17:42
-
-
Save rtsuk/958c6eef5efb593dc77fdd0431daa915 to your computer and use it in GitHub Desktop.
Show call list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Template)] | |
#[template(path = "calls.html")] | |
struct CallsTemplate<'a> { | |
flash: &'a Option<String>, | |
call_records: &'a Vec<CallRecord>, | |
number_map: &'a HashMap<String, Kind>, | |
} | |
async fn show_call_list(mut req: tide::Request<()>) -> tide::Result<tide::Response> { | |
let flash: Option<String> = req.session().get("flash"); | |
req.session_mut().remove("flash"); | |
let number_map = load_number_map()?; | |
let records = load_call_records()?; | |
let mut rdr = ReaderBuilder::new() | |
.has_headers(false) | |
.from_reader(records.as_bytes()); | |
let mut records: Vec<CallRecord> = rdr.deserialize().filter_map(Result::ok).collect(); | |
records.sort_by(CallRecord::compare_by_start); | |
let hello = CallsTemplate { | |
flash: &flash, | |
call_records: &records, | |
number_map: &number_map, | |
}; | |
let response = Response::builder(200) | |
.body(hello.render().expect("render")) | |
.header("x-custom-header", "value") | |
.content_type(tide::http::mime::HTML) | |
.build(); | |
Ok(response) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
{%- import "macros.html" as macros -%} | |
{% block title %}Calls{% endblock %} | |
{% block content %} | |
{% if flash.is_some() %} | |
<div id="flash_messages"> | |
<div class="alert alert-success"> | |
{{ flash.as_ref().unwrap() }} | |
</div> | |
</div> | |
{% endif %} | |
<h2>Calls</h2> | |
<table class="table table-condensed"> | |
<tr><th>Name</th><th>Number</th><th>Date</th><th>Duration</th><th>Action</th></tr> | |
{% for call_record in call_records %} | |
<tr> | |
<td>{{call_record.caller_id_string()}}</td> | |
<td>{{call_record.formatted_number()}}</td> | |
<td>{{call_record.formatted_date()}}</td> | |
<td>{{call_record.duration_as_string()}}</td> | |
<td> | |
{% if call_record.is_allowed_number(number_map) %} | |
{% call macros::button("Unallow Number", "/unallow_number", call_record.src) %} | |
{% else %} | |
{% if call_record.has_name() %} | |
{% if call_record.is_blocked_name(number_map) %} | |
{% call macros::button("Unblock Name", "/unblock_name", call_record.caller_id_string()) %} | |
{% else %} | |
{% call macros::button("Block Name", "/block_name", call_record.caller_id_string()) %} | |
{% endif %} | |
{% endif %} | |
{% if call_record.is_blocked_number(number_map) %} | |
{% call macros::button("Unblock Number", "/unblock_number", call_record.src) %} | |
{% else %} | |
{% call macros::button("Block Number", "/block_number", call_record.src) %} | |
{% call macros::button("Allow Number", "/allow_number", call_record.src) %} | |
{% endif %} | |
{% endif %} | |
</td> | |
</tr> | |
{% endfor %} | |
<table> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment