github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/extend/guilds/plugin_guilds.go (about)

     1  package main
     2  
     3  import (
     4  	c "github.com/Azareal/Gosora/common"
     5  	guilds "github.com/Azareal/Gosora/extend/guilds/lib"
     6  )
     7  
     8  // TODO: Add a better way of splitting up giant plugins like this
     9  
    10  // TODO: Add a plugin interface instead of having a bunch of argument to AddPlugin?
    11  func init() {
    12  	c.Plugins.Add(&c.Plugin{UName: "guilds", Name: "Guilds", Author: "Azareal", URL: "https://github.com/Azareal", Init: initGuilds, Deactivate: deactivateGuilds, Install: installGuilds})
    13  
    14  	// TODO: Is it possible to avoid doing this when the plugin isn't activated?
    15  	c.PrebuildTmplList = append(c.PrebuildTmplList, guilds.PrebuildTmplList)
    16  }
    17  
    18  func initGuilds(pl *c.Plugin) (err error) {
    19  	pl.AddHook("intercept_build_widgets", guilds.Widgets)
    20  	pl.AddHook("trow_assign", guilds.TrowAssign)
    21  	pl.AddHook("topic_create_pre_loop", guilds.TopicCreatePreLoop)
    22  	pl.AddHook("pre_render_forum", guilds.PreRenderViewForum)
    23  	pl.AddHook("simple_forum_check_pre_perms", guilds.ForumCheck)
    24  	pl.AddHook("forum_check_pre_perms", guilds.ForumCheck)
    25  	// TODO: Auto-grant this perm to admins upon installation?
    26  	c.RegisterPluginPerm("CreateGuild")
    27  	router.HandleFunc("/guilds/", guilds.RouteGuildList)
    28  	router.HandleFunc("/guild/", guilds.MiddleViewGuild)
    29  	router.HandleFunc("/guild/create/", guilds.RouteCreateGuild)
    30  	router.HandleFunc("/guild/create/submit/", guilds.RouteCreateGuildSubmit)
    31  	router.HandleFunc("/guild/members/", guilds.RouteMemberList)
    32  
    33  	guilds.Gstore, err = guilds.NewSQLGuildStore()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	acc := qgen.NewAcc()
    39  
    40  	guilds.ListStmt = acc.Select("guilds").Columns("guildID, name, desc, active, privacy, joinable, owner, memberCount, createdAt, lastUpdateTime").Prepare()
    41  
    42  	guilds.MemberListStmt = acc.Select("guilds_members").Columns("guildID, uid, rank, posts, joinedAt").Prepare()
    43  
    44  	guilds.MemberListJoinStmt = acc.SimpleLeftJoin("guilds_members", "users", "users.uid, guilds_members.rank, guilds_members.posts, guilds_members.joinedAt, users.name, users.avatar", "guilds_members.uid = users.uid", "guilds_members.guildID = ?", "guilds_members.rank DESC, guilds_members.joinedat ASC", "")
    45  
    46  	guilds.GetMemberStmt = acc.Select("guilds_members").Columns("rank, posts, joinedAt").Where("guildID = ? AND uid = ?").Prepare()
    47  
    48  	guilds.AttachForumStmt = acc.Update("forums").Set("parentID = ?, parentType = 'guild'").Where("fid = ?").Prepare()
    49  
    50  	guilds.UnattachForumStmt = acc.Update("forums").Set("parentID = 0, parentType = ''").Where("fid = ?").Prepare()
    51  
    52  	guilds.AddMemberStmt = acc.Insert("guilds_members").Columns("guildID, uid, rank, posts, joinedAt").Fields("?,?,?,0,UTC_TIMESTAMP()").Prepare()
    53  
    54  	return acc.FirstError()
    55  }
    56  
    57  func deactivateGuilds(pl *c.Plugin) {
    58  	pl.RemoveHook("intercept_build_widgets", guilds.Widgets)
    59  	pl.RemoveHook("trow_assign", guilds.TrowAssign)
    60  	pl.RemoveHook("topic_create_pre_loop", guilds.TopicCreatePreLoop)
    61  	pl.RemoveHook("pre_render_forum", guilds.PreRenderViewForum)
    62  	pl.RemoveHook("simple_forum_check_pre_perms", guilds.ForumCheck)
    63  	pl.RemoveHook("forum_check_pre_perms", guilds.ForumCheck)
    64  	c.DeregisterPluginPerm("CreateGuild")
    65  	_ = router.RemoveFunc("/guilds/")
    66  	_ = router.RemoveFunc("/guild/")
    67  	_ = router.RemoveFunc("/guild/create/")
    68  	_ = router.RemoveFunc("/guild/create/submit/")
    69  	_ = guilds.ListStmt.Close()
    70  	_ = guilds.MemberListStmt.Close()
    71  	_ = guilds.MemberListJoinStmt.Close()
    72  	_ = guilds.GetMemberStmt.Close()
    73  	_ = guilds.AttachForumStmt.Close()
    74  	_ = guilds.UnattachForumStmt.Close()
    75  	_ = guilds.AddMemberStmt.Close()
    76  }
    77  
    78  // TODO: Stop accessing the query builder directly and add a feature in Gosora which is more easily reversed, if an error comes up during the installation process
    79  type tC = qgen.DBTableColumn
    80  
    81  func installGuilds(plugin *c.Plugin) error {
    82  	guildTableStmt, err := qgen.Builder.CreateTable("guilds", "utf8mb4", "utf8mb4_general_ci",
    83  		[]tC{
    84  			tC{"guildID", "int", 0, false, true, ""},
    85  			tC{"name", "varchar", 100, false, false, ""},
    86  			tC{"desc", "varchar", 200, false, false, ""},
    87  			tC{"active", "boolean", 1, false, false, ""},
    88  			tC{"privacy", "smallint", 0, false, false, ""},
    89  			tC{"joinable", "smallint", 0, false, false, "0"},
    90  			tC{"owner", "int", 0, false, false, ""},
    91  			tC{"memberCount", "int", 0, false, false, ""},
    92  			tC{"mainForum", "int", 0, false, false, "0"}, // The board the user lands on when they click on a group, we'll make it possible for group admins to change what users land on
    93  			//tC{"boards","varchar",255,false,false,""}, // Cap the max number of boards at 8 to avoid overflowing the confines of a 64-bit integer?
    94  			tC{"backdrop", "varchar", 200, false, false, ""}, // File extension for the uploaded file, or an external link
    95  			tC{"createdAt", "createdAt", 0, false, false, ""},
    96  			tC{"lastUpdateTime", "datetime", 0, false, false, ""},
    97  		},
    98  		[]qgen.DBTableKey{
    99  			qgen.DBTableKey{"guildID", "primary"},
   100  		},
   101  	)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	_, err = guildTableStmt.Exec()
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	guildMembersTableStmt, err := qgen.Builder.CreateTable("guilds_members", "", "",
   112  		[]tC{
   113  			tC{"guildID", "int", 0, false, false, ""},
   114  			tC{"uid", "int", 0, false, false, ""},
   115  			tC{"rank", "int", 0, false, false, "0"},  /* 0: Member. 1: Mod. 2: Admin. */
   116  			tC{"posts", "int", 0, false, false, "0"}, /* Per-Group post count. Should we do some sort of score system? */
   117  			tC{"joinedAt", "datetime", 0, false, false, ""},
   118  		}, nil,
   119  	)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	_, err = guildMembersTableStmt.Exec()
   125  	return err
   126  }
   127  
   128  // TO-DO; Implement an uninstallation system into Gosora. And a better installation system.
   129  func uninstallGuilds(plugin *c.Plugin) error {
   130  	return nil
   131  }