1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-12 09:29:55 +01:00
Telegram/Share/TGSendMessageSignals.m

92 lines
3.0 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "TGSendMessageSignals.h"
#import "TGUserModel.h"
2016-02-25 01:03:51 +01:00
#import "TGChannelChatModel.h"
2015-10-01 18:19:52 +02:00
@implementation TGSendMessageSignals
2016-02-25 01:03:51 +01:00
+ (Api48_InputPeer *)inputPeerForPeerId:(TGPeerId)peerId users:(NSArray *)users isChannel:(bool *)isChannel
2015-10-01 18:19:52 +02:00
{
switch (peerId.namespaceId)
{
case TGPeerIdPrivate:
{
2016-02-25 01:03:51 +01:00
for (id model in users)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
if ([model isKindOfClass:[TGUserModel class]] && ((TGUserModel *)model).userId == peerId.peerId)
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
TGUserModel *user = (TGUserModel *)model;
2015-10-01 18:19:52 +02:00
if (user.accessHash == -1)
2016-02-25 01:03:51 +01:00
return [Api48_InputPeer inputPeerSelf];
2015-10-01 18:19:52 +02:00
else
2016-02-25 01:03:51 +01:00
return [Api48_InputPeer inputPeerUserWithUserId:@(user.userId) accessHash:@(user.accessHash)];
2015-10-01 18:19:52 +02:00
}
}
}
2016-02-25 01:03:51 +01:00
break;
2015-10-01 18:19:52 +02:00
case TGPeerIdGroup:
2016-02-25 01:03:51 +01:00
{
return [Api48_InputPeer inputPeerChatWithChatId:@(peerId.peerId)];
}
break;
case TGPeerIdChannel:
{
for (id model in users)
{
if ([model isKindOfClass:[TGChannelChatModel class]] && ((TGChannelChatModel *)model).peerId.peerId == peerId.peerId)
{
TGChannelChatModel *channel = (TGChannelChatModel *)model;
if (isChannel != NULL)
*isChannel = !channel.isGroup;
return [Api48_InputPeer inputPeerChannelWithChannelId:@(channel.peerId.peerId) accessHash:@(channel.accessHash)];
}
}
}
break;
2015-10-01 18:19:52 +02:00
default:
break;
}
return nil;
}
+ (SSignal *)sendTextMessageWithContext:(TGShareContext *)context peerId:(TGPeerId)peerId users:(NSArray *)users text:(NSString *)text
{
2016-02-25 01:03:51 +01:00
bool isChannel = false;
Api48_InputPeer *inputPeer = [self inputPeerForPeerId:peerId users:users isChannel:&isChannel];
2015-10-01 18:19:52 +02:00
if (inputPeer == nil)
return [SSignal fail:nil];
int64_t randomId = 0;
arc4random_buf(&randomId, 8);
2016-02-25 01:03:51 +01:00
int32_t flags = 0;
if (isChannel)
flags |= 16;
flags |= (1 << 6);
return [context function:[Api48 messages_sendMessageWithFlags:@(flags) peer:inputPeer replyToMsgId:@(0) message:text randomId:@(randomId) replyMarkup:nil entities:@[]]];
2015-10-01 18:19:52 +02:00
}
2016-02-25 01:03:51 +01:00
+ (SSignal *)sendMediaWithContext:(TGShareContext *)context peerId:(TGPeerId)peerId users:(NSArray *)users inputMedia:(Api48_InputMedia *)inputMedia
2015-10-01 18:19:52 +02:00
{
2016-02-25 01:03:51 +01:00
bool isChannel = false;
Api48_InputPeer *inputPeer = [self inputPeerForPeerId:peerId users:users isChannel:&isChannel];
2015-10-01 18:19:52 +02:00
if (inputPeer == nil)
return [SSignal fail:nil];
int64_t randomId = 0;
arc4random_buf(&randomId, 8);
2016-02-25 01:03:51 +01:00
int32_t flags = 0;
if (isChannel)
flags |= 16;
flags |= (1 << 6);
return [context function:[Api48 messages_sendMediaWithFlags:@(flags) peer:inputPeer replyToMsgId:@(0) media:inputMedia randomId:@(randomId) replyMarkup:nil]];
2015-10-01 18:19:52 +02:00
}
@end