public void UploadIcsToCalendar(MailBox mailBox, int calendarId, string calendarEventUid, string calendarIcs, string calendarCharset, string calendarContentType, string calendarEventReceiveEmail, string httpContextScheme, ILogger log) { try { if (string.IsNullOrEmpty(calendarEventUid) || string.IsNullOrEmpty(calendarIcs) || calendarContentType != "text/calendar") { return; } var calendar = MailUtil.ParseValidCalendar(calendarIcs, log); if (calendar == null) { return; } var alienEvent = true; var organizer = calendar.Events[0].Organizer; if (organizer != null) { var orgEmail = calendar.Events[0].Organizer.Value.ToString() .ToLowerInvariant() .Replace("mailto:", ""); if (orgEmail.Equals(calendarEventReceiveEmail)) { alienEvent = false; } } else { throw new ArgumentException("calendarIcs.organizer is null"); } if (alienEvent) { if (calendar.Events[0].Attendees.Any( a => a.Value.ToString() .ToLowerInvariant() .Replace("mailto:", "") .Equals(calendarEventReceiveEmail))) { alienEvent = false; } } if (alienEvent) { return; } CoreContext.TenantManager.SetCurrentTenant(mailBox.TenantId); SecurityContext.AuthenticateMe(new Guid(mailBox.UserId)); using (var ms = new MemoryStream(EncodingTools.GetEncodingByCodepageName(calendarCharset).GetBytes(calendarIcs))) { var apiHelper = new ApiHelper(httpContextScheme); apiHelper.UploadIcsToCalendar(calendarId, ms, "calendar.ics", calendarContentType); } } catch (Exception ex) { log.Error("UploadIcsToCalendar with \r\n" + "calendarId: {0}\r\n" + "calendarEventUid: '{1}'\r\n" + "calendarIcs: '{2}'\r\n" + "calendarCharset: '{3}'\r\n" + "calendarContentType: '{4}'\r\n" + "calendarEventReceiveEmail: '{5}'\r\n" + "Exception:\r\n{6}\r\n", calendarId, calendarEventUid, calendarIcs, calendarCharset, calendarContentType, calendarEventReceiveEmail, ex.ToString()); } }
public List <string> SearchMailContacts(int tenant, string user, string searchText, int maxCount) { var contacts = new List <string>(); if (string.IsNullOrEmpty(searchText) || maxCount <= 0) { return(contacts); } const string mail_contacts = "mc"; const string contact_info = "ci"; Func <SqlQuery> getBaseQuery = () => { var query = new SqlQuery(ContactsTable.Name.Alias(mail_contacts)) .InnerJoin(ContactInfoTable.Name.Alias(contact_info), Exp.EqColumns(ContactsTable.Columns.Id.Prefix(mail_contacts), ContactInfoTable.Columns.ContactId.Prefix(contact_info))) .Select( ContactsTable.Columns.ContactName.Prefix(mail_contacts), ContactInfoTable.Columns.Data.Prefix(contact_info)) .Where(ContactInfoTable.Columns.User.Prefix(contact_info), user) .Where(ContactInfoTable.Columns.Tenant.Prefix(contact_info), tenant) .Where(ContactInfoTable.Columns.Type.Prefix(contact_info), (int)ContactInfoType.Email); return(query); }; var ids = new List <int>(); if (FullTextSearch.SupportModule(FullTextSearch.MailContactsModule)) { ids = FullTextSearch.Search(FullTextSearch.MailContactsModule.Match(searchText)); if (!ids.Any()) { return(contacts); } } const int count_per_query = 100; using (var db = GetDb()) { var f = 0; do { var query = getBaseQuery(); if (ids.Any()) { var partIds = ids.Skip(f).Take(count_per_query).ToList(); if (!partIds.Any()) { break; } query .Where(Exp.In(ContactInfoTable.Columns.Id.Prefix(contact_info), partIds)); } else { query .Where( Exp.Like( string.Format("concat({0}, ' <', {1},'>')", ContactsTable.Columns.ContactName.Prefix(mail_contacts), ContactInfoTable.Columns.Data.Prefix(contact_info)), searchText.Replace("\"", "\\\""), SqlLike.AnyWhere)) .SetMaxResults(maxCount); } var partContacts = db.ExecuteList(query) .ConvertAll(r => string.IsNullOrEmpty(r[0] as string) ? r[1] as string : MailUtil.CreateFullEmail((string)r[0], r[1] as string)); foreach (var partContact in partContacts.TakeWhile(partContact => maxCount - contacts.Count != 0)) { contacts.Add(partContact); } if (maxCount - contacts.Count == 0 || !partContacts.Any() || partContacts.Count < count_per_query) { break; } f += count_per_query; } while (true); } return(contacts); }