2017-09-04 11:40:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
#include <rpl/event_stream.h>
|
2017-09-04 11:40:02 +00:00
|
|
|
#include "storage/storage_facade.h"
|
2017-10-29 15:32:01 +00:00
|
|
|
#include "storage/storage_sparse_ids_list.h"
|
2017-09-04 11:40:02 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
|
|
|
|
// Allow forward declarations.
|
|
|
|
enum class SharedMediaType : char {
|
|
|
|
Photo = 0,
|
|
|
|
Video = 1,
|
|
|
|
MusicFile = 2,
|
|
|
|
File = 3,
|
|
|
|
VoiceFile = 4,
|
|
|
|
Link = 5,
|
|
|
|
ChatPhoto = 6,
|
|
|
|
RoundVoiceFile = 7,
|
|
|
|
GIF = 8,
|
2017-09-13 17:01:23 +00:00
|
|
|
RoundFile = 9,
|
2017-09-04 11:40:02 +00:00
|
|
|
|
2017-09-13 17:01:23 +00:00
|
|
|
kCount = 10,
|
2017-09-04 11:40:02 +00:00
|
|
|
};
|
|
|
|
constexpr auto kSharedMediaTypeCount = static_cast<int>(SharedMediaType::kCount);
|
|
|
|
constexpr bool IsValidSharedMediaType(SharedMediaType type) {
|
|
|
|
return (static_cast<int>(type) >= 0)
|
|
|
|
&& (static_cast<int>(type) < kSharedMediaTypeCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
using SharedMediaTypesMask = base::enum_mask<SharedMediaType>;
|
|
|
|
|
|
|
|
struct SharedMediaAddNew {
|
|
|
|
SharedMediaAddNew(PeerId peerId, SharedMediaTypesMask types, MsgId messageId)
|
|
|
|
: peerId(peerId), messageId(messageId), types(types) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
MsgId messageId = 0;
|
|
|
|
SharedMediaTypesMask types;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SharedMediaAddExisting {
|
|
|
|
SharedMediaAddExisting(
|
|
|
|
PeerId peerId,
|
|
|
|
SharedMediaTypesMask types,
|
|
|
|
MsgId messageId,
|
|
|
|
MsgRange noSkipRange)
|
|
|
|
: peerId(peerId)
|
|
|
|
, messageId(messageId)
|
|
|
|
, noSkipRange(noSkipRange)
|
|
|
|
, types(types) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
MsgId messageId = 0;
|
|
|
|
MsgRange noSkipRange;
|
|
|
|
SharedMediaTypesMask types;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SharedMediaAddSlice {
|
|
|
|
SharedMediaAddSlice(
|
|
|
|
PeerId peerId,
|
|
|
|
SharedMediaType type,
|
|
|
|
std::vector<MsgId> &&messageIds,
|
|
|
|
MsgRange noSkipRange,
|
|
|
|
base::optional<int> count = base::none)
|
|
|
|
: peerId(peerId)
|
|
|
|
, messageIds(std::move(messageIds))
|
|
|
|
, noSkipRange(noSkipRange)
|
|
|
|
, type(type)
|
|
|
|
, count(count) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
std::vector<MsgId> messageIds;
|
|
|
|
MsgRange noSkipRange;
|
|
|
|
SharedMediaType type = SharedMediaType::kCount;
|
|
|
|
base::optional<int> count;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SharedMediaRemoveOne {
|
|
|
|
SharedMediaRemoveOne(
|
|
|
|
PeerId peerId,
|
|
|
|
SharedMediaTypesMask types,
|
|
|
|
MsgId messageId)
|
2017-10-29 15:32:01 +00:00
|
|
|
: peerId(peerId)
|
|
|
|
, messageId(messageId)
|
|
|
|
, types(types) {
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
MsgId messageId = 0;
|
|
|
|
SharedMediaTypesMask types;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SharedMediaRemoveAll {
|
|
|
|
SharedMediaRemoveAll(PeerId peerId) : peerId(peerId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
struct SharedMediaKey {
|
|
|
|
SharedMediaKey(
|
2017-09-04 11:40:02 +00:00
|
|
|
PeerId peerId,
|
|
|
|
SharedMediaType type,
|
2017-08-18 19:14:31 +00:00
|
|
|
MsgId messageId)
|
2017-10-29 15:32:01 +00:00
|
|
|
: peerId(peerId)
|
|
|
|
, type(type)
|
|
|
|
, messageId(messageId) {
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const SharedMediaKey &other) const {
|
|
|
|
return (peerId == other.peerId)
|
|
|
|
&& (type == other.type)
|
|
|
|
&& (messageId == other.messageId);
|
|
|
|
}
|
|
|
|
bool operator!=(const SharedMediaKey &other) const {
|
|
|
|
return !(*this == other);
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
2017-08-18 19:14:31 +00:00
|
|
|
SharedMediaType type = SharedMediaType::kCount;
|
2017-09-04 11:40:02 +00:00
|
|
|
MsgId messageId = 0;
|
2017-08-18 19:14:31 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SharedMediaQuery {
|
|
|
|
SharedMediaQuery(
|
|
|
|
SharedMediaKey key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter)
|
2017-10-29 15:32:01 +00:00
|
|
|
: key(key)
|
|
|
|
, limitBefore(limitBefore)
|
|
|
|
, limitAfter(limitAfter) {
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedMediaKey key;
|
2017-09-04 11:40:02 +00:00
|
|
|
int limitBefore = 0;
|
|
|
|
int limitAfter = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
using SharedMediaResult = SparseIdsListResult;
|
2017-09-04 11:40:02 +00:00
|
|
|
|
2017-08-18 19:14:31 +00:00
|
|
|
struct SharedMediaSliceUpdate {
|
|
|
|
SharedMediaSliceUpdate(
|
|
|
|
PeerId peerId,
|
|
|
|
SharedMediaType type,
|
2017-10-29 15:32:01 +00:00
|
|
|
const SparseIdsSliceUpdate &data)
|
|
|
|
: peerId(peerId)
|
|
|
|
, type(type)
|
|
|
|
, data(data) {
|
2017-08-18 19:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
SharedMediaType type = SharedMediaType::kCount;
|
2017-10-29 15:32:01 +00:00
|
|
|
SparseIdsSliceUpdate data;
|
2017-08-18 19:14:31 +00:00
|
|
|
};
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
class SharedMedia {
|
2017-09-04 11:40:02 +00:00
|
|
|
public:
|
|
|
|
using Type = SharedMediaType;
|
|
|
|
|
|
|
|
void add(SharedMediaAddNew &&query);
|
|
|
|
void add(SharedMediaAddExisting &&query);
|
|
|
|
void add(SharedMediaAddSlice &&query);
|
|
|
|
void remove(SharedMediaRemoveOne &&query);
|
|
|
|
void remove(SharedMediaRemoveAll &&query);
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
rpl::producer<SharedMediaResult> query(SharedMediaQuery &&query) const;
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<SharedMediaSliceUpdate> sliceUpdated() const;
|
|
|
|
rpl::producer<SharedMediaRemoveOne> oneRemoved() const;
|
|
|
|
rpl::producer<SharedMediaRemoveAll> allRemoved() const;
|
2017-08-18 19:14:31 +00:00
|
|
|
|
2017-09-04 11:40:02 +00:00
|
|
|
private:
|
2017-10-29 15:32:01 +00:00
|
|
|
using Lists = std::array<SparseIdsList, kSharedMediaTypeCount>;
|
2017-09-04 11:40:02 +00:00
|
|
|
|
2017-08-29 19:52:52 +00:00
|
|
|
std::map<PeerId, Lists>::iterator enforceLists(PeerId peer);
|
2017-08-18 19:14:31 +00:00
|
|
|
|
2017-09-04 11:40:02 +00:00
|
|
|
std::map<PeerId, Lists> _lists;
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
rpl::event_stream<SharedMediaSliceUpdate> _sliceUpdated;
|
|
|
|
rpl::event_stream<SharedMediaRemoveOne> _oneRemoved;
|
|
|
|
rpl::event_stream<SharedMediaRemoveAll> _allRemoved;
|
|
|
|
|
2017-09-30 18:26:45 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2017-09-04 11:40:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Storage
|