Skip to content

Commit

Permalink
refs #6 Handle receving DM
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Jan 15, 2023
1 parent ad42d91 commit 4672d5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src-tauri/src/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub struct DeleteTimelineStatusPayload {
status_id: String,
}

#[derive(Clone, Serialize)]
pub struct ReceiveTimelineConversationPayload {
timeline_id: i64,
conversation: megalodon::entities::Conversation,
}

pub async fn start_user(
app_handle: Arc<AppHandle>,
server: &entities::Server,
Expand Down Expand Up @@ -136,6 +142,9 @@ pub async fn start(
entities::timeline::Kind::Local => {
streaming = client.local_streaming(streaming_url);
}
entities::timeline::Kind::Direct => {
streaming = client.direct_streaming(streaming_url);
}
entities::timeline::Kind::List => match &timeline.list_id {
None => return Err(format!("could not find list_id for {} ", timeline.name)),
Some(list_id) => {
Expand Down Expand Up @@ -187,6 +196,18 @@ pub async fn start(
)
.expect("Failed to delete-timeline-status event");
}
Message::Conversation(conversation) => {
log::debug!("receive conversation");
app_handle
.emit_all(
"receive-timeline-conversation",
ReceiveTimelineConversationPayload {
timeline_id,
conversation,
},
)
.expect("Failed to receive-timeline-conversation event");
}
_ => {}
}));
s.await;
Expand Down
21 changes: 21 additions & 0 deletions src/components/timelines/Conversations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { TIMELINE_STATUSES_COUNT } from 'src/defaults'
import alert from '../utils/alert'
import { Virtuoso } from 'react-virtuoso'
import Conversation from './conversation/Conversation'
import { listen } from '@tauri-apps/api/event'
import { ReceiveTimelineConversationPayload } from 'src/payload'

type Props = {
server: Server
Expand Down Expand Up @@ -53,6 +55,25 @@ const Conversations: React.FC<Props> = props => {
}
}
f()

listen<ReceiveTimelineConversationPayload>('receive-timeline-conversation', ev => {
if (ev.payload.timeline_id !== props.timeline.id) {
return
}
setConversations(current => {
const find = current.find(conv => conv.id === ev.payload.conversation.id)
if (find) {
return current.map(conv => {
if (conv.id === ev.payload.conversation.id) {
return ev.payload.conversation
}
return conv
})
} else {
return [ev.payload.conversation, ...current]
}
})
})
}, [])

const loadConversations = async (client: MegalodonInterface, maxId?: string): Promise<Array<Entity.Conversation>> => {
Expand Down
5 changes: 5 additions & 0 deletions src/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export type DeleteTimelineStatusPayload = {
timeline_id: number
status_id: string
}

export type ReceiveTimelineConversationPayload = {
timeline_id: number
conversation: Entity.Conversation
}

0 comments on commit 4672d5c

Please sign in to comment.