Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 58 additions & 17 deletions src/icpbin_backend/src/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow;

pub const MAX_PASTE_VALUE_SIZE: u32 = 16 * 1024;
pub const DELETE_TEPMLATE: &str = "DELETE";
pub const DELETE_TEMPLATE: &str = "DELETE";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😉


// allow to store Paste Data in the stable memory
// Allow storing PasteData in stable memory
impl Storable for PasteData {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
fn to_bytes(&self) -> Cow<[u8]> {
Cow::Owned(Encode!(self).unwrap())
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
fn from_bytes(bytes: Cow<[u8]>) -> Self {
Decode!(bytes.as_ref(), Self).unwrap()
}

Expand All @@ -24,18 +24,13 @@ impl Storable for PasteData {

#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct PasteData {
// id is the unique identifier of the paste
pub id: String,
pub name: String,
pub description: String,
pub content: String,
// creator is the principal who created the paste and it will be null when the paste is created by not logged in user
pub creator: Option<Principal>,
// track number of change in paste
pub version: i32,
// time to convert date to delete, it is base on second
pub expire_date: u32,
// container to push any extra data about PasteData
pub tags: Vec<String>,
}

Expand All @@ -57,17 +52,63 @@ pub struct PasteDataUpdater {
pub tags: Option<String>,
}

impl PasteData {
pub fn create(id: u64, user_id: Option<Principal>, info: PasteDataCreator) -> Self {
PasteData {
id: id.to_string(),
name: info.name,
creator: user_id,
description: info.description,
expire_date: info.expire_date,
content: info.content,
tags: _create_tags(info.tags),
version: 1,
}
}

pub fn update(&mut self, info: PasteDataUpdater) {
if let Some(name) = info.name {
self.name = name;
}
if let Some(desc) = info.description {
self.description = desc;
}
if let Some(content) = info.content {
self.content = content;
}
if let Some(tags) = info.tags {
self.tags = _create_tags(tags);
}

// Increase the number of changes
self.version += 1;
}

// Clear the content of the paste
pub fn clear(&mut self) {
self.name = DELETE_TEMPLATE.to_string();
self.content = DELETE_TEMPLATE.to_string();
self.tags = Vec::new();
}
}
Comment on lines +55 to +93
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand what is your changes. is it git diff problem?


#[derive(CandidType, Deserialize, Serialize)]
pub enum IcpPasteError {
ShortUrlShouldBeBetween4And10,
ShortUrlAlreadyExist,
PasteNotFound,
PasteAlreadyExist,
PasteIsNotAccessable,
WrongExpireDate,
}

fn _create_tags(input: String) -> Vec<String> {
input
.trim()
.split(" ")
.filter_map(|v| {
if v.is_empty() {
return None;
}
return Some(v.to_string());
})
.collect::<Vec<String>>()
.split_whitespace()
.filter(|v| !v.is_empty())
.map(String::from)
.collect()
Comment on lines +108 to +111
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is much nice and cleaner. thanks

}

impl PasteData {
Expand Down