started development
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
|
||||
MANIFEST-000013
|
||||
@@ -0,0 +1 @@
|
||||
9ae77887-bae0-4a92-a7d2-3df6127bec33
|
||||
Binary file not shown.
@@ -0,0 +1,206 @@
|
||||
# This is a RocksDB option file.
|
||||
#
|
||||
# For detailed file format spec, please refer to the example file
|
||||
# in examples/rocksdb_option_file_example.ini
|
||||
#
|
||||
|
||||
[Version]
|
||||
rocksdb_version=8.10.0
|
||||
options_file_version=1.1
|
||||
|
||||
[DBOptions]
|
||||
compaction_readahead_size=2097152
|
||||
strict_bytes_per_sync=false
|
||||
bytes_per_sync=0
|
||||
max_background_jobs=32
|
||||
avoid_flush_during_shutdown=false
|
||||
max_background_flushes=-1
|
||||
delayed_write_rate=16777216
|
||||
max_open_files=-1
|
||||
max_subcompactions=1
|
||||
writable_file_max_buffer_size=1048576
|
||||
wal_bytes_per_sync=0
|
||||
max_background_compactions=-1
|
||||
max_total_wal_size=0
|
||||
delete_obsolete_files_period_micros=21600000000
|
||||
stats_dump_period_sec=600
|
||||
stats_history_buffer_size=1048576
|
||||
stats_persist_period_sec=600
|
||||
enforce_single_del_contracts=true
|
||||
lowest_used_cache_tier=kNonVolatileBlockTier
|
||||
bgerror_resume_retry_interval=1000000
|
||||
best_efforts_recovery=false
|
||||
log_readahead_size=0
|
||||
write_dbid_to_manifest=false
|
||||
wal_compression=kNoCompression
|
||||
manual_wal_flush=false
|
||||
db_host_id=__hostname__
|
||||
two_write_queues=false
|
||||
random_access_max_buffer_size=1048576
|
||||
avoid_unnecessary_blocking_io=false
|
||||
skip_checking_sst_file_sizes_on_db_open=false
|
||||
flush_verify_memtable_count=true
|
||||
fail_if_options_file_error=true
|
||||
atomic_flush=false
|
||||
verify_sst_unique_id_in_manifest=true
|
||||
skip_stats_update_on_db_open=false
|
||||
track_and_verify_wals_in_manifest=false
|
||||
compaction_verify_record_count=true
|
||||
paranoid_checks=true
|
||||
create_if_missing=true
|
||||
max_write_batch_group_size_bytes=1048576
|
||||
avoid_flush_during_recovery=false
|
||||
file_checksum_gen_factory=nullptr
|
||||
enable_thread_tracking=false
|
||||
allow_fallocate=true
|
||||
allow_data_in_errors=false
|
||||
error_if_exists=false
|
||||
use_direct_io_for_flush_and_compaction=false
|
||||
create_missing_column_families=true
|
||||
WAL_size_limit_MB=0
|
||||
use_direct_reads=false
|
||||
persist_stats_to_disk=false
|
||||
allow_2pc=false
|
||||
is_fd_close_on_exec=true
|
||||
max_log_file_size=0
|
||||
access_hint_on_compaction_start=NORMAL
|
||||
max_file_opening_threads=16
|
||||
wal_filter=nullptr
|
||||
allow_mmap_reads=false
|
||||
allow_mmap_writes=false
|
||||
use_adaptive_mutex=false
|
||||
use_fsync=false
|
||||
table_cache_numshardbits=6
|
||||
dump_malloc_stats=false
|
||||
db_write_buffer_size=0
|
||||
allow_ingest_behind=false
|
||||
keep_log_file_num=20
|
||||
max_bgerror_resume_count=2147483647
|
||||
allow_concurrent_memtable_write=true
|
||||
recycle_log_file_num=0
|
||||
log_file_time_to_roll=0
|
||||
manifest_preallocation_size=4194304
|
||||
enable_write_thread_adaptive_yield=true
|
||||
WAL_ttl_seconds=0
|
||||
max_manifest_file_size=1073741824
|
||||
wal_recovery_mode=kPointInTimeRecovery
|
||||
enable_pipelined_write=true
|
||||
write_thread_slow_yield_usec=3
|
||||
unordered_write=false
|
||||
write_thread_max_yield_usec=100
|
||||
advise_random_on_open=true
|
||||
info_log_level=WARN_LEVEL
|
||||
|
||||
|
||||
[CFOptions "default"]
|
||||
memtable_max_range_deletions=0
|
||||
compression_opts={checksum=false;max_dict_buffer_bytes=0;enabled=false;max_dict_bytes=0;max_compressed_bytes_per_kb=896;parallel_threads=1;zstd_max_train_bytes=0;level=32767;use_zstd_dict_trainer=true;strategy=0;window_bits=-14;}
|
||||
block_protection_bytes_per_key=0
|
||||
bottommost_file_compaction_delay=0
|
||||
memtable_protection_bytes_per_key=0
|
||||
target_file_size_multiplier=1
|
||||
report_bg_io_stats=false
|
||||
write_buffer_size=268435456
|
||||
memtable_huge_page_size=0
|
||||
max_successive_merges=0
|
||||
max_write_buffer_number=32
|
||||
prefix_extractor=nullptr
|
||||
bottommost_compression_opts={checksum=false;max_dict_buffer_bytes=0;enabled=false;max_dict_bytes=0;max_compressed_bytes_per_kb=896;parallel_threads=1;zstd_max_train_bytes=0;level=32767;use_zstd_dict_trainer=true;strategy=0;window_bits=-14;}
|
||||
paranoid_file_checks=false
|
||||
blob_garbage_collection_force_threshold=1.000000
|
||||
enable_blob_files=true
|
||||
blob_file_starting_level=0
|
||||
memtable_prefix_bloom_size_ratio=0.000000
|
||||
inplace_update_num_locks=10000
|
||||
blob_compaction_readahead_size=0
|
||||
ignore_max_compaction_bytes_for_input=true
|
||||
arena_block_size=1048576
|
||||
level0_stop_writes_trigger=36
|
||||
blob_compression_type=kNoCompression
|
||||
level0_slowdown_writes_trigger=20
|
||||
hard_pending_compaction_bytes_limit=274877906944
|
||||
soft_pending_compaction_bytes_limit=68719476736
|
||||
target_file_size_base=67108864
|
||||
level0_file_num_compaction_trigger=4
|
||||
max_compaction_bytes=1677721600
|
||||
disable_auto_compactions=false
|
||||
check_flush_compaction_key_order=true
|
||||
min_blob_size=4096
|
||||
memtable_whole_key_filtering=false
|
||||
max_bytes_for_level_base=268435456
|
||||
last_level_temperature=kUnknown
|
||||
compaction_options_fifo={file_temperature_age_thresholds=;allow_compaction=false;age_for_warm=0;max_table_files_size=1073741824;}
|
||||
max_bytes_for_level_multiplier=10.000000
|
||||
compression_per_level=kNoCompression:kNoCompression:kLZ4HCCompression:kLZ4HCCompression:kLZ4HCCompression
|
||||
max_bytes_for_level_multiplier_additional=1:1:1:1:1:1:1
|
||||
max_sequential_skip_in_iterations=8
|
||||
prepopulate_blob_cache=kDisable
|
||||
compression=kSnappyCompression
|
||||
compaction_options_universal={incremental=false;compression_size_percent=-1;allow_trivial_move=false;max_size_amplification_percent=200;max_merge_width=4294967295;stop_style=kCompactionStopStyleTotalSize;min_merge_width=2;size_ratio=1;}
|
||||
blob_garbage_collection_age_cutoff=0.250000
|
||||
ttl=2592000
|
||||
periodic_compaction_seconds=0
|
||||
sample_for_compression=0
|
||||
blob_file_size=268435456
|
||||
enable_blob_garbage_collection=false
|
||||
experimental_mempurge_threshold=0.000000
|
||||
bottommost_compression=kDisableCompressionOption
|
||||
persist_user_defined_timestamps=true
|
||||
preserve_internal_time_seconds=0
|
||||
preclude_last_level_data_seconds=0
|
||||
sst_partitioner_factory=nullptr
|
||||
num_levels=7
|
||||
force_consistency_checks=true
|
||||
memtable_insert_with_hint_prefix_extractor=nullptr
|
||||
memtable_factory=SkipListFactory
|
||||
compaction_pri=kMinOverlappingRatio
|
||||
max_write_buffer_size_to_maintain=8589934592
|
||||
level_compaction_dynamic_file_size=true
|
||||
max_write_buffer_number_to_maintain=0
|
||||
optimize_filters_for_hits=false
|
||||
table_properties_collectors={{id=CompactOnDeletionCollector;deletion_ratio=0.500000;deletion_trigger=50;window_size=1000;}}
|
||||
level_compaction_dynamic_level_bytes=true
|
||||
default_temperature=kUnknown
|
||||
inplace_update_support=false
|
||||
merge_operator=nullptr
|
||||
table_factory=BlockBasedTable
|
||||
min_write_buffer_number_to_merge=4
|
||||
compaction_filter=nullptr
|
||||
compaction_style=kCompactionStyleLevel
|
||||
bloom_locality=0
|
||||
comparator=leveldb.BytewiseComparator
|
||||
compaction_filter_factory=nullptr
|
||||
|
||||
[TableOptions/BlockBasedTable "default"]
|
||||
initial_auto_readahead_size=8192
|
||||
pin_top_level_index_and_filter=true
|
||||
block_align=false
|
||||
block_size_deviation=10
|
||||
checksum=kXXH3
|
||||
index_shortening=kShortenSeparators
|
||||
num_file_reads_for_auto_readahead=2
|
||||
whole_key_filtering=true
|
||||
data_block_index_type=kDataBlockBinarySearch
|
||||
index_type=kBinarySearch
|
||||
no_block_cache=false
|
||||
index_block_restart_interval=1
|
||||
data_block_hash_table_util_ratio=0.750000
|
||||
prepopulate_block_cache=kDisable
|
||||
pin_l0_filter_and_index_blocks_in_cache=false
|
||||
filter_policy=nullptr
|
||||
cache_index_and_filter_blocks_with_high_priority=true
|
||||
verify_compression=false
|
||||
block_restart_interval=16
|
||||
max_auto_readahead_size=262144
|
||||
flush_block_policy_factory=FlushBlockBySizePolicyFactory
|
||||
partition_filters=false
|
||||
cache_index_and_filter_blocks=false
|
||||
block_size=4096
|
||||
metadata_block_size=4096
|
||||
optimize_filters_for_memory=false
|
||||
detect_filter_construct_corruption=false
|
||||
format_version=5
|
||||
metadata_cache_options={unpartitioned_pinning=kFallback;partition_pinning=kFallback;top_level_index_pinning=kFallback;}
|
||||
read_amp_bytes_per_bit=0
|
||||
enable_index_compression=true
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
# This is a RocksDB option file.
|
||||
#
|
||||
# For detailed file format spec, please refer to the example file
|
||||
# in examples/rocksdb_option_file_example.ini
|
||||
#
|
||||
|
||||
[Version]
|
||||
rocksdb_version=8.10.0
|
||||
options_file_version=1.1
|
||||
|
||||
[DBOptions]
|
||||
compaction_readahead_size=2097152
|
||||
strict_bytes_per_sync=false
|
||||
bytes_per_sync=0
|
||||
max_background_jobs=32
|
||||
avoid_flush_during_shutdown=false
|
||||
max_background_flushes=-1
|
||||
delayed_write_rate=16777216
|
||||
max_open_files=-1
|
||||
max_subcompactions=1
|
||||
writable_file_max_buffer_size=1048576
|
||||
wal_bytes_per_sync=0
|
||||
max_background_compactions=-1
|
||||
max_total_wal_size=0
|
||||
delete_obsolete_files_period_micros=21600000000
|
||||
stats_dump_period_sec=600
|
||||
stats_history_buffer_size=1048576
|
||||
stats_persist_period_sec=600
|
||||
enforce_single_del_contracts=true
|
||||
lowest_used_cache_tier=kNonVolatileBlockTier
|
||||
bgerror_resume_retry_interval=1000000
|
||||
best_efforts_recovery=false
|
||||
log_readahead_size=0
|
||||
write_dbid_to_manifest=false
|
||||
wal_compression=kNoCompression
|
||||
manual_wal_flush=false
|
||||
db_host_id=__hostname__
|
||||
two_write_queues=false
|
||||
random_access_max_buffer_size=1048576
|
||||
avoid_unnecessary_blocking_io=false
|
||||
skip_checking_sst_file_sizes_on_db_open=false
|
||||
flush_verify_memtable_count=true
|
||||
fail_if_options_file_error=true
|
||||
atomic_flush=false
|
||||
verify_sst_unique_id_in_manifest=true
|
||||
skip_stats_update_on_db_open=false
|
||||
track_and_verify_wals_in_manifest=false
|
||||
compaction_verify_record_count=true
|
||||
paranoid_checks=true
|
||||
create_if_missing=true
|
||||
max_write_batch_group_size_bytes=1048576
|
||||
avoid_flush_during_recovery=false
|
||||
file_checksum_gen_factory=nullptr
|
||||
enable_thread_tracking=false
|
||||
allow_fallocate=true
|
||||
allow_data_in_errors=false
|
||||
error_if_exists=false
|
||||
use_direct_io_for_flush_and_compaction=false
|
||||
create_missing_column_families=true
|
||||
WAL_size_limit_MB=0
|
||||
use_direct_reads=false
|
||||
persist_stats_to_disk=false
|
||||
allow_2pc=false
|
||||
is_fd_close_on_exec=true
|
||||
max_log_file_size=0
|
||||
access_hint_on_compaction_start=NORMAL
|
||||
max_file_opening_threads=16
|
||||
wal_filter=nullptr
|
||||
allow_mmap_reads=false
|
||||
allow_mmap_writes=false
|
||||
use_adaptive_mutex=false
|
||||
use_fsync=false
|
||||
table_cache_numshardbits=6
|
||||
dump_malloc_stats=false
|
||||
db_write_buffer_size=0
|
||||
allow_ingest_behind=false
|
||||
keep_log_file_num=20
|
||||
max_bgerror_resume_count=2147483647
|
||||
allow_concurrent_memtable_write=true
|
||||
recycle_log_file_num=0
|
||||
log_file_time_to_roll=0
|
||||
manifest_preallocation_size=4194304
|
||||
enable_write_thread_adaptive_yield=true
|
||||
WAL_ttl_seconds=0
|
||||
max_manifest_file_size=1073741824
|
||||
wal_recovery_mode=kPointInTimeRecovery
|
||||
enable_pipelined_write=true
|
||||
write_thread_slow_yield_usec=3
|
||||
unordered_write=false
|
||||
write_thread_max_yield_usec=100
|
||||
advise_random_on_open=true
|
||||
info_log_level=WARN_LEVEL
|
||||
|
||||
|
||||
[CFOptions "default"]
|
||||
memtable_max_range_deletions=0
|
||||
compression_opts={checksum=false;max_dict_buffer_bytes=0;enabled=false;max_dict_bytes=0;max_compressed_bytes_per_kb=896;parallel_threads=1;zstd_max_train_bytes=0;level=32767;use_zstd_dict_trainer=true;strategy=0;window_bits=-14;}
|
||||
block_protection_bytes_per_key=0
|
||||
bottommost_file_compaction_delay=0
|
||||
memtable_protection_bytes_per_key=0
|
||||
target_file_size_multiplier=1
|
||||
report_bg_io_stats=false
|
||||
write_buffer_size=268435456
|
||||
memtable_huge_page_size=0
|
||||
max_successive_merges=0
|
||||
max_write_buffer_number=32
|
||||
prefix_extractor=nullptr
|
||||
bottommost_compression_opts={checksum=false;max_dict_buffer_bytes=0;enabled=false;max_dict_bytes=0;max_compressed_bytes_per_kb=896;parallel_threads=1;zstd_max_train_bytes=0;level=32767;use_zstd_dict_trainer=true;strategy=0;window_bits=-14;}
|
||||
paranoid_file_checks=false
|
||||
blob_garbage_collection_force_threshold=1.000000
|
||||
enable_blob_files=true
|
||||
blob_file_starting_level=0
|
||||
memtable_prefix_bloom_size_ratio=0.000000
|
||||
inplace_update_num_locks=10000
|
||||
blob_compaction_readahead_size=0
|
||||
ignore_max_compaction_bytes_for_input=true
|
||||
arena_block_size=1048576
|
||||
level0_stop_writes_trigger=36
|
||||
blob_compression_type=kNoCompression
|
||||
level0_slowdown_writes_trigger=20
|
||||
hard_pending_compaction_bytes_limit=274877906944
|
||||
soft_pending_compaction_bytes_limit=68719476736
|
||||
target_file_size_base=67108864
|
||||
level0_file_num_compaction_trigger=4
|
||||
max_compaction_bytes=1677721600
|
||||
disable_auto_compactions=false
|
||||
check_flush_compaction_key_order=true
|
||||
min_blob_size=4096
|
||||
memtable_whole_key_filtering=false
|
||||
max_bytes_for_level_base=268435456
|
||||
last_level_temperature=kUnknown
|
||||
compaction_options_fifo={file_temperature_age_thresholds=;allow_compaction=false;age_for_warm=0;max_table_files_size=1073741824;}
|
||||
max_bytes_for_level_multiplier=10.000000
|
||||
compression_per_level=kNoCompression:kNoCompression:kLZ4HCCompression:kLZ4HCCompression:kLZ4HCCompression
|
||||
max_bytes_for_level_multiplier_additional=1:1:1:1:1:1:1
|
||||
max_sequential_skip_in_iterations=8
|
||||
prepopulate_blob_cache=kDisable
|
||||
compression=kSnappyCompression
|
||||
compaction_options_universal={incremental=false;compression_size_percent=-1;allow_trivial_move=false;max_size_amplification_percent=200;max_merge_width=4294967295;stop_style=kCompactionStopStyleTotalSize;min_merge_width=2;size_ratio=1;}
|
||||
blob_garbage_collection_age_cutoff=0.250000
|
||||
ttl=2592000
|
||||
periodic_compaction_seconds=0
|
||||
sample_for_compression=0
|
||||
blob_file_size=268435456
|
||||
enable_blob_garbage_collection=false
|
||||
experimental_mempurge_threshold=0.000000
|
||||
bottommost_compression=kDisableCompressionOption
|
||||
persist_user_defined_timestamps=true
|
||||
preserve_internal_time_seconds=0
|
||||
preclude_last_level_data_seconds=0
|
||||
sst_partitioner_factory=nullptr
|
||||
num_levels=7
|
||||
force_consistency_checks=true
|
||||
memtable_insert_with_hint_prefix_extractor=nullptr
|
||||
memtable_factory=SkipListFactory
|
||||
compaction_pri=kMinOverlappingRatio
|
||||
max_write_buffer_size_to_maintain=8589934592
|
||||
level_compaction_dynamic_file_size=true
|
||||
max_write_buffer_number_to_maintain=0
|
||||
optimize_filters_for_hits=false
|
||||
table_properties_collectors={{id=CompactOnDeletionCollector;deletion_ratio=0.500000;deletion_trigger=50;window_size=1000;}}
|
||||
level_compaction_dynamic_level_bytes=true
|
||||
default_temperature=kUnknown
|
||||
inplace_update_support=false
|
||||
merge_operator=nullptr
|
||||
table_factory=BlockBasedTable
|
||||
min_write_buffer_number_to_merge=4
|
||||
compaction_filter=nullptr
|
||||
compaction_style=kCompactionStyleLevel
|
||||
bloom_locality=0
|
||||
comparator=leveldb.BytewiseComparator
|
||||
compaction_filter_factory=nullptr
|
||||
|
||||
[TableOptions/BlockBasedTable "default"]
|
||||
initial_auto_readahead_size=8192
|
||||
pin_top_level_index_and_filter=true
|
||||
block_align=false
|
||||
block_size_deviation=10
|
||||
checksum=kXXH3
|
||||
index_shortening=kShortenSeparators
|
||||
num_file_reads_for_auto_readahead=2
|
||||
whole_key_filtering=true
|
||||
data_block_index_type=kDataBlockBinarySearch
|
||||
index_type=kBinarySearch
|
||||
no_block_cache=false
|
||||
index_block_restart_interval=1
|
||||
data_block_hash_table_util_ratio=0.750000
|
||||
prepopulate_block_cache=kDisable
|
||||
pin_l0_filter_and_index_blocks_in_cache=false
|
||||
filter_policy=nullptr
|
||||
cache_index_and_filter_blocks_with_high_priority=true
|
||||
verify_compression=false
|
||||
block_restart_interval=16
|
||||
max_auto_readahead_size=262144
|
||||
flush_block_policy_factory=FlushBlockBySizePolicyFactory
|
||||
partition_filters=false
|
||||
cache_index_and_filter_blocks=false
|
||||
block_size=4096
|
||||
metadata_block_size=4096
|
||||
optimize_filters_for_memory=false
|
||||
detect_filter_construct_corruption=false
|
||||
format_version=5
|
||||
metadata_cache_options={unpartitioned_pinning=kFallback;partition_pinning=kFallback;top_level_index_pinning=kFallback;}
|
||||
read_amp_bytes_per_bit=0
|
||||
enable_index_compression=true
|
||||
|
||||
Generated
+5057
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "chatapp-backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rocket = { version = "0.5.0-rc.3", features = ["json", "secrets", "tls"] }
|
||||
rocket_ws = "0.1.0"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
futures = "0.3"
|
||||
surrealdb = "2.1.2"
|
||||
chrono = "0.4.38"
|
||||
rand = "0.8.5"
|
||||
sha2 = "0.10.8"
|
||||
@@ -0,0 +1,26 @@
|
||||
DEFINE FUNCTION friend::request($from: uuid, $to: uuid) {
|
||||
CREATE FriendRequest SET
|
||||
in = Entity:from,
|
||||
out = Entity:to,
|
||||
created = time::now(),
|
||||
}
|
||||
|
||||
DEFINE FUNCTION friend::accept($request: record<FriendRequest>) {
|
||||
|
||||
LET $fsid = uuid::new();
|
||||
|
||||
CREATE Friendship SET
|
||||
dm_channel = channel::new(),
|
||||
id = $fsid,
|
||||
since = time::now(),
|
||||
|
||||
CREATE HasFriendShip SET
|
||||
in = Entity:request.in,
|
||||
out = Friendship:fsid,
|
||||
nickname = Entity:request.out.displayname,
|
||||
|
||||
CREATE HasFriendShip SET
|
||||
in = Entity:request.out,
|
||||
out = Friendship:fsid,
|
||||
nickname = Entity:request.in.displayname,
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
DEFINE FUNCTION server::join($server_id: uuid, $entity_id: uuid) {
|
||||
|
||||
LET $user = (SELECT displayname FROM Entity WHERE id = $entity_id)[0];
|
||||
|
||||
CREATE HasServer SET
|
||||
in = Entity:entity_id,
|
||||
out = Server:server_id,
|
||||
nickname = $user.displayname,
|
||||
permissions = [],
|
||||
joined = time::now(),
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use rand::{thread_rng, Rng};
|
||||
use rocket::{http::{CookieJar, Status}, options, post, request::{FromRequest, Outcome}, serde::json::Json, Request};
|
||||
use serde::Deserialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use surrealdb::RecordId;
|
||||
|
||||
use crate::{database::DB, user::{
|
||||
User, AuthResponse
|
||||
}};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UserLoginForm {
|
||||
username: String,
|
||||
password: String
|
||||
}
|
||||
|
||||
#[options("/login")]
|
||||
pub fn login_options() -> Status {
|
||||
Status::Ok
|
||||
}
|
||||
|
||||
|
||||
#[post("/login", data = "<user>")]
|
||||
pub async fn login(user: Json<UserLoginForm>, jar: &CookieJar<'_>) -> Status {
|
||||
|
||||
println!("Logging in: {}", user.username);
|
||||
|
||||
if let Ok(response) = User::authenticate(user.username.clone(), user.password.clone()).await {
|
||||
if response.matches {
|
||||
|
||||
let token = SessionToken::new(response.user_id).await;
|
||||
jar.add_private(("auth", token.token));
|
||||
println!("success!");
|
||||
|
||||
return Status::Ok
|
||||
} else {
|
||||
println!("does not match");
|
||||
}
|
||||
} else {
|
||||
println!("response err");
|
||||
}
|
||||
|
||||
println!("failed!");
|
||||
return Status::Unauthorized
|
||||
}
|
||||
|
||||
#[options("/signup")]
|
||||
pub fn signup_options() -> Status {
|
||||
Status::Ok
|
||||
}
|
||||
|
||||
#[post("/signup", data = "<user>")]
|
||||
pub async fn signup(user: Json<UserLoginForm>, jar: &CookieJar<'_>) -> Status {
|
||||
|
||||
println!("signing up: {}", user.username);
|
||||
|
||||
User::create(user.username.clone(), user.password.clone()).await;
|
||||
|
||||
login(user, jar).await
|
||||
}
|
||||
|
||||
pub type SessionTokenGuard = User;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SessionToken {
|
||||
token: String,
|
||||
created_at: i64,
|
||||
expires_at: i64,
|
||||
user_id: RecordId,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for SessionTokenGuard {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||
if let Some(cookie) = req.cookies().get_private("auth") {
|
||||
let token = cookie.value().to_string();
|
||||
return match DB
|
||||
.query("
|
||||
SELECT * FROM User WHERE user_id = (SELECT user_id FROM SessionToken WHERE token = $1)[0]
|
||||
")
|
||||
.bind(("token", token))
|
||||
.await
|
||||
.unwrap()
|
||||
.take::<Option<User>>(0)
|
||||
{
|
||||
Ok(Some(user)) => Outcome::Success(user),
|
||||
_ => Outcome::Error((rocket::http::Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
Outcome::Error((rocket::http::Status::Unauthorized, ()))
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionToken {
|
||||
pub async fn new(user_id: RecordId) -> SessionToken {
|
||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
||||
let expiry = Duration::from_secs(7 * 24 * 60 * 60);
|
||||
let random_value: u32 = thread_rng().gen();
|
||||
let token = format!("{}-{}", current_time.as_secs(), random_value);
|
||||
let hashed = format!("{:x}", Sha256::digest(token.as_bytes()));
|
||||
|
||||
println!("{}", hashed);
|
||||
|
||||
let token = SessionToken {
|
||||
token: hashed,
|
||||
created_at: current_time.as_secs() as i64,
|
||||
expires_at: (current_time + expiry).as_secs() as i64,
|
||||
user_id,
|
||||
};
|
||||
|
||||
let tok = token.clone();
|
||||
DB
|
||||
.query("
|
||||
CREATE SessionToken SET
|
||||
token = $token,
|
||||
created_at = $created_at,
|
||||
expires_at = $expires_at,
|
||||
user_id = $user_id
|
||||
")
|
||||
.bind(("token", tok.token))
|
||||
.bind(("created_at", tok.created_at))
|
||||
.bind(("expires_at", tok.expires_at))
|
||||
.bind(("user_id", tok.user_id))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
token
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use surrealdb::{engine::remote::ws::{Client, Ws}, opt::auth::Root, Surreal};
|
||||
|
||||
pub static DB: LazyLock<Surreal<Client>> = LazyLock::new(|| Surreal::init());
|
||||
|
||||
pub async fn init() -> Result<(), surrealdb::Error> {
|
||||
DB.connect::<Ws>("localhost:8001").await?;
|
||||
|
||||
DB.signin(Root {
|
||||
username: "root",
|
||||
password: "root",
|
||||
}).await?;
|
||||
|
||||
DB.use_ns("database").use_db("database").await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use auth::{login, login_options, signup, signup_options};
|
||||
use rocket::{fs::FileServer, launch, routes};
|
||||
use std::sync::Arc;
|
||||
use rocket::tokio::sync::Mutex;
|
||||
|
||||
mod messenger;
|
||||
mod auth;
|
||||
mod user;
|
||||
mod database;
|
||||
|
||||
use messenger::{MessengerServer, connect};
|
||||
|
||||
#[launch]
|
||||
async fn rocket() -> _ {
|
||||
let messenger = Arc::new(Mutex::new(MessengerServer::new()));
|
||||
|
||||
database::init().await.unwrap();
|
||||
|
||||
rocket::build()
|
||||
.manage(messenger)
|
||||
.mount("/", routes![
|
||||
connect,
|
||||
login,
|
||||
signup,
|
||||
login_options,
|
||||
signup_options,
|
||||
])
|
||||
.mount("/static", FileServer::from("static"))
|
||||
.attach(rocket::fairing::AdHoc::on_response("CORS", |_, res| Box::pin(async move {
|
||||
res.set_header(rocket::http::Header::new(
|
||||
"Access-Control-Allow-Origin",
|
||||
"*"
|
||||
));
|
||||
res.set_header(rocket::http::Header::new(
|
||||
"Access-Control-Allow-Methods",
|
||||
"GET, POST, OPTIONS"
|
||||
));
|
||||
res.set_header(rocket::http::Header::new(
|
||||
"Access-Control-Allow-Headers",
|
||||
"*"
|
||||
));
|
||||
})))
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
use std::{collections::HashMap, sync::Arc, time::{SystemTime, UNIX_EPOCH}};
|
||||
|
||||
use rocket::{
|
||||
futures::{
|
||||
channel::mpsc,
|
||||
stream::{SplitSink, SplitStream},
|
||||
SinkExt,
|
||||
StreamExt
|
||||
},
|
||||
tokio::sync::Mutex,
|
||||
serde::json::to_string,
|
||||
get,
|
||||
http::Status,
|
||||
Shutdown
|
||||
};
|
||||
|
||||
use serde::Serialize;
|
||||
use rocket_ws::{Channel, WebSocket, stream::DuplexStream};
|
||||
use surrealdb::{RecordId, Uuid};
|
||||
|
||||
use crate::{auth::SessionTokenGuard, user::User};
|
||||
|
||||
|
||||
|
||||
|
||||
#[get("/messenger/connect/<channel_id>")]
|
||||
pub async fn connect<'r> (
|
||||
user: SessionTokenGuard,
|
||||
ws: WebSocket,
|
||||
messenger: &'r rocket::State<Arc<Mutex<MessengerServer>>>,
|
||||
channel_id: i32,
|
||||
mut shutdown: Shutdown,
|
||||
) -> Result<Channel<'r>, Status> {
|
||||
|
||||
let messenger = Arc::clone(messenger.inner());
|
||||
|
||||
Ok(ws.channel(move | channel| {
|
||||
Box::pin(async move {
|
||||
|
||||
let (sender, receiver) = mpsc::channel::<RealTimeMessage>(100);
|
||||
let (ws_sender, ws_receiver) = channel.split();
|
||||
|
||||
println!("REGISTERING: {}", user.id);
|
||||
messenger.lock().await.register(user.id.clone(), channel_id, sender);
|
||||
|
||||
tokio::select! {
|
||||
_ = inbound_message(ws_receiver, messenger.clone(), channel_id, &user) => {},
|
||||
_ = outbound_message(ws_sender, receiver) => {},
|
||||
_ = &mut shutdown => {},
|
||||
}
|
||||
|
||||
// Once the client disconnects, or the server is shutdown they are deregistered from the channel.
|
||||
println!("DEREGISTERING: {}", user.id);
|
||||
messenger.lock().await.deregister(user.id);
|
||||
Ok(())
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pub async fn inbound_message(
|
||||
mut ws_receiver: SplitStream<DuplexStream>,
|
||||
messenger: Arc<Mutex<MessengerServer>>,
|
||||
channel_id: i32,
|
||||
user: &User,
|
||||
) {
|
||||
while let Some(Ok(msg)) = ws_receiver.next().await {
|
||||
if let rocket_ws::Message::Text(text) = msg {
|
||||
|
||||
let message = RealTimeMessage {
|
||||
message_id: 0,
|
||||
user_id: user.id.key().to_string(),
|
||||
display_name: user.username.clone(),
|
||||
created_at: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as i64,
|
||||
content: text,
|
||||
};
|
||||
|
||||
messenger.lock().await.send(channel_id, message).await;
|
||||
} else if let rocket_ws::Message::Binary(b) = msg {
|
||||
println!("recieved binary message: {}", String::from_utf8(b).unwrap());
|
||||
} else {
|
||||
println!("OTHER");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn outbound_message(
|
||||
mut ws_sender: SplitSink<DuplexStream, rocket_ws::Message>,
|
||||
mut receiver: mpsc::Receiver<RealTimeMessage>
|
||||
) {
|
||||
while let Some(msg) = receiver.next().await {
|
||||
if let Err(e) = ws_sender.send(to_string(&msg).unwrap().into()).await {
|
||||
println!("Failed to send message to client\nError: {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type UserId = RecordId;
|
||||
type ChannelId = i32;
|
||||
|
||||
pub struct MessengerServer {
|
||||
pub channels: HashMap<i32, HashMap<UserId, mpsc::Sender<RealTimeMessage>>> // map of the channel id to the channel object
|
||||
}
|
||||
|
||||
impl MessengerServer {
|
||||
pub fn new() -> MessengerServer {
|
||||
MessengerServer {
|
||||
channels: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, user_id: UserId, channel_id: ChannelId, sender: mpsc::Sender<RealTimeMessage>) {
|
||||
if let Some(channel) = self.channels.get_mut(&channel_id) {
|
||||
channel.insert(user_id, sender);
|
||||
} else {
|
||||
self.channels.insert(channel_id, HashMap::from([(user_id, sender)]));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deregister(&mut self, user_id: UserId) {
|
||||
for (_, channel) in self.channels.iter_mut() {
|
||||
channel.remove(&user_id);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(&mut self, channel_id: ChannelId, msg: RealTimeMessage) {
|
||||
if let Some(channel) = self.channels.get_mut(&channel_id) {
|
||||
for (_, sender) in channel.iter_mut() {
|
||||
if let Err(e) = sender.send(msg.clone()).await {
|
||||
println!("Failed to send message to channel {}\nError: {}", channel_id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct RealTimeMessage {
|
||||
pub message_id: i32,
|
||||
pub user_id: String,
|
||||
pub display_name: String,
|
||||
pub created_at: i64,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::RecordId;
|
||||
use crate::database::DB;
|
||||
|
||||
use chrono::Utc;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct User {
|
||||
pub id: RecordId,
|
||||
pub username: String,
|
||||
pub passhash: String,
|
||||
pub displayname: String,
|
||||
pub joined: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AuthResponse {
|
||||
pub matches: bool,
|
||||
pub user_id: RecordId,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub async fn authenticate(username: String, password: String) -> Result<AuthResponse, ()> {
|
||||
|
||||
match DB
|
||||
.query("
|
||||
LET $user = (SELECT id, passhash FROM User WHERE username = $username)[0];
|
||||
RETURN {
|
||||
matches: crypto::argon2::compare($user.passhash, $password),
|
||||
user_id: $user.id
|
||||
}"
|
||||
)
|
||||
.bind(("username", username))
|
||||
.bind(("password", password))
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.take::<Option<AuthResponse>>(0)
|
||||
{
|
||||
Ok(Some(response)) => Ok(response),
|
||||
Ok(None) => {
|
||||
println!("User not found");
|
||||
Err(())
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error authenticating user: {}", e);
|
||||
Err(())
|
||||
}
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create(username: String, password: String) -> Result<String, ()> {
|
||||
match DB
|
||||
.query("
|
||||
CREATE User:uuid() SET
|
||||
username = $username,
|
||||
displayname = $displayname,
|
||||
passhash = crypto::argon2::generate($passhash),
|
||||
joined = $joined;
|
||||
SELECT * FROM User WHERE username = $username
|
||||
")
|
||||
.bind(("username", username.clone()))
|
||||
.bind(("passhash", password))
|
||||
.bind(("displayname", username))
|
||||
.bind(("joined", Utc::now().timestamp()))
|
||||
.await
|
||||
.unwrap()
|
||||
.take::<Option<User>>(0)
|
||||
{
|
||||
Ok(Some(user)) => {
|
||||
let k = user.id.key().to_string();
|
||||
println!("Created User: {}", k);
|
||||
Ok(k)
|
||||
},
|
||||
_ => Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 696 KiB |
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+1378
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "chatapp-frontend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
yew = { version = "0.21", features = ["csr"] }
|
||||
yew-router = "0.18"
|
||||
gloo = { version = "0.10", features = ["futures", "storage"] }
|
||||
wasm-bindgen = "0.2"
|
||||
wasm-bindgen-futures = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
gloo-net = "0.4"
|
||||
web-sys = { version = "0.3", features = [
|
||||
"WebSocket",
|
||||
"MessageEvent",
|
||||
"ErrorEvent",
|
||||
"Event",
|
||||
"CloseEvent",
|
||||
"Window",
|
||||
"Location",
|
||||
"History"
|
||||
]}
|
||||
futures = "0.3"
|
||||
chrono = "0.4"
|
||||
+1095
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Vendored
+153
@@ -0,0 +1,153 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Chat App</title>
|
||||
<link rel="stylesheet" href="/styles-a0f557a2c187e84.css" integrity="sha384-IY1p3eyyGzgt1dx8oous21mdUqg5fUoBJDo0cnEt0Dzc0zNGKH5Z/fRO8S/o/z+X"/>
|
||||
<link rel="modulepreload" href="/chatapp-frontend-2d9721327d80d1a4.js" crossorigin=anonymous integrity="sha384-NUMxiXeKeAJvxebMe8KYjMxliEtODVyz5/Z481fZRPDW2uYnad9e2ra9tGdLMFpa"><link rel="preload" href="/chatapp-frontend-2d9721327d80d1a4_bg.wasm" crossorigin=anonymous integrity="sha384-2mAsbkb1UNVlDtYPNO/P2i3j3N0eQcSD4sqqXRdf0Y5CtCnprvY5LjoqgSeLoJul" as="fetch" type="application/wasm"></head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
<script type="module" nonce="2WTPFE+DH5nuGYeLSROE+A==">
|
||||
import init, * as bindings from '/chatapp-frontend-2d9721327d80d1a4.js';
|
||||
const wasm = await init('/chatapp-frontend-2d9721327d80d1a4_bg.wasm');
|
||||
|
||||
|
||||
window.wasmBindings = bindings;
|
||||
|
||||
|
||||
dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));
|
||||
|
||||
</script><script>"use strict";
|
||||
|
||||
(function () {
|
||||
|
||||
const address = '{{__TRUNK_ADDRESS__}}';
|
||||
const base = '{{__TRUNK_WS_BASE__}}';
|
||||
let protocol = '';
|
||||
protocol =
|
||||
protocol
|
||||
? protocol
|
||||
: window.location.protocol === 'https:'
|
||||
? 'wss'
|
||||
: 'ws';
|
||||
const url = protocol + '://' + address + base + '.well-known/trunk/ws';
|
||||
|
||||
class Overlay {
|
||||
constructor() {
|
||||
// create an overlay
|
||||
this._overlay = document.createElement("div");
|
||||
const style = this._overlay.style;
|
||||
style.height = "100vh";
|
||||
style.width = "100vw";
|
||||
style.position = "fixed";
|
||||
style.top = "0";
|
||||
style.left = "0";
|
||||
style.backgroundColor = "rgba(222, 222, 222, 0.5)";
|
||||
style.fontFamily = "sans-serif";
|
||||
// not sure that's the right approach
|
||||
style.zIndex = "1000000";
|
||||
style.backdropFilter = "blur(1rem)";
|
||||
|
||||
const container = document.createElement("div");
|
||||
// center it
|
||||
container.style.position = "absolute";
|
||||
container.style.top = "30%";
|
||||
container.style.left = "15%";
|
||||
container.style.maxWidth = "85%";
|
||||
|
||||
this._title = document.createElement("div");
|
||||
this._title.innerText = "Build failure";
|
||||
this._title.style.paddingBottom = "2rem";
|
||||
this._title.style.fontSize = "2.5rem";
|
||||
|
||||
this._message = document.createElement("div");
|
||||
this._message.style.whiteSpace = "pre-wrap";
|
||||
|
||||
const icon= document.createElement("div");
|
||||
icon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" fill="#dc3545" viewBox="0 0 16 16"><path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/></svg>';
|
||||
this._title.prepend(icon);
|
||||
|
||||
container.append(this._title, this._message);
|
||||
this._overlay.append(container);
|
||||
|
||||
this._inject();
|
||||
window.setInterval(() => {
|
||||
this._inject();
|
||||
}, 250);
|
||||
}
|
||||
|
||||
set reason(reason) {
|
||||
this._message.textContent = reason;
|
||||
}
|
||||
|
||||
_inject() {
|
||||
if (!this._overlay.isConnected) {
|
||||
// prepend it
|
||||
document.body?.prepend(this._overlay);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Client {
|
||||
constructor(url) {
|
||||
this.url = url;
|
||||
this.poll_interval = 5000;
|
||||
this._overlay = null;
|
||||
}
|
||||
|
||||
start() {
|
||||
const ws = new WebSocket(this.url);
|
||||
ws.onmessage = (ev) => {
|
||||
const msg = JSON.parse(ev.data);
|
||||
switch (msg.type) {
|
||||
case "reload":
|
||||
this.reload();
|
||||
break;
|
||||
case "buildFailure":
|
||||
this.buildFailure(msg.data)
|
||||
break;
|
||||
}
|
||||
};
|
||||
ws.onclose = this.onclose;
|
||||
}
|
||||
|
||||
onclose() {
|
||||
window.setTimeout(
|
||||
() => {
|
||||
// when we successfully reconnect, we'll force a
|
||||
// reload (since we presumably lost connection to
|
||||
// trunk due to it being killed, so it will have
|
||||
// rebuilt on restart)
|
||||
const ws = new WebSocket(this.url);
|
||||
ws.onopen = () => window.location.reload();
|
||||
ws.onclose = this.onclose;
|
||||
},
|
||||
this.poll_interval);
|
||||
}
|
||||
|
||||
reload() {
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
buildFailure({reason}) {
|
||||
// also log the console
|
||||
console.error("Build failed:", reason);
|
||||
|
||||
console.debug("Overlay", this._overlay);
|
||||
|
||||
if (!this._overlay) {
|
||||
this._overlay = new Overlay();
|
||||
}
|
||||
this._overlay.reason = reason;
|
||||
}
|
||||
}
|
||||
|
||||
new Client(url).start();
|
||||
|
||||
})()
|
||||
</script></body>
|
||||
</html>
|
||||
+311
@@ -0,0 +1,311 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
--blur-amount: 10px;
|
||||
--border-radius: 15px;
|
||||
--glow-color: rgba(255, 255, 255, 0.3);
|
||||
--message-bubble-color: rgba(255, 255, 255, 0.1);
|
||||
--border-color: rgba(255, 255, 255, 0.1);
|
||||
--shadow-color: rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100vh;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
background: linear-gradient(135deg, #1a2a6c, #2a4858, #141E30);
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.app-container {
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-container {
|
||||
flex: 1;
|
||||
margin: 0 2rem;
|
||||
padding: 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
max-width: 1400px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
margin: 0 0;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.profile-picture {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
background-size: cover !important;
|
||||
background-position: center !important;
|
||||
background-repeat: no-repeat !important;
|
||||
border-radius: 5px;
|
||||
flex-shrink: 0;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.message-bubble {
|
||||
background: var(--message-bubble-color);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
padding: 0.75rem;
|
||||
border-radius: 0 var(--border-radius) var(--border-radius) var(--border-radius);
|
||||
flex-grow: 1;
|
||||
position: relative;
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 2px 12px var(--shadow-color);
|
||||
}
|
||||
|
||||
.message-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-weight: 600;
|
||||
font-size: 0.9rem;
|
||||
color: #fff;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
}
|
||||
|
||||
.timestamp {
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.message-form {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.message-input {
|
||||
flex: 1;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
resize: vertical;
|
||||
min-height: 1.5rem;
|
||||
max-height: 150px;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.message-input:hover, .message-input:focus {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.send-button {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.send-button:hover {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.navbar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1rem 2rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border: none;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.nav-brand {
|
||||
font-size: 1.5rem;
|
||||
font-weight: bold;
|
||||
color: white;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
}
|
||||
|
||||
/* Login styles */
|
||||
.login-container {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 90%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
padding: 2rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
color: white;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.login-input {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.login-input:hover, .login-input:focus {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.login-input::placeholder {
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
}
|
||||
|
||||
.login-button {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.login-button:hover {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.login-text {
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
text-align: center;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.login-link {
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.login-link:hover {
|
||||
text-shadow: 0 0 10px var(--glow-color);
|
||||
}
|
||||
|
||||
.login-error {
|
||||
background: rgba(255, 0, 0, 0.1);
|
||||
border: 1px solid rgba(255, 0, 0, 0.3);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
text-align: center;
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
/* Custom scrollbar */
|
||||
.messages-container::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-thumb:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Chat App</title>
|
||||
<link data-trunk rel="css" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,99 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use yew::prelude::*;
|
||||
use web_sys::HtmlInputElement;
|
||||
use chrono::prelude::*;
|
||||
use crate::hooks::websocket::use_websocket;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct RealTimeMessage {
|
||||
pub message_id: i32,
|
||||
pub user_id: i32,
|
||||
pub display_name: String,
|
||||
pub created_at: i64,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[function_component(Chat)]
|
||||
pub fn chat() -> Html {
|
||||
let ws = use_websocket("ws://localhost:8000/messenger/connect/1");
|
||||
let input_ref = use_node_ref();
|
||||
let dark_theme = use_state(|| true);
|
||||
|
||||
// let theme_toggle = {
|
||||
// let dark_theme = dark_theme.clone();
|
||||
// Callback::from(move |_| {
|
||||
// dark_theme.set(!*dark_theme);
|
||||
// })
|
||||
// };
|
||||
|
||||
let onsubmit = {
|
||||
let ws = ws.ws.clone();
|
||||
let input_ref = input_ref.clone();
|
||||
|
||||
Callback::from(move |e: SubmitEvent| {
|
||||
e.prevent_default();
|
||||
if let Some(input) = input_ref.cast::<HtmlInputElement>() {
|
||||
let message = input.value();
|
||||
if !message.is_empty() {
|
||||
ws.send_with_str(&message).unwrap();
|
||||
input.set_value("");
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class={classes!("app-container", if *dark_theme { "dark-theme" } else { "light-theme" })}>
|
||||
<nav class="navbar">
|
||||
<div class="nav-brand">{"Chat App"}</div>
|
||||
// <div class="theme-toggle">
|
||||
// <button onclick={theme_toggle} class="theme-button">
|
||||
// if *dark_theme {
|
||||
// {"🌞"}
|
||||
// } else {
|
||||
// {"🌙"}
|
||||
// }
|
||||
// </button>
|
||||
// </div>
|
||||
</nav>
|
||||
<div class="chat-container">
|
||||
<div class="messages-container">
|
||||
{ws.messages.messages().iter().map(|msg| {
|
||||
let timestamp = Local.timestamp_millis_opt(msg.created_at).unwrap();
|
||||
let formatted_time = timestamp.format("%d/%m/%y %H:%M").to_string();
|
||||
let userid = msg.user_id;
|
||||
|
||||
html! {
|
||||
<div class="message">
|
||||
<div class="profile-picture" style={ format!(
|
||||
"background-image: url('http://localhost:8000/static/pfp/{userid}.png')"
|
||||
)}></div>
|
||||
<div class="message-bubble">
|
||||
<div class="message-header">
|
||||
<span class="username">{&msg.display_name}</span>
|
||||
<span class="timestamp">{formatted_time}</span>
|
||||
</div>
|
||||
<div class="message-content">{&msg.content}</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}).collect::<Html>()}
|
||||
</div>
|
||||
if let Some(error) = (*ws.error).as_ref() {
|
||||
<div class="error-message">
|
||||
{format!("Error: {}", error)}
|
||||
</div>
|
||||
}
|
||||
<form {onsubmit} class="message-form">
|
||||
<input
|
||||
type="text"
|
||||
ref={input_ref}
|
||||
class="message-input"
|
||||
placeholder="Type a message..."
|
||||
/>
|
||||
<button type="submit" class="send-button">{"Send"}</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
use gloo_net::http::Request;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use yew::prelude::*;
|
||||
use yew_router::{navigator, prelude::*};
|
||||
use gloo::storage::{LocalStorage, Storage};
|
||||
use web_sys::HtmlInputElement;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
|
||||
use crate::Route;
|
||||
|
||||
#[function_component(Login)]
|
||||
pub fn login_page() -> Html {
|
||||
let navigator = use_navigator().unwrap();
|
||||
let username_ref = use_node_ref();
|
||||
let password_ref = use_node_ref();
|
||||
let login_success = use_state(|| true);
|
||||
|
||||
let navigator_clone = navigator.clone();
|
||||
let username_ref_clone = username_ref.clone();
|
||||
let password_ref_clone = password_ref.clone();
|
||||
let login_success_clone = login_success.clone();
|
||||
let onsubmit = Callback::from(move |e: SubmitEvent| {
|
||||
e.prevent_default();
|
||||
let username = username_ref_clone.cast::<HtmlInputElement>().unwrap().value();
|
||||
let password = password_ref_clone.cast::<HtmlInputElement>().unwrap().value();
|
||||
|
||||
// TODO: Replace this with actual authentication
|
||||
// For now, we'll just set a dummy token
|
||||
if !( username.is_empty() || password.is_empty() ) {
|
||||
let navigator = navigator_clone.clone();
|
||||
let login_success = login_success_clone.clone();
|
||||
spawn_local(async move {
|
||||
match login(username, password).await {
|
||||
Ok(_) => navigator.push(&Route::Chat),
|
||||
Err(_) => login_success.set(false),
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let go_to_signup = {
|
||||
let navigator_clone = navigator.clone();
|
||||
Callback::from(move |_| {
|
||||
navigator_clone.push(&Route::Signup);
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class="login-container">
|
||||
<form {onsubmit} class="login-form">
|
||||
<h2 class="login-title">{"Login"}</h2>
|
||||
<input
|
||||
ref={username_ref}
|
||||
class="login-input"
|
||||
type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
placeholder="Username"
|
||||
/>
|
||||
<input
|
||||
ref={password_ref}
|
||||
class="login-input"
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
/>
|
||||
<button class="login-button" type="submit">{"Login"}</button>
|
||||
{
|
||||
if !(*login_success) {
|
||||
html! {
|
||||
<p class="login-error">{"Incorrect username or password"}</p>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<p class="login-text">{"Don't have an account?"}</p>
|
||||
<a onclick={go_to_signup}
|
||||
href=""
|
||||
class="login-button"
|
||||
>
|
||||
{"Create Account"}
|
||||
</a>
|
||||
</form>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct LoginRequest {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
async fn login(username: String, password: String) -> Result<(), String> {
|
||||
let login_request = LoginRequest {
|
||||
username,
|
||||
password,
|
||||
};
|
||||
|
||||
match Request::post("http://127.0.0.1:8000/login")
|
||||
.json(&login_request)
|
||||
.map_err(|e| e.to_string())?
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.status()
|
||||
{
|
||||
200 => Ok(()),
|
||||
_ => Err("Login failed".to_string()),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
use gloo_net::http::Request;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use yew::{function_component, html, use_node_ref, use_state, Callback, Html, SubmitEvent};
|
||||
use yew_router::{navigator, prelude::*};
|
||||
use gloo::storage::{LocalStorage, Storage};
|
||||
use web_sys::HtmlInputElement;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
|
||||
use crate::Route;
|
||||
|
||||
#[function_component(Signup)]
|
||||
pub fn signup_page() -> Html {
|
||||
let navigator = use_navigator().unwrap();
|
||||
let username_ref = use_node_ref();
|
||||
let password_ref = use_node_ref();
|
||||
let confirm_password_ref = use_node_ref();
|
||||
let signup_error = use_state(|| None::<String>);
|
||||
|
||||
let navigator_clone = navigator.clone();
|
||||
let username_ref_clone = username_ref.clone();
|
||||
let password_ref_clone = password_ref.clone();
|
||||
let confirm_password_ref_clone = confirm_password_ref.clone();
|
||||
let signup_error_clone = signup_error.clone();
|
||||
|
||||
let onsubmit = Callback::from(move |e: SubmitEvent| {
|
||||
e.prevent_default();
|
||||
let username = username_ref_clone.cast::<HtmlInputElement>().unwrap().value();
|
||||
let password = password_ref_clone.cast::<HtmlInputElement>().unwrap().value();
|
||||
let confirm_password = confirm_password_ref_clone.cast::<HtmlInputElement>().unwrap().value();
|
||||
|
||||
if username.is_empty() || password.is_empty() {
|
||||
signup_error_clone.set(Some("Please fill in all fields".to_string()));
|
||||
return;
|
||||
}
|
||||
|
||||
if password != confirm_password {
|
||||
signup_error_clone.set(Some("Passwords do not match".to_string()));
|
||||
return;
|
||||
}
|
||||
|
||||
let navigator = navigator_clone.clone();
|
||||
let signup_error = signup_error_clone.clone();
|
||||
spawn_local(async move {
|
||||
match signup(username, password).await {
|
||||
Ok(_) => navigator.push(&Route::Chat),
|
||||
Err(e) => signup_error.set(Some(e)),
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let go_to_login = {
|
||||
let navigator = navigator.clone();
|
||||
Callback::from(move |_| {
|
||||
navigator.push(&Route::Login);
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class="login-container">
|
||||
<form {onsubmit} class="login-form">
|
||||
<h2 class="login-title">{"Sign Up"}</h2>
|
||||
<input
|
||||
ref={username_ref}
|
||||
class="login-input"
|
||||
type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
placeholder="Username"
|
||||
/>
|
||||
<input
|
||||
ref={password_ref}
|
||||
class="login-input"
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
/>
|
||||
<input
|
||||
ref={confirm_password_ref}
|
||||
class="login-input"
|
||||
type="password"
|
||||
id="confirm_password"
|
||||
name="confirm_password"
|
||||
placeholder="Confirm Password"
|
||||
/>
|
||||
<button class="login-button" type="submit">{"Sign Up"}</button>
|
||||
{
|
||||
if let Some(error) = (*signup_error).clone() {
|
||||
html! {
|
||||
<p class="login-error">{error}</p>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<p class="login-text">{"Already have an account?"}</p>
|
||||
<a onclick={go_to_login}
|
||||
href=""
|
||||
class="login-button"
|
||||
>
|
||||
{"Login"}
|
||||
</a>
|
||||
</form>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SignupRequest {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
async fn signup(username: String, password: String) -> Result<(), String> {
|
||||
let signup_request = SignupRequest {
|
||||
username,
|
||||
password,
|
||||
};
|
||||
|
||||
match Request::post("http://127.0.0.1:8000/signup")
|
||||
.json(&signup_request)
|
||||
.map_err(|e| e.to_string())?
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.status()
|
||||
{
|
||||
200 => Ok(()),
|
||||
409 => Err("Username already exists".to_string()),
|
||||
x => Err(format!("Signup failed with status code {}", x)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::{WebSocket, MessageEvent, ErrorEvent};
|
||||
use wasm_bindgen::{prelude::*, JsCast};
|
||||
use std::rc::Rc;
|
||||
use crate::components::chat::RealTimeMessage;
|
||||
|
||||
pub struct UseWebSocketHandle {
|
||||
pub ws: Rc<WebSocket>,
|
||||
pub messages: UseReducerHandle<MessagesState>,
|
||||
pub error: UseStateHandle<Option<String>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MessagesState {
|
||||
messages: Vec<RealTimeMessage>,
|
||||
}
|
||||
|
||||
pub enum MessagesAction {
|
||||
AddMessage(RealTimeMessage),
|
||||
}
|
||||
|
||||
impl Reducible for MessagesState {
|
||||
type Action = MessagesAction;
|
||||
|
||||
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
|
||||
match action {
|
||||
MessagesAction::AddMessage(msg) => {
|
||||
let mut new_messages = self.messages.clone();
|
||||
new_messages.push(msg);
|
||||
Rc::new(MessagesState {
|
||||
messages: new_messages,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MessagesState {
|
||||
pub fn messages(&self) -> Vec<RealTimeMessage> {
|
||||
self.messages.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[hook]
|
||||
pub fn use_websocket(url: &str) -> UseWebSocketHandle {
|
||||
use web_sys::js_sys;
|
||||
|
||||
let messages = use_reducer(|| MessagesState { messages: Vec::new() });
|
||||
let error = use_state(|| None::<String>);
|
||||
|
||||
let ws = use_state_eq(|| {
|
||||
Rc::new(WebSocket::new(url).unwrap_or_else(|e| panic!("Failed to open WebSocket: {:?}", e)))
|
||||
});
|
||||
|
||||
{
|
||||
let messages = messages.clone();
|
||||
let error = error.clone();
|
||||
let ws = (*ws).clone();
|
||||
|
||||
use_effect_with((), move |_| {
|
||||
let ws_clone = ws.clone();
|
||||
let error_clone = error.clone();
|
||||
|
||||
// Set up message handler
|
||||
let onmessage_callback = {
|
||||
let messages = messages.clone();
|
||||
Closure::wrap(Box::new(move |e: MessageEvent| {
|
||||
if let Ok(txt) = e.data().dyn_into::<js_sys::JsString>() {
|
||||
if let Ok(msg) = serde_json::from_str::<RealTimeMessage>(&txt.as_string().unwrap()) {
|
||||
messages.dispatch(MessagesAction::AddMessage(msg));
|
||||
} else {
|
||||
error_clone.set(Some("Failed to parse message".to_string()));
|
||||
}
|
||||
}
|
||||
}) as Box<dyn FnMut(MessageEvent)>)
|
||||
};
|
||||
|
||||
ws.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
|
||||
onmessage_callback.forget();
|
||||
|
||||
// Set up error handler
|
||||
let error_clone = error.clone();
|
||||
let onerror_callback = Closure::wrap(Box::new(move |e: ErrorEvent| {
|
||||
error_clone.set(Some(e.message()));
|
||||
}) as Box<dyn FnMut(ErrorEvent)>);
|
||||
|
||||
ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
|
||||
onerror_callback.forget();
|
||||
|
||||
move || {
|
||||
ws_clone.close().unwrap_or_else(|_| {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
UseWebSocketHandle {
|
||||
ws: (*ws).clone(),
|
||||
messages,
|
||||
error,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
use gloo::storage::{LocalStorage, Storage};
|
||||
|
||||
mod hooks {
|
||||
pub mod websocket;
|
||||
}
|
||||
|
||||
mod components {
|
||||
pub mod chat;
|
||||
pub mod signup;
|
||||
pub mod login;
|
||||
}
|
||||
|
||||
use components::{
|
||||
chat::Chat,
|
||||
login::Login,
|
||||
signup::Signup,
|
||||
};
|
||||
|
||||
#[derive(Clone, Routable, PartialEq)]
|
||||
enum Route {
|
||||
#[at("/")]
|
||||
Root,
|
||||
#[at("/login")]
|
||||
Login,
|
||||
#[at("/signup")]
|
||||
Signup,
|
||||
#[at("/chat")]
|
||||
Chat,
|
||||
#[not_found]
|
||||
#[at("/404")]
|
||||
NotFound,
|
||||
}
|
||||
|
||||
fn switch(route: Route) -> Html {
|
||||
match route {
|
||||
Route::Root => html! { <Redirect<Route> to={Route::Login}/> },
|
||||
Route::Login => html! { <Login /> },
|
||||
Route::Signup => html! { <Signup /> },
|
||||
Route::Chat => {
|
||||
if let Ok(token) = LocalStorage::get::<String>("auth_token") {
|
||||
html! { <Chat /> }
|
||||
} else {
|
||||
html! { <Redirect<Route> to={Route::Login}/> }
|
||||
}
|
||||
}
|
||||
Route::NotFound => html! { <h1>{"404 Not Found"}</h1> },
|
||||
}
|
||||
}
|
||||
|
||||
#[function_component(App)]
|
||||
fn app() -> Html {
|
||||
html! {
|
||||
<BrowserRouter>
|
||||
<Switch<Route> render={switch} />
|
||||
</BrowserRouter>
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
yew::Renderer::<App>::new().render();
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
--blur-amount: 10px;
|
||||
--border-radius: 15px;
|
||||
--glow-color: rgba(255, 255, 255, 0.3);
|
||||
--message-bubble-color: rgba(255, 255, 255, 0.1);
|
||||
--border-color: rgba(255, 255, 255, 0.1);
|
||||
--shadow-color: rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100vh;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
background: linear-gradient(135deg, #1a2a6c, #2a4858, #141E30);
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.app-container {
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-container {
|
||||
flex: 1;
|
||||
margin: 0 2rem;
|
||||
padding: 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
max-width: 1400px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
margin: 0 0;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.profile-picture {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
background-size: cover !important;
|
||||
background-position: center !important;
|
||||
background-repeat: no-repeat !important;
|
||||
border-radius: 5px;
|
||||
flex-shrink: 0;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.message-bubble {
|
||||
background: var(--message-bubble-color);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
padding: 0.75rem;
|
||||
border-radius: 0 var(--border-radius) var(--border-radius) var(--border-radius);
|
||||
flex-grow: 1;
|
||||
position: relative;
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 2px 12px var(--shadow-color);
|
||||
}
|
||||
|
||||
.message-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-weight: 600;
|
||||
font-size: 0.9rem;
|
||||
color: #fff;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
}
|
||||
|
||||
.timestamp {
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.message-form {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.message-input {
|
||||
flex: 1;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
resize: vertical;
|
||||
min-height: 1.5rem;
|
||||
max-height: 150px;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.message-input:hover, .message-input:focus {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.send-button {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.send-button:hover {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.navbar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1rem 2rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border: none;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.nav-brand {
|
||||
font-size: 1.5rem;
|
||||
font-weight: bold;
|
||||
color: white;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
}
|
||||
|
||||
/* Login styles */
|
||||
.login-container {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 90%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
padding: 2rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
border-radius: var(--border-radius);
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: 0 4px 24px var(--shadow-color);
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
color: white;
|
||||
text-shadow: 0 2px 4px var(--shadow-color);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.login-input {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
.login-input:hover, .login-input:focus {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.login-input::placeholder {
|
||||
color: rgba(255, 255, 255, 0.6);
|
||||
}
|
||||
|
||||
.login-button {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.login-button:hover {
|
||||
border-color: var(--glow-color);
|
||||
box-shadow: 0 0 15px var(--glow-color);
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.login-text {
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
text-align: center;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.login-link {
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.login-link:hover {
|
||||
text-shadow: 0 0 10px var(--glow-color);
|
||||
}
|
||||
|
||||
.login-error {
|
||||
background: rgba(255, 0, 0, 0.1);
|
||||
border: 1px solid rgba(255, 0, 0, 0.3);
|
||||
border-radius: var(--border-radius);
|
||||
padding: 0.8rem 1.5rem;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
text-align: center;
|
||||
backdrop-filter: blur(var(--blur-amount));
|
||||
-webkit-backdrop-filter: blur(var(--blur-amount));
|
||||
box-shadow: 0 2px 8px var(--shadow-color);
|
||||
}
|
||||
|
||||
/* Custom scrollbar */
|
||||
.messages-container::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.messages-container::-webkit-scrollbar-thumb:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
Reference in New Issue
Block a user