Exemplo n.º 1
0
        private static async Task <TwitterStatus> LoadDirectMessageAsync([NotNull] DatabaseStatus dbstatus)
        {
            if (dbstatus == null)
            {
                throw new ArgumentNullException("dbstatus");
            }
            if (dbstatus.InReplyToOrRecipientUserId == null)
            {
                throw new ArgumentException("dbstatus.InReplyToUserOrRecipientId is must not be null.");
            }
            var id        = dbstatus.Id;
            var user      = UserProxy.GetUserAsync(dbstatus.UserId).ConfigureAwait(false);
            var recipient = UserProxy.GetUserAsync(dbstatus.InReplyToOrRecipientUserId.Value).ConfigureAwait(false);
            var se        = DatabaseUtil.RetryIfLocked(() => Database.StatusEntityCrud.GetEntitiesAsync(id)).ConfigureAwait(false);

            try
            {
                return(Mapper.Map(dbstatus, await se, await user, await recipient));
            }
            catch (ArgumentNullException anex)
            {
                throw new DatabaseConsistencyException(
                          "Lacking required data in database.(mode: DM, status ID " + dbstatus.Id + ", user ID " + dbstatus.UserId + ")",
                          anex);
            }
        }
Exemplo n.º 2
0
        private static async Task <TwitterStatus> LoadDirectMessageAsync([NotNull] DatabaseStatus dbstatus)
        {
            if (dbstatus == null)
            {
                throw new ArgumentNullException("dbstatus");
            }
            if (dbstatus.InReplyToOrRecipientUserId == null)
            {
                throw new ArgumentException("dbstatus.InReplyToUserOrRecipientId is must not be null.");
            }
            var id        = dbstatus.Id;
            var user      = UserProxy.GetUserAsync(dbstatus.UserId);
            var recipient = UserProxy.GetUserAsync(dbstatus.InReplyToOrRecipientUserId.Value);
            var se        = Database.StatusEntityCrud.GetEntitiesAsync(id);

            try
            {
                return(Mapper.Map(dbstatus,
                                  await DatabaseUtil.RetryIfLocked(async() => await se),
                                  await user, await recipient));
            }
            catch (ArgumentNullException anex)
            {
                throw new DatabaseConsistencyException(
                          "データベースから必要なデータを読み出せませんでした。(モード: DM, ステータスID " + dbstatus.Id + ", ユーザID " + dbstatus.UserId + ")",
                          anex);
            }
        }
Exemplo n.º 3
0
 public static async Task <DatabaseList> GetListDescription(ListInfo listInfo)
 {
     return(await Task.Run(async() =>
     {
         var userId = UserProxy.GetId(listInfo.OwnerScreenName);
         return await Database.ListCrud.GetAsync(userId, listInfo.Slug).ConfigureAwait(false);
     }).ConfigureAwait(false));
 }
Exemplo n.º 4
0
 public static async Task <DatabaseList> GetOrReceiveListDescription(IApiAccessor accessor, ListInfo listInfo)
 {
     return(await Task.Run(async() =>
     {
         var userId = UserProxy.GetId(listInfo.OwnerScreenName);
         var dbl = await Database.ListCrud.GetAsync(userId, listInfo.Slug).ConfigureAwait(false);
         if (dbl != null)
         {
             return dbl;
         }
         var twl = await accessor.ShowListAsync(listInfo.ToParameter(), CancellationToken.None);
         return await SetListDescription(twl.Result).ConfigureAwait(false);
     }).ConfigureAwait(false));
 }
Exemplo n.º 5
0
        private static async Task <TwitterStatus> LoadPublicStatusAsync([CanBeNull] DatabaseStatus dbstatus)
        {
            if (dbstatus == null)
            {
                throw new ArgumentNullException(nameof(dbstatus));
            }
            var id   = dbstatus.Id;
            var user = DatabaseUtil.RetryIfLocked(() =>
                                                  UserProxy.GetUserAsync(dbstatus.UserId)).ConfigureAwait(false);
            var se = DatabaseUtil.RetryIfLocked(() =>
                                                Database.StatusEntityCrud.GetEntitiesAsync(id)).ConfigureAwait(false);
            var favorers = DatabaseUtil.RetryIfLocked(() =>
                                                      Database.FavoritesCrud.GetUsersAsync(id)).ConfigureAwait(false);
            var retweeters = DatabaseUtil.RetryIfLocked(() =>
                                                        Database.RetweetsCrud.GetUsersAsync(id)).ConfigureAwait(false);

            try
            {
                if (dbstatus.RetweetOriginalId != null || dbstatus.QuoteId != null)
                {
                    var orig = dbstatus.RetweetOriginalId == null
                        ? null
                        : await GetStatusAsync(dbstatus.RetweetOriginalId.Value).ConfigureAwait(false);

                    var quote = dbstatus.QuoteId == null
                        ? null
                        : await GetStatusAsync(dbstatus.QuoteId.Value).ConfigureAwait(false);

                    return(Mapper.Map(dbstatus,
                                      await se,
                                      await favorers,
                                      await retweeters,
                                      orig, quote, await user));
                }
                return(Mapper.Map(dbstatus, await se, await favorers, await retweeters, await user));
            }
            catch (ArgumentNullException anex)
            {
                throw new DatabaseConsistencyException(
                          "Lacking required data in database.(mode: PS, status ID " + dbstatus.Id + ", user ID " +
                          dbstatus.UserId + ")",
                          anex);
            }
        }
Exemplo n.º 6
0
        private static async Task <TwitterStatus> LoadPublicStatusAsync([NotNull] DatabaseStatus dbstatus)
        {
            if (dbstatus == null)
            {
                throw new ArgumentNullException("dbstatus");
            }
            var id   = dbstatus.Id;
            var user = DatabaseUtil.RetryIfLocked(async() =>
                                                  await UserProxy.GetUserAsync(dbstatus.UserId));
            var se = DatabaseUtil.RetryIfLocked(async() =>
                                                await Database.StatusEntityCrud.GetEntitiesAsync(id));
            var favorers = DatabaseUtil.RetryIfLocked(async() =>
                                                      await Database.FavoritesCrud.GetUsersAsync(id));
            var retweeters = DatabaseUtil.RetryIfLocked(async() =>
                                                        await Database.RetweetsCrud.GetUsersAsync(id));

            try
            {
                if (dbstatus.RetweetOriginalId != null)
                {
                    var orig = await GetStatusAsync(dbstatus.RetweetOriginalId.Value);

                    if (orig != null)
                    {
                        return(Mapper.Map(dbstatus,
                                          await se,
                                          await favorers,
                                          await retweeters,
                                          orig, await user));
                    }
                }
                return(Mapper.Map(dbstatus, await se, await favorers, await retweeters, await user));
            }
            catch (ArgumentNullException anex)
            {
                throw new DatabaseConsistencyException(
                          "データベースから必要なデータを読み出せませんでした。(モード: PS, ステータスID " + dbstatus.Id + ", ユーザID " + dbstatus.UserId + ")",
                          anex);
            }
        }
Exemplo n.º 7
0
        private static async Task <IEnumerable <TwitterStatus> > LoadStatusesAsync([NotNull] IEnumerable <DatabaseStatus> dbstatus)
        {
            if (dbstatus == null)
            {
                throw new ArgumentNullException("dbstatus");
            }
            var targets = dbstatus.ToArray();

            if (targets.Length == 0)
            {
                return(Enumerable.Empty <TwitterStatus>());
            }
            var retweetOriginalIds  = new HashSet <long>();
            var targetUserIds       = new HashSet <long>();
            var entitiesTargetIds   = new HashSet <long>();
            var activitiesTargetIds = new HashSet <long>();

            foreach (var status in targets)
            {
                targetUserIds.Add(status.UserId);
                entitiesTargetIds.Add(status.Id);
                switch (status.StatusType)
                {
                case StatusType.Tweet:
                    activitiesTargetIds.Add(status.Id);
                    if (status.RetweetOriginalId != null)
                    {
                        Debug.Assert(status.RetweetOriginalUserId != null,
                                     "status.RetweetOriginalUserId != null");
                        targetUserIds.Add(status.RetweetOriginalUserId.Value);
                        retweetOriginalIds.Add(status.RetweetOriginalId.Value);
                        entitiesTargetIds.Add(status.RetweetOriginalId.Value);
                    }
                    break;

                case StatusType.DirectMessage:
                    Debug.Assert(status.InReplyToOrRecipientUserId != null,
                                 "status.InReplyToOrRecipientUserId != null");
                    targetUserIds.Add(status.InReplyToOrRecipientUserId.Value);
                    break;
                }
            }
            // accessing database
            var retweetsTask = DatabaseUtil.RetryIfLocked(() =>
                                                          Database.StatusCrud.GetStatusesAsync(retweetOriginalIds)).ConfigureAwait(false);
            var usersTask = UserProxy.GetUsersAsync(targetUserIds).ConfigureAwait(false);
            var sesTask   = DatabaseUtil.RetryIfLocked(() =>
                                                       Database.StatusEntityCrud.GetEntitiesDictionaryAsync(entitiesTargetIds)).ConfigureAwait(false);
            var favdicTask = DatabaseUtil.RetryIfLocked(() =>
                                                        Database.FavoritesCrud.GetUsersDictionaryAsync(activitiesTargetIds)).ConfigureAwait(false);
            var rtdicTask = DatabaseUtil.RetryIfLocked(() =>
                                                       Database.RetweetsCrud.GetUsersDictionaryAsync(activitiesTargetIds)).ConfigureAwait(false);
            var retweets = (await retweetsTask).ToDictionary(d => d.Id);
            var users    = (await usersTask).ToDictionary(d => d.Id);
            var ses      = await sesTask;
            var favdic   = await favdicTask;
            var rtdic    = await rtdicTask;

            // create status entity
            var result = new List <TwitterStatus>();

            foreach (var status in targets)
            {
                var ents = Mapper.Resolve(ses, status.Id);
                switch (status.StatusType)
                {
                case StatusType.Tweet:
                    var favs = Mapper.Resolve(favdic, status.Id);
                    var rts  = Mapper.Resolve(rtdic, status.Id);
                    if (status.RetweetOriginalId != null)
                    {
                        Debug.Assert(status.RetweetOriginalUserId != null,
                                     "status.RetweetOriginalUserId != null");
                        var rtid    = status.RetweetOriginalId.Value;
                        var retweet = retweets[status.RetweetOriginalId.Value];
                        var orig    = Mapper.Map(retweet, Mapper.Resolve(ses, rtid),
                                                 Mapper.Resolve(favdic, rtid), Mapper.Resolve(rtdic, rtid),
                                                 users[status.RetweetOriginalUserId.Value]);
                        result.Add(Mapper.Map(status, ents, favs, rts, orig, users[status.UserId]));
                    }
                    else
                    {
                        result.Add(Mapper.Map(status, ents, favs, rts, users[status.UserId]));
                    }
                    break;

                case StatusType.DirectMessage:
                    Debug.Assert(status.InReplyToOrRecipientUserId != null,
                                 "status.InReplyToOrRecipientUserId != null");
                    result.Add(Mapper.Map(status, ents, users[status.UserId],
                                          users[status.InReplyToOrRecipientUserId.Value]));
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            return(result);
        }