public async Task <bool> Toot(string status, StatusVisibilityEnum visibility = StatusVisibilityEnum.Public, int inReplyToId = -1, int[] mediaIds = null, bool sensitive = false, string spoilerText = null)
        {
            using (log.BeginScope($"{ nameof(MastodonInstanceService) }->{ nameof(Toot) } "))
            {
                log.LogDebug(" Parameters - status: {status}, visibility: {visibility}, inReplyToId: {inReplyToId}, mediaIds: {mediaIds}, sensitive: {sensitive}, spoilerText: {spoilerText}",
                             status,
                             visibility,
                             inReplyToId,
                             mediaIds,
                             sensitive,
                             spoilerText
                             );

                // Only if configured
                if (!cfg.FullInstanceRegistrationCompleted)
                {
                    log.LogError("No instanceRegistration given.");
                    return(false);
                }

                var instance = cfg.Application.Instance;

                log.LogDebug("Create MastodonClient");

                try
                {
                    using (var mastodonClient = new MastodonClient(instance.Name))
                    {
                        var postResult = await mastodonClient
                                         .PostNewStatusAsync(
                            instance.AccessToken,
                            status, visibility,
                            inReplyToId,
                            mediaIds,
                            sensitive,
                            spoilerText);

                        log.LogDebug("PostNewStatusAsync returned");

                        if (postResult.created_at == null)
                        {
                            log.LogError("postResult.created_at is null");
                            return(false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.LogError(ex, "Toot - Exception");
                    return(false);
                }

                return(true);
            }
        }
        public static string GetVisibility(StatusVisibilityEnum visibility)
        {
            switch (visibility)
            {
            case StatusVisibilityEnum.Public: return(StatusVisibility.Public);

            case StatusVisibilityEnum.Direct: return(StatusVisibility.Direct);

            case StatusVisibilityEnum.Private: return(StatusVisibility.Private);

            case StatusVisibilityEnum.Unlisted: return(StatusVisibility.Unlisted);

            default: throw new ArgumentException("StatusVisibilityEnum value not supported");
            }
        }
Пример #3
0
        public async Task <Status> PostNewStatusAsync(string accessToken, string status, StatusVisibilityEnum visibility = StatusVisibilityEnum.Public, int inReplyToId = -1, int[] mediaIds = null, bool sensitive = false, string spoilerText = null)
        {
            var content = new List <KeyValuePair <string, string> >();

            content.Add(new KeyValuePair <string, string>("status", status));
            content.Add(new KeyValuePair <string, string>("sensitive", sensitive.ToString().ToLowerInvariant()));
            content.Add(new KeyValuePair <string, string>("visibility", StatusVisibilityConverter.GetVisibility(visibility)));

            if (inReplyToId != -1)
            {
                content.Add(new KeyValuePair <string, string>("in_reply_to_id", inReplyToId.ToString()));
            }
            if (!string.IsNullOrWhiteSpace(spoilerText))
            {
                content.Add(new KeyValuePair <string, string>("spoiler_text", spoilerText));
            }

            if (mediaIds != null)
            {
                foreach (var id in mediaIds)
                {
                    content.Add(new KeyValuePair <string, string>("media_ids[]", id.ToString()));
                }
            }

            return(await PostDataAsync <Status>(accessToken, ApiRoutes.PostNewStatus, content));
        }