diff --git a/migrations/001_prerelease/up.sql b/migrations/001_prerelease/up.sql index 80ad3b64..32d17524 100644 --- a/migrations/001_prerelease/up.sql +++ b/migrations/001_prerelease/up.sql @@ -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) ); diff --git a/src/models/presence_event.rs b/src/models/presence_event.rs index c701d135..d070cb01 100644 --- a/src/models/presence_event.rs +++ b/src/models/presence_event.rs @@ -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. @@ -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, - /// The display name. - pub displayname: Option, } /// A Matrix presence stream. @@ -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, - /// The display name. - pub displayname: Option, /// The time the event was created. pub created_at: SystemTime, } @@ -63,21 +54,10 @@ impl PresenceStreamEvent { user_id: &UserId, presence: PresenceState ) -> Result { - 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) diff --git a/src/models/presence_list.rs b/src/models/presence_list.rs index 724c2a03..e3bfe438 100644 --- a/src/models/presence_list.rs +++ b/src/models/presence_list.rs @@ -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; @@ -126,12 +127,17 @@ impl PresenceList { ) -> Result<(i64, Vec), 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 { @@ -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, diff --git a/src/models/presence_status.rs b/src/models/presence_status.rs index f0f9d6e7..12c0aca6 100644 --- a/src/models/presence_status.rs +++ b/src/models/presence_status.rs @@ -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 diff --git a/src/models/profile.rs b/src/models/profile.rs index 1c1aaf6c..e000fb60 100644 --- a/src/models/profile.rs +++ b/src/models/profile.rs @@ -3,10 +3,14 @@ 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; @@ -14,7 +18,7 @@ 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)] @@ -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) } @@ -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) } @@ -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, 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) + } } diff --git a/src/schema.rs b/src/schema.rs index 0a39f404..6a86cd91 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -138,8 +138,6 @@ table! { event_id -> Text, user_id -> Text, presence -> Text, - avatar_url -> Nullable, - displayname -> Nullable, created_at -> Timestamp, } }