public override void Initialize()
        {
            base.Initialize();

            lock (_dataContext)
            {
                _dataContext.Connection.Open();

                // メインチャンネル
                if ((from g1 in _dataContext.Group
                     where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == CurrentSession.Config.ChannelName
                     select g1).Count() == 0)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        Group g = new Group
                                      {Name = CurrentSession.Config.ChannelName, UserId = CurrentSession.TwitterUser.Id};
                        ctx.Group.InsertOnSubmit(g);
                        ctx.SubmitChanges();
                    }
                }

                UpdateGroupCache();
            }
            CurrentSession.AddInsLoadCompleted += new EventHandler<EventArgs>(CurrentSession_AddInsLoadCompleted);
            CurrentSession.PreProcessTimelineStatuses += new EventHandler<TimelineStatusesEventArgs>(CurrentSession_PreProcessTimelineStatuses);
            CurrentSession.PreProcessTimelineStatus += new EventHandler<TimelineStatusEventArgs>(CurrentSession_PreProcessTimelineStatus);
            CurrentSession.PostSendGroupMessageTimelineStatus += new EventHandler<TimelineStatusGroupEventArgs>(CurrentSession_PostSendGroupMessageTimelineStatus);
            CurrentSession.PostProcessTimelineStatuses += new EventHandler<TimelineStatusesEventArgs>(CurrentSession_PostProcessTimelineStatuses);
        }
 void CurrentSession_PostSendGroupMessageTimelineStatus(object sender, TimelineStatusGroupEventArgs e)
 {
     lock (_dataContext)
     {
         using (var ctx = new TwitterIrcGatewayDataContext())
         {
             try
             {
                 Timeline timeline = new Timeline
                                         {
                                             GroupId = _cacheGroup[e.Group.Name].Id,
                                             StatusId = e.Status.Id,
                                             UserId = CurrentSession.TwitterUser.Id
                                         };
                 if (ctx.Timeline.Contains(timeline))
                     return;
                 ctx.Timeline.InsertOnSubmit(timeline);
                 ctx.SubmitChanges();
             }
             catch (Exception)
             {
                 CurrentSession.Logger.Error("Group not found in _cacheGroup: {0}", e.Group.Name);
                 throw;
             }
         }
     }
 }
        public bool TryGetValue(string typableMapId, out Misuzilla.Applications.TwitterIrcGateway.Status status)
        {
            Int64 statusId;

            status = null;

            if (_typableMap.TryGetValue(typableMapId, out statusId))
            {
                using (TwitterIrcGatewayDataContext ctx = new TwitterIrcGatewayDataContext())
                {
                    var dbStatus = ctx.Status.Where(s => s.Id == statusId).FirstOrDefault();
                    if (dbStatus != null)
                    {
                        status      = new TwitterIrcGateway.Status();
                        status.Id   = dbStatus.Id;
                        status.Text = dbStatus.Text;
                        status.InReplyToStatusId = dbStatus.InReplyToId.HasValue ? dbStatus.InReplyToId.Value.ToString() : null;
                        status.CreatedAt         = dbStatus.CreatedAt;
                        status.User            = new TwitterIrcGateway.User();
                        status.User.ScreenName = dbStatus.ScreenName;
                        if (dbStatus.User != null)
                        {
                            status.User.Id              = dbStatus.User.Id;
                            status.User.ScreenName      = dbStatus.User.ScreenName;
                            status.User.Name            = dbStatus.User.Name;
                            status.User.Protected       = dbStatus.User.IsProtected;
                            status.User.ProfileImageUrl = dbStatus.User.ProfileImageUrl;
                        }
                    }
                }
            }

            return(status != null);
        }
Example #4
0
 void CurrentSession_PostSendGroupMessageTimelineStatus(object sender, TimelineStatusGroupEventArgs e)
 {
     lock (_dataContext)
     {
         using (var ctx = new TwitterIrcGatewayDataContext())
         {
             try
             {
                 Timeline timeline = new Timeline
                 {
                     GroupId  = _cacheGroup[e.Group.Name].Id,
                     StatusId = e.Status.Id,
                     UserId   = CurrentSession.TwitterUser.Id
                 };
                 if (ctx.Timeline.Contains(timeline))
                 {
                     return;
                 }
                 ctx.Timeline.InsertOnSubmit(timeline);
                 ctx.SubmitChanges();
             }
             catch (Exception)
             {
                 CurrentSession.Logger.Error("Group not found in _cacheGroup: {0}", e.Group.Name);
                 throw;
             }
         }
     }
 }
Example #5
0
        public override void Initialize()
        {
            base.Initialize();

            lock (_dataContext)
            {
                _dataContext.Connection.Open();

                // メインチャンネル
                if ((from g1 in _dataContext.Group
                     where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == CurrentSession.Config.ChannelName
                     select g1).Count() == 0)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        Group g = new Group
                        {
                            Name = CurrentSession.Config.ChannelName, UserId = CurrentSession.TwitterUser.Id
                        };
                        ctx.Group.InsertOnSubmit(g);
                        ctx.SubmitChanges();
                    }
                }

                UpdateGroupCache();
            }
            CurrentSession.AddInsLoadCompleted                += new EventHandler <EventArgs>(CurrentSession_AddInsLoadCompleted);
            CurrentSession.PreProcessTimelineStatuses         += new EventHandler <TimelineStatusesEventArgs>(CurrentSession_PreProcessTimelineStatuses);
            CurrentSession.PreProcessTimelineStatus           += new EventHandler <TimelineStatusEventArgs>(CurrentSession_PreProcessTimelineStatus);
            CurrentSession.PostSendGroupMessageTimelineStatus += new EventHandler <TimelineStatusGroupEventArgs>(CurrentSession_PostSendGroupMessageTimelineStatus);
            CurrentSession.PostProcessTimelineStatuses        += new EventHandler <TimelineStatusesEventArgs>(CurrentSession_PostProcessTimelineStatuses);
        }
        public bool TryGetValue(string typableMapId, out Misuzilla.Applications.TwitterIrcGateway.Status status)
        {
            Int64 statusId;
            status = null;

            if (_typableMap.TryGetValue(typableMapId, out statusId))
            {
                using (TwitterIrcGatewayDataContext ctx = new TwitterIrcGatewayDataContext())
                {
                    var dbStatus = ctx.Status.Where(s => s.Id == statusId).FirstOrDefault();
                    if (dbStatus != null)
                    {
                        status = new TwitterIrcGateway.Status();
                        status.Id = dbStatus.Id;
                        status.Text = dbStatus.Text;
                        status.InReplyToStatusId = dbStatus.InReplyToId.HasValue ? dbStatus.InReplyToId.Value.ToString() : null;
                        status.CreatedAt = dbStatus.CreatedAt;
                        status.User = new TwitterIrcGateway.User();
                        status.User.ScreenName = dbStatus.ScreenName;
                        if (dbStatus.User != null)
                        {
                            status.User.Id = dbStatus.User.Id;
                            status.User.ScreenName = dbStatus.User.ScreenName;
                            status.User.Name = dbStatus.User.Name;
                            status.User.Protected = dbStatus.User.IsProtected;
                            status.User.ProfileImageUrl = dbStatus.User.ProfileImageUrl;
                        }
                    }
                }
            }

            return (status != null);
        }
Example #7
0
        void CurrentSession_PreProcessTimelineStatus(object sender, TimelineStatusEventArgs e)
        {
            lock (_dataContext)
            {
                var status = new Status
                {
                    Id         = e.Status.Id,
                    CreatedAt  = e.Status.CreatedAt,
                    ScreenName = e.Status.User.ScreenName,
                    Text       = e.Status.Text,
                    UserId     = (e.Status.User.Id == 0) ? null : (Int32?)e.Status.User.Id
                };

                if (!_dataContext.Status.Contains(status))
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        try
                        {
                            ctx.Status.InsertOnSubmit(status);
                            ctx.SubmitChanges();
                        }
                        catch (DuplicateKeyException dupE)
                        {
                        }
                        catch (SqlException sqlE)
                        {
                            // キー制約
                            if (sqlE.Number == 2627)
                            {
                                return;
                            }

                            throw;
                        }
                    }
                }
            }
        }
        void CurrentSession_PreProcessTimelineStatus(object sender, TimelineStatusEventArgs e)
        {
            lock (_dataContext)
            {
                var status = new Status
                                          {
                                              Id = e.Status.Id,
                                              CreatedAt = e.Status.CreatedAt,
                                              ScreenName = e.Status.User.ScreenName,
                                              Text = e.Status.Text,
                                              UserId = (e.Status.User.Id == 0) ? null : (Int32?)e.Status.User.Id
                                          };

                if (!_dataContext.Status.Contains(status))
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        try
                        {
                            ctx.Status.InsertOnSubmit(status);
                            ctx.SubmitChanges();
                        }
                        catch (DuplicateKeyException dupE)
                        {
                        }
                        catch (SqlException sqlE)
                        {
                            // キー制約
                            if (sqlE.Number == 2627)
                                return;

                            throw;
                        }
                    }
                }
            }
        }
        void CurrentSession_PreProcessTimelineStatuses(object sender, TimelineStatusesEventArgs e)
        {
            lock (_dataContext)
            {
                // FIXME: この辺はあとで何とかする
                var notFoundCount = (from g in CurrentSession.Groups.Keys
                                     where !_cacheGroup.ContainsKey(g)
                                     select g).Count();
                if (notFoundCount > 0)
                {
                    var newGroups = from g in CurrentSession.Groups.Keys
                                    let count = (from g1 in _dataContext.Group
                                                 where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == g
                                                 select g1).Count()
                                    where count == 0
                                    select new Group() {Name = g, UserId = CurrentSession.TwitterUser.Id};
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        ctx.Group.InsertAllOnSubmit(newGroups);
                        ctx.SubmitChanges();
                    }
                    UpdateGroupCache();
                }
                // メインチャンネル
                if ((from g1 in _dataContext.Group
                     where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == CurrentSession.Config.ChannelName
                     select g1).Count() == 0)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        Group g = new Group { Name = CurrentSession.Config.ChannelName, UserId = CurrentSession.TwitterUser.Id };
                        ctx.Group.InsertOnSubmit(g);
                        ctx.SubmitChanges();
                    }
                }

                var newTwitterUsers =
                    (from status in e.Statuses.Status where status.User.Id != 0 select status.User).Distinct();
                foreach (var user in newTwitterUsers)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        try
                        {
                            User newUser = new User
                                           {
                                               Id = user.Id,
                                               Name = user.Name,
                                               IsProtected = user.Protected,
                                               ProfileImageUrl = user.ProfileImageUrl,
                                               ScreenName = user.ScreenName
                                           };
                            if (!ctx.User.Contains(newUser))
                            {
                                ctx.User.InsertOnSubmit(newUser);
                                ctx.SubmitChanges();
                            }
                        }
                        catch (DuplicateKeyException dupE)
                        {
                            continue;
                        }
                        catch (SqlException sqlE)
                        {
                            // キー制約
                            if (sqlE.Number == 2627)
                                continue;

                            throw;
                        }
                    }
                }
            }
        }
Example #10
0
        void CurrentSession_PreProcessTimelineStatuses(object sender, TimelineStatusesEventArgs e)
        {
            lock (_dataContext)
            {
                // FIXME: この辺はあとで何とかする
                var notFoundCount = (from g in CurrentSession.Groups.Keys
                                     where !_cacheGroup.ContainsKey(g)
                                     select g).Count();
                if (notFoundCount > 0)
                {
                    var newGroups = from g in CurrentSession.Groups.Keys
                                    let count = (from g1 in _dataContext.Group
                                                 where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == g
                                                 select g1).Count()
                                                where count == 0
                                                select new Group()
                    {
                        Name = g, UserId = CurrentSession.TwitterUser.Id
                    };
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        ctx.Group.InsertAllOnSubmit(newGroups);
                        ctx.SubmitChanges();
                    }
                    UpdateGroupCache();
                }
                // メインチャンネル
                if ((from g1 in _dataContext.Group
                     where g1.UserId == CurrentSession.TwitterUser.Id && g1.Name == CurrentSession.Config.ChannelName
                     select g1).Count() == 0)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        Group g = new Group {
                            Name = CurrentSession.Config.ChannelName, UserId = CurrentSession.TwitterUser.Id
                        };
                        ctx.Group.InsertOnSubmit(g);
                        ctx.SubmitChanges();
                    }
                }

                var newTwitterUsers =
                    (from status in e.Statuses.Status where status.User.Id != 0 select status.User).Distinct();
                foreach (var user in newTwitterUsers)
                {
                    using (var ctx = new TwitterIrcGatewayDataContext())
                    {
                        try
                        {
                            User newUser = new User
                            {
                                Id              = user.Id,
                                Name            = user.Name,
                                IsProtected     = user.Protected,
                                ProfileImageUrl = user.ProfileImageUrl,
                                ScreenName      = user.ScreenName
                            };
                            if (!ctx.User.Contains(newUser))
                            {
                                ctx.User.InsertOnSubmit(newUser);
                                ctx.SubmitChanges();
                            }
                        }
                        catch (DuplicateKeyException dupE)
                        {
                            continue;
                        }
                        catch (SqlException sqlE)
                        {
                            // キー制約
                            if (sqlE.Number == 2627)
                            {
                                continue;
                            }

                            throw;
                        }
                    }
                }
            }
        }