Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Remove profile data
Browse files Browse the repository at this point in the history
  • Loading branch information
farodin91 committed Jan 10, 2017
1 parent a96e179 commit eb6e591
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
2 changes: 0 additions & 2 deletions migrations/001_prerelease/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ CREATE TABLE presence_events (
event_id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
presence TEXT NOT NULL,
avatar_url TEXT,
displayname TEXT,
created_at TIMESTAMP NOT NULL DEFAULT now(),
UNIQUE (ordering)
);
20 changes: 0 additions & 20 deletions src/models/presence_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use ruma_events::presence::PresenceState;
use ruma_identifiers::{EventId, UserId};

use error::ApiError;
use models::profile::Profile;
use schema::{presence_events, presence_list};

/// A Matrix presence stream, not saved yet.
Expand All @@ -29,10 +28,6 @@ pub struct NewPresenceStreamEvent {
pub user_id: UserId,
/// The current presence state.
pub presence: String,
/// The avatar url.
pub avatar_url: Option<String>,
/// The display name.
pub displayname: Option<String>,
}

/// A Matrix presence stream.
Expand All @@ -46,10 +41,6 @@ pub struct PresenceStreamEvent {
pub user_id: UserId,
/// The current presence state.
pub presence: String,
/// The avatar url.
pub avatar_url: Option<String>,
/// The display name.
pub displayname: Option<String>,
/// The time the event was created.
pub created_at: SystemTime,
}
Expand All @@ -63,21 +54,10 @@ impl PresenceStreamEvent {
user_id: &UserId,
presence: PresenceState
) -> Result<PresenceStreamEvent, ApiError> {
let profile = Profile::find_by_uid(connection, user_id)?;

let mut avatar_url = None;
let mut displayname = None;
if let Some(ref profile) = profile {
avatar_url = profile.avatar_url.clone();
displayname = profile.displayname.clone();
}

let new_event = NewPresenceStreamEvent {
event_id: event_id.clone(),
user_id: user_id.clone(),
presence: presence.to_string(),
avatar_url: avatar_url,
displayname: displayname,
};
insert(&new_event)
.into(presence_events::table)
Expand Down
20 changes: 17 additions & 3 deletions src/models/presence_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use ruma_identifiers::UserId;
use error::ApiError;
use models::presence_status::PresenceStatus;
use models::presence_event::PresenceStreamEvent;
use models::profile::Profile;
use models::room_membership::RoomMembership;
use models::user::User;
use schema::presence_list;
Expand Down Expand Up @@ -126,12 +127,17 @@ impl PresenceList {
) -> Result<(i64, Vec<PresenceEvent>), ApiError> {
let mut max_ordering = -1;

let stream_events= PresenceStreamEvent::find_events_by_uid(
let stream_events = PresenceStreamEvent::find_events_by_uid(
connection,
user_id,
since
)?;

let profiles = Profile::find_profiles_by_presence_list(
connection,
user_id
)?;

let mut events = Vec::new();
let now = SystemTime::now();
for stream_event in stream_events {
Expand All @@ -144,11 +150,19 @@ impl PresenceList {
now
)?;

let profile: Option<&Profile> = profiles.iter().filter(|profile| profile.id == stream_event.user_id).next();
let mut avatar_url = None;
let mut displayname = None;
if let Some(ref profile) = profile {
avatar_url = profile.avatar_url.clone();
displayname = profile.displayname.clone();
}

events.push(PresenceEvent {
content: PresenceEventContent {
avatar_url: stream_event.avatar_url,
avatar_url: avatar_url,
currently_active: PresenceState::Online == presence_state,
displayname: stream_event.displayname,
displayname: displayname,
last_active_ago: Some(last_active_ago),
presence: presence_state,
user_id: stream_event.user_id,
Expand Down
3 changes: 2 additions & 1 deletion src/models/presence_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ impl PresenceStatus {
Ok(())
}

pub fn update_after_changed_profile(
/// Update based on current state.
pub fn update_by_uid_and_status(
connection: &PgConnection,
homeserver_domain: &str,
user_id: &UserId
Expand Down
24 changes: 21 additions & 3 deletions src/models/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
use diesel::{
insert,
Connection,
ExpressionMethods,
FilterDsl,
FindDsl,
LoadDsl,
SaveChangesDsl,
SelectDsl,
};
use diesel::expression::dsl::any;
use diesel::pg::PgConnection;
use diesel::result::Error as DieselError;
use ruma_identifiers::UserId;

use error::ApiError;
use models::room_membership::{RoomMembership, RoomMembershipOptions};
use models::presence_status::PresenceStatus;
use schema::profiles;
use schema::{profiles, presence_list};

/// A Matrix profile.
#[derive(AsChangeset, Debug, Clone, Identifiable, Insertable, Queryable)]
Expand Down Expand Up @@ -51,7 +55,7 @@ impl Profile {
}
};

PresenceStatus::update_after_changed_profile(connection, homeserver_domain, &user_id)?;
PresenceStatus::update_by_uid_and_status(connection, homeserver_domain, &user_id)?;
Ok(profile)
}).map_err(ApiError::from)
}
Expand All @@ -78,7 +82,7 @@ impl Profile {
}
};

PresenceStatus::update_after_changed_profile(connection, homeserver_domain, &user_id)?;
PresenceStatus::update_by_uid_and_status(connection, homeserver_domain, &user_id)?;
Ok(profile)
}).map_err(ApiError::from)
}
Expand Down Expand Up @@ -144,4 +148,18 @@ impl Profile {
Err(err) => Err(ApiError::from(err)),
}
}

/// Return `Profile`s for given `UserId` and his `PresenceList` entries.
pub fn find_profiles_by_presence_list(
connection: &PgConnection,
user_id: &UserId,
) -> Result<Vec<Profile>, ApiError> {
let users = presence_list::table
.filter(presence_list::user_id.eq(user_id))
.select(presence_list::observed_user_id);
profiles::table
.filter(profiles::id.eq(any(users)))
.get_results(connection)
.map_err(ApiError::from)
}
}
2 changes: 0 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ table! {
event_id -> Text,
user_id -> Text,
presence -> Text,
avatar_url -> Nullable<Text>,
displayname -> Nullable<Text>,
created_at -> Timestamp,
}
}

0 comments on commit eb6e591

Please sign in to comment.