summaryrefslogtreecommitdiffstats
path: root/server/src/apub/community_inbox.rs
blob: 81cbee8132f54aba2558627b95f4175fc65ed015 (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
use super::*;

#[serde(untagged)]
#[derive(Deserialize, Debug)]
pub enum CommunityAcceptedObjects {
  Follow(Follow),
  Undo(Undo),
}

// TODO Consolidate community and user inboxes into a single shared one
/// Handler for all incoming activities to community inboxes.
pub async fn community_inbox(
  request: HttpRequest,
  input: web::Json<CommunityAcceptedObjects>,
  path: web::Path<String>,
  db: DbPoolParam,
  chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> {
  let input = input.into_inner();
  let community_name = path.into_inner();
  debug!(
    "Community {} received activity {:?}",
    &community_name, &input
  );
  match input {
    CommunityAcceptedObjects::Follow(f) => {
      handle_follow(&f, &request, &community_name, db, chat_server)
    }
    CommunityAcceptedObjects::Undo(u) => {
      handle_undo_follow(&u, &request, &community_name, db, chat_server)
    }
  }
}

/// Handle a follow request from a remote user, adding it to the local database and returning an
/// Accept activity.
fn handle_follow(
  follow: &Follow,
  request: &HttpRequest,
  community_name: &str,
  db: DbPoolParam,
  _chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> {
  let user_uri = follow
    .follow_props
    .get_actor_xsd_any_uri()
    .unwrap()
    .to_string();
  let _community_uri = follow
    .follow_props
    .get_object_xsd_any_uri()
    .unwrap()
    .to_string();

  let conn = db.get()?;

  let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
  let community = Community::read_from_name(&conn, &community_name)?;

  verify(&request, &user)?;

  insert_activity(&conn, user.id, &follow, false)?;

  let community_follower_form = CommunityFollowerForm {
    community_id: community.id,
    user_id: user.id,
  };

  // This will fail if they're already a follower, but ignore the error.
  CommunityFollower::follow(&conn, &community_follower_form).ok();

  community.send_accept_follow(&follow, &conn)?;

  Ok(HttpResponse::Ok().finish())
}

fn handle_undo_follow(
  undo: &Undo,
  request: &HttpRequest,
  community_name: &str,
  db: DbPoolParam,
  _chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> {
  let follow = undo
    .undo_props
    .get_object_base_box()
    .to_owned()
    .unwrap()
    .to_owned()
    .into_concrete::<Follow>()?;

  let user_uri = follow
    .follow_props
    .get_actor_xsd_any_uri()
    .unwrap()
    .to_string();

  let _community_uri = follow
    .follow_props
    .get_object_xsd_any_uri()
    .unwrap()
    .to_string();

  let conn = db.get()?;

  let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
  let community = Community::read_from_name(&conn, &community_name)?;

  verify(&request, &user)?;

  insert_activity(&conn, user.id, &follow, false)?;

  let community_follower_form = CommunityFollowerForm {
    community_id: community.id,
    user_id: user.id,
  };

  CommunityFollower::unfollow(&conn, &community_follower_form).ok();

  Ok(HttpResponse::Ok().finish())
}