using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Threading.Tasks; using Model = Discord.API.Message; namespace Discord.Rest { /// /// Represents a REST-based message sent by a user. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class RestUserMessage : RestMessage, IUserMessage { private bool _isMentioningEveryone, _isTTS, _isPinned, _isSuppressed; private long? _editedTimestampTicks; private ImmutableArray _attachments = ImmutableArray.Create(); private ImmutableArray _embeds = ImmutableArray.Create(); private ImmutableArray _tags = ImmutableArray.Create(); private ImmutableArray _roleMentionIds = ImmutableArray.Create(); private ImmutableArray _userMentions = ImmutableArray.Create(); /// public override bool IsTTS => _isTTS; /// public override bool IsPinned => _isPinned; /// public override bool IsSuppressed => _isSuppressed; /// public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks); /// public override bool MentionedEveryone => _isMentioningEveryone; /// public override IReadOnlyCollection Attachments => _attachments; /// public override IReadOnlyCollection Embeds => _embeds; /// public override IReadOnlyCollection MentionedChannelIds => MessageHelper.FilterTagsByKey(TagType.ChannelMention, _tags); /// public override IReadOnlyCollection MentionedRoleIds => _roleMentionIds; /// public override IReadOnlyCollection MentionedUsers => _userMentions; /// public override IReadOnlyCollection Tags => _tags; internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author, MessageSource source) : base(discord, id, channel, author, source) { } internal new static RestUserMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model) { var entity = new RestUserMessage(discord, model.Id, channel, author, MessageHelper.GetSource(model)); entity.Update(model); return entity; } internal override void Update(Model model) { base.Update(model); if (model.IsTextToSpeech.IsSpecified) _isTTS = model.IsTextToSpeech.Value; if (model.Pinned.IsSpecified) _isPinned = model.Pinned.Value; if (model.EditedTimestamp.IsSpecified) _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks; if (model.MentionEveryone.IsSpecified) _isMentioningEveryone = model.MentionEveryone.Value; if (model.Flags.IsSpecified) { _isSuppressed = model.Flags.Value.HasFlag(API.MessageFlags.Suppressed); } if (model.RoleMentions.IsSpecified) _roleMentionIds = model.RoleMentions.Value.ToImmutableArray(); if (model.Attachments.IsSpecified) { var value = model.Attachments.Value; if (value.Length > 0) { var attachments = ImmutableArray.CreateBuilder(value.Length); for (int i = 0; i < value.Length; i++) attachments.Add(Attachment.Create(value[i])); _attachments = attachments.ToImmutable(); } else _attachments = ImmutableArray.Create(); } if (model.Embeds.IsSpecified) { var value = model.Embeds.Value; if (value.Length > 0) { var embeds = ImmutableArray.CreateBuilder(value.Length); for (int i = 0; i < value.Length; i++) embeds.Add(value[i].ToEntity()); _embeds = embeds.ToImmutable(); } else _embeds = ImmutableArray.Create(); } if (model.UserMentions.IsSpecified) { var value = model.UserMentions.Value; if (value.Length > 0) { var newMentions = ImmutableArray.CreateBuilder(value.Length); for (int i = 0; i < value.Length; i++) { var val = value[i]; if (val.Object != null) newMentions.Add(RestUser.Create(Discord, val.Object)); } _userMentions = newMentions.ToImmutable(); } } if (model.Content.IsSpecified) { var text = model.Content.Value; var guildId = (Channel as IGuildChannel)?.GuildId; var guild = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null; _tags = MessageHelper.ParseTags(text, null, guild, _userMentions); model.Content = text; } } /// public async Task ModifyAsync(Action func, RequestOptions options = null) { var model = await MessageHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false); Update(model); } /// public Task PinAsync(RequestOptions options = null) => MessageHelper.PinAsync(this, Discord, options); /// public Task UnpinAsync(RequestOptions options = null) => MessageHelper.UnpinAsync(this, Discord, options); /// public Task ModifySuppressionAsync(bool suppressEmbeds, RequestOptions options = null) => MessageHelper.SuppressEmbedsAsync(this, Discord, suppressEmbeds, options); public string Resolve(int startIndex, TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name, TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name) => MentionUtils.Resolve(this, startIndex, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling); /// public string Resolve(TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name, TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name) => MentionUtils.Resolve(this, 0, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling); /// /// This operation may only be called on a channel. public async Task CrosspostAsync(RequestOptions options = null) { if (!(Channel is RestNewsChannel)) { throw new InvalidOperationException("Publishing (crossposting) is only valid in news channels."); } await MessageHelper.CrosspostAsync(this, Discord, options); } private string DebuggerDisplay => $"{Author}: {Content} ({Id}{(Attachments.Count > 0 ? $", {Attachments.Count} Attachments" : "")})"; } }