// // CalendarController.cs // // Author: // Paul Schneider // // Copyright (c) 2015 Paul Schneider // // 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 System.Web.Http; using System.ComponentModel.DataAnnotations; using Yavsc.ApiControllers.Calendar.Model; using Yavsc.Helpers; using System.ComponentModel; using Yavsc.Model; using System.Web.Security; using System.Web.Profile; using System.Web.Http.ModelBinding; namespace Yavsc.ApiControllers.Calendar { /// /// Night flash controller. /// public class CalendarController: ApiController { YaEvent[] getTestList() { return new YaEvent[] { new YaEvent () { Description = "Test Descr", Title = "Night club special bubble party", Location = new Position () { Longitude = 0, Latitude = 0 } }, new YaEvent () { Title = "Test2", ImgLocator = "http://bla/im.png", Location = new Position () { Longitude = 0, Latitude = 0 } }, new YaEvent () { Description = "Test Descr", Title = "Night club special bubble party", Location = new Position () { Longitude = 0, Latitude = 0 } }, new YaEvent () { Title = "Test2", ImgLocator = "http://bla/im.png", Location = new Position () { Longitude = 0, Latitude = 0 } } }; } /// /// List events according the specified search arguments. /// /// Arguments. [ValidateAjaxAttribute] [HttpGet] public YaEvent[] List ([FromUri] PositionAndKeyphrase args) { return getTestList(); } /// /// Provider the specified ProviderId. /// /// Provider identifier. [HttpGet] public ProviderPublicInfo ProviderInfo ([FromUri] string ProviderId) { return new ProviderPublicInfo () { DisplayName = "Yavsc clubing", WebPage = "http://yavsc.pschneider.fr/", Calendar = new Schedule () { Period = Periodicity.ThreeM, WeekDays = new OpenDay[] { new OpenDay () { Day = WeekDay.Saturday, Start = new TimeSpan(18,00,00), End = new TimeSpan(2,00,00) } }, Validity = new Period[] { new Period() { Start = new DateTime(2015,5,29), End = new DateTime(2015,5,30)} } }, Description = "Yavsc Entertainment Production, Yet another private party", LogoImgLocator = "http://yavsc.pschneider.fr/favicon.png", Location = new Position () { Longitude = 0, Latitude = 0 }, LocationType = "Salle des fêtes" }; } /// /// Posts the image. /// /// The image. /// NF prov identifier. public string PostImage([FromUri] string NFProvId) { return null; } /// /// Posts the event. /// /// The event identifier. /// Ev. public int PostEvent ([FromBody] ProvidedEvent ev) { throw new NotImplementedException(); } /// /// Notification. /// public class Notification { /// /// The title. /// public string title; /// /// The body. /// public string body; /// /// The icon. /// public string icon; /// /// The sound. /// public string sound; /// /// The tag. /// public string tag; /// /// The color. /// public string color; /// /// The click action. /// public string click_action; } // https://gcm-http.googleapis.com/gcm/send /// /// Message with payload. /// public class MessageWithPayload { /// /// To. /// public string to; /// /// The registration identifiers. /// public string [] registration_ids; /// /// The data. /// public T[] data ; /// /// The notification. /// public Notification notification; /// /// The collapse key. /// public string collapse_key; // in order to collapse ... /// /// The priority. /// public int priority; // between 0 and 10, 10 is the lowest! /// /// The content available. /// public bool content_available; /// /// The delay while idle. /// public bool delay_while_idle; /// /// The time to live. /// public int time_to_live; // seconds /// /// The name of the restricted package. /// public string restricted_package_name; /// /// The dry run. /// public bool dry_run; /// /// Validate the specified modelState. /// /// Model state. public void Validate(ModelStateDictionary modelState) { if (to==null && registration_ids == null) { modelState.AddModelError ("to", "One of \"to\" or \"registration_ids\" parameters must be specified"); modelState.AddModelError ("registration_ids", "*"); } if (notification == null && data == null) { modelState.AddModelError ("notification", "At least one of \"notification\" or \"data\" parameters must be specified"); modelState.AddModelError ("data", "*"); } if (notification != null) { if (notification.icon == null) modelState.AddModelError ("notification.icon", "please, specify an icon resoure name"); if (notification.title == null) modelState.AddModelError ("notification.title", "please, specify a title"); } } } /// /// Message with payload response. /// public class MessageWithPayloadResponse { /// /// The multicast identifier. /// public int multicast_id; /// /// The success count. /// public int success; /// /// The failure count. /// public int failure; /// /// The canonical identifiers... ?!? /// public int canonical_ids; /// /// Detailled result. /// public class Result { /// /// The message identifier. /// public string message_id; /// /// The registration identifier. /// public string registration_id; /// /// The error. /// public string error; } /// /// The results. /// public Result [] results; } /// /// GCM register model. /// public class GCMRegisterModel { /// /// Gets or sets the name of the user. /// /// The name of the user. [Localizable(true)] [Display(ResourceType=typeof(LocalizedText),Name="UserName")] [Required(ErrorMessage = "S'il vous plait, entrez un nom d'utilisateur")] public string UserName { get; set; } /// /// Gets or sets the password. /// /// The password. [DisplayName("Mot de passe")] [Required(ErrorMessage = "S'il vous plait, entez un mot de passe")] public string Password { get; set; } /// /// Gets or sets the email. /// /// The email. [DisplayName("Adresse e-mail")] [Required(ErrorMessage = "S'il vous plait, entrez un e-mail valide")] public string Email { get; set; } /// /// Gets or sets the registration identifier against Google Clood Messaging and their info on this application. /// /// The registration identifier. public string RegistrationId { get; set; } } /// /// Registers with push notifications enabled. /// /// Model. [ValidateAjax] public void RegisterWithPushNotifications(GCMRegisterModel model) { if (ModelState.IsValid) { MembershipCreateStatus mcs; var user = Membership.CreateUser ( model.UserName, model.Password, model.Email, null, null, false, out mcs); switch (mcs) { case MembershipCreateStatus.DuplicateEmail: ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " + "à un compte utilisateur existant"); break; case MembershipCreateStatus.DuplicateUserName: ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " + "déjà enregistré"); break; case MembershipCreateStatus.Success: YavscHelpers.SendActivationEmail (user); // TODO set registration id throw new NotImplementedException (); } } } /// /// Sets the registration identifier. /// /// Registration identifier. [Authorize] public void SetRegistrationId(string registrationId) { // TODO set registration id setRegistrationId (Membership.GetUser ().UserName, registrationId); } private void setRegistrationId(string username, string regid) { ProfileBase pr = ProfileBase.Create(username); pr.SetPropertyValue ("gregid", regid); } /// /// Notifies the event. /// /// Evpub. public MessageWithPayloadResponse NotifyEvent(EventPub evpub) { SimpleJsonPostMethod,MessageWithPayloadResponse> r = new SimpleJsonPostMethod,MessageWithPayloadResponse>( "https://gcm-http.googleapis.com/gcm/send"); using (r) { var msg = new MessageWithPayload () { data = new YaEvent[] { (YaEvent)evpub } }; msg.to = string.Join (" ", Circle.Union (evpub.Circles)); return r.Invoke (msg); } } } }