roles/
notifications.rs

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
use serde::de::DeserializeOwned;
use std::collections::BTreeMap;
use xliff::t::T;

use hc_zome_trait_notifications::*;
use hc_zome_traits::*;
use hdk::prelude::*;
use notifications_types::SendNotificationInput;

use crate::linked_devices::call_local_zome;

pub fn notifications_zome_name() -> Option<ZomeName> {
    match std::option_env!("NOTIFICATIONS_COORDINATOR_ZOME_NAME") {
        Some(zome_name) => Some(zome_name.into()),
        None => None,
    }
}

pub struct RolesNotifications;

#[derive(Serialize, Deserialize, Debug, SerializedBytes)]
#[serde(tag = "type")]
pub enum RolesNotification {
    AssignedRole {
        role: String,
        assign_role_create_link_hash: ActionHash,
    },
    UnassignedRole {
        role: String,
        assign_role_create_link_hash: ActionHash,
    },
}
impl RolesNotification {
    fn role(&self) -> String {
        match self {
            Self::AssignedRole { role, .. } => role.clone(),
            Self::UnassignedRole { role, .. } => role.clone(),
        }
    }
    fn assign_role_create_link_hash(&self) -> ActionHash {
        match self {
            Self::AssignedRole {
                assign_role_create_link_hash,
                ..
            } => assign_role_create_link_hash.clone(),
            Self::UnassignedRole {
                assign_role_create_link_hash,
                ..
            } => assign_role_create_link_hash.clone(),
        }
    }
    fn notification_type(&self) -> String {
        match self {
            Self::AssignedRole { .. } => format!("AssignedRole"),
            Self::UnassignedRole { .. } => format!("UnassignedRole"),
        }
    }
}

#[implement_zome_trait_as_externs]
impl NotificationsZomeTrait for RolesNotifications {
    fn get_notifications_types(locale: String) -> ExternResult<BTreeMap<String, NotificationType>> {
        let mut types: BTreeMap<String, NotificationType> = BTreeMap::new();

        types.insert(
            "AssignedRole".into(),
            NotificationType {
                name: t(&locale, "Role assigned"),
                description: t(&locale, "An administrator assigned a role to you"),
            },
        );

        types.insert(
            "UnassignedRole".into(),
            NotificationType {
                name: t(&locale, "Role removed"),
                description: t(&locale, "An administrator removed one of your roles"),
            },
        );

        Ok(types)
    }
    fn get_notification_contents(
        input: GetNotificationContentsInput,
    ) -> ExternResult<NotificationContents> {
        let notification = RolesNotification::try_from(input.notification.content)
            .map_err(|err| wasm_error!(err))?;

        match notification {
            RolesNotification::AssignedRole { role, .. } => Ok(NotificationContents {
                title: t(&input.locale, ""),
                body: format!(
                    "{} {} {}.",
                    t(&input.locale, "You have been assigned the"),
                    role,
                    t(&input.locale, "role"),
                ),
                icon_src: format!(
                    "data:image/svg+xml;charset=utf-8,{}",
                    md_icons::filled::ICON_ADD_MODERATOR
                ),
                url_path_to_navigate_to_on_click: Some(String::from("")),
            }),
            RolesNotification::UnassignedRole { role, .. } => Ok(NotificationContents {
                title: t(&input.locale, ""),
                body: format!(
                    "{} {} {}.",
                    t(&input.locale, "An administrator removed your"),
                    role,
                    t(&input.locale, "role"),
                ),
                icon_src: format!(
                    "data:image/svg+xml;charset=utf-8,{}",
                    md_icons::filled::ICON_REMOVE_MODERATOR
                ),
                url_path_to_navigate_to_on_click: Some(String::from("")),
            }),
        }
    }
}

fn t(locale: &String, source: &str) -> String {
    match locale.as_str() {
        // "sv" => t_from_xliff(include_str!("../../../../ui/xliff/sv.xlf"), source),
        "en" => source.to_string(),
        _ => source.to_string(),
    }
}

fn t_from_xliff(xliff_str: &str, source: &str) -> String {
    let t = T::load_str(xliff_str);
    let unit = t.t_source(None, source);
    if let Some(unit) = unit {
        if let Some(t) = unit.target_text() {
            return t.clone();
        }
    }
    source.to_string()
}

// Call the notifications zome, if it exists
// If the notifications zome does not exist, it will return None
fn call_notifications<R, P>(fn_name: FunctionName, payload: P) -> ExternResult<Option<R>>
where
    P: serde::Serialize + std::fmt::Debug,
    R: DeserializeOwned + std::fmt::Debug,
{
    let Some(zome_name) = notifications_zome_name() else {
        return Ok(None);
    };
    let result: R = call_local_zome(zome_name, fn_name, payload)?;
    Ok(Some(result))
}

pub fn send_roles_notification(
    recipient: AgentPubKey,
    roles_notification: RolesNotification,
) -> ExternResult<()> {
    // We don't care in this case if the notification is not sent
    let _result: Option<()> = call_notifications(
        "send_notification".into(),
        SendNotificationInput {
            zome_name: zome_info()?.name,
            notification_type: roles_notification.notification_type(),
            notification_group: format!(
                "{}-{}",
                roles_notification.assign_role_create_link_hash(),
                roles_notification.role()
            ),
            recipient,
            content: SerializedBytes::try_from(roles_notification)
                .map_err(|err| wasm_error!(err))?,
        },
    )?;

    Ok(())
}