// // GoogleHelpers.cs // // Author: // Paul Schneider // // Copyright (c) 2015 GNU GPL // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System; using Yavsc.Model.Google; using System.Web.Profile; using System.Configuration; using System.Web; using Yavsc.Model.Calendar; using Yavsc.Model.Circles; using System.Collections.Generic; using System.Web.Security; using Yavsc.Model.Google.Api.Messaging; namespace Yavsc.Model.Google.Api { /// /// Google helpers. /// public static class GoogleHelpers { /// /// Gets the events. /// /// The events. /// Profile. /// Mindate. /// Maxdate. public static CalendarEventList GetEvents(this ProfileBase profile, DateTime mindate, DateTime maxdate) { string gcalid = (string) profile.GetPropertyValue ("gcalid"); if (string.IsNullOrWhiteSpace (gcalid)) throw new ArgumentException ("NULL gcalid"); CalendarApi c = new CalendarApi (); string creds = OAuth2.GetFreshGoogleCredential (profile); return c.GetCalendar (gcalid, mindate, maxdate, creds); } /// /// Gets the calendars. /// /// The calendars. /// Profile. public static CalendarList GetCalendars (this ProfileBase profile) { string cred = OAuth2.GetFreshGoogleCredential (profile); CalendarApi c = new CalendarApi (); return c.GetCalendars (cred); } /// /// Login the specified response, state and callBack. /// /// Response. /// State. /// Call back. public static void Login(this HttpResponseBase response, string state, string callBack) { OAuth2 oa = new OAuth2 (callBack); oa.Login (response, state); } /// /// Cals the login. /// /// Response. /// State. /// Call back. public static void CalLogin(this HttpResponseBase response, string state, string callBack) { OAuth2 oa = new OAuth2 (callBack); oa.GetCalendarScope (response, state); } /// /// Creates the O auth2. /// /// The O auth2. /// Call back. public static OAuth2 CreateOAuth2(string callBack) { return new OAuth2 (callBack); } /// /// Notifies the event. /// /// The event. /// Evpub. public static MessageWithPayloadResponse NotifyEvent(EventCirclesPub evpub) { using (var r = new SimpleJsonPostMethod,MessageWithPayloadResponse>( "https://gcm-http.googleapis.com/gcm/send")) { r.SetCredential (ConfigurationManager.AppSettings ["GOOGLE_GCM_API_KEY"]); if (evpub.CircleIds != null) { var users = Circle.Union (evpub.CircleIds); var regids = new List (); var to = new List (); foreach (var u in users) { var p = ProfileBase.Create (u); if (p != null) { var regid = p.GetPropertyValue ("gregid"); if (regid == null) { var muser = Membership.GetUser (u); to.Add (muser.Email); } else regids.Add ((string)regid); } } if (regids.Count == 0) throw new InvalidOperationException ("No recipient where found for this circle list"); var msg = new MessageWithPayload () { notification = new Notification() { title = evpub.Title, body = evpub.Description, icon = "event" }, data = evpub , registration_ids = regids.ToArray() }; return r.Invoke (msg); } else { throw new NotImplementedException (); } } } /// /// Notifies the event. /// /// The event. /// Evpub. public static MessageWithPayloadResponse NotifyEvent (NominativeEventPub evpub) { using (var r = new SimpleJsonPostMethod,MessageWithPayloadResponse> ( "https://gcm-http.googleapis.com/gcm/send")) { r.SetCredential ("key="+ConfigurationManager.AppSettings ["GOOGLE_API_KEY"]); var userprofile = ProfileBase.Create (evpub.PerformerName); var regid = userprofile.GetPropertyValue ("gregid") as string; if (regid == null) throw new NotImplementedException ("Notification via e-mail"); var msg = new MessageWithPayload () { notification = new Notification() { title = evpub.Title, body = evpub.Description, icon = "event" }, data = evpub, registration_ids = new string[] { regid } }; return r.Invoke (msg); } } } }