public static async Task<UserAppMember> SetNotification(string userId, SimpleMessage[] msgs)
 {
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var cntx = Cntx;
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, userId);
     if (memb != null)
     {
         memb.ChangedMemberCallbacks = (await mbsvc.MaterializeAllMemberCallbacksAsync(cntx, memb)).ToArray();
         var notices = new List<MemberNotification>();
         foreach (var msg in msgs)
         {
             notices.Add(new MemberNotification
             {
                 ID = Guid.NewGuid().ToString(),
                 Title = msg.Title,
                 NoticeMsg = msg.Message,
                 NoticeData = msg.Data,
                 CreatedDate = DateTime.UtcNow,
                 PriorityLevel = (short)msg.Priority,
                 ReadCount = 0,
                 TypeID = msg.TypeId,
                 UserID = userId,
                 ApplicationID = AppId
             });
         }
         MemberNotificationServiceProxy nsvc = new MemberNotificationServiceProxy();
         var results = await nsvc.AddOrUpdateEntitiesAsync(Cntx, new MemberNotificationSet(), notices.ToArray());
         for (int i = 0; i < msgs.Length; i++)
             msgs[i].Id = results.ChangedEntities[i].UpdatedItem.ID;
     }
     return memb;
 }
 public static async Task<OperationResult> AdjustUserRoleLevel(string adminId, string uid, int rid, int del)
 {
     OperationResult OpResult = new OperationResult();
     var maxp = await MemberAdminContext.GetMaxPriority(adminId);
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = usvc.LoadEntityByKey(cntx, uid);
     if (u == null)
     {
         OpResult.Result = new { ok = false, msg = string.Format(ResourceUtils.GetString("b66098049404e4de1356242e8aa6444a", "User \"{0}\" is not found."), uid) };
         return OpResult;
     }
     UsersInRoleServiceProxy uirsvc = new UsersInRoleServiceProxy();
     var uir = await uirsvc.LoadEntityByKeyAsync(cntx, rid, u.ID);
     if (uir == null)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("78257cace857db766d54e6568d7f912b", "The user is not in this role.") };
         return OpResult;
     }
     uir.RoleRef = await uirsvc.MaterializeRoleRefAsync(cntx, uir);
     if (maxp.Major < uir.RoleRef.RolePriority || maxp.Major == uir.RoleRef.RolePriority && uir.SubPriority + del > maxp.Major)
     {
         OpResult.Result = new { ok = false, msg = ResourceUtils.GetString("5986d63fe301793ee7f5b2134a8f8787", "Modifying more priviledged role is not authorized.") };
         return OpResult;
     }
     var oldPrio = uir.SubPriority;
     uir.SubPriority += del;
     uir.LastModified = DateTime.UtcNow;
     uir.AdminID = adminId;
     await uirsvc.AddOrUpdateEntitiesAsync(cntx, new UsersInRoleSet(), new UsersInRole[] { uir });
     uir.UserID = u.ID;
     uir.RoleID = rid;
     await AddUserRoleHistory(uir, UserRoleOperations.Modified);
     UserAppMemberServiceProxy mbsvc = new UserAppMemberServiceProxy();
     var memb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, uid);
     var notice = new SimpleMessage
     {
         TypeId = 1,
         Title = string.Format(ResourceUtils.GetString("54da39696e8014b5ded7a0eaeac1dfc4", "The relative priority of your role: [{0}] is changed from {1} to {2}.", memb.AcceptLanguages),
                               uir.RoleRef.DistinctString, oldPrio, uir.SubPriority),
         Data = "{ id=\"" + rid + "\", type=\"role\", name=\"" + uir.RoleRef.DistinctString + "\" }"
     };
     OpResult.Result = new { ok = true, msg = "" };
     OpResult.notices = new SimpleMessage[] { notice };
     return OpResult;
 }
 public static async Task<List<MemberNotificationType>> GetRecentCategorized(string userId, SimpleMessage[] msgs, int? typeId, int max)
 {
     var cntx = Cntx;
     MembershipPlusServiceProxy svc = new MembershipPlusServiceProxy();
     MemberNotificationTypeServiceProxy tsvc = new MemberNotificationTypeServiceProxy();
     MemberNotificationServiceProxy nsvc = new MemberNotificationServiceProxy();
     var categs = await tsvc.QueryDatabaseAsync(cntx, new MemberNotificationTypeSet(), null);
     List<MemberNotificationType> tlist = new List<MemberNotificationType>();
     DateTime dt = DateTime.UtcNow.AddDays(-1);
     foreach (var categ in categs)
     {
         if (typeId.HasValue && categ.ID != typeId.Value)
             continue;
         var cond = new MemberNotificationSetConstraints
         {
             ApplicationIDWrap = new ForeignKeyData<string> { KeyValue = AppId },
             UserIDWrap = new ForeignKeyData<string> { KeyValue = userId },
             TypeIDWrap = new ForeignKeyData<int> { KeyValue = categ.ID }
         };
         QueryExpresion qexpr = new QueryExpresion();
         qexpr.OrderTks = new List<QToken>(new QToken[] { 
             new QToken { TkName = "PriorityLevel" },
             new QToken { TkName = "desc" },
             new QToken { TkName = "CreatedDate" },
             new QToken { TkName = "desc" }
         });
         qexpr.FilterTks = new List<QToken>(new QToken[] { 
             new QToken { TkName = "ReadCount == 0 && CreatedDate >= " + svc.FormatRepoDateTime(dt) }
         });
         foreach (var msg in msgs)
         {
             qexpr.FilterTks.Add(new QToken
             {
                 TkName = " && ID != \"" + msg.Id + "\""
             });
         }
         var list = (await nsvc.ConstraintQueryLimitedAsync(cntx, new MemberNotificationSet(), cond, qexpr, max)).ToList();
         if (list.Count > 0)
         {
             categ.ChangedMemberNotifications = list.ToArray();
             tlist.Add(categ);
         }
     }
     return tlist;
 }