public async Task SetItem(JabberId jid, OtrSaveMode saveMode)
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                var savedItem = await
                                    (from item in context.UserItemPreferences
                                    where item.ArchivingUserId == UserId && item.Jid == jid.FullId
                                    select item).FirstOrDefaultAsync();
                if (savedItem == null)
                {
                    savedItem = new Model.DbUserItemPreference();
                    savedItem.ArchivingUserId = UserId;
                    savedItem.Jid             = jid.FullId;
                    context.UserItemPreferences.Add(savedItem);
                }

                if (saveMode.Expire != FSharpOption <long> .None)
                {
                    savedItem.Expire = FSharpHelper.FromFSharpS(saveMode.Expire);
                }
                if (saveMode.OtrMode != FSharpOption <OtrMode> .None)
                {
                    savedItem.OtrMode = FromFSharp(saveMode.OtrMode.Value);
                }
                if (saveMode.SaveMode != FSharpOption <SaveMode> .None)
                {
                    savedItem.SaveMode = FromFSharp(saveMode.SaveMode.Value);
                }

                await context.MySaveChanges();
            }
        }
Esempio n. 2
0
        public Tuple <JabberId, XmlStanzas.Stanza <PresenceProcessingType> > ToFSharp()
        {
            var elem           = XElement.Parse(Content);
            var presenceStanza = Parsing.parsePresenceStanza("jabber:server", elem);
            var from           = JabberId.Parse(FromJid);

            return(Tuple.Create(from, presenceStanza));
        }
Esempio n. 3
0
        public static DbSubscriptionRequest FromFSharp(JabberId from, XmlStanzas.Stanza <PresenceProcessingType> presenceStanza)
        {
            var elem = XmlStanzas.Parsing.createStanzaElement("jabber:server", presenceStanza);

            return
                (new DbSubscriptionRequest()
            {
                FromJid = from.BareId,
                Content = elem.ToString()
            });
        }
Esempio n. 4
0
 public async Task RemoveSubscriptionRequest(JabberId value)
 {
     using (var context = CreateContext()) {
         var jid = value.BareId;
         context.SubscriptionRequests.RemoveRange
             (await(from req in context.SubscriptionRequests
                    where req.ApplicationUserId == user && req.FromJid == jid
                    select req).ToListAsync());
         await context.MySaveChanges();
     }
 }
        public async Task <FSharpOption <StoredPreferenceInfo> > GetUserPreferences()
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                var isDefaultModeUnset =
                    user.OtrMode == Model.OtrMode.NotSet && user.SaveMode == Model.SaveMode.NotSet && user.Expire == null;

                var isAnyArchivingModeUnset =
                    user.LocalPreference == Model.ArchivingMode.NotSet ||
                    user.ManualPreference == Model.ArchivingMode.NotSet ||
                    user.AutoPreference == Model.ArchivingMode.NotSet;

                var isAllUnset =
                    isDefaultModeUnset &&
                    user.ItemPreferences.Count == 0 && isAnyArchivingModeUnset && user.AutomaticArchiving == null;
                if (isAllUnset)
                {
                    return(FSharpOption <StoredPreferenceInfo> .None);
                }

                var archivingModePrefs =
                    !isAnyArchivingModeUnset
                                        ? FSharpOption <AllMethodSettings> .Some(
                        new AllMethodSettings (
                            ToFSharp(user.AutoPreference),
                            ToFSharp(user.LocalPreference),
                            ToFSharp(user.ManualPreference)))
                                        : FSharpOption <AllMethodSettings> .None;

                var result = new StoredPreferenceInfo(
                    FSharpHelper.ToFSharpS(user.AutomaticArchiving),
                    new OtrSaveMode(
                        FSharpHelper.ToFSharpS <Int64> (user.Expire),
                        ToFSharp(user.OtrMode),
                        ToFSharp(user.SaveMode)),
                    ListModule.OfSeq <Tuple <JabberId, OtrSaveMode> >
                        (user.ItemPreferences.Select(item =>
                                                     Tuple.Create(
                                                         JabberId.Parse(item.Jid),
                                                         new OtrSaveMode(
                                                             FSharpHelper.ToFSharpS <Int64> (item.Expire),
                                                             ToFSharp(item.OtrMode),
                                                             ToFSharp(item.SaveMode))))),
                    archivingModePrefs
                    );
                return(FSharpOption <StoredPreferenceInfo> .Some(result));
            }
        }
Esempio n. 6
0
 public async Task <Microsoft.FSharp.Core.FSharpOption <RosterItem> > GetItem(JabberId value)
 {
     using (var context = CreateContext()) {
         var jid        = value.BareId;
         var rosterItem = await
                              (from item in context.RosterItems
                              where item.ApplicationUserId == user
                              where item.Jid == jid
                              select item).FirstOrDefaultAsync();
         if (rosterItem == null)
         {
             return(FSharpOption <RosterItem> .None);
         }
         return(FSharpOption <RosterItem> .Some(rosterItem.ToFSharp()));
     }
 }
        //public void SetUserPreferences (StoredPreferenceInfo value)
        //{
        //	using (var context = this.CreateContext ()) {
        //		var user = GetUser (context);
        //		if (value.AutomaticArchiving != FSharpOption<bool>.None) {
        //			user.AutomaticArchiving = FSharpHelper.FromFSharpS (value.AutomaticArchiving);
        //		}
        //
        //		if (value.DefaultOtrSaveMode.Expire != FSharpOption<ulong>.None) {
        //			user.Expire = FSharpHelper.FromFSharpS (value.DefaultOtrSaveMode.Expire);
        //		}
        //		if (value.DefaultOtrSaveMode.OtrMode != FSharpOption<OtrMode>.None) {
        //			user.OtrMode = FromFSharp (value.DefaultOtrSaveMode.OtrMode.Value);
        //		}
        //		if (value.DefaultOtrSaveMode.SaveMode != FSharpOption<SaveMode>.None) {
        //			user.SaveMode = FromFSharp (value.DefaultOtrSaveMode.SaveMode.Value);
        //		}
        //
        //		if (value.MethodSetting != FSharpOption<AllMethodSettings>.None) {
        //			user.AutoPreference = FromFSharp (value.MethodSetting.Value.Auto);
        //			user.LocalPreference = FromFSharp (value.MethodSetting.Value.Local);
        //			user.ManualPreference = FromFSharp (value.MethodSetting.Value.Manual);
        //		}
        //		context.MySaveChanges ();
        //	}
        //}
        public async Task RemoveItem(JabberId jid)
        {
            using (var context = this.CreateContext()) {
                var user = await GetUser(context);

                var savedItem = await
                                    (from item in context.UserItemPreferences
                                    where item.ArchivingUserId == UserId && item.Jid == jid.FullId
                                    select item).FirstOrDefaultAsync();

                if (savedItem != null)
                {
                    context.UserItemPreferences.Remove(savedItem);
                    await context.MySaveChanges();
                }
            }
        }
Esempio n. 8
0
        public async Task StoreSubscriptionRequest(JabberId jid, XmlStanzas.Stanza <PresenceProcessingType> value)
        {
            using (var context = CreateContext()) {
                var request = DbSubscriptionRequest.FromFSharp(jid, value);
                request.ApplicationUserId = user;
                var contextItem = await context.SubscriptionRequests.FindAsync(request.ApplicationUserId, request.FromJid);

                if (contextItem == null)
                {
                    context.SubscriptionRequests.Add(request);
                }
                else
                {
                    context.Entry(contextItem).CurrentValues.SetValues(request);
                }
                await context.MySaveChanges();
            }
        }
Esempio n. 9
0
        public RosterItem ToFSharp()
        {
            Microsoft.FSharp.Core.FSharpOption <AskType>          approved = null;
            Microsoft.FSharp.Core.FSharpOption <SubscriptionType> subs     = null;
            switch (Ask)
            {
            case Yaaf.Xmpp.IM.Sql.Model.DbAskType.Subscribe:
                approved = new Microsoft.FSharp.Core.FSharpOption <AskType>(AskType.Subscribe);
                break;

            default:
                break;
            }
            switch (Subscription)
            {
            case Yaaf.Xmpp.IM.Sql.Model.DbSubscriptionType.Both:
                subs = new Microsoft.FSharp.Core.FSharpOption <SubscriptionType>(SubscriptionType.Both);
                break;

            case Yaaf.Xmpp.IM.Sql.Model.DbSubscriptionType.None:
                subs = new Microsoft.FSharp.Core.FSharpOption <SubscriptionType>(SubscriptionType.SubsNone);
                break;

            case Yaaf.Xmpp.IM.Sql.Model.DbSubscriptionType.To:
                subs = new Microsoft.FSharp.Core.FSharpOption <SubscriptionType>(SubscriptionType.To);
                break;

            case Yaaf.Xmpp.IM.Sql.Model.DbSubscriptionType.From:
                subs = new Microsoft.FSharp.Core.FSharpOption <SubscriptionType>(SubscriptionType.From);
                break;

            default:
                break;
            }
            var list = ListModule.OfSeq(from g in Groups select g.RosterGroup.Name);

            return(new RosterItem(
                       JabberId.Parse(Jid),
                       SqlRosterStore.ToFSharp(Name),
                       !Approved ? null : (SqlRosterStore.ToFSharp(true)),
                       approved, subs, list));
        }
 private async Task <string> GetOrCreateUserId(JabberId value)
 {
     using (var context = this.CreateContext()) {
         var userId      = value.Localpart.Value;
         var currentUser = await
                               (from user in context.Users
                               where user.UserId == userId
                               select user).FirstOrDefaultAsync();
         if (currentUser == null)
         {
             // add new user
             currentUser = new DbArchivingUser()
             {
                 UserId = userId
             };
             context.Users.Add(currentUser);
             await context.MySaveChanges();
         }
         Debug.Assert(currentUser.UserId == userId);
         return(currentUser.UserId);
     }
 }
Esempio n. 11
0
 public async Task <string> DeleteItem(JabberId value)
 {
     using (var context = CreateContext()) {
         var jid = value.BareId;
         context.RosterItems.RemoveRange(await
                                             (from item in context.RosterItems
                                             where item.ApplicationUserId == user
                                             where item.Jid == jid
                                             select item).ToListAsync());
         var change =
             new DbRosterChange()
         {
             Version           = await NextVersion(context),
             ApplicationUserId = user,
             Jid        = jid,
             ChangeType = DbChangeType.Delete
         };
         //user.RosterChanges.Add (change);
         context.RosterChanges.Add(change);
         return(await SaveNextVersion(context));
     }
 }
 public async Task <IUserPreferenceStore> GetPreferenceStore(JabberId value)
 {
     return(new MessageArchivingUserPreferenceStore(this.CreateContext, await GetOrCreateUserId(value)));
 }
Esempio n. 13
0
        public async Task <Microsoft.FSharp.Collections.FSharpList <Tuple <RosterChange, string> > > ChangesSince(string value)
        {
            using (var context = CreateContext()) {
                var lastVersion   = int.Parse(value);
                var latestChanges =

                    /*
                     * // Not working in MySql
                     * from change in context.RosterChanges
                     * where change.ApplicationUserId == user.Id
                     * where change.Version > lastVersion
                     * group change by change.Jid into g
                     * let latest =
                     *                      (from item in g
                     *                      orderby item.Version descending
                     *                      select item)
                     *                      .FirstOrDefault()
                     * orderby latest.Version
                     * select latest;
                     *
                     * // Not working in MySql
                     * context.RosterChanges
                     *      .Where (c => c.ApplicationUserId == user.Id && c.Version > lastVersion)
                     *      .GroupBy (c => c.Jid)
                     *      .Select (g => g.OrderByDescending(i => i.Version).FirstOrDefault())
                     *      .OrderBy(i => i.Version); */

                    from change in context.RosterChanges
                    join gro in
                    (from c in context.RosterChanges
                     group c by c.Jid into g
                     select new { Name = g.Key, MaxVer = g.Select(i => i.Version).Max() })
                    on change.Jid equals gro.Name
                    where change.ApplicationUserId == user &&
                    change.Version > lastVersion &&
                    change.Version == gro.MaxVer
                    orderby change.Version
                    select change;

                var list = new List <Tuple <RosterChange, string> > ();
                foreach (var item in await latestChanges.ToListAsync())
                {
                    // build list
                    RosterChange change = null;
                    switch (item.ChangeType)
                    {
                    case DbChangeType.Set:
                        var items =
                            await(from i in context.RosterItems
                                  where i.ApplicationUserId == user
                                  where i.Jid == item.Jid
                                  select i)
                            .FirstOrDefaultAsync();

                        change = RosterChange.NewSetItem(items.ToFSharp());
                        break;

                    case DbChangeType.Delete:
                        change = RosterChange.NewDeleteItem(JabberId.Parse(item.Jid));
                        break;

                    default:
                        throw new InvalidOperationException("Invalid ChangeType");
                    }
                    var t = Tuple.Create(change, VersionString(item.Version));
                    list.Add(t);
                }
                return(ListModule.OfSeq(list));
                //return null;
            }
        }