summaryrefslogtreecommitdiffstats
path: root/server/src/actions/community.rs
blob: 44d7b749c78abbefebfbb3695ea13dca610db68c (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
extern crate diesel;
use schema::{community, community_user, community_follower};
use diesel::*;
use diesel::result::Error;
use serde::{Deserialize, Serialize};
use {Crud, Followable, Joinable};

#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name="community"]
pub struct Community {
  pub id: i32,
  pub name: String,
  pub published: chrono::NaiveDateTime,
  pub updated: Option<chrono::NaiveDateTime>
}

#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
#[table_name="community"]
pub struct CommunityForm {
  pub name: String,
  pub updated: Option<chrono::NaiveDateTime>
}

#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Community)]
#[table_name = "community_user"]
pub struct CommunityUser {
  pub id: i32,
  pub community_id: i32,
  pub fedi_user_id: String,
  pub published: chrono::NaiveDateTime,
}

#[derive(Insertable, AsChangeset, Clone)]
#[table_name="community_user"]
pub struct CommunityUserForm {
  pub community_id: i32,
  pub fedi_user_id: String,
}

#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Community)]
#[table_name = "community_follower"]
pub struct CommunityFollower {
  pub id: i32,
  pub community_id: i32,
  pub fedi_user_id: String,
  pub published: chrono::NaiveDateTime,
}

#[derive(Insertable, AsChangeset, Clone)]
#[table_name="community_follower"]
pub struct CommunityFollowerForm {
  pub community_id: i32,
  pub fedi_user_id: String,
}


impl Crud<CommunityForm> for Community {
  fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
    use schema::community::dsl::*;
    community.find(community_id)
      .first::<Self>(conn)
  }

  fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
    use schema::community::dsl::*;
    diesel::delete(community.find(community_id))
      .execute(conn)
  }

  fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
    use schema::community::dsl::*;
      insert_into(community)
        .values(new_community)
        .get_result::<Self>(conn)
  }

  fn update(conn: &PgConnection, community_id: i32, new_community: &CommunityForm) -> Result<Self, Error> {
    use schema::community::dsl::*;
    diesel::update(community.find(community_id))
      .set(new_community)
      .get_result::<Self>(conn)
  }
}

impl Followable<CommunityFollowerForm> for CommunityFollower {
  fn follow(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<Self, Error> {
    use schema::community_follower::dsl::*;
    insert_into(community_follower)
      .values(community_follower_form)
      .get_result::<Self>(conn)
  }
  fn ignore(conn: &PgConnection, community_follower_form: &CommunityFollowerForm) -> Result<usize, Error> {
    use schema::community_follower::dsl::*;
    diesel::delete(community_follower
      .filter(community_id.eq(&community_follower_form.community_id))
      .filter(fedi_user_id.eq(&community_follower_form.fedi_user_id)))
      .execute(conn)
  }
}

impl Joinable<CommunityUserForm> for CommunityUser {
  fn join(conn: &PgConnection, community_user_form: &CommunityUserForm) -> Result<Self, Error> {
    use schema::community_user::dsl::*;
    insert_into(community_user)
      .values(community_user_form)
      .get_result::<Self>(conn)
  }

  fn leave(conn: &PgConnection, community_user_form: &CommunityUserForm) -> Result<usize, Error> {
    use schema::community_user::dsl::*;
    diesel::delete(community_user
      .filter(community_id.eq(community_user_form.community_id))
      .filter(fedi_user_id.eq(&community_user_form.fedi_user_id)))
      .execute(conn)
  }
}

#[cfg(test)]
mod tests {
  use establish_connection;
  use super::*;
  use actions::user::*;
  use Crud;
 #[test]
  fn test_crud() {
    let conn = establish_connection();
    
    let new_community = CommunityForm {
      name: "TIL".into(),
      updated: None
    };

    let inserted_community = Community::create(&conn, &new_community).unwrap();

    let expected_community = Community {
      id: inserted_community.id,
      name: "TIL".into(),
      published: inserted_community.published,
      updated: None
    };

    let new_user = UserForm {
      name: "terry".into(),
      preferred_username: None,
      password_encrypted: "nope".into(),
      email: None,
      updated: None
    };

    let inserted_user = User_::create(&conn, &new_user).unwrap();

    let community_follower_form = CommunityFollowerForm {
      community_id: inserted_community.id,
      fedi_user_id: "test".into()
    };

    let inserted_community_follower = CommunityFollower::follow(&conn, &community_follower_form).unwrap();

    let expected_community_follower = CommunityFollower {
      id: inserted_community_follower.id,
      community_id: inserted_community.id,
      fedi_user_id: "test".into(),
      published: inserted_community_follower.published
    };
    
    let community_user_form = CommunityUserForm {
      community_id: inserted_community.id,
      fedi_user_id: "test".into()
    };

    let inserted_community_user = CommunityUser::join(&conn, &community_user_form).unwrap();

    let expected_community_user = CommunityUser {
      id: inserted_community_user.id,
      community_id: inserted_community.id,
      fedi_user_id: "test".into(),
      published: inserted_community_user.published
    };

    let read_community = Community::read(&conn, inserted_community.id).unwrap();
    let updated_community = Community::update(&conn, inserted_community.id, &new_community).unwrap();
    let ignored_community = CommunityFollower::ignore(&conn, &community_follower_form).unwrap();
    let left_community = CommunityUser::leave(&conn, &community_user_form).unwrap();
    let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
    User_::delete(&conn, inserted_user.id).unwrap();

    assert_eq!(expected_community, read_community);
    assert_eq!(expected_community, inserted_community);
    assert_eq!(expected_community, updated_community);
    assert_eq!(expected_community_follower, inserted_community_follower);
    assert_eq!(expected_community_user, inserted_community_user);
    assert_eq!(1, ignored_community);
    assert_eq!(1, left_community);
    assert_eq!(1, num_deleted);

  }
}