summaryrefslogtreecommitdiffstats
path: root/server/src/apub/private_message.rs
blob: bc685b2382019d1e26cc8027ee89ab060b722d73 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::{
  apub::{
    activities::send_activity,
    create_tombstone,
    fetcher::get_or_fetch_and_upsert_remote_user,
    insert_activity,
    ApubObjectType,
    FromApub,
    ToApub,
  },
  blocking,
  DbPool,
  LemmyError,
};
use activitystreams::{
  activity::{Create, Delete, Undo, Update},
  context,
  object::{kind::NoteType, properties::ObjectProperties, Note},
};
use activitystreams_new::object::Tombstone;
use actix_web::client::Client;
use lemmy_db::{
  private_message::{PrivateMessage, PrivateMessageForm},
  user::User_,
  Crud,
};
use lemmy_utils::convert_datetime;

#[async_trait::async_trait(?Send)]
impl ToApub for PrivateMessage {
  type Response = Note;

  async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
    let mut private_message = Note::default();
    let oprops: &mut ObjectProperties = private_message.as_mut();

    let creator_id = self.creator_id;
    let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;

    let recipient_id = self.recipient_id;
    let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;

    oprops
      .set_context_xsd_any_uri(context())?
      .set_id(self.ap_id.to_owned())?
      .set_published(convert_datetime(self.published))?
      .set_content_xsd_string(self.content.to_owned())?
      .set_to_xsd_any_uri(recipient.actor_id)?
      .set_attributed_to_xsd_any_uri(creator.actor_id)?;

    if let Some(u) = self.updated {
      oprops.set_updated(convert_datetime(u))?;
    }

    Ok(private_message)
  }

  fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
    create_tombstone(
      self.deleted,
      &self.ap_id,
      self.updated,
      NoteType.to_string(),
    )
  }
}

#[async_trait::async_trait(?Send)]
impl FromApub for PrivateMessageForm {
  type ApubType = Note;

  /// Parse an ActivityPub note received from another instance into a Lemmy Private message
  async fn from_apub(
    note: &Note,
    client: &Client,
    pool: &DbPool,
  ) -> Result<PrivateMessageForm, LemmyError> {
    let oprops = &note.object_props;
    let creator_actor_id = &oprops.get_attributed_to_xsd_any_uri().unwrap().to_string();

    let creator = get_or_fetch_and_upsert_remote_user(&creator_actor_id, client, pool).await?;

    let recipient_actor_id = &oprops.get_to_xsd_any_uri().unwrap().to_string();

    let recipient = get_or_fetch_and_upsert_remote_user(&recipient_actor_id, client, pool).await?;

    Ok(PrivateMessageForm {
      creator_id: creator.id,
      recipient_id: recipient.id,
      content: oprops
        .get_content_xsd_string()
        .map(|c| c.to_string())
        .unwrap(),
      published: oprops
        .get_published()
        .map(|u| u.as_ref().to_owned().naive_local()),
      updated: oprops
        .get_updated()
        .map(|u| u.as_ref().to_owned().naive_local()),
      deleted: None,
      read: None,
      ap_id: oprops.get_id().unwrap().to_string(),
      local: false,
    })
  }
}

#[async_trait::async_trait(?Send)]
impl ApubObjectType for PrivateMessage {
  /// Send out information about a newly created private message
  async fn send_create(
    &self,
    creator: &User_,
    client: &Client,
    pool: &DbPool,
  ) -> Result<(), LemmyError> {
    let note = self.to_apub(pool).await?;
    let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4());

    let recipient_id = self.recipient_id;
    let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;

    let mut create = Create::new();
    create
      .object_props
      .set_context_xsd_any_uri(context())?
      .set_id(id)?;
    let to = format!("{}/inbox", recipient.actor_id);

    create
      .create_props
      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
      .set_object_base_box(note)?;

    insert_activity(creator.id, create.clone(), true, pool).await?;

    send_activity(client, &create, creator, vec![to]).await?;
    Ok(())
  }

  /// Send out information about an edited post, to the followers of the community.
  async fn send_update(
    &self,
    creator: &User_,
    client: &Client,
    pool: &DbPool,
  ) -> Result<(), LemmyError> {
    let note = self.to_apub(pool).await?;
    let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4());

    let recipient_id = self.recipient_id;
    let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;

    let mut update = Update::new();
    update
      .object_props
      .set_context_xsd_any_uri(context())?
      .set_id(id)?;
    let to = format!("{}/inbox", recipient.actor_id);

    update
      .update_props
      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
      .set_object_base_box(note)?;

    insert_activity(creator.id, update.clone(), true, pool).await?;

    send_activity(client, &update, creator, vec![to]).await?;
    Ok(())
  }

  async fn send_delete(
    &self,
    creator: &User_,
    client: &Client,
    pool: &DbPool,
  ) -> Result<(), LemmyError> {
    let note = self.to_apub(pool).await?;
    let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4());

    let recipient_id = self.recipient_id;
    let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;

    let mut delete = Delete::new();
    delete
      .object_props
      .set_context_xsd_any_uri(context())?
      .set_id(id)?;
    let to = format!("{}/inbox", recipient.actor_id);

    delete
      .delete_props
      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
      .set_object_base_box(note)?;

    insert_activity(creator.id, delete.clone(), true, pool).await?;

    send_activity(client, &delete, creator, vec![to]).await?;
    Ok(())
  }

  async fn send_undo_delete(
    &self,
    creator: &User_,
    client: &Client,
    pool: &DbPool,
  ) -> Result<(), LemmyError> {
    let note = self.to_apub(pool).await?;
    let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4());

    let recipient_id = self.recipient_id;
    let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;

    let mut delete = Delete::new();
    delete
      .object_props
      .set_context_xsd_any_uri(context())?
      .set_id(id)?;
    let to = format!("{}/inbox", recipient.actor_id);

    delete
      .delete_props
      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
      .set_object_base_box(note)?;

    // TODO
    // Undo that fake activity
    let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4());
    let mut undo = Undo::default();

    undo
      .object_props
      .set_context_xsd_any_uri(context())?
      .set_id(undo_id)?;

    undo
      .undo_props
      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
      .set_object_base_box(delete)?;

    insert_activity(creator.id, undo.clone(), true, pool).await?;

    send_activity(client, &undo, creator, vec![to]).await?;
    Ok(())
  }

  async fn send_remove(
    &self,
    _mod_: &User_,
    _client: &Client,
    _pool: &DbPool,
  ) -> Result<(), LemmyError> {
    unimplemented!()
  }

  async fn send_undo_remove(
    &self,
    _mod_: &User_,
    _client: &Client,
    _pool: &DbPool,