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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::collections::HashMap;
use std::sync::mpsc::Sender;
use std::sync::{Arc, Condvar, Mutex};

use crate::error::Error;

use crate::types::Event;
use crate::types::Medium;
use crate::types::Member;
use crate::types::Message;
use crate::types::ProtocolInstance;
use crate::types::Room;
use crate::types::Sticker;
use crate::types::StickerGroup;
use crate::types::ThirdPartyIdentifier;

use crate::cache::CacheMap;
use url::Url;

#[derive(Debug)]
pub enum BKCommand {
    Login(String, String, String),
    SetToken(String, String, String),
    Logout,
    #[allow(dead_code)]
    Register(String, String, String),
    #[allow(dead_code)]
    Guest(String),
    GetUsername,
    SetUserName(String),
    GetThreePID,
    GetTokenEmail(String, String, String),
    GetTokenPhone(String, String, String),
    SubmitPhoneToken(String, String, String, String),
    AddThreePID(String, String, String),
    DeleteThreePID(Medium, String),
    ChangePassword(String, String, String),
    AccountDestruction(String, String, bool),
    GetAvatar,
    SetUserAvatar(String),
    Sync(Option<String>, bool),
    SyncForced,
    GetRoomMembers(String),
    GetRoomMessages(String, String),
    GetRoomMessagesFromMsg(String, Message),
    GetMessageContext(Message),
    GetRoomAvatar(String),
    GetThumbAsync(String, Sender<String>),
    GetMediaAsync(String, Sender<String>),
    GetMediaListAsync(
        String,
        Option<String>,
        Option<String>,
        Sender<(Vec<Message>, String)>,
    ),
    GetFileAsync(String, Sender<String>),
    GetAvatarAsync(Option<Member>, Sender<String>),
    GetMedia(String),
    GetMediaUrl(String, Sender<String>),
    GetUserInfoAsync(String, Option<Sender<(String, String)>>),
    GetUserNameAsync(String, Sender<String>),
    SendMsg(Message),
    SendMsgRedaction(Message),
    SendTyping(String),
    SetRoom(String),
    ShutDown,
    DirectoryProtocols,
    DirectorySearch(String, String, String, bool),
    JoinRoom(String),
    MarkAsRead(String, String),
    LeaveRoom(String),
    SetRoomName(String, String),
    SetRoomTopic(String, String),
    SetRoomAvatar(String, String),
    AttachFile(Message),
    NewRoom(String, RoomType, String),
    DirectChat(Member, String),
    AddToFav(String, bool),
    AcceptInv(String),
    RejectInv(String),
    UserSearch(String),
    Invite(String, String),
    ListStickers,
    SendSticker(String, Sticker),
    PurchaseSticker(StickerGroup),
}

#[derive(Debug)]
pub enum BKResponse {
    ShutDown,
    Token(String, String, Option<String>),
    Logout,
    Name(String),
    SetUserName(String),
    GetThreePID(Vec<ThirdPartyIdentifier>),
    GetTokenEmail(String, String),
    GetTokenPhone(String, String),
    SubmitPhoneToken(Option<String>, String),
    AddThreePID(String),
    GetTokenPhoneUsed,
    GetTokenEmailUsed,
    DeleteThreePID,
    ChangePassword,
    AccountDestruction,
    Avatar(String),
    SetUserAvatar(String),
    Sync(String),
    Rooms(Vec<Room>, Option<Room>),
    UpdateRooms(Vec<Room>),
    RoomDetail(String, String, String),
    RoomAvatar(String, Option<Url>),
    NewRoomAvatar(String),
    RoomMemberEvent(Event),
    RoomMessages(Vec<Message>),
    RoomMessagesInit(Vec<Message>),
    RoomMessagesTo(Vec<Message>, String, Option<String>),
    RoomMembers(String, Vec<Member>),
    SentMsg(String, String),
    SentMsgRedaction(String, String),
    DirectoryProtocols(Vec<ProtocolInstance>),
    DirectorySearch(Vec<Room>),
    JoinRoom,
    LeaveRoom,
    MarkedAsRead(String, String),
    SetRoomName,
    SetRoomTopic,
    SetRoomAvatar,
    RoomName(String, String),
    RoomTopic(String, String),
    Media(String),
    MediaUrl(Url),
    AttachedFile(Message),
    NewRoom(Room, String),
    AddedToFav(String, bool),
    RoomNotifications(String, i32, i32),
    UserSearch(Vec<Member>),
    Stickers(Vec<StickerGroup>),

    //errors
    UserNameError(Error),
    SetUserNameError(Error),
    GetThreePIDError(Error),
    GetTokenEmailError(Error),
    GetTokenPhoneError(Error),
    SubmitPhoneTokenError(Error),
    AddThreePIDError(Error),
    DeleteThreePIDError(Error),
    ChangePasswordError(Error),
    AccountDestructionError(Error),
    AvatarError(Error),
    SetUserAvatarError(Error),
    LoginError(Error),
    LogoutError(Error),
    GuestLoginError(Error),
    SyncError(Error),
    RoomDetailError(Error),
    RoomAvatarError(Error),
    RoomMessagesError(Error),
    RoomMembersError(Error),
    SendMsgError(Error),
    SendMsgRedactionError(Error),
    SendTypingError(Error),
    SetRoomError(Error),
    CommandError(Error),
    DirectoryError(Error),
    JoinRoomError(Error),
    MarkAsReadError(Error),
    LeaveRoomError(Error),
    SetRoomNameError(Error),
    SetRoomTopicError(Error),
    SetRoomAvatarError(Error),
    GetRoomAvatarError(Error),
    MediaError(Error),
    AttachFileError(Error),
    NewRoomError(Error, String),
    AddToFavError(Error),
    AcceptInvError(Error),
    RejectInvError(Error),
    InviteError(Error),
    StickersError(Error),
}

#[derive(Debug, Clone, Copy)]
pub enum RoomType {
    Public,
    Private,
}

pub struct BackendData {
    pub user_id: String,
    pub access_token: String,
    pub server_url: Url,
    pub scalar_token: Option<String>,
    pub scalar_url: Url,
    pub sticker_widget: Option<String>,
    pub since: Option<String>,
    pub rooms_since: String,
    pub join_to_room: String,
    pub m_direct: HashMap<String, Vec<String>>,
}

pub struct Backend {
    pub tx: Sender<BKResponse>,
    pub data: Arc<Mutex<BackendData>>,
    pub internal_tx: Option<Sender<BKCommand>>,

    // user info cache, uid -> (name, avatar)
    pub user_info_cache: CacheMap<Arc<Mutex<(String, String)>>>,
    // semaphore to limit the number of threads downloading images
    pub limit_threads: Arc<(Mutex<u8>, Condvar)>,
}

impl Clone for Backend {
    fn clone(&self) -> Backend {
        Backend {
            tx: self.tx.clone(),
            data: self.data.clone(),
            internal_tx: self.internal_tx.clone(),
            user_info_cache: self.user_info_cache.clone(),
            limit_threads: self.limit_threads.clone(),
        }
    }
}