Skip to content

Commit

Permalink
Merge branch 'mute-sessions' into 'master'
Browse files Browse the repository at this point in the history
Implement muting and unmuting sessions

Closes aebruno#12

See merge request whisperfish/whisperfish!231
  • Loading branch information
rubdos committed Mar 13, 2022
2 parents d66811b + 0a2bc94 commit 83ec031
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
24 changes: 22 additions & 2 deletions qml/delegates/SessionDelegate.qml
Expand Up @@ -12,6 +12,7 @@ ListItem {
property var contact: (isGroup || !mainWindow.contactsReady) ? null : resolvePeopleModel.personByPhoneNumber(model.source, true)
property int unreadCount: 0 // TODO implement in model
property bool isRead: model.read // TODO investigate: is this really a bool?
property bool isMuted: model.isMuted
property bool isUnread: !isRead // TODO investigate: is this really a bool?
property bool isNoteToSelf: SetupWorker.phoneNumber === model.source
property bool pinned: false // TODO implement in model
Expand Down Expand Up @@ -70,6 +71,10 @@ ListItem {
console.warn("setting archived/not archived is not implemented yet")
}

function toggleMutedState() {
SessionModel.markMuted(model.index, !isMuted)
}

Item {
anchors { fill: parent; leftMargin: Theme.horizontalPageMargin }

Expand All @@ -80,13 +85,14 @@ ListItem {
imageSource: profilePicture
isNoteToSelf: delegate.isNoteToSelf
isGroup: delegate.isGroup
showInfoMark: pinned || archived || hasDraft || isNoteToSelf || hasAttachment
showInfoMark: pinned || archived || hasDraft || isNoteToSelf || hasAttachment || isMuted
infoMark.source: {
if (hasDraft) 'image://theme/icon-s-edit'
else if (isNoteToSelf) 'image://theme/icon-s-retweet' // task|secure|retweet
else if (pinned) 'image://theme/icon-s-low-importance'
else if (archived) 'image://theme/icon-s-time'
else if (hasAttachment) 'image://theme/icon-s-attach'
else if (isMuted) 'image://theme/icon-s-low-importance'
else ''
}
infoMark.rotation: {
Expand Down Expand Up @@ -227,6 +233,7 @@ ListItem {

ContextMenu {
id: menu

/* MenuItem {
text: isUnread ?
//: Mark conversation as 'read', even though it isn't
Expand All @@ -246,8 +253,20 @@ ListItem {
//% "Pin to top"
qsTrId("whisperfish-session-unpin-view")
onClicked: togglePinState()
}
} */

MenuItem {
text: isMuted ?
//: Mark conversation as unmuted
//% "Mark as unread"
qsTrId("whisperfish-session-mark-unmuted") :
//: Mark conversation as muted
//% "Mark as muted"
qsTrId("whisperfish-session-mark-muted")
onClicked: toggleMutedState()
}

/* MenuItem {
text: archived ?
//: Show hidden messages again
//% "Un-archive conversation"
Expand All @@ -257,6 +276,7 @@ ListItem {
qsTrId("whisperfish-session-archive")
onClicked: toggleArchivedState()
} */

MenuItem {
//: Delete all messages from session menu
//% "Delete conversation"
Expand Down
26 changes: 26 additions & 0 deletions src/actor/sessionactor.rs
Expand Up @@ -32,6 +32,13 @@ pub struct MarkSessionRead {
pub already_unread: bool,
}

#[derive(actix::Message)]
#[rtype(result = "()")]
pub struct MarkSessionMuted {
pub sid: i32,
pub muted: bool,
}

#[derive(actix::Message)]
#[rtype(result = "()")]
pub struct DeleteSession {
Expand Down Expand Up @@ -158,6 +165,25 @@ impl Handler<MarkSessionRead> for SessionActor {
}
}

impl Handler<MarkSessionMuted> for SessionActor {
type Result = ();

fn handle(
&mut self,
MarkSessionMuted { sid, muted }: MarkSessionMuted,
_ctx: &mut Self::Context,
) -> Self::Result {
self.storage
.as_ref()
.unwrap()
.mark_session_muted(sid, muted);
self.inner
.pinned()
.borrow_mut()
.handle_mark_session_muted(sid, muted);
}
}

impl Handler<DeleteSession> for SessionActor {
type Result = ();

Expand Down
40 changes: 40 additions & 0 deletions src/model/session.rs
Expand Up @@ -52,6 +52,7 @@ pub struct SessionModel {
markRead: qt_method!(fn(&mut self, id: usize)),
markReceived: qt_method!(fn(&self, id: usize)),
markSent: qt_method!(fn(&self, id: usize, message: QString)),
markMuted: qt_method!(fn(&self, idx: usize, muted: bool)),
}

impl SessionModel {
Expand Down Expand Up @@ -166,6 +167,25 @@ impl SessionModel {
// XXX: don't forget sync messages
}

#[with_executor]
fn markMuted(&self, idx: usize, muted: bool) {
if idx > self.content.len() - 1 {
log::error!("Invalid index for session model");
return;
}

let sid = self.content[idx].id;

actix::spawn(
self.actor
.as_ref()
.unwrap()
.send(actor::MarkSessionMuted { sid, muted })
.map(Result::unwrap),
);
log::trace!("Dispatched actor::MarkSessionMuted({}, {})", idx, muted);
}

// Event handlers below this line

/// Handle loaded session
Expand Down Expand Up @@ -316,6 +336,21 @@ impl SessionModel {
}
}

pub fn handle_mark_session_muted(&mut self, sid: i32, muted: bool) {
if let Some((i, session)) = self
.content
.iter_mut()
.enumerate()
.find(|(_, s)| s.session.id == sid)
{
session.session.is_muted = muted;
let idx = (self as &mut dyn QAbstractListModel).row_index(i as i32);
(self as &mut dyn QAbstractListModel).data_changed(idx, idx);
} else {
log::warn!("Could not call data_changed for non-existing session!");
}
}

/// Remove deleted session from QML
pub fn handle_delete_session(&mut self, idx: usize) {
(self as &mut dyn QAbstractListModel).begin_remove_rows(idx as i32, idx as i32);
Expand Down Expand Up @@ -449,6 +484,10 @@ impl AugmentedSession {
}
}

fn is_muted(&self) -> bool {
self.session.is_muted
}

fn viewed(&self) -> u32 {
if let Some(m) = &self.last_message {
m.receipts
Expand Down Expand Up @@ -479,6 +518,7 @@ define_model_roles! {
Sent(fn sent(&self)): "sent",
Delivered(fn delivered(&self)): "deliveryCount",
Read(fn read(&self)): "readCount",
IsMuted(fn is_muted(&self)): "isMuted",
Viewed(fn viewed(&self)): "viewCount",
HasAttachment(fn has_attachment(&self)): "hasAttachment"
}
Expand Down
14 changes: 14 additions & 0 deletions src/store/mod.rs
Expand Up @@ -55,6 +55,7 @@ pub struct Session {
pub received: bool,
pub unread: bool,
pub is_group: bool,
pub is_muted: bool,
pub group_members: Option<String>,
#[allow(dead_code)]
pub group_id: Option<String>,
Expand Down Expand Up @@ -1550,6 +1551,19 @@ impl Storage {
.expect("mark session read");
}

pub fn mark_session_muted(&self, sid: i32, muted: bool) {
let db = self.db.lock();

log::trace!("Called mark_session_muted({}, {})", sid, muted);

use schema::sessions::dsl::*;

diesel::update(sessions.filter(id.eq(sid)))
.set((is_muted.eq(muted),))
.execute(&*db)
.expect("mark session (un)muted");
}

pub fn register_attachment(&mut self, mid: i32, path: &str, mime_type: &str) {
// XXX: multiple attachments https://gitlab.com/rubdos/whisperfish/-/issues/11

Expand Down
2 changes: 1 addition & 1 deletion src/worker/client.rs
Expand Up @@ -438,7 +438,7 @@ impl ClientActor {
.messageReceived(session.id, message.id);

// XXX If from ourselves, skip
if settings.get_bool("enable_notify") && !is_sync_sent {
if settings.get_bool("enable_notify") && !is_sync_sent && !session.is_muted {
let session_name: &str = match &session.r#type {
orm::SessionType::GroupV1(group) => &group.name,
orm::SessionType::GroupV2(group) => &group.name,
Expand Down

0 comments on commit 83ec031

Please sign in to comment.